Skip to content

Commit db2d6a4

Browse files
FDUTCHHashimTheArabRestartFU
authored
item/stack.go: add option to apply incompatible enchantments (df-mc#1117)
* add ability to enchant item with incompatible enchantments * accept variadic arguments * requested * rename function * Update server/item/stack.go Co-authored-by: RestartFU <45609733+RestartFU@users.noreply.github.com> * Update server/internal/nbtconv/read.go * Update server/item/stack.go --------- Co-authored-by: Hashim <hashimssoftware@gmail.com> Co-authored-by: RestartFU <45609733+RestartFU@users.noreply.github.com>
1 parent 0121d71 commit db2d6a4

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

server/internal/nbtconv/read.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package nbtconv
33
import (
44
"bytes"
55
"encoding/gob"
6+
"time"
7+
68
"github.com/df-mc/dragonfly/server/block/cube"
79
"github.com/df-mc/dragonfly/server/item"
810
"github.com/df-mc/dragonfly/server/world"
911
"github.com/go-gl/mathgl/mgl64"
1012
"golang.org/x/exp/constraints"
11-
"time"
1213
)
1314

1415
// Bool reads a uint8 value from a map at key k and returns true if it equals 1.
@@ -225,7 +226,7 @@ func readEnchantments(m map[string]any, s *item.Stack) {
225226
}
226227
for _, ench := range enchantments {
227228
if t, ok := item.EnchantmentByID(int(Int16(ench, "id"))); ok {
228-
*s = s.WithEnchantments(item.NewEnchantment(t, int(Int16(ench, "lvl"))))
229+
*s = s.WithForcedEnchantments(item.NewEnchantment(t, int(Int16(ench, "lvl"))))
229230
}
230231
}
231232
}

server/item/stack.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,17 @@ func (s Stack) WithEnchantments(enchants ...Enchantment) Stack {
281281
return s
282282
}
283283

284+
// WithForcedEnchantments returns the current stack with the passed enchantments applied,
285+
// bypassing compatibility checks that would normally prevent incompatible enchantments
286+
// from being applied together.
287+
func (s Stack) WithForcedEnchantments(enchants ...Enchantment) Stack {
288+
s.enchantments = cloneMap(s.enchantments)
289+
for _, enchant := range enchants {
290+
s.enchantments[enchant.t] = enchant
291+
}
292+
return s
293+
}
294+
284295
// WithoutEnchantments returns the current stack but with the passed enchantments removed.
285296
func (s Stack) WithoutEnchantments(enchants ...EnchantmentType) Stack {
286297
s.enchantments = cloneMap(s.enchantments)

0 commit comments

Comments
 (0)