forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cactus.go
126 lines (109 loc) · 3.52 KB
/
cactus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package block
import (
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/block/cube"
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/block/model"
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/item"
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/world"
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/world/particle"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
)
// Cactus is a plant block that generates naturally in dry areas and causes damage.
type Cactus struct {
transparent
// Age is the growth state of cactus. Values range from 0 to 15.
Age int
}
// UseOnBlock handles making sure the neighbouring blocks are air.
func (c Cactus) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) (used bool) {
pos, _, used = firstReplaceable(w, pos, face, c)
if !used {
return false
}
if !c.canGrowHere(pos, w, true) {
return false
}
place(w, pos, c, user, ctx)
return placed(ctx)
}
// NeighbourUpdateTick ...
func (c Cactus) NeighbourUpdateTick(pos, _ cube.Pos, w *world.World) {
if !c.canGrowHere(pos, w, true) {
w.SetBlock(pos, nil, nil)
w.AddParticle(pos.Vec3Centre(), particle.BlockBreak{Block: c})
dropItem(w, item.NewStack(c, 1), pos.Vec3Centre())
}
}
// RandomTick ...
func (c Cactus) RandomTick(pos cube.Pos, w *world.World, _ *rand.Rand) {
if c.Age < 15 {
c.Age++
} else if c.Age == 15 {
c.Age = 0
if c.canGrowHere(pos.Side(cube.FaceDown), w, false) {
for y := 1; y < 3; y++ {
if _, ok := w.Block(pos.Add(cube.Pos{0, y})).(Air); ok {
w.SetBlock(pos.Add(cube.Pos{0, y}), Cactus{Age: 0}, nil)
break
} else if _, ok := w.Block(pos.Add(cube.Pos{0, y})).(Cactus); !ok {
break
}
}
}
}
w.SetBlock(pos, c, nil)
}
// canGrowHere implements logic to check if cactus can live/grow here.
func (c Cactus) canGrowHere(pos cube.Pos, w *world.World, recursive bool) bool {
for _, face := range cube.HorizontalFaces() {
if _, ok := w.Block(pos.Side(face)).(Air); !ok {
return false
}
}
if _, ok := w.Block(pos.Side(cube.FaceDown)).(Cactus); ok && recursive {
return c.canGrowHere(pos.Side(cube.FaceDown), w, recursive)
}
return supportsVegetation(c, w.Block(pos.Sub(cube.Pos{0, 1})))
}
// EntityInside ...
func (c Cactus) EntityInside(_ cube.Pos, _ *world.World, e world.Entity) {
if l, ok := e.(livingEntity); ok && !l.AttackImmune() {
l.Hurt(0.5, DamageSource{Block: c})
}
}
// BreakInfo ...
func (c Cactus) BreakInfo() BreakInfo {
return newBreakInfo(0.4, alwaysHarvestable, nothingEffective, oneOf(c))
}
// CompostChance ...
func (Cactus) CompostChance() float64 {
return 0.5
}
// EncodeItem ...
func (c Cactus) EncodeItem() (name string, meta int16) {
return "minecraft:cactus", 0
}
// EncodeBlock ...
func (c Cactus) EncodeBlock() (name string, properties map[string]any) {
return "minecraft:cactus", map[string]any{"age": int32(c.Age)}
}
// Model ...
func (c Cactus) Model() world.BlockModel {
return model.Cactus{}
}
// allCactus returns all possible states of a cactus block.
func allCactus() (b []world.Block) {
for i := 0; i < 16; i++ {
b = append(b, Cactus{Age: i})
}
return
}
// DamageSource is passed as world.DamageSource for damage caused by a block,
// such as a cactus or a falling anvil.
type DamageSource struct {
// Block is the block that caused the damage.
Block world.Block
}
func (DamageSource) ReducedByResistance() bool { return true }
func (DamageSource) ReducedByArmour() bool { return true }
func (DamageSource) Fire() bool { return false }