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

//检查玩家是否穿戴了特定的套装

function itemName(itemTypeId) {
    return itemTypeId.split(':')[1]
}

const playerStatus = new Map();

system.runInterval(() => {
    for (const player of world.getPlayers()) {
        const equipment = player.getComponent("equippable");

        if (!equipment) continue;

        const slots = ["Head", "Chest", "Legs", "Feet"];

        const suits = {
            "pm_m8:combat_uniform": "combat_uniform",
            "pm_m8:juggernaut": "juggernaut",
            "pm_m8:cyclone_suit": "cyclone_suit",
            "pm_m8:inferno_exoskeleton": "inferno_exoskeleton",
            "pm_m8:pyroclasm": "pyroclasm"
        };

        let suitType = null;
        let isWearingFullSuit = false;

        for (const [suit, suitKeyword] of Object.entries(suits)) {
            const isWearingSuit = slots.every((slot) => {
                const equippedItem = equipment.getEquipment(slot);
                return equippedItem?.typeId.includes(suitKeyword);
            });

            if (isWearingSuit) {
                suitType = suit;
                isWearingFullSuit = true;
                break;
            }
        }

        const playerStatusData = playerStatus.get(player.name) || {
            suit: null
        };

        const suitTypeName = suitType ? itemName(suitType) : null;

        if (isWearingFullSuit && playerStatusData.suit !== suitType) {
            player.runCommandAsync(`function pm_m8/suit/${suitTypeName}`);
            //player.runCommandAsync(`say pm_m8/suit/${suitTypeName}`);
            player.addTag(suitTypeName);
            playerStatusData.suit = suitType;
        } else if (!isWearingFullSuit && playerStatusData.suit) {
            player.runCommandAsync(`function pm_m8/suit/reset`);
            player.removeTag(itemName(playerStatusData.suit));
            playerStatusData.suit = null;
        }

        playerStatus.set(player.name, playerStatusData);
    }
}, 20);
