

export function onHitEntity(event) {
    const { attackingEntity, hadEffect, hitEntity, itemStack } = event;
    try {
        hitEntity.setOnFire(5, true)
    } catch (error) { }
}

export function onUse(event) {
    const { itemStack, source } = event;
    const block = source.getBlockFromViewDirection({ maxDistance: 8 });

    if (block) {
        if (block.block.above().isAir) {
            const durability = itemStack.getComponent("minecraft:durability");
            if (durability.damage === durability.maxDurability) {
                source.runCommandAsync(`replaceitem entity @s slot.weapon.mainhand 0 air`);
            } else {
                durability.damage += 1;
                source.getComponent('equippable').setEquipment('Mainhand', itemStack);
                block.block.above().setType(`fire`)
            }
        }
    }
}

const magma_sword = {
    onHitEntity: onHitEntity,
    onUse: onUse,
}
export default magma_sword;
