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 all 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.ArmourSmithingTemplateFromString(pattern),
Material: item.ArmourTrimMaterialFromString(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 armour trim of an item to a map for NBT encoding.
func writeArmourTrim(m map[string]any, s item.Stack) {
if t, ok := s.ArmourTrim(); ok {
m["Trim"] = map[string]any{
"Material": t.Material.TrimMaterial(),
"Pattern": t.Template.String(),
}
}
}

// 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
12 changes: 12 additions & 0 deletions server/item/amethyst_shard.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package item

import "github.com/sandertv/gophertunnel/minecraft/text"

// AmethystShard is a crystalline mineral obtained from mining a fully grown amethyst cluster.
type AmethystShard struct{}

// EncodeItem ...
func (AmethystShard) EncodeItem() (name string, meta int16) {
return "minecraft:amethyst_shard", 0
}

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

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

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

type ArmourTrim struct {
Template ArmourSmithingTemplate
Material ArmourTrimMaterial
}

type ArmourTrimMaterial interface {
// TrimMaterial returns the material name used for reading and writing trim data.
TrimMaterial() string
// MaterialColour returns the colour code used for internal text formatting.
MaterialColour() string
}

// ArmourTrimMaterialFromString returns a TrimMaterial from a string.
func ArmourTrimMaterialFromString(name string) ArmourTrimMaterial {
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")
}

// ArmourTrimMaterials returns all the items that can be trim materials.
func ArmourTrimMaterials() []world.Item {
return []world.Item{
AmethystShard{},
CopperIngot{},
Diamond{},
Emerald{},
GoldIngot{},
IronIngot{},
LapisLazuli{},
NetheriteIngot{},
NetherQuartz{},
}
}
12 changes: 12 additions & 0 deletions server/item/copper_ingot.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package item

import "github.com/sandertv/gophertunnel/minecraft/text"

// CopperIngot is a metal ingot melted from copper ore.
type CopperIngot struct{}

// EncodeItem ...
func (c CopperIngot) EncodeItem() (name string, meta int16) {
return "minecraft:copper_ingot", 0
}

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

// MaterialColour ...
func (CopperIngot) MaterialColour() string {
return text.Copper
}
12 changes: 12 additions & 0 deletions server/item/diamond.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package item

import "github.com/sandertv/gophertunnel/minecraft/text"

// Diamond is a rare mineral obtained from diamond ore or loot chests.
type Diamond struct{}

Expand All @@ -8,6 +10,16 @@ func (Diamond) EncodeItem() (name string, meta int16) {
return "minecraft:diamond", 0
}

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

// MaterialColour ...
func (Diamond) MaterialColour() string {
return text.Diamond
}

// PayableForBeacon ...
func (Diamond) PayableForBeacon() bool {
return true
Expand Down
12 changes: 12 additions & 0 deletions server/item/emerald.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package item

import "github.com/sandertv/gophertunnel/minecraft/text"

// Emerald is a rare mineral obtained from emerald ore or from villagers.
type Emerald struct{}

Expand All @@ -8,6 +10,16 @@ func (Emerald) EncodeItem() (name string, meta int16) {
return "minecraft:emerald", 0
}

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

// MaterialColour ...
func (Emerald) MaterialColour() string {
return text.Emerald
}

// PayableForBeacon ...
func (Emerald) PayableForBeacon() bool {
return true
Expand Down
12 changes: 12 additions & 0 deletions server/item/gold_ingot.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package item

import "github.com/sandertv/gophertunnel/minecraft/text"

// GoldIngot is a metal ingot melted from raw gold or obtained from loot chests.
type GoldIngot struct{}

Expand All @@ -8,6 +10,16 @@ func (GoldIngot) EncodeItem() (name string, meta int16) {
return "minecraft:gold_ingot", 0
}

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

// MaterialColour ...
func (GoldIngot) MaterialColour() string {
return text.Gold
}

// PayableForBeacon ...
func (GoldIngot) PayableForBeacon() bool {
return true
Expand Down
12 changes: 12 additions & 0 deletions server/item/iron_ingot.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package item

import "github.com/sandertv/gophertunnel/minecraft/text"

// IronIngot is a metal ingot melted from raw iron or obtained from loot chests.
type IronIngot struct{}

Expand All @@ -8,6 +10,16 @@ func (IronIngot) EncodeItem() (name string, meta int16) {
return "minecraft:iron_ingot", 0
}

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

// MaterialColour ...
func (IronIngot) MaterialColour() string {
return text.Iron
}

// PayableForBeacon ...
func (IronIngot) PayableForBeacon() bool {
return true
Expand Down
12 changes: 12 additions & 0 deletions server/item/lapis_lazuli.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package item

import "github.com/sandertv/gophertunnel/minecraft/text"

// LapisLazuli is a mineral used for enchanting and decoration.
type LapisLazuli struct{}

// EncodeItem ...
func (LapisLazuli) EncodeItem() (name string, meta int16) {
return "minecraft:lapis_lazuli", 0
}

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

// MaterialColour ...
func (LapisLazuli) MaterialColour() string {
return text.Lapis
}
12 changes: 12 additions & 0 deletions server/item/nether_quartz.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package item

import "github.com/sandertv/gophertunnel/minecraft/text"

// NetherQuartz is a smooth, white mineral found in the Nether.
type NetherQuartz struct{}

// EncodeItem ...
func (NetherQuartz) EncodeItem() (name string, meta int16) {
return "minecraft:quartz", 0
}

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

// MaterialColour ...
func (NetherQuartz) MaterialColour() string {
return text.Quartz
}
12 changes: 12 additions & 0 deletions server/item/netherite_ingot.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package item

import "github.com/sandertv/gophertunnel/minecraft/text"

// NetheriteIngot is a rare mineral crafted with 4 pieces of netherite scrap and 4 gold ingots.
type NetheriteIngot struct{}

Expand All @@ -8,6 +10,16 @@ func (NetheriteIngot) EncodeItem() (name string, meta int16) {
return "minecraft:netherite_ingot", 0
}

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

// MaterialColour ...
func (NetheriteIngot) MaterialColour() string {
return text.Netherite
}

// PayableForBeacon ...
func (NetheriteIngot) PayableForBeacon() bool {
return true
Expand Down
26 changes: 19 additions & 7 deletions server/item/recipe/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,34 @@ func NewShapeless(input []Item, output item.Stack, block string) Shapeless {
}}
}

// Smithing represents a recipe only craftable on a smithing table.
type Smithing struct {
// SmithingTransform represents a recipe only craftable on a smithing table.
type SmithingTransform struct {
recipe
}

// NewSmithing creates a new smithing recipe and returns it. The recipe can only be crafted on the block passed in the
// parameters. If the block given a crafting table, the recipe can also be crafted in the 2x2 crafting grid in the
// player's inventory.
func NewSmithing(base, addition, template Item, output item.Stack, block string) Smithing {
return Smithing{recipe: recipe{
// NewSmithingTransform creates a new smithing recipe and returns it.
func NewSmithingTransform(base, addition, template Item, output item.Stack, block string) SmithingTransform {
TwistedAsylumMC marked this conversation as resolved.
Show resolved Hide resolved
return SmithingTransform{recipe: recipe{
input: []Item{base, addition, template},
output: []item.Stack{output},
block: block,
}}
}

// SmithingTrim represents a recipe only craftable on a smithing table using an armour trim.
type SmithingTrim struct {
recipe
}

// NewSmithingTrim creates a new smithing trim recipe and returns it. This is
// almost identical to SmithingTransform except there is no output item.
func NewSmithingTrim(base, addition, template Item, block string) SmithingTrim {
return SmithingTrim{recipe: recipe{
input: []Item{base, addition, template},
block: block,
}}
}

// Shaped is a recipe that has a specific shape that must be used to craft the output of the recipe.
type Shaped struct {
recipe
Expand Down
Binary file modified server/item/recipe/smithing_data.nbt
Binary file not shown.
Binary file added server/item/recipe/smithing_trim_data.nbt
Binary file not shown.
Loading
Loading