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;
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<ItemStack> items, boolean custom, int droppedExp) {
private void handleWholeStackDeath(LivingEntity killed, List<Drop> 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<ItemStack> items, boolean custom, int droppedExp) {
public void onDeath(LivingEntity killed, List<Drop> 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<String> 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;
}
}

View File

@ -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<ItemStack> items = Setting.CUSTOM_DROPS.getBoolean()
List<Drop> 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());
}
}

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 {
// 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;
}

View File

@ -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<ItemStack> getDrops(LivingEntity entity) {
List<ItemStack> toDrop = new ArrayList<>();
public List<Drop> getDrops(LivingEntity entity) {
List<Drop> 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<ItemStack> runLoot(LivingEntity entity, Loot loot, int rerollChance, int looting) {
List<ItemStack> toDrop = new ArrayList<>();
private List<Drop> runLoot(LivingEntity entity, Loot loot, int rerollChance, int looting) {
List<Drop> 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;
if (material != null) {
ItemStack item = new ItemStack(loot.getBurnedMaterial() != null && entity.getFireTicks() != -1
? loot.getBurnedMaterial() : material, amount);
item.setDurability(data);
toDrop.add(item);
toDrop.add(new Drop(item));
}
if (command != null) {
toDrop.add(new Drop(command));
}
}
return toDrop;
}

View File

@ -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());