#Apply Enchantment causes crash

1 messages · Page 1 of 1 (latest)

celest prairie
#

when the system apply enchantments and leave the world, the game crashes

rich valve
#

addEnchantments() should be like this: addEnchantments([{ type: EnchantmentType(‘sharpness’), level: 2 }])

shadow spruce
#

it's new EnchantmentType() not EnchantmentType()

rich valve
#

Oops mb, I was in class lol

solemn jacinth
# shadow spruce smal mistake.

yo this

enchantableComponent.getEnchantment(new EnchantmentType("sharpness"))?.level || 0;

is crashing my game when i leave the world is there anyway to fix it?

#

@rich valve

solemn jacinth
# shadow spruce How script looks like??
function getSharpnessDamage(item) {
  const enchantableComponent = item.getComponent("enchantable");
  if (!enchantableComponent) {return 0;}
  const sharpnessLevel = enchantableComponent.getEnchantment(new EnchantmentType('sharpness'))?.level || 0;
  const sharpnessDamageMap = {1: 1, 2: 1.5, 3: 2, 4: 2.5, 5: 3};
  return sharpnessDamageMap[sharpnessLevel] || 0;
}

elfin girder
#

Try disabling/rewriting parts of your code, making sure and find what crashed the game.

It could be an infinite loop, a heavy operation, or even an invalid component/entity instance.

Or might be the game is just failing to save 'something' after the world is closed.

solemn jacinth
#

like if i set
sharpnesslevel to 0 not enchantableComponent.getEnchantment(new EnchantmentType('sharpness'))?.level || 0;
it doesnt crash

#

is there any other way of getting enchants

elfin girder
#

Maybe try staging it.

const sharpnessEnch = enchantableComponent.getEnchantment(new EnchantmentType('sharpness'));
const sharpnessLevel = sharpnessEnch ? sharpnessEnch.level : 0;
solemn jacinth
#
function getSharpnessDamage(item) {
  const enchantableComponent = item.getComponent("enchantable");
  if (!enchantableComponent) {return 0;}
  const sharpnessEnch = enchantableComponent.getEnchantment(new EnchantmentType('sharpness'));
  const sharpnessLevel = sharpnessEnch ? sharpnessEnch.level : 0;
  const sharpnessDamageMap = {1: 1, 2: 1.5, 3: 2, 4: 2.5, 5: 3};
  return sharpnessDamageMap[sharpnessLevel] || 0;
}
#

it looks like this now

elfin girder
#

crash on exiting the world?

solemn jacinth
solemn jacinth
elfin girder
#

let me try

#

Doesn't seem to be a problem for me.
-# (Android - 1.21.70 - 2.0.0-beta)

world.afterEvents.entityHitEntity.subscribe(({damagingEntity,hitEntity})=>{
    if (!(damagingEntity instanceof Player)) return;
    const equippable = damagingEntity.getComponent("equippable");
    const itemStack = equippable.getEquipment("Mainhand");
    if (!itemStack) return;
    const sharpnessDmg = getSharpnessDamage(itemStack)
    damagingEntity.sendMessage("§bSharpness: §e"+sharpnessDmg)
})
#

try using it on a different world I guess

solemn jacinth
#

to see if it crashes

spiral sage
#

nothing you can do about it sadly

solemn jacinth
spiral sage
#

I don't know any other than that one

solemn jacinth
# spiral sage I don't know any other than that one

i figured it out

function getSharpnessDamage(item) {
  const enchantableComponent = item.getComponent("enchantable");
  if (!enchantableComponent) {return 0;}
  let sharpness = 0;
  for (const enchantment of enchantableComponent.getEnchantments()) {
    if (enchantment.type.id === "sharpness") {
      sharpness = enchantment.level;
    }
  }
  const sharpnessLevel = sharpness || 0;
  const sharpnessDamageMap = {1: 1, 2: 1.5, 3: 2, 4: 2.5, 5: 3};
  return sharpnessDamageMap[sharpnessLevel] || 0;
}
})
#

idk why this doesnt crash but

elfin girder
#

"If it works, don't touch it."

solemn jacinth
# elfin girder ### "If it works, don't touch it."

this video is a fun take when in programming things work out somehow but now how it was supposed
follow form more programming content specially in javascript , python , c++ , java and become skillful coder and developer

Don't forget to LIKE, COMMENT, and SUBSCRIBE for more tech comedy and insightful developer content!

#programming #coding #De...

▶ Play video
safe stratus
safe stratus
#

just did some testing and found that the issue arises from repeatedly running a function that contains class declaration for EnchantmentType. So if you declare the classes outside of the function, you can use them inside it and it wont cause crashes.

#

export const sharpnessEnchantment = new EnchantmentType("sharpness");

function getSharpnessDamage(item) {
    const sharpnessLevel = item.getComponent("enchantable").getEnchantment(sharpnessEnchantment) ?? {level: 0};
    const damage = Math.floor(1.25 * sharpnessLevel.level);
    return damage
}