diff --git a/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java b/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java index de8a7a7..e9d2541 100644 --- a/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java +++ b/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java @@ -15,6 +15,7 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.ExperienceOrb; import org.bukkit.entity.LivingEntity; import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.metadata.FixedMetadataValue; @@ -108,7 +109,7 @@ public class EntityStack { return null; } - private void handleWholeStackDeath(LivingEntity killed, List drops, boolean custom, int droppedExp) { + private void handleWholeStackDeath(LivingEntity killed, List drops, boolean custom, int droppedExp, EntityDeathEvent event) { Location killedLocation = killed.getLocation(); List preStackedDrops = new ArrayList<>(); for (int i = 1; i < amount; i++) { @@ -124,7 +125,7 @@ public class EntityStack { preStackedDrops.addAll(drops); } if (!preStackedDrops.isEmpty()) - DropUtils.processStackedDrop(killed, preStackedDrops); + DropUtils.processStackedDrop(killed, preStackedDrops, event); if (droppedExp > 0) killedLocation.getWorld().spawn(killedLocation, ExperienceOrb.class).setExperience(droppedExp * amount); @@ -133,7 +134,7 @@ public class EntityStack { UltimateStacker.getInstance().addExp(killed.getKiller(), this); } - private void handleSingleStackDeath(LivingEntity killed, List drops) { + private void handleSingleStackDeath(LivingEntity killed, List drops, EntityDeathEvent event) { EntityStackManager stackManager = plugin.getEntityStackManager(); LivingEntity newEntity = plugin.getEntityUtils().newEntity(killed); @@ -153,7 +154,7 @@ public class EntityStack { newEntity.setMetadata(entityMetadataKey, new FixedMetadataValue(UltimateStacker.getInstance(), true)); } - DropUtils.processStackedDrop(killed, drops); + DropUtils.processStackedDrop(killed, drops, event); EntityStack entityStack = stackManager.updateStack(killed, newEntity); @@ -166,7 +167,7 @@ public class EntityStack { } } - public void onDeath(LivingEntity killed, List drops, boolean custom, int droppedExp) { + public void onDeath(LivingEntity killed, List drops, boolean custom, int droppedExp, EntityDeathEvent event) { killed.setCustomName(null); killed.setCustomNameVisible(true); killed.setCustomName(Methods.formatText("&7")); @@ -175,7 +176,7 @@ public class EntityStack { || plugin.getMobFile().getBoolean("Mobs." + killed.getType().name() + ".Kill Whole Stack"); if (killWholeStack && getAmount() != 1) { - handleWholeStackDeath(killed, drops, custom, droppedExp); + handleWholeStackDeath(killed, drops, custom, droppedExp, event); } else if (getAmount() != 1) { List reasons = Settings.INSTANT_KILL.getStringList(); EntityDamageEvent lastDamageCause = killed.getLastDamageCause(); @@ -184,11 +185,11 @@ public class EntityStack { EntityDamageEvent.DamageCause cause = lastDamageCause.getCause(); for (String s : reasons) { if (!cause.name().equalsIgnoreCase(s)) continue; - handleWholeStackDeath(killed, drops, custom, Settings.NO_EXP_INSTANT_KILL.getBoolean() ? 0 : droppedExp); + handleWholeStackDeath(killed, drops, custom, Settings.NO_EXP_INSTANT_KILL.getBoolean() ? 0 : droppedExp, event); return; } } - handleSingleStackDeath(killed, drops); + handleSingleStackDeath(killed, drops, event); } } diff --git a/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java b/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java index 624b51e..5eb679b 100644 --- a/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java +++ b/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java @@ -19,13 +19,12 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.Damageable; -import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; +import java.util.stream.Collectors; public class DeathListeners implements Listener { @@ -43,22 +42,21 @@ public class DeathListeners implements Listener { || event.getEntityType() == EntityType.ARMOR_STAND) return; boolean custom = Settings.CUSTOM_DROPS.getBoolean(); - List drops = custom ? instance.getLootablesManager().getDrops(event.getEntity()) : new ArrayList<>(); + List drops = custom ? instance.getLootablesManager().getDrops(event.getEntity()) + : event.getDrops().stream().map(Drop::new).collect(Collectors.toList()); - if (!custom) { - for (ItemStack item : event.getDrops()) - drops.add(new Drop(item)); - } - for (ItemStack item : new ArrayList<>(event.getDrops())) { - if (!shouldDrop(event.getEntity(), item.getType())) - event.getDrops().remove(item); + if (custom) { + for (ItemStack item : new ArrayList<>(event.getDrops())) { + if (shouldDrop(event.getEntity(), item.getType())) + drops.add(new Drop(item)); + } } if (instance.getEntityStackManager().isStacked(event.getEntity())) instance.getEntityStackManager().getStack(event.getEntity()) - .onDeath(event.getEntity(), drops, custom, event.getDroppedExp()); + .onDeath(event.getEntity(), drops, custom, event.getDroppedExp(), event); else - DropUtils.processStackedDrop(event.getEntity(), drops); + DropUtils.processStackedDrop(event.getEntity(), drops, event); } private boolean shouldDrop(LivingEntity entity, Material material) { @@ -128,7 +126,7 @@ public class DeathListeners implements Listener { if (checkUnbreakingChance(unbreakingLevel)) actualDamage++; - tool.setDurability((short)(tool.getDurability() + actualDamage)); + tool.setDurability((short) (tool.getDurability() + actualDamage)); if (!this.hasEnoughDurability(tool, 1)) player.getInventory().setItemInHand(null); diff --git a/src/main/java/com/songoda/ultimatestacker/utils/DropUtils.java b/src/main/java/com/songoda/ultimatestacker/utils/DropUtils.java index 5ccbdb1..7146289 100644 --- a/src/main/java/com/songoda/ultimatestacker/utils/DropUtils.java +++ b/src/main/java/com/songoda/ultimatestacker/utils/DropUtils.java @@ -3,24 +3,15 @@ package com.songoda.ultimatestacker.utils; import com.songoda.lootables.loot.Drop; import org.bukkit.Bukkit; import org.bukkit.entity.LivingEntity; +import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.inventory.ItemStack; import java.util.ArrayList; -import java.util.Collections; import java.util.List; public class DropUtils { - public static void processDrop(LivingEntity entity, Drop drop) { - if (drop == null) return; - - if (drop.getItemStack() != null) - dropItems(entity, Collections.singletonList(drop.getItemStack())); - if (drop.getCommand() != null) - runCommands(entity, Collections.singletonList(drop.getCommand())); - } - - public static void processStackedDrop(LivingEntity entity, List drops) { + public static void processStackedDrop(LivingEntity entity, List drops, EntityDeathEvent event) { List items = new ArrayList<>(); List commands = new ArrayList<>(); for (Drop drop : drops) { @@ -44,14 +35,15 @@ public class DropUtils { commands.add(drop.getCommand()); } if (!items.isEmpty()) - dropItems(entity, items); + dropItems(items, event); else if (!commands.isEmpty()) runCommands(entity, commands); } - private static void dropItems(LivingEntity entity, List items) { + private static void dropItems(List items, EntityDeathEvent event) { + event.getDrops().clear(); for (ItemStack item : items) - entity.getWorld().dropItemNaturally(entity.getLocation(), item); + event.getDrops().add(item); } private static void runCommands(LivingEntity entity, List commands) {