From 69cca3adcac5310a1d9826f2e3b464491e981028 Mon Sep 17 00:00:00 2001 From: Sandertv Date: Tue, 24 Dec 2024 17:35:57 +0100 Subject: [PATCH] item/stack.go: Fixed panic when copying empty enchantments/data map. --- server/item/stack.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/server/item/stack.go b/server/item/stack.go index ee1681172..8bf120cb8 100644 --- a/server/item/stack.go +++ b/server/item/stack.go @@ -211,7 +211,7 @@ func (s Stack) Lore() []string { // WithValue stores Values by encoding them using the encoding/gob package. Users of WithValue must ensure // that their value is valid for encoding with this package. func (s Stack) WithValue(key string, val any) Stack { - s.data = maps.Clone(s.data) + s.data = cloneMap(s.data) if val != nil { s.data[key] = val } else { @@ -233,7 +233,7 @@ func (s Stack) WithEnchantments(enchants ...Enchantment) Stack { if _, ok := s.item.(Book); ok { s.item = EnchantedBook{} } - s.enchantments = maps.Clone(s.enchantments) + s.enchantments = cloneMap(s.enchantments) for _, enchant := range enchants { if _, ok := s.Item().(EnchantedBook); !ok && !enchant.t.CompatibleWithItem(s.item) { // Enchantment is not compatible with the item. @@ -246,7 +246,7 @@ func (s Stack) WithEnchantments(enchants ...Enchantment) Stack { // WithoutEnchantments returns the current stack but with the passed enchantments removed. func (s Stack) WithoutEnchantments(enchants ...EnchantmentType) Stack { - s.enchantments = maps.Clone(s.enchantments) + s.enchantments = cloneMap(s.enchantments) for _, enchant := range enchants { delete(s.enchantments, enchant) } @@ -386,6 +386,14 @@ func (s Stack) Values() map[string]any { return maps.Clone(s.data) } +// cloneMap calls maps.Clone, but initialises m if it does not yet exist. +func cloneMap[M ~map[K]V, K comparable, V any](m M) M { + if m == nil { + m = make(M) + } + return maps.Clone(m) +} + // stackID is a counter for unique stack IDs. var stackID = new(int32)