forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jukebox.go
108 lines (93 loc) · 2.84 KB
/
jukebox.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
package block
import (
"fmt"
"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"
"time"
)
// Jukebox is a block used to play music discs.
type Jukebox struct {
solid
bass
// Item is the music disc played by the jukebox.
Item item.Stack
}
// FuelInfo ...
func (j Jukebox) FuelInfo() item.FuelInfo {
return newFuelInfo(time.Second * 15)
}
// BreakInfo ...
func (j Jukebox) BreakInfo() BreakInfo {
d := []item.Stack{item.NewStack(Jukebox{}, 1)}
if !j.Item.Empty() {
d = append(d, j.Item)
}
return newBreakInfo(0.8, alwaysHarvestable, axeEffective, simpleDrops(d...)).withBreakHandler(func(pos cube.Pos, w *world.World, u item.User) {
if _, hasDisc := j.Disc(); hasDisc {
w.PlaySound(pos.Vec3Centre(), sound.MusicDiscEnd{})
}
})
}
// jukeboxUser represents an item.User that can use a jukebox.
type jukeboxUser interface {
item.User
// SendJukeboxPopup sends a jukebox popup to the item.User.
SendJukeboxPopup(a ...any)
}
// Activate ...
func (j Jukebox) Activate(pos cube.Pos, _ cube.Face, w *world.World, u item.User, ctx *item.UseContext) bool {
if _, hasDisc := j.Disc(); hasDisc {
dropItem(w, j.Item, pos.Side(cube.FaceUp).Vec3Middle())
j.Item = item.Stack{}
w.SetBlock(pos, j, nil)
w.PlaySound(pos.Vec3Centre(), sound.MusicDiscEnd{})
} else if held, _ := u.HeldItems(); !held.Empty() {
if m, ok := held.Item().(item.MusicDisc); ok {
j.Item = held
w.SetBlock(pos, j, nil)
w.PlaySound(pos.Vec3Centre(), sound.MusicDiscEnd{})
ctx.SubtractFromCount(1)
w.PlaySound(pos.Vec3Centre(), sound.MusicDiscPlay{DiscType: m.DiscType})
if u, ok := u.(jukeboxUser); ok {
u.SendJukeboxPopup(fmt.Sprintf("Now playing: %v - %v", m.DiscType.Author(), m.DiscType.DisplayName()))
}
}
}
return true
}
// Disc returns the currently playing music disc
func (j Jukebox) Disc() (sound.DiscType, bool) {
if !j.Item.Empty() {
if m, ok := j.Item.Item().(item.MusicDisc); ok {
return m.DiscType, true
}
}
return sound.DiscType{}, false
}
// EncodeNBT ...
func (j Jukebox) EncodeNBT() map[string]any {
m := map[string]any{"id": "Jukebox"}
if _, hasDisc := j.Disc(); hasDisc {
m["RecordItem"] = nbtconv.WriteItem(j.Item, true)
}
return m
}
// DecodeNBT ...
func (j Jukebox) DecodeNBT(data map[string]any) any {
s := nbtconv.MapItem(data, "RecordItem")
if _, ok := s.Item().(item.MusicDisc); ok {
j.Item = s
}
return j
}
// EncodeItem ...
func (Jukebox) EncodeItem() (name string, meta int16) {
return "minecraft:jukebox", 0
}
// EncodeBlock ...
func (Jukebox) EncodeBlock() (string, map[string]any) {
return "minecraft:jukebox", nil
}