forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wood_trapdoor.go
112 lines (98 loc) · 3.59 KB
/
wood_trapdoor.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
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"
"math"
"time"
)
// WoodTrapdoor is a block that can be used as an openable 1x1 barrier.
type WoodTrapdoor struct {
transparent
bass
sourceWaterDisplacer
// Wood is the type of wood of the trapdoor. This field must have one of the values found in the material
// package.
Wood WoodType
// Facing is the direction the trapdoor is facing.
Facing cube.Direction
// Open is whether the trapdoor is open.
Open bool
// Top is whether the trapdoor occupies the top or bottom part of a block.
Top bool
}
// FlammabilityInfo ...
func (t WoodTrapdoor) FlammabilityInfo() FlammabilityInfo {
if !t.Wood.Flammable() {
return newFlammabilityInfo(0, 0, false)
}
return newFlammabilityInfo(0, 0, true)
}
// Model ...
func (t WoodTrapdoor) Model() world.BlockModel {
return model.Trapdoor{Facing: t.Facing, Top: t.Top, Open: t.Open}
}
// UseOnBlock handles the directional placing of trapdoors and makes sure they are properly placed upside down
// when needed.
func (t WoodTrapdoor) UseOnBlock(pos cube.Pos, face cube.Face, clickPos mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) bool {
pos, face, used := firstReplaceable(w, pos, face, t)
if !used {
return false
}
t.Facing = user.Rotation().Direction().Opposite()
t.Top = (clickPos.Y() > 0.5 && face != cube.FaceUp) || face == cube.FaceDown
place(w, pos, t, user, ctx)
return placed(ctx)
}
// Activate ...
func (t WoodTrapdoor) Activate(pos cube.Pos, _ cube.Face, w *world.World, _ item.User, _ *item.UseContext) bool {
t.Open = !t.Open
w.SetBlock(pos, t, nil)
if t.Open {
w.PlaySound(pos.Vec3Centre(), sound.TrapdoorOpen{Block: t})
return true
}
w.PlaySound(pos.Vec3Centre(), sound.TrapdoorClose{Block: t})
return true
}
// BreakInfo ...
func (t WoodTrapdoor) BreakInfo() BreakInfo {
return newBreakInfo(3, alwaysHarvestable, axeEffective, oneOf(t))
}
// FuelInfo ...
func (WoodTrapdoor) FuelInfo() item.FuelInfo {
return newFuelInfo(time.Second * 15)
}
// SideClosed ...
func (t WoodTrapdoor) SideClosed(cube.Pos, cube.Pos, *world.World) bool {
return false
}
// EncodeItem ...
func (t WoodTrapdoor) EncodeItem() (name string, meta int16) {
if t.Wood == OakWood() {
return "minecraft:trapdoor", 0
}
return "minecraft:" + t.Wood.String() + "_trapdoor", 0
}
// EncodeBlock ...
func (t WoodTrapdoor) EncodeBlock() (name string, properties map[string]any) {
if t.Wood == OakWood() {
return "minecraft:trapdoor", map[string]any{"direction": int32(math.Abs(float64(t.Facing) - 3)), "open_bit": t.Open, "upside_down_bit": t.Top}
}
return "minecraft:" + t.Wood.String() + "_trapdoor", map[string]any{"direction": int32(math.Abs(float64(t.Facing) - 3)), "open_bit": t.Open, "upside_down_bit": t.Top}
}
// allTrapdoors returns a list of all trapdoor types
func allTrapdoors() (trapdoors []world.Block) {
for _, w := range WoodTypes() {
for i := cube.Direction(0); i <= 3; i++ {
trapdoors = append(trapdoors, WoodTrapdoor{Wood: w, Facing: i, Open: false, Top: false})
trapdoors = append(trapdoors, WoodTrapdoor{Wood: w, Facing: i, Open: false, Top: true})
trapdoors = append(trapdoors, WoodTrapdoor{Wood: w, Facing: i, Open: true, Top: true})
trapdoors = append(trapdoors, WoodTrapdoor{Wood: w, Facing: i, Open: true, Top: false})
}
}
return
}