// CUNICULUM RPG set layer.
// Keeps the large mob script stable while adding coherent class sets, readable set lore,
// and a more reliable area-mining implementation.

var CUNIRPG_SET_TIERS = [
  { key: 'madriguera', label: 'Comun', min: 1, rarity: 'common', color: 'gray' },
  { key: 'guardia', label: 'Incomun', min: 8, rarity: 'uncommon', color: 'green' },
  { key: 'explorador', label: 'Raro', min: 18, rarity: 'rare', color: 'blue' },
  { key: 'vigia', label: 'Epico', min: 35, rarity: 'epic', color: 'dark_purple' },
  { key: 'juramento', label: 'Legendario', min: 55, rarity: 'epic', color: 'gold' },
  { key: 'mitico', label: 'Mitico', min: 80, rarity: 'mythic', color: 'gold' }
];

var CUNIRPG_SLOT_LABELS = {
  head: 'cabeza',
  chest: 'pechera',
  legs: 'piernas',
  feet: 'botas'
};

var CUNIRPG_SET_FAMILIES = [
  {
    key: 'archer_path', className: 'Arquero', short: 'Sendero Flechado',
    match: ['archers:', 'archers_expansion:', 'bow', 'crossbow', 'ranger', 'deadeye', 'war_archer'],
    bonuses: {
      one: '1 pieza: vision y paso firme para explorar.',
      two: '2 piezas: velocidad y precision con arcos.',
      three: '3 piezas: suerte de botin y mejor control a distancia.',
      four: '4 piezas: fuerza ligera y movilidad de cazador.'
    },
    effects: {
      one: [{ effect: 'minecraft:night_vision', seconds: 12, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:speed', seconds: 7, amp: 0, powerAmp: 5, maxAmp: 1 }],
      three: [{ effect: 'minecraft:luck', seconds: 7, amp: 0, powerAmp: 4, maxAmp: 1 }],
      four: [{ effect: 'minecraft:strength', seconds: 7, amp: 0, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'minecraft:leather_helmet', chest: 'minecraft:leather_chestplate', legs: 'minecraft:leather_leggings', feet: 'minecraft:leather_boots' },
      guardia: { head: 'archers:archer_armor_head', chest: 'archers:archer_armor_chest', legs: 'archers:archer_armor_legs', feet: 'archers:archer_armor_feet' },
      explorador: { head: 'archers:ranger_armor_head', chest: 'archers:ranger_armor_chest', legs: 'archers:ranger_armor_legs', feet: 'archers:ranger_armor_feet' },
      vigia: { head: 'archers_expansion:sentinel_archer_head', chest: 'archers_expansion:sentinel_archer_chest', legs: 'archers_expansion:sentinel_archer_legs', feet: 'archers_expansion:sentinel_archer_feet' },
      juramento: { head: 'archers:netherite_ranger_armor_head', chest: 'archers:netherite_ranger_armor_chest', legs: 'archers:netherite_ranger_armor_legs', feet: 'archers:netherite_ranger_armor_feet' },
      mitico: { head: 'archers_expansion:netherite_war_archer_head', chest: 'archers_expansion:netherite_war_archer_chest', legs: 'archers_expansion:netherite_war_archer_legs', feet: 'archers_expansion:netherite_war_archer_feet' }
    }
  },
  {
    key: 'deadeye_path', className: 'Tirador', short: 'Ojo de Madriguera',
    match: ['deadeye', 'bounty_hunter', 'tundra_hunter', 'polar_stalker', 'rapid_crossbow', 'heavy_crossbow'],
    bonuses: {
      one: '1 pieza: enfoque de rastreador.',
      two: '2 piezas: velocidad de reposicion.',
      three: '3 piezas: punteria y suerte de recompensa.',
      four: '4 piezas: defensa contra proyectiles y presion constante.'
    },
    effects: {
      one: [{ effect: 'minecraft:night_vision', seconds: 12, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:speed', seconds: 7, amp: 0, maxAmp: 1 }],
      three: [{ effect: 'minecraft:luck', seconds: 7, amp: 0, powerAmp: 5, maxAmp: 2 }],
      four: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'minecraft:leather_helmet', chest: 'minecraft:leather_chestplate', legs: 'minecraft:leather_leggings', feet: 'minecraft:leather_boots' },
      guardia: { head: 'archers:archer_armor_head', chest: 'archers:archer_armor_chest', legs: 'archers:archer_armor_legs', feet: 'archers:archer_armor_feet' },
      explorador: { head: 'archers_expansion:deadeye_head', chest: 'archers_expansion:deadeye_chest', legs: 'archers_expansion:deadeye_legs', feet: 'archers_expansion:deadeye_feet' },
      vigia: { head: 'archers_expansion:bounty_hunter_head', chest: 'archers_expansion:bounty_hunter_chest', legs: 'archers_expansion:bounty_hunter_legs', feet: 'archers_expansion:bounty_hunter_feet' },
      juramento: { head: 'archers_expansion:netherite_tundra_hunter_head', chest: 'archers_expansion:netherite_tundra_hunter_chest', legs: 'archers_expansion:netherite_tundra_hunter_legs', feet: 'archers_expansion:netherite_tundra_hunter_feet' },
      mitico: { head: 'archers_expansion:netherite_deadeye_head', chest: 'archers_expansion:netherite_deadeye_chest', legs: 'archers_expansion:netherite_deadeye_legs', feet: 'archers_expansion:netherite_deadeye_feet' }
    }
  },
  {
    key: 'rogue_path', className: 'Picaro', short: 'Salto Sombrio',
    match: ['rogues:', 'dagger', 'assassin', 'rogue', 'shadow', 'bruma'],
    bonuses: {
      one: '1 pieza: velocidad breve al acechar.',
      two: '2 piezas: salto y movilidad de flanco.',
      three: '3 piezas: sigilo ofensivo y ventaja al reposicionarse.',
      four: '4 piezas: fuerza ligera y salto sostenido.'
    },
    effects: {
      one: [{ effect: 'minecraft:speed', seconds: 7, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:jump_boost', seconds: 7, amp: 0, maxAmp: 1 }],
      three: [{ effect: 'minecraft:luck', seconds: 7, amp: 0, maxAmp: 1 }],
      four: [{ effect: 'minecraft:strength', seconds: 7, amp: 0, powerAmp: 5, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'minecraft:leather_helmet', chest: 'minecraft:leather_chestplate', legs: 'minecraft:leather_leggings', feet: 'minecraft:leather_boots' },
      guardia: { head: 'rogues:rogue_armor_head', chest: 'rogues:rogue_armor_chest', legs: 'rogues:rogue_armor_legs', feet: 'rogues:rogue_armor_feet' },
      explorador: { head: 'rogues:warrior_armor_head', chest: 'rogues:warrior_armor_chest', legs: 'rogues:warrior_armor_legs', feet: 'rogues:warrior_armor_feet' },
      vigia: { head: 'rogues:assassin_armor_head', chest: 'rogues:assassin_armor_chest', legs: 'rogues:assassin_armor_legs', feet: 'rogues:assassin_armor_feet' },
      juramento: { head: 'rogues:netherite_assassin_armor_head', chest: 'rogues:netherite_assassin_armor_chest', legs: 'rogues:netherite_assassin_armor_legs', feet: 'rogues:netherite_assassin_armor_feet' },
      mitico: { head: 'rogues:netherite_berserker_armor_head', chest: 'rogues:netherite_berserker_armor_chest', legs: 'rogues:netherite_berserker_armor_legs', feet: 'rogues:netherite_berserker_armor_feet' }
    }
  },
  {
    key: 'paladin_path', className: 'Paladin', short: 'Santuario Bajo',
    match: ['paladins:', 'paladin', 'crusader', 'mace', 'hammer', 'shield', 'baluarte'],
    bonuses: {
      one: '1 pieza: defensa estable.',
      two: '2 piezas: resistencia en combate cercano.',
      three: '3 piezas: proteccion de santuario.',
      four: '4 piezas: absorcion y resistencia al fuego.'
    },
    effects: {
      one: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, powerAmp: 5, maxAmp: 1 }],
      three: [{ effect: 'minecraft:absorption', seconds: 7, amp: 0, maxAmp: 1 }],
      four: [{ effect: 'minecraft:fire_resistance', seconds: 8, amp: 0, maxAmp: 0 }]
    },
    pieces: {
      madriguera: { head: 'minecraft:chainmail_helmet', chest: 'minecraft:chainmail_chestplate', legs: 'minecraft:chainmail_leggings', feet: 'minecraft:chainmail_boots' },
      guardia: { head: 'minecraft:iron_helmet', chest: 'minecraft:iron_chestplate', legs: 'minecraft:iron_leggings', feet: 'minecraft:iron_boots' },
      explorador: { head: 'paladins:paladin_armor_head', chest: 'paladins:paladin_armor_chest', legs: 'paladins:paladin_armor_legs', feet: 'paladins:paladin_armor_feet' },
      vigia: { head: 'paladins:crusader_armor_head', chest: 'paladins:crusader_armor_chest', legs: 'paladins:crusader_armor_legs', feet: 'paladins:crusader_armor_feet' },
      juramento: { head: 'paladins:netherite_crusader_armor_head', chest: 'paladins:netherite_crusader_armor_chest', legs: 'paladins:netherite_crusader_armor_legs', feet: 'paladins:netherite_crusader_armor_feet' },
      mitico: { head: 'paladins:netherite_crusader_armor_head', chest: 'paladins:netherite_crusader_armor_chest', legs: 'paladins:netherite_crusader_armor_legs', feet: 'paladins:netherite_crusader_armor_feet' }
    }
  },
  {
    key: 'cleric_path', className: 'Clerigo', short: 'Voto de Raiz',
    match: ['priest', 'prior', 'holy_staff', 'cleric', 'curacion'],
    bonuses: {
      one: '1 pieza: recuperacion ligera.',
      two: '2 piezas: estabilidad sagrada.',
      three: '3 piezas: defensa contra presion elemental.',
      four: '4 piezas: regeneracion y absorcion de apoyo.'
    },
    effects: {
      one: [{ effect: 'minecraft:regeneration', seconds: 5, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, maxAmp: 0 }],
      three: [{ effect: 'minecraft:fire_resistance', seconds: 8, amp: 0, maxAmp: 0 }],
      four: [{ effect: 'minecraft:absorption', seconds: 7, amp: 0, powerAmp: 6, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'minecraft:leather_helmet', chest: 'minecraft:leather_chestplate', legs: 'minecraft:leather_leggings', feet: 'minecraft:leather_boots' },
      guardia: { head: 'paladins:priest_robe_head', chest: 'paladins:priest_robe_chest', legs: 'paladins:priest_robe_legs', feet: 'paladins:priest_robe_feet' },
      explorador: { head: 'paladins:prior_robe_head', chest: 'paladins:prior_robe_chest', legs: 'paladins:prior_robe_legs', feet: 'paladins:prior_robe_feet' },
      vigia: { head: 'paladins:prior_robe_head', chest: 'paladins:prior_robe_chest', legs: 'paladins:prior_robe_legs', feet: 'paladins:prior_robe_feet' },
      juramento: { head: 'paladins:netherite_prior_robe_head', chest: 'paladins:netherite_prior_robe_chest', legs: 'paladins:netherite_prior_robe_legs', feet: 'paladins:netherite_prior_robe_feet' },
      mitico: { head: 'paladins:netherite_prior_robe_head', chest: 'paladins:netherite_prior_robe_chest', legs: 'paladins:netherite_prior_robe_legs', feet: 'paladins:netherite_prior_robe_feet' }
    }
  },
  {
    key: 'wizard_arcane_path', className: 'Mago arcano', short: 'Polvo Arcano',
    match: ['wizards:', 'wizard_robe', 'arcane_robe', 'staff_arcane', 'staff_wizard'],
    bonuses: {
      one: '1 pieza: foco arcano menor.',
      two: '2 piezas: resistencia a presion elemental.',
      three: '3 piezas: recuperacion controlada.',
      four: '4 piezas: defensa arcana y regeneracion.'
    },
    effects: {
      one: [{ effect: 'minecraft:night_vision', seconds: 12, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, maxAmp: 0 }],
      three: [{ effect: 'minecraft:regeneration', seconds: 5, amp: 0, maxAmp: 0 }],
      four: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, powerAmp: 5, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'wizards:wizard_robe_head', chest: 'wizards:wizard_robe_chest', legs: 'wizards:wizard_robe_legs', feet: 'wizards:wizard_robe_feet' },
      guardia: { head: 'wizards:wizard_robe_head', chest: 'wizards:wizard_robe_chest', legs: 'wizards:wizard_robe_legs', feet: 'wizards:wizard_robe_feet' },
      explorador: { head: 'wizards:arcane_robe_head', chest: 'wizards:arcane_robe_chest', legs: 'wizards:arcane_robe_legs', feet: 'wizards:arcane_robe_feet' },
      vigia: { head: 'wizards:arcane_robe_head', chest: 'wizards:arcane_robe_chest', legs: 'wizards:arcane_robe_legs', feet: 'wizards:arcane_robe_feet' },
      juramento: { head: 'wizards:netherite_arcane_robe_head', chest: 'wizards:netherite_arcane_robe_chest', legs: 'wizards:netherite_arcane_robe_legs', feet: 'wizards:netherite_arcane_robe_feet' },
      mitico: { head: 'wizards:netherite_arcane_robe_head', chest: 'wizards:netherite_arcane_robe_chest', legs: 'wizards:netherite_arcane_robe_legs', feet: 'wizards:netherite_arcane_robe_feet' }
    }
  },
  {
    key: 'wizard_fire_path', className: 'Mago de fuego', short: 'Brasa Serena',
    match: ['fire_robe', 'staff_fire', 'brasa', 'fuego'],
    bonuses: {
      one: '1 pieza: abrigo contra fuego.',
      two: '2 piezas: avance entre llamas.',
      three: '3 piezas: defensa y fuerza de brasa.',
      four: '4 piezas: fuego controlado y aguante.'
    },
    effects: {
      one: [{ effect: 'minecraft:fire_resistance', seconds: 8, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:speed', seconds: 7, amp: 0, maxAmp: 0 }],
      three: [{ effect: 'minecraft:strength', seconds: 7, amp: 0, maxAmp: 0 }],
      four: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, powerAmp: 5, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'wizards:wizard_robe_head', chest: 'wizards:wizard_robe_chest', legs: 'wizards:wizard_robe_legs', feet: 'wizards:wizard_robe_feet' },
      guardia: { head: 'wizards:fire_robe_head', chest: 'wizards:fire_robe_chest', legs: 'wizards:fire_robe_legs', feet: 'wizards:fire_robe_feet' },
      explorador: { head: 'wizards:fire_robe_head', chest: 'wizards:fire_robe_chest', legs: 'wizards:fire_robe_legs', feet: 'wizards:fire_robe_feet' },
      vigia: { head: 'wizards:fire_robe_head', chest: 'wizards:fire_robe_chest', legs: 'wizards:fire_robe_legs', feet: 'wizards:fire_robe_feet' },
      juramento: { head: 'wizards:netherite_fire_robe_head', chest: 'wizards:netherite_fire_robe_chest', legs: 'wizards:netherite_fire_robe_legs', feet: 'wizards:netherite_fire_robe_feet' },
      mitico: { head: 'wizards:netherite_fire_robe_head', chest: 'wizards:netherite_fire_robe_chest', legs: 'wizards:netherite_fire_robe_legs', feet: 'wizards:netherite_fire_robe_feet' }
    }
  },
  {
    key: 'wizard_frost_path', className: 'Mago de escarcha', short: 'Nieve Subterranea',
    match: ['frost_robe', 'staff_frost', 'escarcha', 'glacial'],
    bonuses: {
      one: '1 pieza: paso frio estable.',
      two: '2 piezas: caida suave y resistencia.',
      three: '3 piezas: vision en cavernas frias.',
      four: '4 piezas: defensa glacial de galeria.'
    },
    effects: {
      one: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:slow_falling', seconds: 5, amp: 0, maxAmp: 0 }],
      three: [{ effect: 'minecraft:night_vision', seconds: 12, amp: 0, maxAmp: 0 }],
      four: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, powerAmp: 4, maxAmp: 2 }]
    },
    pieces: {
      madriguera: { head: 'wizards:wizard_robe_head', chest: 'wizards:wizard_robe_chest', legs: 'wizards:wizard_robe_legs', feet: 'wizards:wizard_robe_feet' },
      guardia: { head: 'wizards:frost_robe_head', chest: 'wizards:frost_robe_chest', legs: 'wizards:frost_robe_legs', feet: 'wizards:frost_robe_feet' },
      explorador: { head: 'wizards:frost_robe_head', chest: 'wizards:frost_robe_chest', legs: 'wizards:frost_robe_legs', feet: 'wizards:frost_robe_feet' },
      vigia: { head: 'wizards:frost_robe_head', chest: 'wizards:frost_robe_chest', legs: 'wizards:frost_robe_legs', feet: 'wizards:frost_robe_feet' },
      juramento: { head: 'wizards:netherite_frost_robe_head', chest: 'wizards:netherite_frost_robe_chest', legs: 'wizards:netherite_frost_robe_legs', feet: 'wizards:netherite_frost_robe_feet' },
      mitico: { head: 'wizards:netherite_frost_robe_head', chest: 'wizards:netherite_frost_robe_chest', legs: 'wizards:netherite_frost_robe_legs', feet: 'wizards:netherite_frost_robe_feet' }
    }
  },
  {
    key: 'elemental_water_path', className: 'Elemental agua', short: 'Marea Oculta',
    match: ['kelp', 'ocean_robe', 'staff_aqua', 'marea', 'aqua'],
    bonuses: {
      one: '1 pieza: respiracion acuática.',
      two: '2 piezas: movilidad en rios y mares.',
      three: '3 piezas: suerte de exploracion acuatica.',
      four: '4 piezas: corriente defensiva.'
    },
    effects: {
      one: [{ effect: 'minecraft:water_breathing', seconds: 9, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:dolphins_grace', seconds: 5, amp: 0, maxAmp: 1 }],
      three: [{ effect: 'minecraft:luck', seconds: 7, amp: 0, maxAmp: 1 }],
      four: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'elemental_wizards_rpg:kelp_head', chest: 'elemental_wizards_rpg:kelp_chest', legs: 'elemental_wizards_rpg:kelp_legs', feet: 'elemental_wizards_rpg:kelp_feet' },
      guardia: { head: 'elemental_wizards_rpg:kelp_head', chest: 'elemental_wizards_rpg:kelp_chest', legs: 'elemental_wizards_rpg:kelp_legs', feet: 'elemental_wizards_rpg:kelp_feet' },
      explorador: { head: 'elemental_wizards_rpg:ocean_robe_head', chest: 'elemental_wizards_rpg:ocean_robe_chest', legs: 'elemental_wizards_rpg:ocean_robe_legs', feet: 'elemental_wizards_rpg:ocean_robe_feet' },
      vigia: { head: 'elemental_wizards_rpg:ocean_robe_head', chest: 'elemental_wizards_rpg:ocean_robe_chest', legs: 'elemental_wizards_rpg:ocean_robe_legs', feet: 'elemental_wizards_rpg:ocean_robe_feet' },
      juramento: { head: 'elemental_wizards_rpg:netherite_kelp_head', chest: 'elemental_wizards_rpg:netherite_kelp_chest', legs: 'elemental_wizards_rpg:netherite_kelp_legs', feet: 'elemental_wizards_rpg:netherite_kelp_feet' },
      mitico: { head: 'elemental_wizards_rpg:netherite_kelp_head', chest: 'elemental_wizards_rpg:netherite_kelp_chest', legs: 'elemental_wizards_rpg:netherite_kelp_legs', feet: 'elemental_wizards_rpg:netherite_kelp_feet' }
    }
  },
  {
    key: 'elemental_wind_path', className: 'Elemental viento', short: 'Brisa Alta',
    match: ['wind_', 'hurricane_robe', 'staff_wind', 'viento', 'brisa'],
    bonuses: {
      one: '1 pieza: caida ligera.',
      two: '2 piezas: velocidad de viento.',
      three: '3 piezas: salto y avance.',
      four: '4 piezas: defensa movil.'
    },
    effects: {
      one: [{ effect: 'minecraft:slow_falling', seconds: 5, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:speed', seconds: 7, amp: 0, powerAmp: 5, maxAmp: 2 }],
      three: [{ effect: 'minecraft:jump_boost', seconds: 7, amp: 0, maxAmp: 1 }],
      four: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'elemental_wizards_rpg:wind_head', chest: 'elemental_wizards_rpg:wind_chest', legs: 'elemental_wizards_rpg:wind_legs', feet: 'elemental_wizards_rpg:wind_feet' },
      guardia: { head: 'elemental_wizards_rpg:wind_head', chest: 'elemental_wizards_rpg:wind_chest', legs: 'elemental_wizards_rpg:wind_legs', feet: 'elemental_wizards_rpg:wind_feet' },
      explorador: { head: 'elemental_wizards_rpg:hurricane_robe_head', chest: 'elemental_wizards_rpg:hurricane_robe_chest', legs: 'elemental_wizards_rpg:hurricane_robe_legs', feet: 'elemental_wizards_rpg:hurricane_robe_feet' },
      vigia: { head: 'elemental_wizards_rpg:hurricane_robe_head', chest: 'elemental_wizards_rpg:hurricane_robe_chest', legs: 'elemental_wizards_rpg:hurricane_robe_legs', feet: 'elemental_wizards_rpg:hurricane_robe_feet' },
      juramento: { head: 'elemental_wizards_rpg:netherite_wind_head', chest: 'elemental_wizards_rpg:netherite_wind_chest', legs: 'elemental_wizards_rpg:netherite_wind_legs', feet: 'elemental_wizards_rpg:netherite_wind_feet' },
      mitico: { head: 'elemental_wizards_rpg:netherite_wind_head', chest: 'elemental_wizards_rpg:netherite_wind_chest', legs: 'elemental_wizards_rpg:netherite_wind_legs', feet: 'elemental_wizards_rpg:netherite_wind_feet' }
    }
  },
  {
    key: 'elemental_earth_path', className: 'Elemental tierra', short: 'Galeria de Piedra',
    match: ['dripstone', 'mountain_robe', 'staff_terra', 'terra', 'piedra'],
    bonuses: {
      one: '1 pieza: firmeza de galeria.',
      two: '2 piezas: resistencia minera.',
      three: '3 piezas: prisa controlada.',
      four: '4 piezas: aguante de roca viva.'
    },
    effects: {
      one: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:haste', seconds: 7, amp: 0, maxAmp: 1 }],
      three: [{ effect: 'minecraft:luck', seconds: 7, amp: 0, maxAmp: 1 }],
      four: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, powerAmp: 4, maxAmp: 2 }]
    },
    pieces: {
      madriguera: { head: 'elemental_wizards_rpg:dripstone_head', chest: 'elemental_wizards_rpg:dripstone_chest', legs: 'elemental_wizards_rpg:dripstone_legs', feet: 'elemental_wizards_rpg:dripstone_feet' },
      guardia: { head: 'elemental_wizards_rpg:dripstone_head', chest: 'elemental_wizards_rpg:dripstone_chest', legs: 'elemental_wizards_rpg:dripstone_legs', feet: 'elemental_wizards_rpg:dripstone_feet' },
      explorador: { head: 'elemental_wizards_rpg:mountain_robe_head', chest: 'elemental_wizards_rpg:mountain_robe_chest', legs: 'elemental_wizards_rpg:mountain_robe_legs', feet: 'elemental_wizards_rpg:mountain_robe_feet' },
      vigia: { head: 'elemental_wizards_rpg:mountain_robe_head', chest: 'elemental_wizards_rpg:mountain_robe_chest', legs: 'elemental_wizards_rpg:mountain_robe_legs', feet: 'elemental_wizards_rpg:mountain_robe_feet' },
      juramento: { head: 'elemental_wizards_rpg:netherite_dripstone_head', chest: 'elemental_wizards_rpg:netherite_dripstone_chest', legs: 'elemental_wizards_rpg:netherite_dripstone_legs', feet: 'elemental_wizards_rpg:netherite_dripstone_feet' },
      mitico: { head: 'elemental_wizards_rpg:netherite_dripstone_head', chest: 'elemental_wizards_rpg:netherite_dripstone_chest', legs: 'elemental_wizards_rpg:netherite_dripstone_legs', feet: 'elemental_wizards_rpg:netherite_dripstone_feet' }
    }
  },
  {
    key: 'berserker_path', className: 'Berserker', short: 'Furia del Prado Rojo',
    match: ['berserker_rpg:', 'berserker', 'wildling', 'northling', 'warlord', 'furia'],
    bonuses: {
      one: '1 pieza: fuerza salvaje menor.',
      two: '2 piezas: golpe pesado.',
      three: '3 piezas: movilidad de asalto.',
      four: '4 piezas: fuerza y resistencia de furia.'
    },
    effects: {
      one: [{ effect: 'minecraft:strength', seconds: 7, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:strength', seconds: 7, amp: 0, powerAmp: 4, maxAmp: 2 }],
      three: [{ effect: 'minecraft:speed', seconds: 7, amp: 0, maxAmp: 0 }],
      four: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'berserker_rpg:wildling_head', chest: 'berserker_rpg:wildling_chest', legs: 'berserker_rpg:wildling_legs', feet: 'berserker_rpg:wildling_feet' },
      guardia: { head: 'berserker_rpg:wildling_head', chest: 'berserker_rpg:wildling_chest', legs: 'berserker_rpg:wildling_legs', feet: 'berserker_rpg:wildling_feet' },
      explorador: { head: 'berserker_rpg:northling_head', chest: 'berserker_rpg:northling_chest', legs: 'berserker_rpg:northling_legs', feet: 'berserker_rpg:northling_feet' },
      vigia: { head: 'berserker_rpg:warlord_head', chest: 'berserker_rpg:warlord_chest', legs: 'berserker_rpg:warlord_legs', feet: 'berserker_rpg:warlord_feet' },
      juramento: { head: 'berserker_rpg:netherite_northling_head', chest: 'berserker_rpg:netherite_northling_chest', legs: 'berserker_rpg:netherite_northling_legs', feet: 'berserker_rpg:netherite_northling_feet' },
      mitico: { head: 'berserker_rpg:netherite_northling_head', chest: 'berserker_rpg:netherite_northling_chest', legs: 'berserker_rpg:netherite_northling_legs', feet: 'berserker_rpg:netherite_northling_feet' }
    }
  },
  {
    key: 'bard_path', className: 'Bardo', short: 'Canto de Madriguera',
    match: ['bards_rpg:', 'bard', 'garb', 'harp', 'canto', 'melodia'],
    bonuses: {
      one: '1 pieza: suerte de relato.',
      two: '2 piezas: apoyo y sustento.',
      three: '3 piezas: movilidad de escena.',
      four: '4 piezas: regeneracion y fortuna de grupo.'
    },
    effects: {
      one: [{ effect: 'minecraft:luck', seconds: 7, amp: 0, maxAmp: 1 }],
      two: [{ effect: 'minecraft:saturation', seconds: 3, amp: 0, maxAmp: 0 }],
      three: [{ effect: 'minecraft:speed', seconds: 7, amp: 0, maxAmp: 0 }],
      four: [{ effect: 'minecraft:regeneration', seconds: 5, amp: 0, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'bards_rpg:entertainer_garb_head', chest: 'bards_rpg:entertainer_garb_chest', legs: 'bards_rpg:entertainer_garb_legs', feet: 'bards_rpg:entertainer_garb_feet' },
      guardia: { head: 'bards_rpg:entertainer_garb_head', chest: 'bards_rpg:entertainer_garb_chest', legs: 'bards_rpg:entertainer_garb_legs', feet: 'bards_rpg:entertainer_garb_feet' },
      explorador: { head: 'bards_rpg:storyteller_garb_head', chest: 'bards_rpg:storyteller_garb_chest', legs: 'bards_rpg:storyteller_garb_legs', feet: 'bards_rpg:storyteller_garb_feet' },
      vigia: { head: 'bards_rpg:troubadour_garb_head', chest: 'bards_rpg:troubadour_garb_chest', legs: 'bards_rpg:troubadour_garb_legs', feet: 'bards_rpg:troubadour_garb_feet' },
      juramento: { head: 'bards_rpg:netherite_troubadour_garb_head', chest: 'bards_rpg:netherite_troubadour_garb_chest', legs: 'bards_rpg:netherite_troubadour_garb_legs', feet: 'bards_rpg:netherite_troubadour_garb_feet' },
      mitico: { head: 'bards_rpg:netherite_troubadour_garb_head', chest: 'bards_rpg:netherite_troubadour_garb_chest', legs: 'bards_rpg:netherite_troubadour_garb_legs', feet: 'bards_rpg:netherite_troubadour_garb_feet' }
    }
  },
  {
    key: 'forcemaster_path', className: 'Forcemaster', short: 'Pulso Oculto',
    match: ['forcemaster_rpg:', 'knuckle', 'phasleb', 'oriene', 'aken', 'force', 'nudillo'],
    bonuses: {
      one: '1 pieza: fuerza de pulso.',
      two: '2 piezas: velocidad de impacto.',
      three: '3 piezas: salto y ruptura.',
      four: '4 piezas: fuerza y resistencia de maestro.'
    },
    effects: {
      one: [{ effect: 'minecraft:strength', seconds: 7, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:speed', seconds: 7, amp: 0, maxAmp: 1 }],
      three: [{ effect: 'minecraft:jump_boost', seconds: 7, amp: 0, maxAmp: 1 }],
      four: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'forcemaster_rpg:billporon_head', chest: 'forcemaster_rpg:billporon_chest', legs: 'forcemaster_rpg:billporon_legs', feet: 'forcemaster_rpg:billporon_feet' },
      guardia: { head: 'forcemaster_rpg:billporon_head', chest: 'forcemaster_rpg:billporon_chest', legs: 'forcemaster_rpg:billporon_legs', feet: 'forcemaster_rpg:billporon_feet' },
      explorador: { head: 'forcemaster_rpg:aken_head', chest: 'forcemaster_rpg:aken_chest', legs: 'forcemaster_rpg:aken_legs', feet: 'forcemaster_rpg:aken_feet' },
      vigia: { head: 'forcemaster_rpg:oriene_head', chest: 'forcemaster_rpg:oriene_chest', legs: 'forcemaster_rpg:oriene_legs', feet: 'forcemaster_rpg:oriene_feet' },
      juramento: { head: 'forcemaster_rpg:phasleb_head', chest: 'forcemaster_rpg:phasleb_chest', legs: 'forcemaster_rpg:phasleb_legs', feet: 'forcemaster_rpg:phasleb_feet' },
      mitico: { head: 'forcemaster_rpg:phasleb_head', chest: 'forcemaster_rpg:phasleb_chest', legs: 'forcemaster_rpg:phasleb_legs', feet: 'forcemaster_rpg:phasleb_feet' }
    }
  },
  {
    key: 'witcher_wolven_path', className: 'Witcher', short: 'Lobo de Sendero',
    match: ['witcher_rpg:', 'wolven', 'witcher', 'monster_hunter', 'silver'],
    bonuses: {
      one: '1 pieza: lectura de presas.',
      two: '2 piezas: defensa de cazador.',
      three: '3 piezas: movilidad y suerte contra monstruos.',
      four: '4 piezas: fuerza de acero y plata.'
    },
    effects: {
      one: [{ effect: 'minecraft:night_vision', seconds: 12, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:resistance', seconds: 7, amp: 0, maxAmp: 1 }],
      three: [{ effect: 'minecraft:speed', seconds: 7, amp: 0, maxAmp: 1 }, { effect: 'minecraft:luck', seconds: 7, amp: 0, maxAmp: 1 }],
      four: [{ effect: 'minecraft:strength', seconds: 7, amp: 0, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'witcher_rpg:witcher_head', chest: 'witcher_rpg:witcher_chest', legs: 'witcher_rpg:witcher_legs', feet: 'witcher_rpg:witcher_feet' },
      guardia: { head: 'witcher_rpg:wolven_head', chest: 'witcher_rpg:wolven_chest', legs: 'witcher_rpg:wolven_legs', feet: 'witcher_rpg:wolven_feet' },
      explorador: { head: 'witcher_rpg:enhanced_wolven_head', chest: 'witcher_rpg:enhanced_wolven_chest', legs: 'witcher_rpg:enhanced_wolven_legs', feet: 'witcher_rpg:enhanced_wolven_feet' },
      vigia: { head: 'witcher_rpg:superior_wolven_head', chest: 'witcher_rpg:superior_wolven_chest', legs: 'witcher_rpg:superior_wolven_legs', feet: 'witcher_rpg:superior_wolven_feet' },
      juramento: { head: 'witcher_rpg:mastercrafted_wolven_head', chest: 'witcher_rpg:mastercrafted_wolven_chest', legs: 'witcher_rpg:mastercrafted_wolven_legs', feet: 'witcher_rpg:mastercrafted_wolven_feet' },
      mitico: { head: 'witcher_rpg:grandmaster_wolven_head', chest: 'witcher_rpg:grandmaster_wolven_chest', legs: 'witcher_rpg:grandmaster_wolven_legs', feet: 'witcher_rpg:grandmaster_wolven_feet' }
    }
  },
  {
    key: 'witcher_feline_path', className: 'Witcher felino', short: 'Garra del Prado',
    match: ['feline', 'witcher_rpg:', 'aerondight', 'iris_sword'],
    bonuses: {
      one: '1 pieza: reflejo rapido.',
      two: '2 piezas: velocidad de duelist.',
      three: '3 piezas: salto y suerte de caza.',
      four: '4 piezas: dano sostenido contra presas.'
    },
    effects: {
      one: [{ effect: 'minecraft:speed', seconds: 7, amp: 0, maxAmp: 0 }],
      two: [{ effect: 'minecraft:speed', seconds: 7, amp: 0, powerAmp: 5, maxAmp: 2 }],
      three: [{ effect: 'minecraft:jump_boost', seconds: 7, amp: 0, maxAmp: 1 }],
      four: [{ effect: 'minecraft:strength', seconds: 7, amp: 0, maxAmp: 1 }]
    },
    pieces: {
      madriguera: { head: 'witcher_rpg:witcher_head', chest: 'witcher_rpg:witcher_chest', legs: 'witcher_rpg:witcher_legs', feet: 'witcher_rpg:witcher_feet' },
      guardia: { head: 'witcher_rpg:feline_head', chest: 'witcher_rpg:feline_chest', legs: 'witcher_rpg:feline_legs', feet: 'witcher_rpg:feline_feet' },
      explorador: { head: 'witcher_rpg:enhanced_feline_head', chest: 'witcher_rpg:enhanced_feline_chest', legs: 'witcher_rpg:enhanced_feline_legs', feet: 'witcher_rpg:enhanced_feline_feet' },
      vigia: { head: 'witcher_rpg:superior_feline_head', chest: 'witcher_rpg:superior_feline_chest', legs: 'witcher_rpg:superior_feline_legs', feet: 'witcher_rpg:superior_feline_feet' },
      juramento: { head: 'witcher_rpg:mastercrafted_feline_head', chest: 'witcher_rpg:mastercrafted_feline_chest', legs: 'witcher_rpg:mastercrafted_feline_legs', feet: 'witcher_rpg:mastercrafted_feline_feet' },
      mitico: { head: 'witcher_rpg:grandmaster_feline_head', chest: 'witcher_rpg:grandmaster_feline_chest', legs: 'witcher_rpg:grandmaster_feline_legs', feet: 'witcher_rpg:grandmaster_feline_feet' }
    }
  }
];

function cunirpgSetTierByKey(key) {
  var raw = String(key || '').toLowerCase();
  if (raw === 'common' || raw === 'comun') raw = 'madriguera';
  if (raw === 'uncommon' || raw === 'incomun') raw = 'guardia';
  if (raw === 'rare' || raw === 'raro') raw = 'explorador';
  if (raw === 'epic' || raw === 'epico') raw = 'vigia';
  if (raw === 'legendary' || raw === 'legendario') raw = 'juramento';
  if (raw === 'mythic' || raw === 'mitico') raw = 'mitico';
  for (var i = 0; i < CUNIRPG_SET_TIERS.length; i++) {
    if (CUNIRPG_SET_TIERS[i].key === raw) {
      return CUNIRPG_SET_TIERS[i];
    }
  }
  return CUNIRPG_SET_TIERS[0];
}

function cunirpgSetFamilyByKey(key) {
  var raw = String(key || '').toLowerCase();
  for (var i = 0; i < CUNIRPG_SET_FAMILIES.length; i++) {
    if (CUNIRPG_SET_FAMILIES[i].key === raw || CUNIRPG_SET_FAMILIES[i].className.toLowerCase() === raw) {
      return CUNIRPG_SET_FAMILIES[i];
    }
  }
  return null;
}

function cunirpgSetFamilyForItem(itemId, fallbackText) {
  var text = String(itemId || '') + ' ' + String(fallbackText || '');
  text = text.toLowerCase();
  var best = null;
  var bestScore = -1;
  for (var i = 0; i < CUNIRPG_SET_FAMILIES.length; i++) {
    var family = CUNIRPG_SET_FAMILIES[i];
    var score = 0;
    for (var j = 0; j < family.match.length; j++) {
      if (text.indexOf(String(family.match[j]).toLowerCase()) >= 0) {
        score++;
      }
    }
    if (score > bestScore) {
      best = family;
      bestScore = score;
    }
  }
  return bestScore > 0 ? best : null;
}

function cunirpgSetSlotFromItem(itemId) {
  var id = String(itemId || '').toLowerCase();
  if (id.indexOf('_head') >= 0 || id.indexOf('_helmet') >= 0 || id.indexOf('helmet') >= 0 || id.indexOf('head') >= 0) return 'head';
  if (id.indexOf('_chest') >= 0 || id.indexOf('_chestplate') >= 0 || id.indexOf('chestplate') >= 0 || id.indexOf('chest') >= 0) return 'chest';
  if (id.indexOf('_legs') >= 0 || id.indexOf('_leggings') >= 0 || id.indexOf('leggings') >= 0 || id.indexOf('legs') >= 0) return 'legs';
  if (id.indexOf('_feet') >= 0 || id.indexOf('_boots') >= 0 || id.indexOf('boots') >= 0 || id.indexOf('feet') >= 0) return 'feet';
  return '';
}

function cunirpgSetEntryName(family, tier, slot) {
  var slotLabel = CUNIRPG_SLOT_LABELS[slot] || 'pieza';
  return slotLabel + ' de ' + family.short + ' ' + tier.label;
}

function cunirpgSetMetaForItem(itemId, data, displayName) {
  var itemData = data || {};
  if (itemData.cuniculum_kind !== 'armor' && String(itemId || '').indexOf('_helmet') < 0 && String(itemId || '').indexOf('_chestplate') < 0 && String(itemId || '').indexOf('_leggings') < 0 && String(itemId || '').indexOf('_boots') < 0) {
    return null;
  }
  var tier = cunirpgSetTierByKey(itemData.cuniculum_tier || itemData.cuniculum_set || itemData.cuniculum_set_tier || 'madriguera');
  var family = cunirpgSetFamilyForItem(itemId, displayName || '');
  if (!family) {
    family = CUNIRPG_SET_FAMILIES[0];
  }
  var slot = cunirpgSetSlotFromItem(itemId);
  var setId = family.key + '_' + tier.key;
  return {
    id: setId,
    name: family.short + ' ' + tier.label,
    className: family.className,
    slot: slot,
    tier: tier,
    family: family
  };
}

function cunirpgApplySetData(itemId, name, data) {
  var itemData = data || {};
  var meta = cunirpgSetMetaForItem(itemId, itemData, name);
  if (!meta) {
    return itemData;
  }
  itemData.cuniculum_set = meta.tier.key;
  itemData.cuniculum_set_id = meta.id;
  itemData.cuniculum_set_name = meta.name;
  itemData.cuniculum_set_class = meta.className;
  itemData.cuniculum_set_piece = meta.slot || 'pieza';
  itemData.cuniculum_set_bonus = meta.family.key;
  return itemData;
}

function cunirpgSetLoreLines(itemData) {
  var loreLines = [];
  if (itemData.cuniculum_set_id) {
    var family = cunirpgSetFamilyByKey(itemData.cuniculum_set_bonus);
    loreLines.push({ text: 'Set CUNICULUM: ' + itemData.cuniculum_set_name, color: 'dark_aqua' });
    loreLines.push({ text: 'Pieza: ' + (CUNIRPG_SLOT_LABELS[itemData.cuniculum_set_piece] || itemData.cuniculum_set_piece) + ' | Clase: ' + itemData.cuniculum_set_class, color: 'gray' });
    if (family && family.bonuses) {
      loreLines.push({ text: family.bonuses.one, color: 'green' });
      loreLines.push({ text: family.bonuses.two, color: 'green' });
      loreLines.push({ text: family.bonuses.three, color: 'yellow' });
      loreLines.push({ text: family.bonuses.four, color: 'gold' });
    }
    loreLines.push({ text: 'Las piezas caen repartidas entre enemigos y rarezas compatibles.', color: 'dark_gray' });
  }
  return loreLines;
}

function cunirpgCountSetPieces(player) {
  var result = {};
  var armorSlots = [
    ['headArmorItem', 'getHeadArmorItem', 'helmetItem'],
    ['chestArmorItem', 'getChestArmorItem', 'chestplateItem'],
    ['legsArmorItem', 'getLegsArmorItem', 'leggingsItem'],
    ['feetArmorItem', 'getFeetArmorItem', 'bootsItem']
  ];
  for (var i = 0; i < armorSlots.length; i++) {
    var stack = cunimobReadStack(player, armorSlots[i]);
    var text = String(cunimobStackText(stack));
    var match = text.match(/cuniculum_set_id:"([^"]+)"/);
    if (!match) {
      match = text.match(/cuniculum_set_id:([a-z0-9_]+)/);
    }
    if (match && match[1]) {
      result[match[1]] = (result[match[1]] || 0) + 1;
    }
  }
  return result;
}

var CUNIRPG_BASE_cunimobApplyClassSetBonuses = cunimobApplyClassSetBonuses;
cunimobApplyClassSetBonuses = function (server, safeName, player, armorPower) {
  CUNIRPG_BASE_cunimobApplyClassSetBonuses(server, safeName, player, armorPower);
  var counts = cunirpgCountSetPieces(player);
  var keys = Object.keys(counts);
  for (var i = 0; i < keys.length; i++) {
    var setId = keys[i];
    var count = counts[setId];
    for (var f = 0; f < CUNIRPG_SET_FAMILIES.length; f++) {
      var family = CUNIRPG_SET_FAMILIES[f];
      if (setId.indexOf(family.key + '_') !== 0) {
        continue;
      }
      if (count >= 1) cunimobApplySetEffectPlan(server, safeName, family.effects.one, armorPower);
      if (count >= 2) cunimobApplySetEffectPlan(server, safeName, family.effects.two, armorPower);
      if (count >= 3) cunimobApplySetEffectPlan(server, safeName, family.effects.three, armorPower);
      if (count >= 4) cunimobApplySetEffectPlan(server, safeName, family.effects.four, armorPower);
    }
  }
};

function cunirpgSetEntryFor(family, tier, slot) {
  if (!family || !tier || !slot || !family.pieces || !family.pieces[tier.key]) {
    return null;
  }
  var id = family.pieces[tier.key][slot];
  if (!id) {
    return null;
  }
  return { id: id, name: cunirpgSetEntryName(family, tier, slot), role: family.key, family: family.key };
}

function cunirpgGiveSet(source, familyKey, rawTier, rawItemRarity, rawLevel) {
  var server = source ? source.getServer() : null;
  var player = null;
  try { player = source.getPlayer(); } catch (ignoredPlayer) { }
  if (!server || !player) {
    return 0;
  }
  var safeName = cunihordeSafePlayer(player.username);
  var family = cunirpgSetFamilyByKey(familyKey);
  var tier = cuniitemTierByKey(rawTier) || cuniitemTierByKey(cunirpgSetTierByKey(rawTier).key);
  if (!family || !tier) {
    cuniitemFeedback(server, safeName, 'Uso: /cuniset give <clase> <tier> <rareza_item> <nivel>. Clase o tier invalido.', 'red');
    return 0;
  }
  var slots = ['head', 'chest', 'legs', 'feet'];
  var count = 0;
  for (var i = 0; i < slots.length; i++) {
    var entry = cunirpgSetEntryFor(family, cunirpgSetTierByKey(tier.key), slots[i]);
    if (!entry) {
      continue;
    }
    var selector = { kind: 'armor', slot: slots[i], role: family.key, family: family.key };
    var itemText = cuniitemBuildFromEntry(entry, tier, selector, rawLevel || tier.min || 1, rawItemRarity || cunirpgSetTierByKey(tier.key).rarity, 'minecraft:zombie', '');
    if (cuniitemGiveRaw(server, safeName, itemText, family.className + ' / ' + slots[i])) {
      count++;
    }
  }
  cuniitemFeedback(server, safeName, 'Set ' + family.short + ' entregado. Piezas: ' + count + '/4.', count === 4 ? 'green' : 'yellow');
  return count > 0 ? 1 : 0;
}

var CUNIRPG_BASE_cuniitemGiveSetFromSource = cuniitemGiveSetFromSource;
cuniitemGiveSetFromSource = function (source, rawTier, rawItemRarity, rawLevel) {
  var tier = cunirpgSetTierByKey(rawTier);
  var pool = [];
  for (var i = 0; i < CUNIRPG_SET_FAMILIES.length; i++) {
    if (CUNIRPG_SET_FAMILIES[i].pieces && CUNIRPG_SET_FAMILIES[i].pieces[tier.key]) {
      pool.push(CUNIRPG_SET_FAMILIES[i]);
    }
  }
  var family = cunimobRandomChoice(pool) || CUNIRPG_SET_FAMILIES[0];
  return cunirpgGiveSet(source, family.key, tier.key, rawItemRarity, rawLevel);
};

ServerEvents.commandRegistry(function (event) {
  var commands = event.commands;
  var args = event.arguments;
  event.register(commands.literal('cuniset')
    .then(commands.literal('list')
      .executes(function (ctx) {
        var server = ctx.getSource().getServer();
        var player = null;
        try { player = ctx.getSource().getPlayer(); } catch (ignored) { }
        var safeName = player ? cunihordeSafePlayer(player.username) : '';
        if (safeName) {
          var names = [];
          for (var i = 0; i < CUNIRPG_SET_FAMILIES.length; i++) names.push(CUNIRPG_SET_FAMILIES[i].key);
          cuniitemFeedback(server, safeName, 'Clases de set: ' + names.join(', '), 'gold');
        }
        return 1;
      }))
    .then(commands.literal('give')
      .then(commands.argument('clase', args.STRING.create(event))
        .then(commands.argument('tier', args.STRING.create(event))
          .then(commands.argument('rareza_item', args.STRING.create(event))
            .then(commands.argument('nivel', args.STRING.create(event))
              .executes(function (ctx) {
                return cunirpgGiveSet(
                  ctx.getSource(),
                  args.STRING.getResult(ctx, 'clase'),
                  args.STRING.getResult(ctx, 'tier'),
                  args.STRING.getResult(ctx, 'rareza_item'),
                  args.STRING.getResult(ctx, 'nivel')
                );
              })))))));
});

function cunirpgPlayerRotation(player, names) {
  for (var i = 0; i < names.length; i++) {
    try {
      var value = player[names[i]];
      if (typeof value === 'function') value = value.call(player);
      if (value || value === 0) return Number(value);
    } catch (ignored) { }
  }
  return 0;
}

function cunirpgMinePlane(player) {
  var pitch = cunirpgPlayerRotation(player, ['xRot', 'getXRot', 'pitch']);
  if (Math.abs(pitch) >= 55) {
    return 'xz';
  }
  var yaw = cunirpgPlayerRotation(player, ['yRot', 'getYRot', 'yaw']);
  yaw = ((yaw % 360) + 360) % 360;
  if ((yaw >= 45 && yaw < 135) || (yaw >= 225 && yaw < 315)) {
    return 'yz';
  }
  return 'xy';
}

function cunirpgExtent(center, size) {
  var n = Math.max(1, Math.floor(Number(size || 1)));
  if (n <= 1) return [center, center];
  if (n === 2) return [center, center + 1];
  return [center - 1, center + 1];
}

function cunirpgShapeSize(shape) {
  var s = String(shape || '1x1').toLowerCase();
  var parts = s.split('x');
  return { w: Math.max(1, Number(parts[0] || 1)), h: Math.max(1, Number(parts[1] || 1)) };
}

function cunirpgAreaMineBounds(shape, pos, player) {
  var size = cunirpgShapeSize(shape);
  var plane = cunirpgMinePlane(player);
  var w = cunirpgExtent(0, size.w);
  var h = cunirpgExtent(0, size.h);
  if (plane === 'xz') {
    return { x1: pos.x + w[0], y1: pos.y, z1: pos.z + h[0], x2: pos.x + w[1], y2: pos.y, z2: pos.z + h[1] };
  }
  if (plane === 'yz') {
    return { x1: pos.x, y1: pos.y + h[0], z1: pos.z + w[0], x2: pos.x, y2: pos.y + h[1], z2: pos.z + w[1] };
  }
  return { x1: pos.x + w[0], y1: pos.y + h[0], z1: pos.z, x2: pos.x + w[1], y2: pos.y + h[1], z2: pos.z };
}

cunimobTryAreaMine = function (event) {
  var player = event ? event.player : null;
  if (!event || !player) {
    return false;
  }
  var shape = cunimobHeldMiningShape(player);
  if (!shape) {
    return false;
  }
  var dimension = cunimobDimensionFromEvent(event, player);
  if (!cunimobIsCombatDimension(dimension) || dimension === CUNIMOB_SPAWN_DIMENSION) {
    return false;
  }
  var now = Date.now();
  try {
    if (Number(player.persistentData.cuniculum_area_mine_ms || 0) + CUNIMOB_AREA_MINE_COOLDOWN_MS > now) {
      return false;
    }
    player.persistentData.cuniculum_area_mine_ms = now;
  } catch (ignoredCooldown) { }
  var pos = cunimobBlockPosition(event);
  if (!pos.ok) {
    return false;
  }
  var bounds = cunirpgAreaMineBounds(shape, pos, player);
  if (!bounds) {
    return false;
  }
  var server = null;
  try { server = event.server; } catch (ignoredServer) { }
  try { if (!server) { server = player.server; } } catch (ignoredPlayerServer) { }
  if (!server) {
    return false;
  }
  server.runCommandSilent('execute in ' + dimension + ' run fill ' + bounds.x1 + ' ' + bounds.y1 + ' ' + bounds.z1 + ' ' + bounds.x2 + ' ' + bounds.y2 + ' ' + bounds.z2 + ' minecraft:air destroy');
  return true;
};
