package net.nunnerycode.bukkit.mythicdrops.leveledmobs;

import net.nunnerycode.bukkit.mythicdrops.api.MythicDrops;
import net.nunnerycode.bukkit.mythicdrops.api.items.ItemGenerationReason;
import net.nunnerycode.bukkit.mythicdrops.api.tiers.Tier;
import net.nunnerycode.bukkit.mythicdrops.events.EntityEquipEvent;
import net.nunnerycode.bukkit.mythicdrops.events.RandomItemGenerationEvent;
import net.nunnerycode.bukkit.mythicdrops.items.MythicDropBuilder;
import net.nunnerycode.bukkit.mythicdrops.utils.TierUtil;

import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;

import java.util.Collection;
import java.util.List;

import mainLeveledMobs.LeveledMobs;

public class LeveledMobsWrapper implements Listener {

  private MythicDrops mythicDrops;
  private LeveledMobs leveledMobs;

  public LeveledMobsWrapper(MythicDrops mythicDrops) {
    this.mythicDrops = mythicDrops;
    leveledMobs = (LeveledMobs) Bukkit.getPluginManager().getPlugin("LeveledMobs");
  }

  @EventHandler(priority = EventPriority.HIGH)
  public void onRandomItemGenerationEvent(EntityEquipEvent event) {
    if (event.isCancelled()) {
      return;
    }

    Collection<Tier> originalTiers = mythicDrops.getCreatureSpawningSettings().getEntityTypeTiers
        (event.getLivingEntity().getType());
    Collection<Tier> skewedTiers = TierUtil.skewTierCollectionToRarer(originalTiers,
        Math.max(originalTiers.size() - leveledMobs.getCreatureLevel(event.getLivingEntity()), 1));
    Tier t = TierUtil.randomTierWithChance(skewedTiers, event.getLivingEntity().getWorld()
        .getName());
    ItemStack is = new MythicDropBuilder().inWorld(event.getLivingEntity().getWorld())
        .useDurability(true).withTier(t).withItemGenerationReason(ItemGenerationReason
                                                                      .MONSTER_SPAWN).build();
    event.setItemStack(is);
  }

}

@@ -540,47 +540,8 @@
 it;%0A
-import org.bukkit.entity.LivingEntity;%0A
 impo
@@ -719,31 +719,8 @@
 ion;
-%0Aimport java.util.List;
 %0A%0Aim
@@ -1495,16 +1495,17 @@
 ize() - 
+(
 leveledM
@@ -1549,16 +1549,62 @@
 ntity())
+ -%0A                                         1)
 , 1));%0A 

subtracting one in case of zero