import * as Vector3 from "../vector3";
import * as utils from "../utils";
import { system, world, EquipmentSlot } from "@minecraft/server";

export async function onItemUse(event) {
    const player = event.source;
    const item = event.itemStack;
    if (item && item.typeId === "pu_sg:demon_sword") {
        utils.runPlayerCommand(player, "effect @s levitation 1 20 true");
        utils.runPlayerCommand(player, "playsound pu_sg:demon_sword_right @a[r=7] ~ ~ ~");
    }
}

export async function onEntityHurt(event) {
    const source = event.damageSource.damagingEntity;
    const entity = event.hurtEntity;
    if (source?.typeId === "minecraft:player") {
        const equippable = source.getComponent('equippable');
        if (!equippable) return;
        const mainhand = equippable.getEquipment(EquipmentSlot.Mainhand);
        if (mainhand?.typeId === 'pu_sg:demon_sword') {
            if(entity.isValid()){
                const dimension = entity.dimension;
                dimension.spawnParticle("pu_sg:red_demon_sword_1", {x: source.location.x, y: source.location.y + 1, z: source.location.z});
                dimension.spawnParticle("pu_sg:red_demon_sword_2", {x: entity.location.x, y: entity.location.y + 1, z: entity.location.z});
                dimension.playSound("pu_sg:demon_sword_left", entity.location);
            }
        }
    }
}