Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smithing templates + Table and Armour Trims #861

Merged
merged 16 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions server/internal/nbtconv/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func MapItem(x map[string]any, k string) item.Stack {
}

s := readItemStack(m, tag)
readArmourTrim(tag, &s)
readDamage(tag, &s, true)
readEnchantments(tag, &s)
readDisplay(tag, &s)
Expand All @@ -171,6 +172,7 @@ func Item(data map[string]any, s *item.Stack) item.Stack {
s = &a
}

readArmourTrim(tag, s)
readAnvilCost(tag, s)
readDamage(tag, s, disk)
readDisplay(tag, s)
Expand Down Expand Up @@ -222,6 +224,20 @@ func readAnvilCost(m map[string]any, s *item.Stack) {
*s = s.WithAnvilCost(int(Int32(m, "RepairCost")))
}

// readArmourTrim reads the armour trim stored in the NBT and saves it to the item.Stack passed.
func readArmourTrim(m map[string]any, s *item.Stack) {
if trim, ok := m["Trim"].(map[string]any); ok {
material, ok := trim["Material"].(string)
pattern, ok2 := trim["Pattern"].(string)
if ok && ok2 {
*s = s.WithArmourTrim(item.ArmourTrim{
Template: item.TemplateFromString(pattern),
Material: item.MaterialFromString(material),
})
}
}
}

// readEnchantments reads the enchantments stored in the ench tag of the NBT passed and stores it into an item.Stack.
func readEnchantments(m map[string]any, s *item.Stack) {
enchantments, ok := m["ench"].([]map[string]any)
Expand Down
11 changes: 11 additions & 0 deletions server/internal/nbtconv/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func WriteItem(s item.Stack, disk bool) map[string]any {
tag[k] = v
}
}
writeArmourTrim(tag, s)
writeAnvilCost(tag, s)
writeDamage(tag, s, disk)
writeDisplay(tag, s)
Expand Down Expand Up @@ -111,6 +112,16 @@ func writeEnchantments(m map[string]any, s item.Stack) {
}
}

// writeArmourTrim writes the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writes the ...?

func writeArmourTrim(m map[string]any, s item.Stack) {
if t := s.ArmourTrim(); t != nil {
m["Trim"] = map[string]any{
"Material": t.Material.TrimMaterial(),
"Pattern": t.Template.Name,
}
}
}

// writeDisplay writes the display name and lore of an item to a map for NBT encoding.
func writeDisplay(m map[string]any, s item.Stack) {
name, lore := s.CustomName(), s.Lore()
Expand Down
10 changes: 10 additions & 0 deletions server/item/amethyst_shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ type AmethystShard struct{}
func (AmethystShard) EncodeItem() (name string, meta int16) {
return "minecraft:amethyst_shard", 0
}

// TrimMaterial ...
func (AmethystShard) TrimMaterial() string {
return "amethyst"
}

// MaterialColor ...
func (AmethystShard) MaterialColor() string {
return "u"
}
48 changes: 48 additions & 0 deletions server/item/armour_trim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package item

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

type ArmourTrim struct {
Template ArmourTrimTemplate
Material TrimMaterial
}

type TrimMaterial interface {
// TrimMaterial returns the material name used for reading and writing trim data.
TrimMaterial() string
// MaterialColor returns the color code used for internal text formatting. Use text.Colourf for proper formatting.
MaterialColor() string
}

// MaterialFromString returns a TrimMaterial from a string.
func MaterialFromString(name string) TrimMaterial {
switch name {
case "amethyst":
return AmethystShard{}
case "copper":
return CopperIngot{}
case "diamond":
return Diamond{}
case "emerald":
return Emerald{}
case "gold":
return GoldIngot{}
case "iron":
return IronIngot{}
case "lapis":
return LapisLazuli{}
case "netherite":
return NetheriteIngot{}
case "quartz":
return NetherQuartz{}
}

//TODO: add redstone material once pr is merged

panic("should not happen")
}

// TrimMaterials returns all the items that can be trim materials.
func TrimMaterials() []world.Item {
return []world.Item{AmethystShard{}, CopperIngot{}, Diamond{}, Emerald{}, GoldIngot{}, IronIngot{}, LapisLazuli{}, NetheriteIngot{}, NetherQuartz{}}
}
10 changes: 10 additions & 0 deletions server/item/copper_ingot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ type CopperIngot struct{}
func (c CopperIngot) EncodeItem() (name string, meta int16) {
return "minecraft:copper_ingot", 0
}

// TrimMaterial ...
func (CopperIngot) TrimMaterial() string {
return "copper"
}

// MaterialColor ...
func (CopperIngot) MaterialColor() string {
return "n"
}
10 changes: 10 additions & 0 deletions server/item/diamond.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ func (Diamond) EncodeItem() (name string, meta int16) {
return "minecraft:diamond", 0
}

// TrimMaterial ...
func (Diamond) TrimMaterial() string {
return "diamond"
}

// MaterialColor ...
func (Diamond) MaterialColor() string {
return "s"
}

// PayableForBeacon ...
func (Diamond) PayableForBeacon() bool {
return true
Expand Down
10 changes: 10 additions & 0 deletions server/item/emerald.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ func (Emerald) EncodeItem() (name string, meta int16) {
return "minecraft:emerald", 0
}

// TrimMaterial ...
func (Emerald) TrimMaterial() string {
return "emerald"
}

// MaterialColor ...
func (Emerald) MaterialColor() string {
return "q"
}

// PayableForBeacon ...
func (Emerald) PayableForBeacon() bool {
return true
Expand Down
10 changes: 10 additions & 0 deletions server/item/gold_ingot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ func (GoldIngot) EncodeItem() (name string, meta int16) {
return "minecraft:gold_ingot", 0
}

// TrimMaterial ...
func (GoldIngot) TrimMaterial() string {
return "gold"
}

// MaterialColor ...
func (GoldIngot) MaterialColor() string {
return "p"
}

// PayableForBeacon ...
func (GoldIngot) PayableForBeacon() bool {
return true
Expand Down
10 changes: 10 additions & 0 deletions server/item/iron_ingot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ func (IronIngot) EncodeItem() (name string, meta int16) {
return "minecraft:iron_ingot", 0
}

// TrimMaterial ...
func (IronIngot) TrimMaterial() string {
return "iron"
}

// MaterialColor ...
func (IronIngot) MaterialColor() string {
return "i"
}

// PayableForBeacon ...
func (IronIngot) PayableForBeacon() bool {
return true
Expand Down
10 changes: 10 additions & 0 deletions server/item/lapis_lazuli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ type LapisLazuli struct{}
func (LapisLazuli) EncodeItem() (name string, meta int16) {
return "minecraft:lapis_lazuli", 0
}

// TrimMaterial ...
func (LapisLazuli) TrimMaterial() string {
return "lapis"
}

// MaterialColor ...
func (LapisLazuli) MaterialColor() string {
return "t"
}
10 changes: 10 additions & 0 deletions server/item/nether_quartz.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ type NetherQuartz struct{}
func (NetherQuartz) EncodeItem() (name string, meta int16) {
return "minecraft:quartz", 0
}

// TrimMaterial ...
func (NetherQuartz) TrimMaterial() string {
return "quartz"
}

// MaterialColor ...
func (NetherQuartz) MaterialColor() string {
return "h"
}
10 changes: 10 additions & 0 deletions server/item/netherite_ingot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ func (NetheriteIngot) EncodeItem() (name string, meta int16) {
return "minecraft:netherite_ingot", 0
}

// TrimMaterial ...
func (NetheriteIngot) TrimMaterial() string {
return "netherite"
}

// MaterialColor ...
func (NetheriteIngot) MaterialColor() string {
return "j"
}

// PayableForBeacon ...
func (NetheriteIngot) PayableForBeacon() bool {
return true
Expand Down
4 changes: 4 additions & 0 deletions server/item/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ func init() {
world.RegisterItem(SplashPotion{Type: p})
world.RegisterItem(Potion{Type: p})
}
world.RegisterItem(Template{TemplateNetheriteUpgrade()})
for _, t := range Templates() {
world.RegisterItem(Template{Template: t})
}
for _, t := range ToolTiers() {
world.RegisterItem(Pickaxe{Tier: t})
world.RegisterItem(Axe{Tier: t})
Expand Down
19 changes: 19 additions & 0 deletions server/item/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type Stack struct {
data map[string]any

enchantments map[EnchantmentType]Enchantment

armorTrim *ArmourTrim
}

// NewStack returns a new stack using the item type and the count passed. NewStack panics if the count passed
Expand All @@ -42,6 +44,23 @@ func NewStack(t world.Item, count int) Stack {
return Stack{item: t, count: count, id: newID()}
}

// WithArmourTrim returns a new stack with the ArmorTrim passed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"ArmourTrim" instead of "ArmorTrim"

// This only applies if the stack is type Armour.
func (s Stack) WithArmourTrim(trim ArmourTrim) Stack {
if _, ok := s.item.(Armour); !ok {
return s
}

s.armorTrim = &trim
return s
}

// ArmourTrim returns the ArmourTrim.
// if this returns nil it does not have an armor trim, or it is not an armour
func (s Stack) ArmourTrim() *ArmourTrim {
return s.armorTrim
}

// Count returns the amount of items that is present on the stack. The count is guaranteed never to be
// negative.
func (s Stack) Count() int {
Expand Down
16 changes: 16 additions & 0 deletions server/item/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package item

// Template is an item used in smithing tables to alter tools and armor.
// They are consumed when used, but can be duplicated using an existing template, its material and diamonds.
type Template struct {
// Template the upgrade item used in smithing tables.
Template ArmourTrimTemplate
}

// EncodeItem ...
func (t Template) EncodeItem() (name string, meta int16) {
if t.Template == TemplateNetheriteUpgrade() {
return "minecraft:netherite_upgrade_smithing_template", 0
}
return "minecraft:" + t.Template.Name + "_armor_trim_smithing_template", 0
}
Loading
Loading