github.com/df-mc/dragonfly@v0.9.13/server/block/cake.go (about) 1 package block 2 3 import ( 4 "github.com/df-mc/dragonfly/server/block/cube" 5 "github.com/df-mc/dragonfly/server/block/model" 6 "github.com/df-mc/dragonfly/server/item" 7 "github.com/df-mc/dragonfly/server/world" 8 "github.com/df-mc/dragonfly/server/world/particle" 9 "github.com/df-mc/dragonfly/server/world/sound" 10 "github.com/go-gl/mathgl/mgl64" 11 ) 12 13 // Cake is an edible block. 14 type Cake struct { 15 transparent 16 sourceWaterDisplacer 17 18 // Bites is the amount of bites taken out of the cake. 19 Bites int 20 } 21 22 // SideClosed ... 23 func (c Cake) SideClosed(cube.Pos, cube.Pos, *world.World) bool { 24 return false 25 } 26 27 // UseOnBlock ... 28 func (c Cake) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) bool { 29 pos, _, used := firstReplaceable(w, pos, face, c) 30 if !used { 31 return false 32 } 33 34 if _, air := w.Block(pos.Side(cube.FaceDown)).(Air); air { 35 return false 36 } 37 38 place(w, pos, c, user, ctx) 39 return placed(ctx) 40 } 41 42 // NeighbourUpdateTick ... 43 func (c Cake) NeighbourUpdateTick(pos, _ cube.Pos, w *world.World) { 44 if _, air := w.Block(pos.Side(cube.FaceDown)).(Air); air { 45 w.SetBlock(pos, nil, nil) 46 w.AddParticle(pos.Vec3Centre(), particle.BlockBreak{Block: c}) 47 } 48 } 49 50 // Activate ... 51 func (c Cake) Activate(pos cube.Pos, _ cube.Face, w *world.World, u item.User, _ *item.UseContext) bool { 52 if i, ok := u.(interface { 53 Saturate(food int, saturation float64) 54 }); ok { 55 i.Saturate(2, 0.4) 56 w.PlaySound(u.Position().Add(mgl64.Vec3{0, 1.5}), sound.Burp{}) 57 c.Bites++ 58 if c.Bites > 6 { 59 w.SetBlock(pos, nil, nil) 60 return true 61 } 62 w.SetBlock(pos, c, nil) 63 return true 64 } 65 return false 66 } 67 68 // BreakInfo ... 69 func (c Cake) BreakInfo() BreakInfo { 70 return newBreakInfo(0.5, neverHarvestable, nothingEffective, simpleDrops()) 71 } 72 73 // EncodeItem ... 74 func (c Cake) EncodeItem() (name string, meta int16) { 75 return "minecraft:cake", 0 76 } 77 78 // EncodeBlock ... 79 func (c Cake) EncodeBlock() (name string, properties map[string]any) { 80 return "minecraft:cake", map[string]any{"bite_counter": int32(c.Bites)} 81 } 82 83 // Model ... 84 func (c Cake) Model() world.BlockModel { 85 return model.Cake{Bites: c.Bites} 86 } 87 88 // allCake ... 89 func allCake() (cake []world.Block) { 90 for bites := 0; bites < 7; bites++ { 91 cake = append(cake, Cake{Bites: bites}) 92 } 93 return 94 }