#canAddEnchantment not working as previous version

1 messages · Page 1 of 1 (latest)

barren jewel
#

Hi there, after the update a few changes was made to the api enchantments and some of them I was not able to fix yet, do someone know how is the canAddEnchantment working now?

Before it was checking not only the the item is able to receive the enchant but also the level if it already have the enchantment, now it throws errors but I cant receive the true/false to interact and execute other scripts.

This is how it is right now: javascript if (!item.getComponent('enchantable').canAddEnchantment({ type: 'protection', level: 4 })) { world.sendMessage(`cannot be enchanted`) } else { world.sendMessage(`can be enchanted`) }Any idea how can I make it work?

This is what shows on console when[2024-02-07 00:46:12:417 ERROR] [Scripting] Unhandled promise rejection: EnchantmentLevelOutOfBoundsError: When trying to add enchantment instance - Tyring to set enchantment level to 8, range for type protection is [0 - 4].

brave rock
#

okay so it tells you the error

#

basically you already have protection 4 and you are trying to see if you can add 4 more levels

#

which is not possible

#

oh i get what u mean

#

yeah its odd that it doesnt return a boolean value

unborn heron
brazen pasture
#

i cant figure out the new beta either

barren jewel
barren jewel
#

I did few tests and seems lile the canAddEnchantment is broken, instead testing if the item can/not add enchantment, when the item "accept" the enchantment it is literally trying to add the enchantment on top of the existing one, so it is also trying to sum the total level generating the error ERROR] [Scripting] Unhandled promise rejection: EnchantmentLevelOutOfBoundsError: When trying to add enchantment instance - Tyring to set enchantment level to 8, range for type protection is [0 - 4].

devout gorge
#

then get the enchantment level first.

#

getEnchantment(enchantmentType: EnchantmentType | string): Enchantment | undefined

barren jewel
#

I manage to do some workaround, needed to do much more checks that before was done with a single line, anyway it is working as expected now```javascript
if (!item.getComponent('enchantable')) return player.sendMessage(§l§cThe item cannot be enchanted.)
if (charObj.MANACURRENT < enchantmentObj.MANA) return player.sendMessage(§l§cYou dont have enough mana to apply this enchantment.)
if (item.getComponent('enchantable').hasEnchantment(enchantmentObj.MINEID) && item.getComponent('enchantable').getEnchantment(enchantmentObj.MINEID)?.level == enchantmentObj.ENCLVL) return player.sendMessage(§l§cThe item already have the same enchantment.)
if (item.getComponent('enchantable').hasEnchantment(enchantmentObj.MINEID)) item.getComponent('enchantable').removeEnchantment(enchantmentObj.MINEID)
if (!item.getComponent('enchantable').canAddEnchantment({ type: enchantmentObj.MINEID, level: enchantmentObj.ENCLVL })) return player.sendMessage(§l§cThe item cannot be enchanted.)

    item.getComponent('enchantable').addEnchantment({ type: enchantmentObj.MINEID, level: enchantmentObj.ENCLVL })```

I basically have to check if the item has the enchantable component before, then test if it has the enchantment at same level I m applying to return the msg
if it has the same enchantment but on a lower level I have to remove the enchantment before otherwise the canAddEnchantment will generate errors
then the canAddEnchantment will check if the item can receive or not the enchantment
only after all these checks I can add the enchantment

jagged otter
#

Hey @barren jewel not sure if you found the actual cause, but the error is completely incorrect in what it's trying to tell you

#

I'm having the same problem, and the issue seems to be that you are trying to add an enchantment type that is already taken by the item

#

ex: You are trying to add the Smite enchantment to a sword already containing Sharpness

#

Not sure if this helps you at all, because I am completeley stuck on how to filter out similar "types"

barren jewel
jagged otter
#

went from this js for (const key of Object.keys(enchantList)) { if (enchantListItem.canAddEnchantment(new Enchantment(enchantList[key].id, 1)) && !enchantListItem.hasEnchantment(enchantList[key].id)) { possibleEnchantArray.push(enchantList[key]) } }
to this js for (const key of Object.keys(enchantList)) { try { if (enchantmentComponentItem.canAddEnchantment({type:enchantList[key].id, level:1}) && !enchantmentComponentItem.hasEnchantment(enchantList[key].id)) { possibleEnchantArray.push(enchantList[key]) } } catch {} }

#

works perfectly now

barren jewel
# jagged otter went from this ```js for (const key of Object.keys(enchantList)) { i...

but what happens if the item have an enchantment lvl 1 and you want to add the same enchantment but lvl 4 wich would be the max for this type? in this case you have to remove the previous enchantment because when I tried the canAddEnchantment it was basically making a sum between the lvl 1 and 4, resulting in five and returning error because it was trying to add an enchantment higher than the limit for this type

jagged otter
#

Oh, I didn't test that far