import { world } from "@minecraft/server";
import { ConfigGeral } from "./config.js";

world.afterEvents.entityHitEntity.subscribe((evento) => {
    const atacante = evento.damagingEntity;
    const vitima = evento.hitEntity;

    if (!atacante || atacante.typeId !== "minecraft:player") return;

    const inventario = atacante.getComponent("minecraft:inventory");
    const itemMao = inventario.container.getItem(atacante.selectedSlotIndex);

    if (!itemMao) return;

    const configMelee = ConfigGeral.Melee[itemMao.typeId];

    if (configMelee && configMelee.tempo > 0) {
        const ticks = Math.floor(configMelee.tempo * 20);
        
        if (ticks >= 1) {
            vitima.addEffect(configMelee.efeito, ticks, { amplifier: configMelee.nivel });
        }
    }
});