diff --git a/pom.xml b/pom.xml index aeb36ec..d9a3a3b 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,7 @@ com.songoda:songodaupdater + com.songoda:Lootables @@ -73,6 +74,11 @@ songodaupdater 1 + + com.songoda + Lootables + 1 + net.milkbowl vault diff --git a/src/main/java/com/songoda/ultimatestacker/UltimateStacker.java b/src/main/java/com/songoda/ultimatestacker/UltimateStacker.java index 3ab3dcb..5a700b5 100644 --- a/src/main/java/com/songoda/ultimatestacker/UltimateStacker.java +++ b/src/main/java/com/songoda/ultimatestacker/UltimateStacker.java @@ -8,7 +8,7 @@ import com.songoda.ultimatestacker.hologram.HologramHolographicDisplays; import com.songoda.ultimatestacker.hook.StackerHook; import com.songoda.ultimatestacker.hook.hooks.JobsHook; import com.songoda.ultimatestacker.listeners.*; -import com.songoda.ultimatestacker.lootables.LootManager; +import com.songoda.ultimatestacker.lootables.LootablesManager; import com.songoda.ultimatestacker.spawner.SpawnerStack; import com.songoda.ultimatestacker.spawner.SpawnerStackManager; import com.songoda.ultimatestacker.storage.Storage; @@ -51,7 +51,7 @@ public class UltimateStacker extends JavaPlugin { private SettingsManager settingsManager; private EntityStackManager entityStackManager; private SpawnerStackManager spawnerStackManager; - private LootManager lootManager; + private LootablesManager lootablesManager; private CommandManager commandManager; private StackingTask stackingTask; private Hologram hologram; @@ -92,10 +92,10 @@ public class UltimateStacker extends JavaPlugin { this.commandManager = new CommandManager(this); - this.lootManager = new LootManager(); + this.lootablesManager = new LootablesManager(); + this.lootablesManager.createDefaultLootables(); + this.getLootablesManager().getLootManager().loadLootables(); - lootManager.createDefaultLootables(); - lootManager.loadLootables(); for (EntityType value : EntityType.values()) { if (value.isSpawnable() && value.isAlive() && !value.toString().contains("ARMOR")) { @@ -224,7 +224,7 @@ public class UltimateStacker extends JavaPlugin { this.itemFile = new ConfigWrapper(this, "", "items.yml"); this.spawnerFile = new ConfigWrapper(this, "", "spawners.yml"); this.settingsManager.reloadConfig(); - this.getLootManager().loadLootables(); + this.getLootablesManager().getLootManager().loadLootables(); } public boolean spawnersEnabled() { @@ -259,8 +259,8 @@ public class UltimateStacker extends JavaPlugin { return commandManager; } - public LootManager getLootManager() { - return lootManager; + public LootablesManager getLootablesManager() { + return lootablesManager; } public EntityStackManager getEntityStackManager() { diff --git a/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java b/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java index 1477162..1d2bdb0 100644 --- a/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java +++ b/src/main/java/com/songoda/ultimatestacker/entity/EntityStack.java @@ -1,7 +1,7 @@ package com.songoda.ultimatestacker.entity; +import com.songoda.lootables.loot.Drop; 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; @@ -119,7 +119,7 @@ public class EntityStack { } } if (custom) - drops = UltimateStacker.getInstance().getLootManager().getDrops(killed); + drops = UltimateStacker.getInstance().getLootablesManager().getDrops(killed); for (Drop drop : drops) { Methods.processDrop(killed, drop); } diff --git a/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java b/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java index b95c2b1..60e7394 100644 --- a/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java +++ b/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java @@ -1,7 +1,7 @@ package com.songoda.ultimatestacker.listeners; +import com.songoda.lootables.loot.Drop; 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.event.EventHandler; @@ -23,11 +23,10 @@ public class DeathListeners implements Listener { @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onEntityDeath(EntityDeathEvent event) { - List drops = Setting.CUSTOM_DROPS.getBoolean() - ? instance.getLootManager().getDrops(event.getEntity()) : new ArrayList<>(); + List drops = instance.getLootablesManager().getDrops(event.getEntity()); boolean custom = false; - if (drops.size() != 0) { + if (Setting.CUSTOM_DROPS.getBoolean()) { event.getDrops().clear(); for (Drop drop : drops) { diff --git a/src/main/java/com/songoda/ultimatestacker/lootables/Drop.java b/src/main/java/com/songoda/ultimatestacker/lootables/Drop.java deleted file mode 100644 index 1a00fb9..0000000 --- a/src/main/java/com/songoda/ultimatestacker/lootables/Drop.java +++ /dev/null @@ -1,34 +0,0 @@ -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 deleted file mode 100644 index 9684445..0000000 --- a/src/main/java/com/songoda/ultimatestacker/lootables/Loot.java +++ /dev/null @@ -1,225 +0,0 @@ -package com.songoda.ultimatestacker.lootables; - -import com.google.gson.annotations.SerializedName; -import com.songoda.ultimatestacker.utils.Methods; -import org.bukkit.Material; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.EntityType; - -import java.util.*; - -public class Loot { - - // Material used for this drop. - @SerializedName("Command") - private String command; - - // Material used for this drop. - @SerializedName("Type") - private Material material; - - // Data value for old crappy versions of Minecraft. - @SerializedName("Data") - private Short data; - - // The override for the item name. - @SerializedName("Name") - private String name = null; - - // The override for the item lore. - @SerializedName("Lore") - private List lore = null; - - // The override for the item enchantments. - @SerializedName("Enchantments") - private Map enchants = null; - - // Material used if entity died on fire. - @SerializedName("Burned Type") - private Material burnedMaterial = null; - - // Chance that this drop will take place. - @SerializedName("Chance") - private double chance = 100; - - // Minimum amount of this item. - @SerializedName("Min") - private int min = 1; - - // Maximum amount of this item. - @SerializedName("Max") - private int max = 1; - - // Will the looting enchantment be usable for this loot? - @SerializedName("Looting") - private boolean allowLootingEnchant = true; - - // The looting chance increase. - @SerializedName("Looting Chance Increase") - private Double lootingIncrease; - - // Should this drop only be applicable for specific entities? - @SerializedName("Only Drop For") - private List onlyDropFor; - - // How many child loots should drop? - @SerializedName("Child Loot Drop Count Min") - private Integer childDropCountMin; - @SerializedName("Child Loot Drop Count Max") - private Integer childDropCountMax; - - // Should this drop house child drops? - @SerializedName("Child Loot") - private List childLoot; - - public Material getMaterial() { - return material; - } - - public void setMaterial(Material material) { - this.material = material; - } - - public String getCommand() { - return command; - } - - public void setCommand(String command) { - this.command = command; - } - - public Short getData() { - return data; - } - - public void setData(Short data) { - this.data = data; - } - - public String getName() { - return Methods.formatText(name); - } - - public void setName(String name) { - this.name = name; - } - - public List getLore() { - if (lore == null) return null; - List lore = new ArrayList<>(); - for (String line : this.lore) - lore.add(Methods.formatText(line)); - - return lore; - } - - public void setLore(List lore) { - this.lore = new ArrayList<>(lore); - } - - public Map getEnchants() { - if (enchants == null) return null; - Map enchants = new HashMap<>(); - for (Map.Entry entry : this.enchants.entrySet()) - enchants.put(Enchantment.getByName(entry.getKey()), entry.getValue()); - return enchants; - } - - public void setEnchants(Map enchants) { - this.enchants = enchants; - } - - public Material getBurnedMaterial() { - return burnedMaterial; - } - - public void setBurnedMaterial(Material burnedMaterial) { - this.burnedMaterial = burnedMaterial; - } - - public double getChance() { - return chance; - } - - public void setChance(double chance) { - this.chance = chance; - } - - public boolean runChance(int looting) { - return (Math.random() * 100) - (chance + (lootingIncrease == null ? 1 - : lootingIncrease * looting)) < 0 || chance == 100; - } - - public int getMin() { - return min; - } - - public void setMin(int min) { - this.min = min; - } - - public int getMax() { - return max; - } - - public void setMax(int max) { - this.max = max; - } - - public int getAmountToDrop(int looting) { - return min == max ? (max + getLooting(looting)) : new Random().nextInt((max + getLooting(looting)) - min + 1) + min; - } - - public int getLooting(int looting) { - return allowLootingEnchant ? looting : 0; - } - - public boolean isAllowLootingEnchant() { - return allowLootingEnchant; - } - - public void setAllowLootingEnchant(boolean allowLootingEnchant) { - this.allowLootingEnchant = allowLootingEnchant; - } - - public void setLootingIncrease(double increase) { - this.lootingIncrease = increase; - } - - public List getChild() { - return childLoot == null ? new ArrayList<>() : new ArrayList<>(childLoot); - } - - public void addChildLoots(Loot... loots) { - this.childDropCountMin = 1; - this.childDropCountMax = 1; - this.childLoot = new ArrayList<>(); - this.childLoot.addAll(Arrays.asList(loots)); - } - - public List getChildLoot() { - return childLoot == null ? new ArrayList<>() : new ArrayList<>(childLoot); - } - - public List getOnlyDropFor() { - return onlyDropFor == null ? new ArrayList<>() : new ArrayList<>(onlyDropFor); - } - - public void addOnlyDropFors(EntityType... types) { - this.onlyDropFor = new ArrayList<>(); - this.onlyDropFor.addAll(Arrays.asList(types)); - } - - public void setChildDropCountMin(int childDropCountMin) { - this.childDropCountMin = childDropCountMin; - } - - public void setChildDropCountMax(int childDropCountMax) { - this.childDropCountMax = childDropCountMax; - } - - public int getChildDropCount() { - if (childDropCountMin == null || childDropCountMax == null) return 0; - return new Random().nextInt(childDropCountMax - min + 1) + childDropCountMin; - } -} diff --git a/src/main/java/com/songoda/ultimatestacker/lootables/LootBuilder.java b/src/main/java/com/songoda/ultimatestacker/lootables/LootBuilder.java deleted file mode 100644 index 45c9bac..0000000 --- a/src/main/java/com/songoda/ultimatestacker/lootables/LootBuilder.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.songoda.ultimatestacker.lootables; - - -import com.songoda.ultimatestacker.utils.Methods; -import org.bukkit.Material; -import org.bukkit.entity.EntityType; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -public final class LootBuilder { - - private final Loot loot; - - public LootBuilder() { - this.loot = new Loot(); - } - - public LootBuilder setMaterial(Material material) { - this.loot.setMaterial(material); - return this; - } - - public LootBuilder setData(int data) { - return setData((short)data); - } - - public LootBuilder setData(short data) { - this.loot.setData(data); - return this; - } - - public LootBuilder setName(String name) { - this.loot.setName(name); - return this; - } - - public LootBuilder addLore(String... lore) { - this.loot.setLore(Arrays.asList(lore)); - return this; - } - - public LootBuilder addEnchants(Methods.Tuple... tuples) { - Map enchants = new HashMap<>(); - for (Methods.Tuple tuple : tuples) - enchants.put((String)tuple.getKey(), (int)tuple.getValue()); - this.loot.setEnchants(enchants); - return this; - } - - public LootBuilder setBurnedMaterial(Material material) { - this.loot.setBurnedMaterial(material); - return this; - } - - public LootBuilder setChance(double chance) { - this.loot.setChance(chance); - return this; - } - - public LootBuilder setMin(int min) { - this.loot.setMin(min); - return this; - } - - public LootBuilder setMax(int max) { - this.loot.setMax(max); - return this; - } - - public LootBuilder setAllowLootingEnchant(boolean allow) { - this.loot.setAllowLootingEnchant(allow); - return this; - } - - public LootBuilder setLootingIncrease(double increase) { - this.loot.setLootingIncrease(increase); - return this; - } - - public LootBuilder addOnlyDropFors(EntityType... types) { - this.loot.addOnlyDropFors(types); - return this; - } - - public LootBuilder addChildLoot(Loot... loots) { - this.loot.addChildLoots(loots); - return this; - } - - public LootBuilder setChildDropCount(int count) { - this.loot.setChildDropCountMin(count); - this.loot.setChildDropCountMax(count); - return this; - } - - public LootBuilder setChildDropCounMin(int count) { - this.loot.setChildDropCountMin(count); - return this; - } - - public LootBuilder setChildDropCountMax(int count) { - this.loot.setChildDropCountMax(count); - return this; - } - - public Loot build() { - return this.loot; - } -} \ No newline at end of file diff --git a/src/main/java/com/songoda/ultimatestacker/lootables/Lootable.java b/src/main/java/com/songoda/ultimatestacker/lootables/Lootable.java deleted file mode 100644 index 5fbcb3e..0000000 --- a/src/main/java/com/songoda/ultimatestacker/lootables/Lootable.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.songoda.ultimatestacker.lootables; - -import com.google.gson.annotations.SerializedName; -import org.bukkit.entity.EntityType; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class Lootable { - - // The entity applicable to this lootable. - @SerializedName("Type") - private final EntityType type; - - // Registered loot. - @SerializedName("Loot") - private final List registeredLoot = new ArrayList<>(); - - public Lootable(EntityType type, Loot... loots) { - this.type = type; - registeredLoot.addAll(Arrays.asList(loots)); - } - - public List getRegisteredLoot() { - return new ArrayList<>(registeredLoot); - } - - public EntityType getType() { - return type; - } -} diff --git a/src/main/java/com/songoda/ultimatestacker/lootables/LootManager.java b/src/main/java/com/songoda/ultimatestacker/lootables/LootablesManager.java similarity index 87% rename from src/main/java/com/songoda/ultimatestacker/lootables/LootManager.java rename to src/main/java/com/songoda/ultimatestacker/lootables/LootablesManager.java index c13dc34..bd81d68 100644 --- a/src/main/java/com/songoda/ultimatestacker/lootables/LootManager.java +++ b/src/main/java/com/songoda/ultimatestacker/lootables/LootablesManager.java @@ -2,143 +2,28 @@ package com.songoda.ultimatestacker.lootables; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.stream.JsonReader; +import com.songoda.lootables.Lootables; import com.songoda.ultimatestacker.UltimateStacker; -import com.songoda.ultimatestacker.utils.Methods; import com.songoda.ultimatestacker.utils.ServerVersion; -import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.Ageable; import org.bukkit.entity.EntityType; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Sheep; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.loot.Lootable; -import java.io.*; -import java.util.*; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; public class LootManager { - private final Map registeredLootables = new HashMap<>(); + private final Lootables instance; + + public LootManager() { + this.instance = new Lootables(); + } private final String lootablesDir = UltimateStacker.getInstance().getDataFolder() + File.separator + "lootables"; - public Lootable addLootable(Lootable lootable) { - return registeredLootables.put(lootable.getType(), lootable); - } - - public List getDrops(LivingEntity entity) { - List toDrop = new ArrayList<>(); - if (entity instanceof Ageable && !((Ageable) entity).isAdult() - || !registeredLootables.containsKey(entity.getType())) return toDrop; - - Lootable lootable = registeredLootables.get(entity.getType()); - int looting = entity.getKiller() != null - && entity.getKiller().getItemInHand().containsEnchantment(Enchantment.LOOT_BONUS_MOBS) - ? entity.getKiller().getItemInHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_MOBS) - : 0; - - int rerollChance = looting / (looting + 1); - - for (Loot loot : lootable.getRegisteredLoot()) - toDrop.addAll(runLoot(entity, loot, rerollChance, looting)); - - return toDrop; - } - - 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)) { - - if (loot.getOnlyDropFor().size() != 0 - && loot.getOnlyDropFor().stream().noneMatch(type -> entity.getKiller() != null && type == entity.getKiller().getType())) - return toDrop; - - if (loot.getChildLoot().size() > 0) { - List childLoot = loot.getChildLoot(); - Collections.shuffle(childLoot); - - int amt = loot.getChildDropCount(); - - for (int i = 0; i < amt; i++) { - toDrop.addAll(runLoot(entity, childLoot.get(i), rerollChance, looting)); - } - } - Material material = loot.getMaterial(); - String command = loot.getCommand(); - - if (material == null && command == null) return toDrop; - - short data = loot.getData() != null ? loot.getData() : 0; - - if (entity.getType() == EntityType.SHEEP - && material == (UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) - ? Material.WHITE_WOOL : Material.valueOf("WOOL")) - && ((Sheep) entity).getColor() != null) { - if (((Sheep) entity).isSheared()) return null; - if (UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13)) - material = Material.valueOf(((Sheep) entity).getColor() + "_WOOL"); - else - data = ((Sheep) entity).getColor().getWoolData(); - - } - - 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); - ItemMeta meta = item.getItemMeta() == null ? Bukkit.getItemFactory().getItemMeta(loot.getMaterial()) - : item.getItemMeta(); - - if (loot.getName() != null) - meta.setDisplayName(loot.getName()); - - if (loot.getLore() != null) - meta.setLore(loot.getLore()); - item.setItemMeta(meta); - - if (loot.getEnchants() != null) - item.addEnchantments(loot.getEnchants()); - - toDrop.add(new Drop(item)); - } - if (command != null) { - for (int i = 0; i < amount; i++) - toDrop.add(new Drop(command)); - } - } - return toDrop; - } - - public void loadLootables() { - registeredLootables.clear(); - File dir = new File(lootablesDir); - File[] directoryListing = dir.listFiles(); - if (directoryListing != null) { - for (File file : directoryListing) { - if (!file.getName().endsWith(".json")) continue; - try { - Gson gson = new Gson(); - JsonReader reader = new JsonReader(new FileReader(file.getPath())); - - Lootable lootable = gson.fromJson(reader, Lootable.class); - - if (lootable.getRegisteredLoot().size() != 0) - addLootable(lootable); - - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - } - public void createDefaultLootables() { UltimateStacker plugin = UltimateStacker.getInstance(); diff --git a/src/main/java/com/songoda/ultimatestacker/utils/Methods.java b/src/main/java/com/songoda/ultimatestacker/utils/Methods.java index ae29572..dc4c5bb 100644 --- a/src/main/java/com/songoda/ultimatestacker/utils/Methods.java +++ b/src/main/java/com/songoda/ultimatestacker/utils/Methods.java @@ -1,9 +1,9 @@ package com.songoda.ultimatestacker.utils; +import com.songoda.lootables.loot.Drop; 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;