// Priority: 10

// ==========================================================================
// 1. ZENITH & WEAPON TAGS
// ==========================================================================
ServerEvents.tags('item', event => {
    const meleeMods = [
        '/wizards:.*/', 
        '/elemental_wizards_rpg:.*/', 
        '/paladins:.*/', 
        '/rogues:.*/', 
        '/berserker_rpg:.*/', 
        '/forcemaster_rpg:.*/', 
        '/death_knights:.*/'
    ];
    const rangedMods = ['/archers:.*/'];

    event.add('c:swords', meleeMods);
    event.add('c:weapons/melee', meleeMods);
    event.add('c:weapons/ranged', rangedMods);
    
    event.add('zenith:affix_heavy_weapons', [
        '/paladins:.*hammer/', 
        '/paladins:.*claymore/', 
        '/death_knights:.*greatsword/'
    ]);
})

// ==========================================================================
// 2. WORLD SETUP (ONE-TIME INITIALIZATION)
// ==========================================================================
LevelEvents.loaded(event => {
    const { server, level } = event;
    if (!level.persistentData.contains('pota_initialized')) {
        server.runCommandSilent('function true_ending:action/crystalcount_0');
        level.persistentData.putBoolean('pota_initialized', true);
        console.log("Path of the Ascended: Initial Crystal Count Reset.");
    }
});

// ==========================================================================
// 3. INTERACTION & BLOCK RESTRICTIONS
// ==========================================================================
BlockEvents.rightClicked('minecraft:enchanting_table', event => {
    event.cancel();
    event.player.displayClientMessage(Text.gold("The table's ancient magic is dormant; it serves only as a material for higher crafts."), true);
})

BlockEvents.broken(event => {
    const { block, player, server } = event
    if (block.id === 'minecraft:spawner') {
        if (player && !player.isCreative()) {
            server.runCommandSilent(`execute at ${player.uuid} run playsound minecraft:block.anvil.place blocks @p ~ ~ ~ 0.6 1.8`)
            server.runCommandSilent(`title ${player.username} actionbar {"text":"The cage is bound; survive the trial to break the seal.","color":"gold"}`)
            player.attack(0.5) 
            server.runCommandSilent(`execute at ${block.x} ${block.y} ${block.z} run particle minecraft:enchant ~0.5 ~0.5 ~0.5 0.2 0.2 0.2 0.1 15`)
            event.cancel()
        }
    }
})

// ==========================================================================
// 4. LOOT MODIFIERS
// ==========================================================================
LootJS.modifiers((event) => {
    event.addBlockLootModifier([
        'minecraft:grass', 
        'minecraft:tall_grass', 
        '/biomesoplenty:.*grass/', 
        '/biomesoplenty:.*sprout/', 
        '/biomesoplenty:.*bush/'
    ])
    .matchMainHand("#notreepunching:knives")
    .randomChance(0.5) 
    .addLoot("notreepunching:plant_fiber");
});

// ==========================================================================
// 5. TOTAL EXPERIENCE LOSS ON DEATH
// ==========================================================================
PlayerEvents.respawned(event => {
    const { player, server } = event;

    server.scheduleInTicks(1, () => {
        // Resets the player's level and points to exactly zero
        server.runCommandSilent(`experience set ${player.username} 0 levels`);

        // Notify the player of their total loss
        player.tell([
            Text.darkRed('「 '),
            Text.darkRed('The void has consumed what experience you were holding. You should store it should you wish to keep it safe.'),
            Text.darkRed(' 」')
        ]);
    });
});