Skip to content

Commit b02583e

Browse files
committed
item/stack.go: Moved armour trims into item.Helmet, item.Chestplate, item.Leggings and item.Boots.
1 parent 5136cb2 commit b02583e

File tree

12 files changed

+176
-153
lines changed

12 files changed

+176
-153
lines changed

server/internal/nbtconv/read.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ func MapItem(x map[string]any, k string) item.Stack {
148148
}
149149

150150
s := readItemStack(m, tag)
151-
readArmourTrim(tag, &s)
152151
readDamage(tag, &s, true)
153152
readEnchantments(tag, &s)
154153
readDisplay(tag, &s)
@@ -172,7 +171,6 @@ func Item(data map[string]any, s *item.Stack) item.Stack {
172171
s = &a
173172
}
174173

175-
readArmourTrim(tag, s)
176174
readAnvilCost(tag, s)
177175
readDamage(tag, s, disk)
178176
readDisplay(tag, s)
@@ -224,20 +222,6 @@ func readAnvilCost(m map[string]any, s *item.Stack) {
224222
*s = s.WithAnvilCost(int(Int32(m, "RepairCost")))
225223
}
226224

227-
// readArmourTrim reads the armour trim stored in the NBT and saves it to the item.Stack passed.
228-
func readArmourTrim(m map[string]any, s *item.Stack) {
229-
if trim, ok := m["Trim"].(map[string]any); ok {
230-
material, ok := trim["Material"].(string)
231-
pattern, ok2 := trim["Pattern"].(string)
232-
if ok && ok2 {
233-
*s = s.WithArmourTrim(item.ArmourTrim{
234-
Template: item.ArmourSmithingTemplateFromString(pattern),
235-
Material: item.ArmourTrimMaterialFromString(material),
236-
})
237-
}
238-
}
239-
}
240-
241225
// readEnchantments reads the enchantments stored in the ench tag of the NBT passed and stores it into an item.Stack.
242226
func readEnchantments(m map[string]any, s *item.Stack) {
243227
enchantments, ok := m["ench"].([]map[string]any)

server/internal/nbtconv/write.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func WriteItem(s item.Stack, disk bool) map[string]any {
1717
tag[k] = v
1818
}
1919
}
20-
writeArmourTrim(tag, s)
2120
writeAnvilCost(tag, s)
2221
writeDamage(tag, s, disk)
2322
writeDisplay(tag, s)
@@ -112,16 +111,6 @@ func writeEnchantments(m map[string]any, s item.Stack) {
112111
}
113112
}
114113

115-
// writeArmourTrim writes the armour trim of an item to a map for NBT encoding.
116-
func writeArmourTrim(m map[string]any, s item.Stack) {
117-
if t, ok := s.ArmourTrim(); ok {
118-
m["Trim"] = map[string]any{
119-
"Material": t.Material.TrimMaterial(),
120-
"Pattern": t.Template.String(),
121-
}
122-
}
123-
}
124-
125114
// writeDisplay writes the display name and lore of an item to a map for NBT encoding.
126115
func writeDisplay(m map[string]any, s item.Stack) {
127116
name, lore := s.CustomName(), s.Lore()

server/item/armour_trim.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,52 @@ package item
22

33
import "github.com/df-mc/dragonfly/server/world"
44

5+
// ArmourTrim is a decorative addition to an armour piece. It consists of a
6+
// template that specifies the pattern and a material that specifies the colour.
57
type ArmourTrim struct {
6-
Template ArmourSmithingTemplate
8+
Template SmithingTemplateType
79
Material ArmourTrimMaterial
810
}
911

12+
// Zero checks if an ArmourTrim is considered zero: Either its material is nil
13+
// or its template TemplateNetheriteUpgrade.
14+
func (trim ArmourTrim) Zero() bool {
15+
return trim.Material == nil || trim.Template == TemplateNetheriteUpgrade()
16+
}
17+
18+
// ArmourTrimMaterial is the material of an ArmourTrim, such as an IronIngot,
19+
// that modifies the colour of an ArmourTrim.
1020
type ArmourTrimMaterial interface {
1121
// TrimMaterial returns the material name used for reading and writing trim data.
1222
TrimMaterial() string
1323
// MaterialColour returns the colour code used for internal text formatting.
1424
MaterialColour() string
1525
}
1626

17-
// ArmourTrimMaterialFromString returns a TrimMaterial from a string.
18-
func ArmourTrimMaterialFromString(name string) ArmourTrimMaterial {
27+
// trimMaterialFromString returns a TrimMaterial from a string.
28+
func trimMaterialFromString(name string) (ArmourTrimMaterial, bool) {
1929
switch name {
2030
case "amethyst":
21-
return AmethystShard{}
31+
return AmethystShard{}, true
2232
case "copper":
23-
return CopperIngot{}
33+
return CopperIngot{}, true
2434
case "diamond":
25-
return Diamond{}
35+
return Diamond{}, true
2636
case "emerald":
27-
return Emerald{}
37+
return Emerald{}, true
2838
case "gold":
29-
return GoldIngot{}
39+
return GoldIngot{}, true
3040
case "iron":
31-
return IronIngot{}
41+
return IronIngot{}, true
3242
case "lapis":
33-
return LapisLazuli{}
43+
return LapisLazuli{}, true
3444
case "netherite":
35-
return NetheriteIngot{}
45+
return NetheriteIngot{}, true
3646
case "quartz":
37-
return NetherQuartz{}
47+
return NetherQuartz{}, true
3848
}
39-
4049
// TODO: add redstone material once pr is merged
41-
42-
panic("should not happen")
50+
return nil, false
4351
}
4452

4553
// ArmourTrimMaterials returns all the items that can be trim materials.
@@ -56,3 +64,9 @@ func ArmourTrimMaterials() []world.Item {
5664
NetherQuartz{},
5765
}
5866
}
67+
68+
// Trimmable represents an item, generally Armour, that can have an ArmourTrim
69+
// applied to it in a smithing table.
70+
type Trimmable interface {
71+
WithTrim(trim ArmourTrim) world.Item
72+
}

server/item/boots.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
type Boots struct {
1111
// Tier is the tier of the boots.
1212
Tier ArmourTier
13+
// Trim specifies the trim of the armour.
14+
Trim ArmourTrim
1315
}
1416

1517
// Use handles the auto-equipping of boots in the armour slot when using it.
@@ -80,6 +82,12 @@ func (b Boots) Boots() bool {
8082
return true
8183
}
8284

85+
// WithTrim ...
86+
func (b Boots) WithTrim(trim ArmourTrim) world.Item {
87+
b.Trim = trim
88+
return b
89+
}
90+
8391
// EncodeItem ...
8492
func (b Boots) EncodeItem() (name string, meta int16) {
8593
return "minecraft:" + b.Tier.Name() + "_boots", 0
@@ -93,13 +101,16 @@ func (b Boots) DecodeNBT(data map[string]any) any {
93101
b.Tier = t
94102
}
95103
}
104+
b.Trim = readTrim(data)
96105
return b
97106
}
98107

99108
// EncodeNBT ...
100109
func (b Boots) EncodeNBT() map[string]any {
110+
m := map[string]any{}
101111
if t, ok := b.Tier.(ArmourTierLeather); ok && t.Colour != (color.RGBA{}) {
102-
return map[string]any{"customColor": int32FromRGBA(t.Colour)}
112+
m["customColor"] = int32FromRGBA(t.Colour)
103113
}
104-
return nil
114+
writeTrim(m, b.Trim)
115+
return m
105116
}

server/item/chestplate.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
type Chestplate struct {
1111
// Tier is the tier of the chestplate.
1212
Tier ArmourTier
13+
// Trim specifies the trim of the armour.
14+
Trim ArmourTrim
1315
}
1416

1517
// Use handles the using of a chestplate to auto-equip it in the designated armour slot.
@@ -82,6 +84,12 @@ func (c Chestplate) Chestplate() bool {
8284
return true
8385
}
8486

87+
// WithTrim ...
88+
func (c Chestplate) WithTrim(trim ArmourTrim) world.Item {
89+
c.Trim = trim
90+
return c
91+
}
92+
8593
// EncodeItem ...
8694
func (c Chestplate) EncodeItem() (name string, meta int16) {
8795
return "minecraft:" + c.Tier.Name() + "_chestplate", 0
@@ -95,13 +103,16 @@ func (c Chestplate) DecodeNBT(data map[string]any) any {
95103
c.Tier = t
96104
}
97105
}
106+
c.Trim = readTrim(data)
98107
return c
99108
}
100109

101110
// EncodeNBT ...
102111
func (c Chestplate) EncodeNBT() map[string]any {
112+
m := map[string]any{}
103113
if t, ok := c.Tier.(ArmourTierLeather); ok && t.Colour != (color.RGBA{}) {
104-
return map[string]any{"customColor": int32FromRGBA(t.Colour)}
114+
m["customColor"] = int32FromRGBA(t.Colour)
105115
}
106-
return nil
116+
writeTrim(m, c.Trim)
117+
return m
107118
}

server/item/helmet.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
type Helmet struct {
1111
// Tier is the tier of the armour.
1212
Tier ArmourTier
13+
// Trim specifies the trim of the armour.
14+
Trim ArmourTrim
1315
}
1416

1517
// Use handles the using of a helmet to auto-equip it in an armour slot.
@@ -80,6 +82,12 @@ func (h Helmet) Helmet() bool {
8082
return true
8183
}
8284

85+
// WithTrim ...
86+
func (h Helmet) WithTrim(trim ArmourTrim) world.Item {
87+
h.Trim = trim
88+
return h
89+
}
90+
8391
// EncodeItem ...
8492
func (h Helmet) EncodeItem() (name string, meta int16) {
8593
return "minecraft:" + h.Tier.Name() + "_helmet", 0
@@ -93,13 +101,38 @@ func (h Helmet) DecodeNBT(data map[string]any) any {
93101
h.Tier = t
94102
}
95103
}
104+
h.Trim = readTrim(data)
96105
return h
97106
}
98107

99108
// EncodeNBT ...
100109
func (h Helmet) EncodeNBT() map[string]any {
110+
m := map[string]any{}
101111
if t, ok := h.Tier.(ArmourTierLeather); ok && t.Colour != (color.RGBA{}) {
102-
return map[string]any{"customColor": int32FromRGBA(t.Colour)}
112+
m["customColor"] = int32FromRGBA(t.Colour)
113+
}
114+
writeTrim(m, h.Trim)
115+
return m
116+
}
117+
118+
func readTrim(m map[string]any) ArmourTrim {
119+
if trim, ok := m["Trim"].(map[string]any); ok {
120+
material, _ := trim["Material"].(string)
121+
pattern, _ := trim["Pattern"].(string)
122+
template, ok := smithingTemplateFromString(pattern)
123+
trimMaterial, ok2 := trimMaterialFromString(material)
124+
if ok && ok2 {
125+
return ArmourTrim{Template: template, Material: trimMaterial}
126+
}
127+
}
128+
return ArmourTrim{}
129+
}
130+
131+
func writeTrim(m map[string]any, t ArmourTrim) {
132+
if !t.Zero() {
133+
m["Trim"] = map[string]any{
134+
"Material": t.Material.TrimMaterial(),
135+
"Pattern": t.Template.String(),
136+
}
103137
}
104-
return nil
105138
}

server/item/leggings.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
type Leggings struct {
1111
// Tier is the tier of the leggings.
1212
Tier ArmourTier
13+
// Trim specifies the trim of the armour.
14+
Trim ArmourTrim
1315
}
1416

1517
// Use handles the auto-equipping of leggings in an armour slot by using the item.
@@ -84,6 +86,12 @@ func (l Leggings) SmeltInfo() SmeltInfo {
8486
return SmeltInfo{}
8587
}
8688

89+
// WithTrim ...
90+
func (l Leggings) WithTrim(trim ArmourTrim) world.Item {
91+
l.Trim = trim
92+
return l
93+
}
94+
8795
// EncodeItem ...
8896
func (l Leggings) EncodeItem() (name string, meta int16) {
8997
return "minecraft:" + l.Tier.Name() + "_leggings", 0
@@ -97,13 +105,16 @@ func (l Leggings) DecodeNBT(data map[string]any) any {
97105
l.Tier = t
98106
}
99107
}
108+
l.Trim = readTrim(data)
100109
return l
101110
}
102111

103112
// EncodeNBT ...
104113
func (l Leggings) EncodeNBT() map[string]any {
114+
m := map[string]any{}
105115
if t, ok := l.Tier.(ArmourTierLeather); ok && t.Colour != (color.RGBA{}) {
106-
return map[string]any{"customColor": int32FromRGBA(t.Colour)}
116+
m["customColor"] = int32FromRGBA(t.Colour)
107117
}
108-
return nil
118+
writeTrim(m, l.Trim)
119+
return m
109120
}

server/item/smithing_template.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package item
22

3-
// SmithingTemplate is an item used in smithing tables to alter tools and armour.
4-
// They are consumed when used, but can be duplicated using an existing template, its material and diamonds.
3+
// SmithingTemplate is an item used in smithing tables to alter tools and
4+
// armour. They are consumed when used, but can be duplicated using an existing
5+
// template, its material and diamonds.
56
type SmithingTemplate struct {
67
// Template the upgrade item used in smithing tables.
7-
Template ArmourSmithingTemplate
8+
Template SmithingTemplateType
89
}
910

1011
// EncodeItem ...

0 commit comments

Comments
 (0)