<commit_before>package net.nunnerycode.bukkit.mythicdrops.spawning;

import net.nunnerycode.bukkit.mythicdrops.MythicDropsPlugin;
import net.nunnerycode.bukkit.mythicdrops.api.MythicDrops;
import net.nunnerycode.bukkit.mythicdrops.api.items.CustomItem;
import net.nunnerycode.bukkit.mythicdrops.api.items.ItemGenerationReason;
import net.nunnerycode.bukkit.mythicdrops.api.names.NameType;
import net.nunnerycode.bukkit.mythicdrops.api.tiers.Tier;
import net.nunnerycode.bukkit.mythicdrops.events.EntitySpawningEvent;
import net.nunnerycode.bukkit.mythicdrops.identification.IdentityTome;
import net.nunnerycode.bukkit.mythicdrops.identification.UnidentifiedItem;
import net.nunnerycode.bukkit.mythicdrops.names.NameMap;
import net.nunnerycode.bukkit.mythicdrops.socketting.SocketGem;
import net.nunnerycode.bukkit.mythicdrops.socketting.SocketItem;
import net.nunnerycode.bukkit.mythicdrops.utils.CustomItemUtil;
import net.nunnerycode.bukkit.mythicdrops.utils.EntityUtil;
import net.nunnerycode.bukkit.mythicdrops.utils.ItemStackUtil;
import net.nunnerycode.bukkit.mythicdrops.utils.SocketGemUtil;
import net.nunnerycode.bukkit.mythicdrops.utils.TierUtil;

import org.apache.commons.lang.math.RandomUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.entity.Skeleton;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;

public final class ItemSpawningListener implements Listener {

  private MythicDrops mythicDrops;

  public ItemSpawningListener(MythicDropsPlugin mythicDrops) {
    this.mythicDrops = mythicDrops;
  }

  public MythicDrops getMythicDrops() {
    return mythicDrops;
  }

  @EventHandler(priority = EventPriority.LOWEST)
  public void onCreatureSpawnEventLowest(CreatureSpawnEvent event) {
    if (!(event.getEntity() instanceof Monster) || event.isCancelled()) {
      return;
    }
    if (!mythicDrops.getConfigSettings().getEnabledWorlds().contains(event.getEntity().getWorld()
                                                                         .getName())) {
      return;
    }
    if (mythicDrops.getConfigSettings().isGiveAllMobsNames()) {
      nameMobs(event.getEntity());
    }
    if (mythicDrops.getConfigSettings().isBlankMobSpawnEnabled()) {
      event.getEntity().getEquipment().clear();
      if (event.getEntity() instanceof Skeleton && !mythicDrops.getConfigSettings()
          .isSkeletonsSpawnWithoutBows()) {
        event.getEntity().getEquipment().setItemInHand(new ItemStack(Material.BOW, 1));
      }
    }
    if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER
        && mythicDrops.getCreatureSpawningSettings().isPreventSpawner()) {
      event.getEntity()
          .setCanPickupItems(mythicDrops.getConfigSettings().isMobsPickupEquipment());
      return;
    }
    if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG
        && mythicDrops.getCreatureSpawningSettings().isPreventSpawner()) {
      event.getEntity()
          .setCanPickupItems(mythicDrops.getConfigSettings().isMobsPickupEquipment());
      return;
    }
    if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.CUSTOM &&
        mythicDrops.getCreatureSpawningSettings().isPreventCustom()) {
      event.getEntity()
          .setCanPickupItems(mythicDrops.getConfigSettings().isMobsPickupEquipment());
      return;
    }
    if (event.getEntity().getLocation().getY() > mythicDrops.getCreatureSpawningSettings()
        .getSpawnHeightLimit(event.getEntity
            ().getWorld().getName())) {
      event.getEntity()
          .setCanPickupItems(mythicDrops.getConfigSettings().isMobsPickupEquipment());
      return;
    }
    event.getEntity()
        .setCanPickupItems(mythicDrops.getConfigSettings().isMobsPickupEquipment());
  }

  @EventHandler(priority = EventPriority.LOW)
  public void onCreatureSpawnEvent(CreatureSpawnEvent event) {
    if (!(event.getEntity() instanceof Monster) || event.isCancelled()) {
      return;
    }
    if (!mythicDrops.getConfigSettings().getEnabledWorlds().contains(event.getEntity().getWorld()
                                                                         .getName())) {
      return;
    }
    if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER
        && mythicDrops.getCreatureSpawningSettings().isPreventSpawner()) {
      return;
    }
    if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG
        && mythicDrops.getCreatureSpawningSettings().isPreventSpawner()) {
      return;
    }
    if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.CUSTOM &&
        mythicDrops.getCreatureSpawningSettings().isPreventSpawner()) {
      return;
    }
    if (mythicDrops.getCreatureSpawningSettings()
            .getSpawnHeightLimit(event.getEntity().getWorld().getName()) <= event
            .getEntity().getLocation().getY()) {
      return;
    }
    if (!mythicDrops.getConfigSettings().isDisplayMobEquipment()) {
      return;
    }

    // Start off with the random item chance. If the mob doesn't pass that, it gets no items.
    double chanceToGetDrop = mythicDrops.getConfigSettings().getItemChance() * mythicDrops
        .getCreatureSpawningSettings().getEntityTypeChanceToSpawn(event.getEntity().getType());
    if (RandomUtils.nextDouble() > chanceToGetDrop) {
      return;
    }

    // Choose a tier for the item that the mob is given. If the tier is null, it gets no items.
    Collection<Tier> allowableTiers = mythicDrops.getCreatureSpawningSettings()
        .getEntityTypeTiers(event.getEntity().getType());
    Tier tier = TierUtil.randomTierWithChance(allowableTiers);
    if (tier == null) {
      return;
    }

    // Create the item for the mob.
    ItemStack itemStack = MythicDropsPlugin.getNewDropBuilder().withItemGenerationReason(
        ItemGenerationReason.MONSTER_SPAWN).useDurability(false).withTier(tier).build();

    if (itemStack == null) {
      return;
    }

    // Begin to check for socket gem, identity tome, and unidentified.
    double socketGemChance = mythicDrops.getConfigSettings().getSocketGemChance();
    double unidentifiedItemChance = mythicDrops.getConfigSettings().getUnidentifiedItemChance();
    double identityTomeChance = mythicDrops.getConfigSettings().getIdentityTomeChance();
    boolean sockettingEnabled = mythicDrops.getConfigSettings().isSockettingEnabled();
    boolean identifyingEnabled = mythicDrops.getConfigSettings().isIdentifyingEnabled();

    if (sockettingEnabled && RandomUtils.nextDouble() <= socketGemChance) {
      SocketGem socketGem = SocketGemUtil.getRandomSocketGemWithChance();
      Material material = SocketGemUtil.getRandomSocketGemMaterial();
      mythicDrops.debug(Level.FINEST, "socketGem != null: " + (socketGem != null));
      mythicDrops.debug(Level.FINEST, "material != null: " + (material != null));
      if (socketGem != null && material != null) {
        itemStack = new SocketItem(material, socketGem);
      }
    } else if (identifyingEnabled && RandomUtils.nextDouble() <= unidentifiedItemChance) {
      Material material = itemStack.getType();
      itemStack = new UnidentifiedItem(material);
    } else if (identifyingEnabled && RandomUtils.nextDouble() <= identityTomeChance) {
      itemStack = new IdentityTome();
    }

    EntitySpawningEvent ese = new EntitySpawningEvent(event.getEntity());
    Bukkit.getPluginManager().callEvent(ese);

    EntityUtil.equipEntity(event.getEntity(), itemStack);

    nameMobs(event.getEntity());
  }

  @EventHandler
  public void onEntityDeath(EntityDeathEvent event) {
    if (event.getEntity() instanceof Player || event.getEntity().getLastDamageCause() == null
        || event.getEntity().getLastDamageCause().isCancelled()) {
      return;
    }<commit_msg>commenting out some debug lines<commit_after>package net.nunnerycode.bukkit.mythicdrops.spawning;

import net.nunnerycode.bukkit.mythicdrops.MythicDropsPlugin;
import net.nunnerycode.bukkit.mythicdrops.api.MythicDrops;
import net.nunnerycode.bukkit.mythicdrops.api.items.CustomItem;
import net.nunnerycode.bukkit.mythicdrops.api.items.ItemGenerationReason;
import net.nunnerycode.bukkit.mythicdrops.api.names.NameType;
import net.nunnerycode.bukkit.mythicdrops.api.tiers.Tier;
import net.nunnerycode.bukkit.mythicdrops.events.EntitySpawningEvent;
import net.nunnerycode.bukkit.mythicdrops.identification.IdentityTome;
import net.nunnerycode.bukkit.mythicdrops.identification.UnidentifiedItem;
import net.nunnerycode.bukkit.mythicdrops.names.NameMap;
import net.nunnerycode.bukkit.mythicdrops.socketting.SocketGem;
import net.nunnerycode.bukkit.mythicdrops.socketting.SocketItem;
import net.nunnerycode.bukkit.mythicdrops.utils.CustomItemUtil;
import net.nunnerycode.bukkit.mythicdrops.utils.EntityUtil;
import net.nunnerycode.bukkit.mythicdrops.utils.ItemStackUtil;
import net.nunnerycode.bukkit.mythicdrops.utils.SocketGemUtil;
import net.nunnerycode.bukkit.mythicdrops.utils.TierUtil;

import org.apache.commons.lang.math.RandomUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.entity.Skeleton;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;

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

public final class ItemSpawningListener implements Listener {

  private MythicDrops mythicDrops;

  public ItemSpawningListener(MythicDropsPlugin mythicDrops) {
    this.mythicDrops = mythicDrops;
  }

  public MythicDrops getMythicDrops() {
    return mythicDrops;
  }

  @EventHandler(priority = EventPriority.LOWEST)
  public void onCreatureSpawnEventLowest(CreatureSpawnEvent event) {
    if (!(event.getEntity() instanceof Monster) || event.isCancelled()) {
      return;
    }
    if (!mythicDrops.getConfigSettings().getEnabledWorlds().contains(event.getEntity().getWorld()
                                                                         .getName())) {
      return;
    }
    if (mythicDrops.getConfigSettings().isGiveAllMobsNames()) {
      nameMobs(event.getEntity());
    }
    if (mythicDrops.getConfigSettings().isBlankMobSpawnEnabled()) {
      event.getEntity().getEquipment().clear();
      if (event.getEntity() instanceof Skeleton && !mythicDrops.getConfigSettings()
          .isSkeletonsSpawnWithoutBows()) {
        event.getEntity().getEquipment().setItemInHand(new ItemStack(Material.BOW, 1));
      }
    }
    if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER
        && mythicDrops.getCreatureSpawningSettings().isPreventSpawner()) {
      event.getEntity()
          .setCanPickupItems(mythicDrops.getConfigSettings().isMobsPickupEquipment());
      return;
    }
    if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG
        && mythicDrops.getCreatureSpawningSettings().isPreventSpawner()) {
      event.getEntity()
          .setCanPickupItems(mythicDrops.getConfigSettings().isMobsPickupEquipment());
      return;
    }
    if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.CUSTOM &&
        mythicDrops.getCreatureSpawningSettings().isPreventCustom()) {
      event.getEntity()
          .setCanPickupItems(mythicDrops.getConfigSettings().isMobsPickupEquipment());
      return;
    }
    if (event.getEntity().getLocation().getY() > mythicDrops.getCreatureSpawningSettings()
        .getSpawnHeightLimit(event.getEntity
            ().getWorld().getName())) {
      event.getEntity()
          .setCanPickupItems(mythicDrops.getConfigSettings().isMobsPickupEquipment());
      return;
    }
    event.getEntity()
        .setCanPickupItems(mythicDrops.getConfigSettings().isMobsPickupEquipment());
  }

  @EventHandler(priority = EventPriority.LOW)
  public void onCreatureSpawnEvent(CreatureSpawnEvent event) {
    if (!(event.getEntity() instanceof Monster) || event.isCancelled()) {
      return;
    }
    if (!mythicDrops.getConfigSettings().getEnabledWorlds().contains(event.getEntity().getWorld()
                                                                         .getName())) {
      return;
    }
    if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER
        && mythicDrops.getCreatureSpawningSettings().isPreventSpawner()) {
      return;
    }
    if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG
        && mythicDrops.getCreatureSpawningSettings().isPreventSpawner()) {
      return;
    }
    if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.CUSTOM &&
        mythicDrops.getCreatureSpawningSettings().isPreventSpawner()) {
      return;
    }
    if (mythicDrops.getCreatureSpawningSettings()
            .getSpawnHeightLimit(event.getEntity().getWorld().getName()) <= event
            .getEntity().getLocation().getY()) {
      return;
    }
    if (!mythicDrops.getConfigSettings().isDisplayMobEquipment()) {
      return;
    }

    // Start off with the random item chance. If the mob doesn't pass that, it gets no items.
    double chanceToGetDrop = mythicDrops.getConfigSettings().getItemChance() * mythicDrops
        .getCreatureSpawningSettings().getEntityTypeChanceToSpawn(event.getEntity().getType());
    if (RandomUtils.nextDouble() > chanceToGetDrop) {
      return;
    }

    // Choose a tier for the item that the mob is given. If the tier is null, it gets no items.
    Collection<Tier> allowableTiers = mythicDrops.getCreatureSpawningSettings()
        .getEntityTypeTiers(event.getEntity().getType());
    Tier tier = TierUtil.randomTierWithChance(allowableTiers);
    if (tier == null) {
      return;
    }

    // Create the item for the mob.
    ItemStack itemStack = MythicDropsPlugin.getNewDropBuilder().withItemGenerationReason(
        ItemGenerationReason.MONSTER_SPAWN).useDurability(false).withTier(tier).build();

    if (itemStack == null) {
      return;
    }

    // Begin to check for socket gem, identity tome, and unidentified.
    double socketGemChance = mythicDrops.getConfigSettings().getSocketGemChance();
    double unidentifiedItemChance = mythicDrops.getConfigSettings().getUnidentifiedItemChance();
    double identityTomeChance = mythicDrops.getConfigSettings().getIdentityTomeChance();
    boolean sockettingEnabled = mythicDrops.getConfigSettings().isSockettingEnabled();
    boolean identifyingEnabled = mythicDrops.getConfigSettings().isIdentifyingEnabled();

    if (sockettingEnabled && RandomUtils.nextDouble() <= socketGemChance) {
      SocketGem socketGem = SocketGemUtil.getRandomSocketGemWithChance();
      Material material = SocketGemUtil.getRandomSocketGemMaterial();
//      mythicDrops.debug(Level.FINEST, "socketGem != null: " + (socketGem != null));
//      mythicDrops.debug(Level.FINEST, "material != null: " + (material != null));
      if (socketGem != null && material != null) {
        itemStack = new SocketItem(material, socketGem);
      }
    } else if (identifyingEnabled && RandomUtils.nextDouble() <= unidentifiedItemChance) {
      Material material = itemStack.getType();
      itemStack = new UnidentifiedItem(material);
    } else if (identifyingEnabled && RandomUtils.nextDouble() <= identityTomeChance) {
      itemStack = new IdentityTome();
    }

    EntitySpawningEvent ese = new EntitySpawningEvent(event.getEntity());
    Bukkit.getPluginManager().callEvent(ese);

    EntityUtil.equipEntity(event.getEntity(), itemStack);

    nameMobs(event.getEntity());
  }

  @EventHandler
  public void onEntityDeath(EntityDeathEvent event) {
    if (event.getEntity() instanceof Player || event.getEntity().getLastDamageCause() == null
        || event.getEntity().getLastDamageCause().isCancelled()) {
      return;
    }
{'fraction_non_alphanumeric': 0.11306349975763451, 'fraction_numerical': 0.0001211827435773146, 'mean_word_length': 4.025883069427527, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1698473', 'n_tokens_mistral': 5458, 'n_tokens_neox': 5226, 'n_words': 726}