github.com/df-mc/dragonfly@v0.9.13/server/world/handler.go (about) 1 package world 2 3 import ( 4 "github.com/df-mc/dragonfly/server/block/cube" 5 "github.com/df-mc/dragonfly/server/event" 6 "github.com/go-gl/mathgl/mgl64" 7 ) 8 9 // Handler handles events that are called by a world. Implementations of Handler may be used to listen to 10 // specific events such as when an entity is added to the world. 11 type Handler interface { 12 // HandleLiquidFlow handles the flowing of a liquid from one block position from into another block 13 // position into. The liquid that will replace the block is also passed. This replaced block might 14 // also be a Liquid. The Liquid's depth and falling state can be checked to see if the resulting 15 // liquid is a new source block (in the case of water). 16 HandleLiquidFlow(ctx *event.Context, from, into cube.Pos, liquid Liquid, replaced Block) 17 // HandleLiquidDecay handles the decaying of a Liquid block at a position. Liquid decaying happens 18 // when there is no Liquid that can serve as the source block neighbouring it. The state of the 19 // Liquid before and after the decaying is passed. The Liquid after is nil if the liquid is 20 // completely removed as a result of the decay. 21 HandleLiquidDecay(ctx *event.Context, pos cube.Pos, before, after Liquid) 22 // HandleLiquidHarden handles the hardening of a liquid at hardenedPos. The liquid that was hardened, 23 // liquidHardened, and the liquid that caused it to harden, otherLiquid, are passed. The block created 24 // as a result is also passed. 25 HandleLiquidHarden(ctx *event.Context, hardenedPos cube.Pos, liquidHardened, otherLiquid, newBlock Block) 26 // HandleSound handles a Sound being played in the World at a specific position. ctx.Cancel() may be called 27 // to stop the Sound from playing to viewers of the position. 28 HandleSound(ctx *event.Context, s Sound, pos mgl64.Vec3) 29 // HandleFireSpread handles when a fire block spreads from one block to another block. When this event handler gets 30 // called, both the position of the original fire will be passed, and the position where it will spread to after the 31 // event. The age of the fire may also be altered by changing the underlying value of the newFireAge pointer, which 32 // decides how long the fire will stay before burning out. 33 HandleFireSpread(ctx *event.Context, from, to cube.Pos) 34 // HandleBlockBurn handles a block at a cube.Pos being burnt by fire. This event may be called for blocks such as 35 // wood, that can be broken by fire. HandleBlockBurn is often succeeded by HandleFireSpread, when fire spreads to 36 // the position of the original block and the event.Context is not cancelled in HandleBlockBurn. 37 HandleBlockBurn(ctx *event.Context, pos cube.Pos) 38 // HandleEntitySpawn handles an entity being spawned into a World through a call to World.AddEntity. 39 HandleEntitySpawn(e Entity) 40 // HandleEntityDespawn handles an entity being despawned from a World through a call to World.RemoveEntity. 41 HandleEntityDespawn(e Entity) 42 // HandleClose handles the World being closed. HandleClose may be used as a moment to finish code running on other 43 // goroutines that operates on the World specifically. HandleClose is called directly before the World stops 44 // ticking and before any chunks are saved to disk. 45 HandleClose() 46 } 47 48 // Compile time check to make sure NopHandler implements Handler. 49 var _ Handler = (*NopHandler)(nil) 50 51 // NopHandler implements the Handler interface but does not execute any code when an event is called. The 52 // default Handler of worlds is set to NopHandler. 53 // Users may embed NopHandler to avoid having to implement each method. 54 type NopHandler struct{} 55 56 func (NopHandler) HandleLiquidFlow(*event.Context, cube.Pos, cube.Pos, Liquid, Block) {} 57 func (NopHandler) HandleLiquidDecay(*event.Context, cube.Pos, Liquid, Liquid) {} 58 func (NopHandler) HandleLiquidHarden(*event.Context, cube.Pos, Block, Block, Block) {} 59 func (NopHandler) HandleSound(*event.Context, Sound, mgl64.Vec3) {} 60 func (NopHandler) HandleFireSpread(*event.Context, cube.Pos, cube.Pos) {} 61 func (NopHandler) HandleBlockBurn(*event.Context, cube.Pos) {} 62 func (NopHandler) HandleEntitySpawn(Entity) {} 63 func (NopHandler) HandleEntityDespawn(Entity) {} 64 func (NopHandler) HandleClose() {}