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

Implement unbreakable item #971

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions server/internal/nbtconv/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func MapItem(x map[string]any, k string) item.Stack {
readEnchantments(tag, &s)
readDisplay(tag, &s)
readDragonflyData(tag, &s)
readUnbreakable(tag, &s)
return s
}
return item.Stack{}
Expand All @@ -176,6 +177,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 @@ -281,3 +283,9 @@ func readDragonflyData(m map[string]any, s *item.Stack) {
}
}
}

// readDamage 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) {
*s = s.WithUnbreakable(Bool(m, "Unbreakable"))
}
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)
}
}
20 changes: 18 additions & 2 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,7 +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 {
if !ok || s.unbreakable {
// Not a durable item.
return s
}
Expand Down Expand Up @@ -146,6 +147,21 @@ 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
}

// WithUnbreakable returns a copy of the Stack with the unbreakable tag set to the value passed. If the item
// does not implement the Durable interface, the original stack is returned.
func (s Stack) WithUnbreakable(u bool) Stack {
if _, ok := s.Item().(Durable); !ok {
return s
}
s.unbreakable = u
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
Loading