import { world, MolangVariableMap, ItemStack, EquipmentSlot } from "@minecraft/server";

world.afterEvents.entityHurt.subscribe((event) => {
    const { damage, damageSource, hurtEntity } = event;
    if (hurtEntity.typeId === "oct_eg:training_dummy") {
        // world.sendMessage(`§e${hurtEntity.name} took §c${damage}§r damage`);
        const location = hurtEntity.location;
        const molang = new MolangVariableMap();

        molang.setFloat('num', damage);

        hurtEntity.dimension.spawnParticle(
            "oct_eg:damage_number",
            {
                x: location.x,
                y: location.y + 1.8,
                z: location.z
            },
            molang
        ),
            hurtEntity.addEffect("minecraft:instant_health", 1, { amplifier: 254, showParticles: false });
    }
})

world.afterEvents.playerInteractWithEntity.subscribe(e=>{
    const dummy = e.target;
    const p = e.player;
    
    if(dummy.typeId != "oct_eg:training_dummy")return;
    if(!p.isSneaking)return;
    if(p.getComponent("minecraft:equippable").getEquipment(EquipmentSlot.Mainhand))return;

    dummy.triggerEvent("oct_eg:despawn");
    dummy.dimension.spawnItem(new ItemStack("oct_eg:training_dummy",1),dummy.location);
})