Added command support for lootables.

This commit is contained in:
Brianna 2019-07-09 16:10:41 -04:00
parent 1b2731fd2d
commit 005af2515b
6 changed files with 103 additions and 33 deletions

View File

@ -1,6 +1,7 @@
package com.songoda.ultimatestacker.entity; package com.songoda.ultimatestacker.entity;
import com.songoda.ultimatestacker.UltimateStacker; import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.lootables.Drop;
import com.songoda.ultimatestacker.utils.Methods; import com.songoda.ultimatestacker.utils.Methods;
import com.songoda.ultimatestacker.utils.ServerVersion; import com.songoda.ultimatestacker.utils.ServerVersion;
import com.songoda.ultimatestacker.utils.settings.Setting; import com.songoda.ultimatestacker.utils.settings.Setting;
@ -107,19 +108,20 @@ public class EntityStack {
return null; return null;
} }
private void handleWholeStackDeath(LivingEntity killed, List<ItemStack> items, boolean custom, int droppedExp) { private void handleWholeStackDeath(LivingEntity killed, List<Drop> drops, boolean custom, int droppedExp) {
Location killedLocation = killed.getLocation(); Location killedLocation = killed.getLocation();
for (int i = 1; i < amount; i++) { for (int i = 1; i < amount; i++) {
if (i == 1) { 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()) { for (ItemStack item : killed.getEquipment().getArmorContents()) {
items.removeIf(it -> it.isSimilar(item)); drops.removeIf(it -> it.getItemStack() != null && it.getItemStack().isSimilar(item));
} }
} }
if (custom) if (custom)
items = UltimateStacker.getInstance().getLootManager().getDrops(killed); drops = UltimateStacker.getInstance().getLootManager().getDrops(killed);
for (ItemStack item : items) { for (Drop drop : drops) {
killedLocation.getWorld().dropItemNaturally(killedLocation, item); Methods.processDrop(killed, drop);
} }
} }
@ -162,13 +164,13 @@ public class EntityStack {
} }
} }
public void onDeath(LivingEntity killed, List<ItemStack> items, boolean custom, int droppedExp) { public void onDeath(LivingEntity killed, List<Drop> drops, boolean custom, int droppedExp) {
killed.setCustomName(null); killed.setCustomName(null);
killed.setCustomNameVisible(true); killed.setCustomNameVisible(true);
killed.setCustomName(Methods.formatText("&7")); killed.setCustomName(Methods.formatText("&7"));
if (Setting.KILL_WHOLE_STACK_ON_DEATH.getBoolean() && getAmount() != 1) { if (Setting.KILL_WHOLE_STACK_ON_DEATH.getBoolean() && getAmount() != 1) {
handleWholeStackDeath(killed, items, custom, droppedExp); handleWholeStackDeath(killed, drops, custom, droppedExp);
} else if (getAmount() != 1) { } else if (getAmount() != 1) {
List<String> reasons = Setting.INSTANT_KILL.getStringList(); List<String> reasons = Setting.INSTANT_KILL.getStringList();
EntityDamageEvent lastDamageCause = killed.getLastDamageCause(); EntityDamageEvent lastDamageCause = killed.getLastDamageCause();
@ -177,7 +179,7 @@ public class EntityStack {
EntityDamageEvent.DamageCause cause = lastDamageCause.getCause(); EntityDamageEvent.DamageCause cause = lastDamageCause.getCause();
for (String s : reasons) { for (String s : reasons) {
if (!cause.name().equalsIgnoreCase(s)) continue; 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; return;
} }
} }

View File

@ -1,11 +1,9 @@
package com.songoda.ultimatestacker.listeners; package com.songoda.ultimatestacker.listeners;
import com.songoda.ultimatestacker.UltimateStacker; 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 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.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -25,24 +23,26 @@ public class DeathListeners implements Listener {
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityDeath(EntityDeathEvent event) { public void onEntityDeath(EntityDeathEvent event) {
List<ItemStack> items = Setting.CUSTOM_DROPS.getBoolean() List<Drop> drops = Setting.CUSTOM_DROPS.getBoolean()
? instance.getLootManager().getDrops(event.getEntity()) : new ArrayList<>(); ? instance.getLootManager().getDrops(event.getEntity()) : new ArrayList<>();
boolean custom = false; boolean custom = false;
if (items.size() != 0) { if (drops.size() != 0) {
event.getDrops().clear(); event.getDrops().clear();
for (ItemStack item : items) { for (Drop drop : drops) {
if (item == null) continue; if (drop == null) continue;
event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(), item); Methods.processDrop(event.getEntity(), drop);
} }
custom = true; custom = true;
} else } else {
items = event.getDrops(); for (ItemStack item : event.getDrops())
drops.add(new Drop(item));
}
if (instance.getEntityStackManager().isStacked(event.getEntity())) if (instance.getEntityStackManager().isStacked(event.getEntity()))
instance.getEntityStackManager().getStack(event.getEntity()) instance.getEntityStackManager().getStack(event.getEntity())
.onDeath(event.getEntity(), items, custom, event.getDroppedExp()); .onDeath(event.getEntity(), drops, custom, event.getDroppedExp());
} }
} }

View File

@ -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;
}
}

View File

@ -11,6 +11,10 @@ import java.util.Random;
public class Loot { public class Loot {
// Material used for this drop.
@SerializedName("Command")
private String command;
// Material used for this drop. // Material used for this drop.
@SerializedName("Type") @SerializedName("Type")
private Material material; private Material material;
@ -65,6 +69,14 @@ public class Loot {
this.material = material; this.material = material;
} }
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public Short getData() { public Short getData() {
return data; return data;
} }

View File

@ -5,6 +5,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.songoda.ultimatestacker.UltimateStacker; import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.utils.ServerVersion; import com.songoda.ultimatestacker.utils.ServerVersion;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Ageable; import org.bukkit.entity.Ageable;
@ -26,8 +27,8 @@ public class LootManager {
return registeredLootables.put(lootable.getType(), lootable); return registeredLootables.put(lootable.getType(), lootable);
} }
public List<ItemStack> getDrops(LivingEntity entity) { public List<Drop> getDrops(LivingEntity entity) {
List<ItemStack> toDrop = new ArrayList<>(); List<Drop> toDrop = new ArrayList<>();
if (entity instanceof Ageable && !((Ageable) entity).isAdult() if (entity instanceof Ageable && !((Ageable) entity).isAdult()
|| !registeredLootables.containsKey(entity.getType())) return toDrop; || !registeredLootables.containsKey(entity.getType())) return toDrop;
@ -45,8 +46,8 @@ public class LootManager {
return toDrop; return toDrop;
} }
private List<ItemStack> runLoot(LivingEntity entity, Loot loot, int rerollChance, int looting) { private List<Drop> runLoot(LivingEntity entity, Loot loot, int rerollChance, int looting) {
List<ItemStack> toDrop = new ArrayList<>(); List<Drop> toDrop = new ArrayList<>();
if (loot.runChance(looting) || ((Math.random() * 100) - rerollChance < 0 || rerollChance == 100) if (loot.runChance(looting) || ((Math.random() * 100) - rerollChance < 0 || rerollChance == 100)
&& loot.runChance(looting)) { && loot.runChance(looting)) {
@ -65,10 +66,10 @@ public class LootManager {
toDrop.addAll(runLoot(entity, childLoot.get(i), rerollChance, looting)); toDrop.addAll(runLoot(entity, childLoot.get(i), rerollChance, looting));
} }
} }
if (loot.getMaterial() == null) return toDrop;
Material material = loot.getMaterial(); Material material = loot.getMaterial();
String command = loot.getCommand();
if (material == null && command == null) return toDrop;
short data = loot.getData() != null ? loot.getData() : 0; short data = loot.getData() != null ? loot.getData() : 0;
@ -87,10 +88,15 @@ public class LootManager {
int amount = loot.getAmountToDrop(looting); int amount = loot.getAmountToDrop(looting);
if (amount == 0) return toDrop; if (amount == 0) return toDrop;
ItemStack item = new ItemStack(loot.getBurnedMaterial() != null && entity.getFireTicks() != -1 if (material != null) {
? loot.getBurnedMaterial() : material, amount); ItemStack item = new ItemStack(loot.getBurnedMaterial() != null && entity.getFireTicks() != -1
item.setDurability(data); ? loot.getBurnedMaterial() : material, amount);
toDrop.add(item); item.setDurability(data);
toDrop.add(new Drop(item));
}
if (command != null) {
toDrop.add(new Drop(command));
}
} }
return toDrop; return toDrop;
} }

View File

@ -3,6 +3,7 @@ package com.songoda.ultimatestacker.utils;
import com.songoda.ultimatestacker.UltimateStacker; import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.entity.Check; import com.songoda.ultimatestacker.entity.Check;
import com.songoda.ultimatestacker.entity.EntityStack; import com.songoda.ultimatestacker.entity.EntityStack;
import com.songoda.ultimatestacker.lootables.Drop;
import com.songoda.ultimatestacker.utils.settings.Setting; import com.songoda.ultimatestacker.utils.settings.Setting;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.Block; 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) { public static LivingEntity newEntity(LivingEntity toClone) {
LivingEntity newEntity = (LivingEntity) toClone.getWorld().spawnEntity(toClone.getLocation(), toClone.getType()); LivingEntity newEntity = (LivingEntity) toClone.getWorld().spawnEntity(toClone.getLocation(), toClone.getType());
newEntity.setVelocity(toClone.getVelocity()); newEntity.setVelocity(toClone.getVelocity());
@ -183,7 +199,7 @@ public class Methods {
if (Setting.KEEP_POTION.getBoolean()) if (Setting.KEEP_POTION.getBoolean())
newEntity.addPotionEffects(toClone.getActivePotionEffects()); newEntity.addPotionEffects(toClone.getActivePotionEffects());
return newEntity; return newEntity;
} }
public static List<LivingEntity> getSimilarEntitiesAroundEntity(LivingEntity initalEntity) { public static List<LivingEntity> getSimilarEntitiesAroundEntity(LivingEntity initalEntity) {