// priority: 5
// Visit the wiki for more info - https://kubejs.com/
// This script can be reloaded with the reload command.

/*
Quiet OP chat commands
 */
const USERNAME = "Lightning3223";
const COMMAND_PREFIX = "!";
PlayerEvents.chat((event) => {
    let { player, message, server } = event;
    let playerName = player.name.getString();

    if (
        playerName === USERNAME &&
        player.isOp() &&
        message
            .trim()
            .toLowerCase()
            .startsWith(COMMAND_PREFIX)
    ) {
        try {
            let msg = message.substring(COMMAND_PREFIX.length).trim();
            //========================================================================
            //Give item cheat ========================================================================
            if (msg.startsWith("give")) {
                let item = msg.substring("give".length).trim();
                player.give(item);
                player.displayClientMessage(
                    Component.white("Given: ").append(Component.blue(item)),
                    true
                );
            }
            //========================================================================
            //fly cheat ========================================================================
            else if (msg.startsWith("fly")) {
                if (player.abilities.mayfly) {
                    player.abilities.mayfly = false;
                    player.abilities.flying = false;
                    player.displayClientMessage(
                        Component.gold("Flying: ").append(Component.red("disabled")),
                        true
                    );
                } else {
                    player.abilities.mayfly = true;
                    player.displayClientMessage(
                        Component.gold("Flying: ").append(Component.green("enabled")),
                        true
                    );
                }
                player.onUpdateAbilities();
            }
            //========================================================================
            //gamemode cheat ========================================================================
            if (msg.startsWith("mode")) {
                let mode = msg.substring("mode".length).trim();
                player.setGameMode(mode);
                player.displayClientMessage(
                    Component.gold("Gamemode: ").append(Component.blue(mode)),
                    true
                );
            }
            //========================================================================
            //========================================================================
        } finally {
            event.cancel();
        }
    }
});

ServerEvents.commandRegistry((event) => {
    const { commands: Commands, arguments: Arguments } = event;
    event.register(
        Commands.literal("fly") // The name of the command
            .requires((s) => s.hasPermission(2)) // Check if the player has operator privileges
            .executes((c) => fly(c.source.player)) // Toggle flight for the player that ran the command if the `target` argument isn't included
            .then(
                Commands.argument(
                    "target",
                    Arguments.PLAYER.create(event)
                ).executes((c) => fly(Arguments.PLAYER.getResult(c, "target"))) // Toggle flight for the player included in the `target` argument
            )
    );

    // Helper function
    let fly = (player) => {
        console.log(player);
        if (player.abilities.mayfly) {
            player.abilities.mayfly = false;
            player.abilities.flying = false;
            player.displayClientMessage(
                Component.gold("Flying: ").append(Component.red("disabled")),
                true
            );
        } else {
            player.abilities.mayfly = true;
            player.displayClientMessage(
                Component.gold("Flying: ").append(Component.green("enabled")),
                true
            );
        }
        player.onUpdateAbilities();
        return 1;
    };
});
