-
Notifications
You must be signed in to change notification settings - Fork 141
/
armour.go
139 lines (119 loc) · 3.92 KB
/
armour.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
package inventory
import (
"fmt"
"github.com/df-mc/dragonfly/server/item"
)
// Armour represents an inventory for armour. It has 4 slots, one for a helmet, chestplate, leggings and
// boots respectively. NewArmour() must be used to create a valid armour inventory.
// Armour inventories, like normal Inventories, are safe for concurrent usage.
type Armour struct {
inv *Inventory
}
// NewArmour returns an armour inventory that is ready to be used. The zero value of an inventory.Armour is
// not valid for usage.
// The function passed is called when a slot is changed. It may be nil to not call anything.
func NewArmour(f func(slot int, item item.Stack)) *Armour {
inv := New(4, f)
inv.canAdd = canAddArmour
return &Armour{inv: inv}
}
// canAddArmour checks if the item passed can be worn as armour in the slot passed.
func canAddArmour(s item.Stack, slot int) bool {
if s.Empty() {
return true
}
switch slot {
case 0:
if h, ok := s.Item().(item.HelmetType); ok {
return h.Helmet()
}
case 1:
if c, ok := s.Item().(item.ChestplateType); ok {
return c.Chestplate()
}
case 2:
if l, ok := s.Item().(item.LeggingsType); ok {
return l.Leggings()
}
case 3:
if b, ok := s.Item().(item.BootsType); ok {
return b.Boots()
}
}
return false
}
// Set sets all individual pieces of armour in one go. It is equivalent to calling SetHelmet, SetChestplate, SetLeggings
// and SetBoots sequentially.
func (a *Armour) Set(helmet, chestplate, leggings, boots item.Stack) {
a.SetHelmet(helmet)
a.SetChestplate(chestplate)
a.SetLeggings(leggings)
a.SetBoots(boots)
}
// SetHelmet sets the item stack passed as the helmet in the inventory.
func (a *Armour) SetHelmet(helmet item.Stack) {
_ = a.inv.SetItem(0, helmet)
}
// Helmet returns the item stack set as helmet in the inventory.
func (a *Armour) Helmet() item.Stack {
i, _ := a.inv.Item(0)
return i
}
// SetChestplate sets the item stack passed as the chestplate in the inventory.
func (a *Armour) SetChestplate(chestplate item.Stack) {
_ = a.inv.SetItem(1, chestplate)
}
// Chestplate returns the item stack set as chestplate in the inventory.
func (a *Armour) Chestplate() item.Stack {
i, _ := a.inv.Item(1)
return i
}
// SetLeggings sets the item stack passed as the leggings in the inventory.
func (a *Armour) SetLeggings(leggings item.Stack) {
_ = a.inv.SetItem(2, leggings)
}
// Leggings returns the item stack set as leggings in the inventory.
func (a *Armour) Leggings() item.Stack {
i, _ := a.inv.Item(2)
return i
}
// SetBoots sets the item stack passed as the boots in the inventory.
func (a *Armour) SetBoots(boots item.Stack) {
_ = a.inv.SetItem(3, boots)
}
// Boots returns the item stack set as boots in the inventory.
func (a *Armour) Boots() item.Stack {
i, _ := a.inv.Item(3)
return i
}
// Slots returns all items (including) air of the armour inventory in the order of helmet, chestplate, leggings,
// boots.
func (a *Armour) Slots() []item.Stack {
return a.inv.Slots()
}
// Items returns a slice of all non-empty armour items equipped.
func (a *Armour) Items() []item.Stack {
return a.inv.Items()
}
// Clear clears the armour inventory, removing all items currently present.
func (a *Armour) Clear() {
a.inv.Clear()
}
// String converts the armour to a readable string representation.
func (a *Armour) String() string {
return fmt.Sprintf("{helmet: %v, chestplate: %v, leggings: %v, boots: %v}", a.Helmet(), a.Chestplate(), a.Leggings(), a.Boots())
}
// Inventory returns the underlying Inventory instance.
func (a *Armour) Inventory() *Inventory {
return a.inv
}
// Handle assigns a Handler to an Armour inventory so that its methods are called for the respective events. Nil may be
// passed to set the default NopHandler.
// Handle is the equivalent of calling (*Armour).Inventory().Handle.
func (a *Armour) Handle(h Handler) {
a.inv.Handle(h)
}
// Close closes the armour inventory, removing the slot change function.
func (a *Armour) Close() error {
return a.inv.Close()
}