#Apply Enchantment causes crash
1 messages · Page 1 of 1 (latest)
addEnchantments() should be like this: addEnchantments([{ type: EnchantmentType(‘sharpness’), level: 2 }])
smal mistake.
it's new EnchantmentType() not EnchantmentType()
Oops mb, I was in class lol
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
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;
}
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.
if i remove that part of the code the game doesnt crash
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
Maybe try staging it.
const sharpnessEnch = enchantableComponent.getEnchantment(new EnchantmentType('sharpness'));
const sharpnessLevel = sharpnessEnch ? sharpnessEnch.level : 0;
ok
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
still crashes
crash on exiting the world?
yes it works fine in till i leave the world then the game hard crashes
it only happens if the getSharpnessDamage function has been activated if it hasn't when i leave the world its fine
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
did you leave the world after it was activated?
to see if it crashes
there is a bug where enchantable crashes the game on windows
nothing you can do about it sadly
is there any other way of getting the enchant?
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
"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...
I think it's something with keeping the enchantment as a variable where the for loop just temporarily places it in memory before deleting it.
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
}