github.com/df-mc/dragonfly@v0.9.13/server/entity/falling_block.go (about) 1 package entity 2 3 import ( 4 "github.com/df-mc/dragonfly/server/block/cube" 5 "github.com/df-mc/dragonfly/server/internal/nbtconv" 6 "github.com/df-mc/dragonfly/server/world" 7 "github.com/go-gl/mathgl/mgl64" 8 "math/rand" 9 ) 10 11 // NewFallingBlock creates a new FallingBlock entity. 12 func NewFallingBlock(block world.Block, pos mgl64.Vec3) *Ent { 13 return Config{Behaviour: fallingBlockConf.New(block)}.New(FallingBlockType{}, pos) 14 } 15 16 var fallingBlockConf = FallingBlockBehaviourConfig{ 17 Gravity: 0.04, 18 Drag: 0.02, 19 } 20 21 // FallingBlockType is a world.EntityType implementation for FallingBlock. 22 type FallingBlockType struct{} 23 24 func (FallingBlockType) EncodeEntity() string { return "minecraft:falling_block" } 25 func (FallingBlockType) NetworkOffset() float64 { return 0.49 } 26 func (FallingBlockType) BBox(world.Entity) cube.BBox { 27 return cube.Box(-0.49, 0, -0.49, 0.49, 0.98, 0.49) 28 } 29 30 func (FallingBlockType) DecodeNBT(m map[string]any) world.Entity { 31 b := nbtconv.Block(m, "FallingBlock") 32 if b == nil { 33 return nil 34 } 35 n := NewFallingBlock(b, nbtconv.Vec3(m, "Pos")) 36 n.SetVelocity(nbtconv.Vec3(m, "Motion")) 37 n.Behaviour().(*FallingBlockBehaviour).passive.fallDistance = nbtconv.Float64(m, "FallDistance") 38 return n 39 } 40 41 func (FallingBlockType) EncodeNBT(e world.Entity) map[string]any { 42 f := e.(*Ent) 43 b := f.Behaviour().(*FallingBlockBehaviour) 44 return map[string]any{ 45 "UniqueID": -rand.Int63(), 46 "FallDistance": b.passive.fallDistance, 47 "Pos": nbtconv.Vec3ToFloat32Slice(f.Position()), 48 "Motion": nbtconv.Vec3ToFloat32Slice(f.Velocity()), 49 "FallingBlock": nbtconv.WriteBlock(b.block), 50 } 51 }