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

/**
 * @class LockGamerule By Carchi77.
 */
export class LockGamerule {
    /**
     * Game rules and their locked states.
     * Set a rule to `true` or `false` to lock it; leave as `null` for no locking.
     */
    static settings = {
    	commandBlockOutput: null,
        /** @description Controls whether coordinates are displayed. */
        showCoordinates: null,
        /** @description Controls whether entities drop items upon death. */
        doEntityDrops: null,
        /** @description Controls whether fire spreads in the world. */
        doFireTick: null,
        /** @description Controls whether built-in recipes automatically unlock. */
        recipesUnlock: null,
        /** @description Determines if players can craft only recipes they've unlocked. */
        doLimitedCrafting: null,
        /** @description Controls whether mobs drop loot. */
        doMobLoot: null,
        /** @description Controls whether mobs spawn naturally in the game. */
        doMobSpawning: null,
        /** @description Controls whether blocks drop items when broken. */
        doTileDrops: null,
        /** @description Controls whether the weather cycle is enabled. */
        doWeatherCycle: null,
        /** @description Controls whether entities take drowning damage. */
        drowningDamage: null,
        /** @description Controls whether entities take fall damage. */
        fallDamage: null,
        /** @description Controls whether entities take fire damage. */
        fireDamage: null,
        /** @description Controls whether players keep their inventory upon death. */
        keepInventory: null,
        /** @description Controls whether mob griefing is enabled in the world. */
        mobGriefing: null,
        /** @description Controls whether players can deal damage to each other. */
        pvp: null,
        /** @description Displays the number of days played in the world. */
        showDaysPlayed: null,
        /** @description Controls whether players can regenerate health naturally. */
        naturalRegeneration: null,
        /** @description Controls whether TNT explosions are enabled. */
        tntExplodes: null,
        /** @description Controls whether command feedback messages are shown. */
        sendCommandFeedback: null,
        /** @description Controls whether players experience insomnia effects (e.g., phantoms). */
        doInsomnia: null,
        /** @description Enables or disables command blocks in the world. */
        commandBlocksEnabled: null,
        /** @description Controls whether players respawn immediately without confirmation. */
        doImmediateRespawn: null,
        /** @description Controls whether death messages are shown in chat. */
        showDeathMessages: null,
        /** @description Controls whether entity tags are displayed. */
        showTags: null,
        /** @description Controls whether freezing damage is enabled. */
        freezeDamage: null,
        /** @description Controls whether respawn blocks explode. */
        respawnBlocksExplode: null,
        /** @description Displays border effects in the game. */
        showBorderEffect: null,
        /** @description Controls if recipe notifications are displayed to players. */
        showRecipeMessages: null,
        /** @description Defines if projectiles can break certain blocks. */
        projectilesCanBreakBlocks: null,
        /** @description Controls if TNT explosions affect drop decay. */
        tntExplosionDropDecay: null
    };

    static {
        system.runTimeout(() => {
            for (const rule in this.settings) {
                if (this.settings[rule] !== null)
                    world.gameRules[rule] = this.settings[rule];
            }
        }, 2);
        world.afterEvents.gameRuleChange.subscribe(({ rule, value }) => {
            console.log(`${rule} = ${this.settings[rule]}`);
            if (rule in this.settings && value !== this.settings[rule] && this.settings[rule] !== null) {
                world.gameRules[rule] = this.settings[rule];
            }
        });
    }
}