-
Notifications
You must be signed in to change notification settings - Fork 141
/
firework.go
86 lines (74 loc) · 2.62 KB
/
firework.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
package item
import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"math/rand"
"time"
)
// Firework is an item (and entity) used for creating decorative explosions, boosting when flying with elytra, and
// loading into a crossbow as ammunition.
type Firework struct {
// Duration is the flight duration of the firework.
Duration time.Duration
// Explosions is the list of explosions the firework should create when launched.
Explosions []FireworkExplosion
}
// Use ...
func (f Firework) Use(w *world.World, user User, ctx *UseContext) bool {
if g, ok := user.(interface {
Gliding() bool
}); !ok || !g.Gliding() {
return false
}
pos := user.Position()
w.PlaySound(pos, sound.FireworkLaunch{})
create := w.EntityRegistry().Config().Firework
w.AddEntity(create(pos, user.Rotation(), true, f, user))
ctx.SubtractFromCount(1)
return true
}
// UseOnBlock ...
func (f Firework) UseOnBlock(blockPos cube.Pos, _ cube.Face, clickPos mgl64.Vec3, w *world.World, user User, ctx *UseContext) bool {
pos := blockPos.Vec3().Add(clickPos)
create := w.EntityRegistry().Config().Firework
w.AddEntity(create(pos, cube.Rotation{rand.Float64() * 360, 90}, false, f, user))
w.PlaySound(pos, sound.FireworkLaunch{})
ctx.SubtractFromCount(1)
return true
}
// EncodeNBT ...
func (f Firework) EncodeNBT() map[string]any {
explosions := make([]any, 0, len(f.Explosions))
for _, explosion := range f.Explosions {
explosions = append(explosions, explosion.EncodeNBT())
}
return map[string]any{"Fireworks": map[string]any{
"Explosions": explosions,
"Flight": uint8((f.Duration/10 - time.Millisecond*50).Milliseconds() / 50),
}}
}
// DecodeNBT ...
func (f Firework) DecodeNBT(data map[string]any) any {
if fireworks, ok := data["Fireworks"].(map[string]any); ok {
if explosions, ok := fireworks["Explosions"].([]any); ok {
f.Explosions = make([]FireworkExplosion, len(explosions))
for i, explosion := range f.Explosions {
f.Explosions[i] = explosion.DecodeNBT(explosions[i].(map[string]any)).(FireworkExplosion)
}
}
if durationTicks, ok := fireworks["Flight"].(uint8); ok {
f.Duration = (time.Duration(durationTicks)*time.Millisecond*50 + time.Millisecond*50) * 10
}
}
return f
}
// RandomisedDuration returns the randomised flight duration of the firework.
func (f Firework) RandomisedDuration() time.Duration {
return f.Duration + time.Duration(rand.Intn(int(time.Millisecond*600)))
}
// EncodeItem ...
func (Firework) EncodeItem() (name string, meta int16) {
return "minecraft:firework_rocket", 0
}