forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pumpkin_seeds.go
119 lines (104 loc) · 3.14 KB
/
pumpkin_seeds.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
package block
import (
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/block/cube"
"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"
)
// PumpkinSeeds grow pumpkin blocks.
type PumpkinSeeds struct {
crop
// Direction is the direction from the stem to the pumpkin.
Direction cube.Face
}
// SameCrop ...
func (PumpkinSeeds) SameCrop(c Crop) bool {
_, ok := c.(PumpkinSeeds)
return ok
}
// NeighbourUpdateTick ...
func (p PumpkinSeeds) NeighbourUpdateTick(pos, _ cube.Pos, w *world.World) {
if _, ok := w.Block(pos.Side(cube.FaceDown)).(Farmland); !ok {
w.SetBlock(pos, nil, nil)
w.AddParticle(pos.Vec3Centre(), particle.BlockBreak{Block: p})
} else if p.Direction != cube.FaceDown {
if pumpkin, ok := w.Block(pos.Side(p.Direction)).(Pumpkin); !ok || pumpkin.Carved {
p.Direction = cube.FaceDown
w.SetBlock(pos, p, nil)
}
}
}
// RandomTick ...
func (p PumpkinSeeds) RandomTick(pos cube.Pos, w *world.World, r *rand.Rand) {
if r.Float64() <= p.CalculateGrowthChance(pos, w) && w.Light(pos) >= 8 {
if p.Growth < 7 {
p.Growth++
w.SetBlock(pos, p, nil)
} else {
directions := []cube.Direction{cube.North, cube.South, cube.West, cube.East}
for _, i := range directions {
if _, ok := w.Block(pos.Side(i.Face())).(Pumpkin); ok {
return
}
}
direction := directions[r.Intn(len(directions))].Face()
stemPos := pos.Side(direction)
if _, ok := w.Block(stemPos).(Air); ok {
switch w.Block(stemPos.Side(cube.FaceDown)).(type) {
case Farmland, Dirt, Grass:
p.Direction = direction
w.SetBlock(pos, p, nil)
w.SetBlock(stemPos, Pumpkin{}, nil)
}
}
}
}
}
// BoneMeal ...
func (p PumpkinSeeds) BoneMeal(pos cube.Pos, w *world.World) bool {
if p.Growth == 7 {
return false
}
p.Growth = min(p.Growth+rand.Intn(4)+2, 7)
w.SetBlock(pos, p, nil)
return true
}
// UseOnBlock ...
func (p PumpkinSeeds) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) bool {
pos, _, used := firstReplaceable(w, pos, face, p)
if !used {
return false
}
if _, ok := w.Block(pos.Side(cube.FaceDown)).(Farmland); !ok {
return false
}
place(w, pos, p, user, ctx)
return placed(ctx)
}
// BreakInfo ...
func (p PumpkinSeeds) BreakInfo() BreakInfo {
return newBreakInfo(0, alwaysHarvestable, nothingEffective, oneOf(p))
}
// CompostChance ...
func (PumpkinSeeds) CompostChance() float64 {
return 0.3
}
// EncodeItem ...
func (p PumpkinSeeds) EncodeItem() (name string, meta int16) {
return "minecraft:pumpkin_seeds", 0
}
// EncodeBlock ...
func (p PumpkinSeeds) EncodeBlock() (name string, properties map[string]any) {
return "minecraft:pumpkin_stem", map[string]any{"facing_direction": int32(p.Direction), "growth": int32(p.Growth)}
}
// allPumpkinStems
func allPumpkinStems() (stems []world.Block) {
for i := 0; i <= 7; i++ {
for j := cube.Face(0); j <= 5; j++ {
stems = append(stems, PumpkinSeeds{Direction: j, crop: crop{Growth: i}})
}
}
return
}