From 005af2515bd88c890893c89ebf0fa201751fc518 Mon Sep 17 00:00:00 2001 From: Brianna Date: Tue, 9 Jul 2019 16:10:41 -0400 Subject: [PATCH] Added command support for lootables. --- .../ultimatestacker/entity/EntityStack.java | 20 ++++++----- .../listeners/DeathListeners.java | 24 ++++++------- .../ultimatestacker/lootables/Drop.java | 34 +++++++++++++++++++ .../ultimatestacker/lootables/Loot.java | 12 +++++++ .../lootables/LootManager.java | 28 +++++++++------ .../ultimatestacker/utils/Methods.java | 18 +++++++++- 6 files changed, 103 insertions(+), 33 deletions(-) create mode 100644 src/main/java/com/songoda/ultimatestacker/lootables/Drop.java diff --git a/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java b/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java index 9b31ac8..1477162 100644 --- a/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java +++ b/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java @@ -1,6 +1,7 @@ package com.songoda.ultimatestacker.entity; import com.songoda.ultimatestacker.UltimateStacker; +import com.songoda.ultimatestacker.lootables.Drop; import com.songoda.ultimatestacker.utils.Methods; import com.songoda.ultimatestacker.utils.ServerVersion; import com.songoda.ultimatestacker.utils.settings.Setting; @@ -107,19 +108,20 @@ public class EntityStack { return null; } - private void handleWholeStackDeath(LivingEntity killed, List items, boolean custom, int droppedExp) { + private void handleWholeStackDeath(LivingEntity killed, List drops, boolean custom, int droppedExp) { Location killedLocation = killed.getLocation(); for (int i = 1; i < amount; i++) { if (i == 1) { - items.removeIf(it -> it.isSimilar(killed.getEquipment().getItemInHand())); + drops.removeIf(it -> it.getItemStack() != null + && it.getItemStack().isSimilar(killed.getEquipment().getItemInHand())); for (ItemStack item : killed.getEquipment().getArmorContents()) { - items.removeIf(it -> it.isSimilar(item)); + drops.removeIf(it -> it.getItemStack() != null && it.getItemStack().isSimilar(item)); } } if (custom) - items = UltimateStacker.getInstance().getLootManager().getDrops(killed); - for (ItemStack item : items) { - killedLocation.getWorld().dropItemNaturally(killedLocation, item); + drops = UltimateStacker.getInstance().getLootManager().getDrops(killed); + for (Drop drop : drops) { + Methods.processDrop(killed, drop); } } @@ -162,13 +164,13 @@ public class EntityStack { } } - public void onDeath(LivingEntity killed, List items, boolean custom, int droppedExp) { + public void onDeath(LivingEntity killed, List drops, boolean custom, int droppedExp) { killed.setCustomName(null); killed.setCustomNameVisible(true); killed.setCustomName(Methods.formatText("&7")); if (Setting.KILL_WHOLE_STACK_ON_DEATH.getBoolean() && getAmount() != 1) { - handleWholeStackDeath(killed, items, custom, droppedExp); + handleWholeStackDeath(killed, drops, custom, droppedExp); } else if (getAmount() != 1) { List reasons = Setting.INSTANT_KILL.getStringList(); EntityDamageEvent lastDamageCause = killed.getLastDamageCause(); @@ -177,7 +179,7 @@ public class EntityStack { EntityDamageEvent.DamageCause cause = lastDamageCause.getCause(); for (String s : reasons) { if (!cause.name().equalsIgnoreCase(s)) continue; - handleWholeStackDeath(killed, items, custom, Setting.NO_EXP_INSTANT_KILL.getBoolean() ? 0 : droppedExp); + handleWholeStackDeath(killed, drops, custom, Setting.NO_EXP_INSTANT_KILL.getBoolean() ? 0 : droppedExp); return; } } diff --git a/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java b/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java index 6721285..b95c2b1 100644 --- a/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java +++ b/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java @@ -1,11 +1,9 @@ package com.songoda.ultimatestacker.listeners; import com.songoda.ultimatestacker.UltimateStacker; +import com.songoda.ultimatestacker.lootables.Drop; +import com.songoda.ultimatestacker.utils.Methods; import com.songoda.ultimatestacker.utils.settings.Setting; -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.entity.Llama; -import org.bukkit.entity.Pig; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -25,24 +23,26 @@ public class DeathListeners implements Listener { @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onEntityDeath(EntityDeathEvent event) { - List items = Setting.CUSTOM_DROPS.getBoolean() + List drops = Setting.CUSTOM_DROPS.getBoolean() ? instance.getLootManager().getDrops(event.getEntity()) : new ArrayList<>(); boolean custom = false; - if (items.size() != 0) { + if (drops.size() != 0) { event.getDrops().clear(); - for (ItemStack item : items) { - if (item == null) continue; - event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(), item); + for (Drop drop : drops) { + if (drop == null) continue; + Methods.processDrop(event.getEntity(), drop); } custom = true; - } else - items = event.getDrops(); + } else { + for (ItemStack item : event.getDrops()) + drops.add(new Drop(item)); + } if (instance.getEntityStackManager().isStacked(event.getEntity())) instance.getEntityStackManager().getStack(event.getEntity()) - .onDeath(event.getEntity(), items, custom, event.getDroppedExp()); + .onDeath(event.getEntity(), drops, custom, event.getDroppedExp()); } } diff --git a/src/main/java/com/songoda/ultimatestacker/lootables/Drop.java b/src/main/java/com/songoda/ultimatestacker/lootables/Drop.java new file mode 100644 index 0000000..1a00fb9 --- /dev/null +++ b/src/main/java/com/songoda/ultimatestacker/lootables/Drop.java @@ -0,0 +1,34 @@ +package com.songoda.ultimatestacker.lootables; + +import org.bukkit.inventory.ItemStack; + +public class Drop { + + private ItemStack itemStack; + + private String command; + + public Drop(ItemStack itemStack) { + this.itemStack = itemStack; + } + + public Drop(String command) { + this.command = command; + } + + public String getCommand() { + return command; + } + + public void setCommand(String command) { + this.command = command; + } + + public ItemStack getItemStack() { + return itemStack; + } + + public void setItemStack(ItemStack itemStack) { + this.itemStack = itemStack; + } +} diff --git a/src/main/java/com/songoda/ultimatestacker/lootables/Loot.java b/src/main/java/com/songoda/ultimatestacker/lootables/Loot.java index f4c217b..39bfe8e 100644 --- a/src/main/java/com/songoda/ultimatestacker/lootables/Loot.java +++ b/src/main/java/com/songoda/ultimatestacker/lootables/Loot.java @@ -11,6 +11,10 @@ import java.util.Random; public class Loot { + // Material used for this drop. + @SerializedName("Command") + private String command; + // Material used for this drop. @SerializedName("Type") private Material material; @@ -65,6 +69,14 @@ public class Loot { this.material = material; } + public String getCommand() { + return command; + } + + public void setCommand(String command) { + this.command = command; + } + public Short getData() { return data; } diff --git a/src/main/java/com/songoda/ultimatestacker/lootables/LootManager.java b/src/main/java/com/songoda/ultimatestacker/lootables/LootManager.java index 643483c..f8dbb11 100644 --- a/src/main/java/com/songoda/ultimatestacker/lootables/LootManager.java +++ b/src/main/java/com/songoda/ultimatestacker/lootables/LootManager.java @@ -5,6 +5,7 @@ import com.google.gson.GsonBuilder; import com.google.gson.stream.JsonReader; import com.songoda.ultimatestacker.UltimateStacker; import com.songoda.ultimatestacker.utils.ServerVersion; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Ageable; @@ -26,8 +27,8 @@ public class LootManager { return registeredLootables.put(lootable.getType(), lootable); } - public List getDrops(LivingEntity entity) { - List toDrop = new ArrayList<>(); + public List getDrops(LivingEntity entity) { + List toDrop = new ArrayList<>(); if (entity instanceof Ageable && !((Ageable) entity).isAdult() || !registeredLootables.containsKey(entity.getType())) return toDrop; @@ -45,8 +46,8 @@ public class LootManager { return toDrop; } - private List runLoot(LivingEntity entity, Loot loot, int rerollChance, int looting) { - List toDrop = new ArrayList<>(); + private List runLoot(LivingEntity entity, Loot loot, int rerollChance, int looting) { + List toDrop = new ArrayList<>(); if (loot.runChance(looting) || ((Math.random() * 100) - rerollChance < 0 || rerollChance == 100) && loot.runChance(looting)) { @@ -65,10 +66,10 @@ public class LootManager { toDrop.addAll(runLoot(entity, childLoot.get(i), rerollChance, looting)); } } - - if (loot.getMaterial() == null) return toDrop; - Material material = loot.getMaterial(); + String command = loot.getCommand(); + + if (material == null && command == null) return toDrop; short data = loot.getData() != null ? loot.getData() : 0; @@ -87,10 +88,15 @@ public class LootManager { int amount = loot.getAmountToDrop(looting); if (amount == 0) return toDrop; - ItemStack item = new ItemStack(loot.getBurnedMaterial() != null && entity.getFireTicks() != -1 - ? loot.getBurnedMaterial() : material, amount); - item.setDurability(data); - toDrop.add(item); + if (material != null) { + ItemStack item = new ItemStack(loot.getBurnedMaterial() != null && entity.getFireTicks() != -1 + ? loot.getBurnedMaterial() : material, amount); + item.setDurability(data); + toDrop.add(new Drop(item)); + } + if (command != null) { + toDrop.add(new Drop(command)); + } } return toDrop; } diff --git a/src/main/java/com/songoda/ultimatestacker/utils/Methods.java b/src/main/java/com/songoda/ultimatestacker/utils/Methods.java index b3e59d7..3790d09 100644 --- a/src/main/java/com/songoda/ultimatestacker/utils/Methods.java +++ b/src/main/java/com/songoda/ultimatestacker/utils/Methods.java @@ -3,6 +3,7 @@ package com.songoda.ultimatestacker.utils; import com.songoda.ultimatestacker.UltimateStacker; import com.songoda.ultimatestacker.entity.Check; import com.songoda.ultimatestacker.entity.EntityStack; +import com.songoda.ultimatestacker.lootables.Drop; import com.songoda.ultimatestacker.utils.settings.Setting; import org.bukkit.*; import org.bukkit.block.Block; @@ -34,6 +35,21 @@ public class Methods { } } + public static void processDrop(LivingEntity entity, Drop drop) { + if (drop == null) return; + + if (drop.getItemStack() != null) + entity.getWorld().dropItemNaturally(entity.getLocation(), drop.getItemStack()); + if (drop.getCommand() != null) { + String command = drop.getCommand(); + if (entity.getKiller() != null) { + command = command.replace("%player%", entity.getKiller().getName()); + } + if (!command.contains("%player%")) + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command); + } + } + public static LivingEntity newEntity(LivingEntity toClone) { LivingEntity newEntity = (LivingEntity) toClone.getWorld().spawnEntity(toClone.getLocation(), toClone.getType()); newEntity.setVelocity(toClone.getVelocity()); @@ -183,7 +199,7 @@ public class Methods { if (Setting.KEEP_POTION.getBoolean()) newEntity.addPotionEffects(toClone.getActivePotionEffects()); - return newEntity; + return newEntity; } public static List getSimilarEntitiesAroundEntity(LivingEntity initalEntity) {