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

const FLIGHT_CHESTPLATE_ID = "gojidraw:ender_wing"; // Change this to your armor ID

system.runInterval(() => {
    for (const player of world.getPlayers()) {
        const equipment = player.getComponent("minecraft:equippable");
        if (!equipment) continue;

        const chestItem = equipment.getEquipment(EquipmentSlot.Chest);
        const hasFlightArmor = chestItem && chestItem.typeId === FLIGHT_CHESTPLATE_ID;

        // Store if the player had the flight armor last tick
        let wasWearingFlightArmor = player.getDynamicProperty("wasWearingFlightArmor");

        if (hasFlightArmor && !wasWearingFlightArmor) {
            // Player just equipped the chestplate
            player.runCommandAsync("ability @s mayfly true"); // Example command
            player.sendMessage("§bFlight Mode Activated!");
        } else if (!hasFlightArmor && wasWearingFlightArmor) {
            // Player just unequipped the chestplate
            player.runCommandAsync("ability @s mayfly false"); // Example command
            player.sendMessage("§cFlight Mode Deactivated!");
        }

        // Update the stored state
        player.setDynamicProperty("wasWearingFlightArmor", hasFlightArmor);
    }
}, 10); // Runs every 10 ticks (0.5 seconds)