Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

world/handler.go: Added handler for leaves decay #905

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions server/block/leaves.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package block

import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/event"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
Expand Down Expand Up @@ -66,6 +67,14 @@ func (l Leaves) RandomTick(pos cube.Pos, w *world.World, _ *rand.Rand) {
l.ShouldUpdate = false
w.SetBlock(pos, l, nil)
} else {
ctx := event.C()
if w.Handler().HandleLeavesDecay(ctx, pos); !ctx.Cancelled() {
drops := l.BreakInfo().Drops(nil, nil)
for _, drop := range drops {
dropItem(w, drop, pos.Vec3Centre())
}
}

w.SetBlock(pos, nil, nil)
Comment on lines +70 to 78
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it not only set the block to nil if the event was not cancelled? I think the logic of only dropping items would be slightly unexpected?

}
}
Expand Down
5 changes: 5 additions & 0 deletions server/world/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
// Handler handles events that are called by a world. Implementations of Handler may be used to listen to
// specific events such as when an entity is added to the world.
type Handler interface {
// HandleLeavesDecay handles the decaying of a Leaves block at a position. Leaves decaying happens when
// there is no wood block neighbouring it. The position of the Leaves block is passed to the handler.
// if ctx.Cancel() is called, the leaves don't drop any items.
HandleLeavesDecay(ctx *event.Context, pos cube.Pos)
// HandleLiquidFlow handles the flowing of a liquid from one block position from into another block
// position into. The liquid that will replace the block is also passed. This replaced block might
// also be a Liquid. The Liquid's depth and falling state can be checked to see if the resulting
Expand Down Expand Up @@ -53,6 +57,7 @@ var _ Handler = (*NopHandler)(nil)
// Users may embed NopHandler to avoid having to implement each method.
type NopHandler struct{}

func (NopHandler) HandleLeavesDecay(*event.Context, cube.Pos) {}
func (NopHandler) HandleLiquidFlow(*event.Context, cube.Pos, cube.Pos, Liquid, Block) {}
func (NopHandler) HandleLiquidDecay(*event.Context, cube.Pos, Liquid, Liquid) {}
func (NopHandler) HandleLiquidHarden(*event.Context, cube.Pos, Block, Block, Block) {}
Expand Down
Loading