// 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;
    
    // Check if the world has already been initialized to reset crystals
    if (!level.persistentData.contains('pota_initialized')) {
        
        // 1. Initialize True Ending Logic
        server.runCommandSilent('function true_ending:action/crystalcount_0');
        
        // 2. Mark world as initialized so this never runs again
        level.persistentData.putBoolean('pota_initialized', true);
        
        console.log("Path of the Ascended: Initial Crystal Count Reset.");
    }
});

// ==========================================================================
// 3. INTERACTION & BLOCK RESTRICTIONS
// ==========================================================================

// Enchanting Table Restriction
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);
})

// Spawner Rework (Invulnerability & Feedback)
BlockEvents.broken(event => {
    const { block, player, server } = event
    
    if (block.id.contains('spawner')) {
        if (player && !player.isCreative()) {
            
            // 1. Play Sound
            server.runCommandSilent(`execute at ${player.uuid} run playsound minecraft:block.anvil.place blocks @p ~ ~ ~ 0.6 1.8`)
            
            // 2. Show Subtitle (Action Bar)
            server.runCommandSilent(`title ${player.username} actionbar {"text":"The cage is bound; survive the trial to break the seal.","color":"gold"}`)
            
            // 3. Recoil & Particles
            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`)

            // 4. Cancel the break
            event.cancel()
        }
    }
})

// ==========================================================================
// 4. LOOT MODIFIERS (Early Game Fixes)
// ==========================================================================
LootJS.modifiers((event) => {
    // Allows knives to harvest plant fiber from various flora
    event.addBlockLootModifier([
        'minecraft:grass', 
        'minecraft:tall_grass', 
        '/biomesoplenty:.*grass/', 
        '/biomesoplenty:.*sprout/', 
        '/biomesoplenty:.*bush/'
    ])
    .matchMainHand("#notreepunching:knives")
    .randomChance(0.5) 
    .addLoot("notreepunching:plant_fiber");
});