
//============ REGRAS QUE O ADDON MUDA =============//

//gamerule tntexplodes false
//gamerule dofiretick false
//gamerule keepinventory true
//gamerule sendcommandfeedback false
//gamerule recipesunlock false
//gamerule showdeathmessages false
//gamerule showtags false
//gamerule commandblockoutput false
//gamerule showcoordinates true
//gamerule doimmediaterespawn true


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

// Gamerules que o addon força no carregamento normal (padrão do seu modo "não-dev")
system.beforeEvents.startup.subscribe(() => {
    // Configurações padrão do seu addon (modo normal/jogo)
    system.run(() => {
        world.setGameRule("tntexplodes", false);
        world.setGameRule("dofiretick", false);
        world.setGameRule("keepinventory", true);
        world.setGameRule("sendcommandfeedback", false);
        world.setGameRule("showdeathmessages", false);
        world.setGameRule("showtags", false);
        world.setGameRule("commandblockoutput", false);
        world.setGameRule("showcoordinates", true);
        world.setGameRule("doimmediaterespawn", true);
    });
});

// Registro único de todos os comandos customizados
system.beforeEvents.startup.subscribe((init) => {

    // ─── /well:dev_enable ───────────────────────────────────────────────
    init.customCommandRegistry.registerCommand({
        name: "well:dev_enable",
        description: "§6Habilita o modo Dev para testes [CUIDADO: pode quebrar o mundo!]",
        permissionLevel: CommandPermissionLevel.GameDirectors,
        cheatsRequired: true
    }, (origin) => {
        const player = origin.sourceEntity;
        if (!player) return { status: CustomCommandStatus.Failure };

        system.run(() => {
            player.runCommand("say §l§6Modo Dev Ativado!");
            player.runCommand("say §l§2MENSAGENS DE EVENTOS APARECEM NO CHAT");
            player.runCommand("say §l§2KEEPINVENTORY DESABILITADO");
            player.runCommand("say §l§2EXPLOSÕES DESTRUEM O MUNDO");
            player.runCommand("say §l§2FOGO SE ESPALHA E QUEIMA TUDO");
            player.runCommand("say §l§2MENSAGENS DE MORTE HABILITADAS");
            player.runCommand("say §l§2TAGS DE ITENS ATIVAS");
            player.runCommand("say §l§2MENU DE RESPAWN ATIVADO");

            player.runCommand("gamerule sendcommandfeedback true");
            player.runCommand("gamerule keepinventory false");
            player.runCommand("gamerule tntexplodes true");
            player.runCommand("gamerule dofiretick true");
            player.runCommand("gamerule showdeathmessages true");
            player.runCommand("gamerule showtags true");
            player.runCommand("gamerule doimmediaterespawn false");
        });

        return { status: CustomCommandStatus.Success };
    });

    // ─── /well:dev_disable ──────────────────────────────────────────────
    init.customCommandRegistry.registerCommand({
        name: "well:dev_disable",
        description: "§6Desabilita o modo Dev e restaura as configurações padrão",
        permissionLevel: CommandPermissionLevel.GameDirectors,
        cheatsRequired: true
    }, (origin) => {
        const player = origin.sourceEntity;
        if (!player) return { status: CustomCommandStatus.Failure };

        system.run(() => {
            player.runCommand("say §l§6Modo Dev Desativado!");
            player.runCommand("say §l§2CONFIGURAÇÕES RESTAURADAS PARA O PADRÃO!");

            player.runCommand("gamerule sendcommandfeedback false");
            player.runCommand("gamerule keepinventory true");
            player.runCommand("gamerule tntexplodes false");
            player.runCommand("gamerule dofiretick false");
            player.runCommand("gamerule showdeathmessages false");
            player.runCommand("gamerule showtags false");
            player.runCommand("gamerule commandblockoutput false");
            player.runCommand("gamerule doimmediaterespawn true");
        });

        return { status: CustomCommandStatus.Success };
    });

    // ─── /well:fix_journey ──────────────────────────────────────────────
    init.customCommandRegistry.registerCommand({
        name: "well:fix_journey",
        description: "§6Restaura pergaminho e arma de classe pro slot padrão",
        permissionLevel: CommandPermissionLevel.Any,
        cheatsRequired: false
    }, (origin) => {
        const player = origin.sourceEntity;
        if (!player) return { status: CustomCommandStatus.Failure };

        system.run(() => {
            player.runCommand("say §l§6Pergaminho e classe restaurados!");

            // Limpa itens soltos (se tiver duplicados na mão ou inventário)
            player.runCommand("clear @s well:teleporterapido");
            player.runCommand("clear @s well:cajado_mago");
            player.runCommand("clear @s well:espada_humano");

            // Coloca o teleporter no slot 8 (geral)
            player.runCommand(`replaceitem entity @s slot.hotbar 8 well:teleporterapido 1 0 {"item_lock":{"mode":"lock_in_slot"},"keep_on_death":{}}`);

            // Coloca arma/classe no slot 0 se tiver a score certa
            player.runCommand(`replaceitem entity @s[scores={classe_mago=1}] slot.hotbar 0 well:cajado_mago 1 0 {"item_lock":{"mode":"lock_in_slot"},"keep_on_death":{}}`);
            player.runCommand(`replaceitem entity @s[scores={classe_humana=1}] slot.hotbar 0 well:espada_humano 1 0 {"item_lock":{"mode":"lock_in_slot"},"keep_on_death":{}}`);
        });

        return { status: CustomCommandStatus.Success };
    });

});