forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
furnace.go
129 lines (113 loc) · 3.85 KB
/
furnace.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
package block
import (
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/block/cube"
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/internal/nbtconv"
"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/rand"
"time"
)
// Furnace is a utility block used for the smelting of blocks and items.
// The empty value of Furnace is not valid. It must be created using block.NewFurnace(cube.Face).
type Furnace struct {
solid
bassDrum
*smelter
// Facing is the direction the furnace is facing.
Facing cube.Direction
// Lit is true if the furnace is lit.
Lit bool
}
// NewFurnace creates a new initialised furnace. The smelter is properly initialised.
func NewFurnace(face cube.Direction) Furnace {
return Furnace{
Facing: face,
smelter: newSmelter(),
}
}
// Tick is called to check if the furnace should update and start or stop smelting.
func (f Furnace) Tick(_ int64, pos cube.Pos, w *world.World) {
if f.Lit && rand.Float64() <= 0.016 { // Every three or so seconds.
w.PlaySound(pos.Vec3Centre(), sound.FurnaceCrackle{})
}
if lit := f.smelter.tickSmelting(time.Second*10, time.Millisecond*100, f.Lit, func(item.SmeltInfo) bool {
return true
}); f.Lit != lit {
f.Lit = lit
w.SetBlock(pos, f, nil)
}
}
// EncodeItem ...
func (f Furnace) EncodeItem() (name string, meta int16) {
return "minecraft:furnace", 0
}
// EncodeBlock ...
func (f Furnace) EncodeBlock() (name string, properties map[string]interface{}) {
if f.Lit {
return "minecraft:lit_furnace", map[string]interface{}{"minecraft:cardinal_direction": f.Facing.String()}
}
return "minecraft:furnace", map[string]interface{}{"minecraft:cardinal_direction": f.Facing.String()}
}
// UseOnBlock ...
func (f Furnace) 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, f)
if !used {
return false
}
place(w, pos, NewFurnace(user.Rotation().Direction().Opposite()), user, ctx)
return placed(ctx)
}
// BreakInfo ...
func (f Furnace) BreakInfo() BreakInfo {
xp := f.Experience()
return newBreakInfo(3.5, alwaysHarvestable, pickaxeEffective, oneOf(f)).withXPDropRange(xp, xp)
}
// Activate ...
func (f Furnace) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item.User, _ *item.UseContext) bool {
if opener, ok := u.(ContainerOpener); ok {
opener.OpenBlockContainer(pos)
return true
}
return false
}
// EncodeNBT ...
func (f Furnace) EncodeNBT() map[string]interface{} {
if f.smelter == nil {
//noinspection GoAssignmentToReceiver
f = NewFurnace(f.Facing)
}
remaining, maximum, cook := f.Durations()
return map[string]interface{}{
"BurnTime": int16(remaining.Milliseconds() / 50),
"CookTime": int16(cook.Milliseconds() / 50),
"BurnDuration": int16(maximum.Milliseconds() / 50),
"StoredXPInt": int16(f.Experience()),
"Items": nbtconv.InvToNBT(f.Inventory()),
"id": "Furnace",
}
}
// DecodeNBT ...
func (f Furnace) DecodeNBT(data map[string]interface{}) interface{} {
remaining := nbtconv.TickDuration[int16](data, "BurnTime")
maximum := nbtconv.TickDuration[int16](data, "BurnDuration")
cook := nbtconv.TickDuration[int16](data, "CookTime")
xp := int(nbtconv.Int16(data, "StoredXPInt"))
lit := f.Lit
//noinspection GoAssignmentToReceiver
f = NewFurnace(f.Facing)
f.Lit = lit
f.setExperience(xp)
f.setDurations(remaining, maximum, cook)
nbtconv.InvFromNBT(f.Inventory(), nbtconv.Slice(data, "Items"))
return f
}
// allFurnaces ...
func allFurnaces() (furnaces []world.Block) {
for _, face := range cube.Directions() {
furnaces = append(furnaces, Furnace{Facing: face})
furnaces = append(furnaces, Furnace{Facing: face, Lit: true})
}
return
}