Skip to content

Commit

Permalink
world/handler.go: Added a crop trampling handler (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
xNatsuri authored Aug 31, 2024
1 parent 29a214b commit 06e5131
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion server/block/farmland.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/world"
"math/rand"
)
Expand Down Expand Up @@ -72,7 +73,10 @@ func (f Farmland) hydrated(pos cube.Pos, w *world.World) bool {
func (f Farmland) EntityLand(pos cube.Pos, w *world.World, e world.Entity, distance *float64) {
if living, ok := e.(livingEntity); ok {
if fall, ok := living.(fallDistanceEntity); ok && rand.Float64() < fall.FallDistance()-0.5 {
w.SetBlock(pos, Dirt{}, nil)
ctx := event.C()
if w.Handler().HandleCropTrample(ctx, pos); !ctx.Cancelled() {
w.SetBlock(pos, Dirt{}, nil)
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions server/world/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Handler interface {
// wood, that can be broken by fire. HandleBlockBurn is often succeeded by HandleFireSpread, when fire spreads to
// the position of the original block and the event.Context is not cancelled in HandleBlockBurn.
HandleBlockBurn(ctx *event.Context, pos cube.Pos)
// HandleCropTrample handles an entity trampling a crop.
HandleCropTrample(ctx *event.Context, pos cube.Pos)
// HandleEntitySpawn handles an entity being spawned into a World through a call to World.AddEntity.
HandleEntitySpawn(e Entity)
// HandleEntityDespawn handles an entity being despawned from a World through a call to World.RemoveEntity.
Expand All @@ -59,6 +61,7 @@ func (NopHandler) HandleLiquidHarden(*event.Context, cube.Pos, Block, Block, Blo
func (NopHandler) HandleSound(*event.Context, Sound, mgl64.Vec3) {}
func (NopHandler) HandleFireSpread(*event.Context, cube.Pos, cube.Pos) {}
func (NopHandler) HandleBlockBurn(*event.Context, cube.Pos) {}
func (NopHandler) HandleCropTrample(*event.Context, cube.Pos) {}
func (NopHandler) HandleEntitySpawn(Entity) {}
func (NopHandler) HandleEntityDespawn(Entity) {}
func (NopHandler) HandleClose() {}

0 comments on commit 06e5131

Please sign in to comment.