forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slab.go
192 lines (174 loc) · 5.37 KB
/
slab.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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/sound"
"github.com/go-gl/mathgl/mgl64"
"time"
)
// Slab is a half block that allows entities to walk up blocks without jumping.
type Slab struct {
// Block is the block to use for the type of slab.
Block world.Block
// Top specifies if the slab is in the top part of the block.
Top bool
// Double specifies if the slab is a double slab. These double slabs can be made by placing another slab
// on an existing slab.
Double bool
}
// UseOnBlock handles the placement of slabs with relation to them being upside down or not and handles slabs
// being turned into double slabs.
func (s Slab) UseOnBlock(pos cube.Pos, face cube.Face, clickPos mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) (used bool) {
id, meta := s.EncodeItem()
clickedBlock := w.Block(pos)
if clickedSlab, ok := clickedBlock.(Slab); ok && !s.Double {
clickedId, clickedMeta := clickedSlab.EncodeItem()
if !clickedSlab.Double && id == clickedId && meta == clickedMeta && ((face == cube.FaceUp && !clickedSlab.Top) || (face == cube.FaceDown && clickedSlab.Top)) {
// A half slab of the same type was clicked at the top, so we can make it full.
clickedSlab.Double = true
place(w, pos, clickedSlab, user, ctx)
return placed(ctx)
}
}
if sideSlab, ok := w.Block(pos.Side(face)).(Slab); ok && !replaceableWith(w, pos, s) && !s.Double {
sideId, sideMeta := sideSlab.EncodeItem()
// The block on the side of the one clicked was a slab and the block clicked was not replaceableWith, so
// the slab on the side must've been half and may now be filled if the wood types are the same.
if !sideSlab.Double && id == sideId && meta == sideMeta {
sideSlab.Double = true
place(w, pos.Side(face), sideSlab, user, ctx)
return placed(ctx)
}
}
pos, face, used = firstReplaceable(w, pos, face, s)
if !used {
return
}
if face == cube.FaceDown || (clickPos[1] > 0.5 && face != cube.FaceUp) {
s.Top = true
}
place(w, pos, s, user, ctx)
return placed(ctx)
}
// Instrument ...
func (s Slab) Instrument() sound.Instrument {
if _, ok := s.Block.(Planks); ok {
return sound.Bass()
}
return sound.BassDrum()
}
// FlammabilityInfo ...
func (s Slab) FlammabilityInfo() FlammabilityInfo {
if w, ok := s.Block.(Planks); ok && w.Wood.Flammable() {
return newFlammabilityInfo(5, 20, true)
}
return newFlammabilityInfo(0, 0, false)
}
// FuelInfo ...
func (s Slab) FuelInfo() item.FuelInfo {
if w, ok := s.Block.(Planks); ok && w.Wood.Flammable() {
return newFuelInfo(time.Second * 15)
}
return item.FuelInfo{}
}
// CanDisplace ...
func (s Slab) CanDisplace(b world.Liquid) bool {
water, ok := b.(Water)
return !s.Double && ok && water.Depth == 8
}
// SideClosed ...
func (s Slab) SideClosed(pos, side cube.Pos, _ *world.World) bool {
// Only returns true if the side is below the slab and if the slab is not upside down.
return !s.Top && side[1] == pos[1]-1
}
// LightDiffusionLevel returns 0 if the slab is a half slab, or 15 if it is double.
func (s Slab) LightDiffusionLevel() uint8 {
if s.Double {
return 15
}
return 0
}
// BreakInfo ...
func (s Slab) BreakInfo() BreakInfo {
hardness, blastResistance, harvestable, effective := 2.0, 30.0, pickaxeHarvestable, pickaxeEffective
switch block := s.Block.(type) {
// TODO: Copper
// TODO: Deepslate
case EndBricks:
hardness = 3.0
blastResistance = 45.0
case StoneBricks:
if block.Type == MossyStoneBricks() {
hardness = 1.5
}
case Andesite:
if block.Polished {
hardness = 1.5
}
case Diorite:
if block.Polished {
hardness = 1.5
}
case Granite:
if block.Polished {
hardness = 1.5
}
case Prismarine:
hardness = 1.5
case Planks:
harvestable = alwaysHarvestable
effective = axeEffective
blastResistance = 15.0
}
return newBreakInfo(hardness, harvestable, effective, func(tool item.Tool, enchantments []item.Enchantment) []item.Stack {
if s.Double {
return []item.Stack{item.NewStack(s, 2)}
}
return []item.Stack{item.NewStack(s, 1)}
}).withBlastResistance(blastResistance)
}
// Model ...
func (s Slab) Model() world.BlockModel {
return model.Slab{Double: s.Double, Top: s.Top}
}
// EncodeItem ...
func (s Slab) EncodeItem() (string, int16) {
id, slabType, meta := encodeSlabBlock(s.Block)
if slabType != "" {
return "minecraft:" + encodeLegacySlabId(slabType), meta
}
return "minecraft:" + id + "_slab", meta
}
// EncodeBlock ...
func (s Slab) EncodeBlock() (string, map[string]any) {
id, slabType, _ := encodeSlabBlock(s.Block)
side := "bottom"
if s.Top {
side = "top"
}
properties := map[string]any{"minecraft:vertical_half": side}
if slabType != "" {
properties[slabType] = id
id = encodeLegacySlabId(slabType)
if s.Double {
id = "double_" + id
}
} else if s.Double {
id = id + "_double_slab"
} else {
id = id + "_slab"
}
return "minecraft:" + id, properties
}
// allSlabs ...
func allSlabs() (b []world.Block) {
for _, s := range SlabBlocks() {
b = append(b, Slab{Block: s, Double: true})
b = append(b, Slab{Block: s, Top: true, Double: true})
b = append(b, Slab{Block: s, Top: true})
b = append(b, Slab{Block: s})
}
return
}