Skip to content

Implement unbreakable item #971

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

Merged
merged 9 commits into from
Feb 19, 2025
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
9 changes: 9 additions & 0 deletions server/internal/nbtconv/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func Item(data map[string]any, s *item.Stack) item.Stack {
readDisplay(tag, s)
readDragonflyData(tag, s)
readEnchantments(tag, s)
readUnbreakable(tag, s)
return *s
}

Expand Down Expand Up @@ -271,3 +272,11 @@ func readDragonflyData(m map[string]any, s *item.Stack) {
}
}
}

// readUnbreakable reads the unbreakable value stored in the NBT with the Unbreakable tag and saves it to the item.Stack
// passed.
func readUnbreakable(m map[string]any, s *item.Stack) {
if Bool(m, "Unbreakable") {
*s = s.AsUnbreakable()
}
}
8 changes: 8 additions & 0 deletions server/internal/nbtconv/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func WriteItem(s item.Stack, disk bool) map[string]any {
writeDisplay(tag, s)
writeDragonflyData(tag, s)
writeEnchantments(tag, s)
writeUnbreakable(tag, s)

data := make(map[string]any)
if disk {
Expand Down Expand Up @@ -146,3 +147,10 @@ func writeAnvilCost(m map[string]any, s item.Stack) {
m["RepairCost"] = int32(cost)
}
}

// writeUnbreakable writes the unbreakable tag to an item stack if it is unbreakable.
func writeUnbreakable(m map[string]any, s item.Stack) {
if s.Unbreakable() {
m["Unbreakable"] = byte(1)
}
}
35 changes: 30 additions & 5 deletions server/item/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type Stack struct {
customName string
lore []string

damage int
damage int
unbreakable bool

anvilCost int

Expand Down Expand Up @@ -98,8 +99,7 @@ func (s Stack) MaxDurability() int {
// get the maximum durability.
func (s Stack) Damage(d int) Stack {
durable, ok := s.Item().(Durable)
if !ok {
// Not a durable item.
if !ok || s.unbreakable {
return s
}
durability := s.Durability()
Expand All @@ -122,14 +122,13 @@ func (s Stack) Damage(d int) Stack {
}

// WithDurability returns a new item stack with the durability passed. If the item does not implement the
// Durable interface, WithDurability returns the original stack.
// Durable interface, the original stack is returned.
// The closer the durability d is to 0, the closer the item is to being broken. If a durability of 0 is passed,
// a stack with the item type of the BrokenItem is returned. If a durability is passed that exceeds the
// maximum durability, the stack returned will have the maximum durability.
func (s Stack) WithDurability(d int) Stack {
durable, ok := s.Item().(Durable)
if !ok {
// Not a durable item.
return s
}
maxDurability := durable.DurabilityInfo().MaxDurability
Expand All @@ -146,6 +145,31 @@ func (s Stack) WithDurability(d int) Stack {
return s
}

// Unbreakable checks if the item stack is unbreakable.
func (s Stack) Unbreakable() bool {
return s.unbreakable
}

// AsUnbreakable returns a copy of the Stack with the unbreakable tag set. If the item does not implement the
// Durable interface, the original stack is returned.
func (s Stack) AsUnbreakable() Stack {
if _, ok := s.Item().(Durable); !ok {
return s
}
s.unbreakable = true
return s
}

// AsBreakable returns a copy of the Stack without the unbreakable tag set. If the item does not implement the
// Durable interface, the original stack is returned.
func (s Stack) AsBreakable() Stack {
if _, ok := s.Item().(Durable); !ok {
return s
}
s.unbreakable = false
return s
}

// Empty checks if the stack is empty (has a count of 0).
func (s Stack) Empty() bool {
return s.Count() == 0 || s.item == nil
Expand Down Expand Up @@ -304,6 +328,7 @@ func (s Stack) WithItem(t world.Item) Stack {
WithLore(s.lore...).
WithEnchantments(s.Enchantments()...).
WithAnvilCost(s.anvilCost)
cp.unbreakable = s.unbreakable && s.MaxDurability() != -1
cp.data = s.data
return cp
}
Expand Down