From ec30269a14f67d5116ab8cbaf0e5531e08aa3997 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Tue, 17 Mar 2015 18:08:47 +1300 Subject: [PATCH] Add new loot classes to manage loot --- .../src/mineplex/core/loot/ChestLoot.java | 63 +++++++++++++++++++ .../src/mineplex/core/loot/RandomItem.java | 51 +++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/loot/ChestLoot.java create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/loot/RandomItem.java diff --git a/Plugins/Mineplex.Core/src/mineplex/core/loot/ChestLoot.java b/Plugins/Mineplex.Core/src/mineplex/core/loot/ChestLoot.java new file mode 100644 index 000000000..069c67cb1 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/loot/ChestLoot.java @@ -0,0 +1,63 @@ +package mineplex.core.loot; + +import java.util.ArrayList; + +import mineplex.core.common.util.UtilMath; + +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +public class ChestLoot +{ + private int _totalLoot; + private ArrayList _randomItems = new ArrayList(); + private boolean _unbreakableLoot; + + public ChestLoot(boolean unbreakableLoot) + { + _unbreakableLoot = unbreakableLoot; + } + + public ChestLoot() + { + this(false); + } + + public void cloneLoot(ChestLoot loot) + { + _totalLoot += loot._totalLoot; + _randomItems.addAll(loot._randomItems); + } + + public void registerLoot(RandomItem item) + { + _totalLoot += item.getAmount(); + _randomItems.add(item); + } + + public ItemStack getLoot() + { + int no = UtilMath.r(_totalLoot); + + for (RandomItem item : _randomItems) + { + no -= item.getAmount(); + + if (no < 0) + { + ItemStack itemstack = item.getItemStack(); + + if (_unbreakableLoot && itemstack.getType().getMaxDurability() > 16) + { + ItemMeta meta = itemstack.getItemMeta(); + meta.spigot().setUnbreakable(true); + itemstack.setItemMeta(meta); + } + + return itemstack; + } + } + + return null; + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/loot/RandomItem.java b/Plugins/Mineplex.Core/src/mineplex/core/loot/RandomItem.java new file mode 100644 index 000000000..ebcce4fc4 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/loot/RandomItem.java @@ -0,0 +1,51 @@ +package mineplex.core.loot; + +import java.util.Random; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +public class RandomItem +{ + private int _amount; + private ItemStack _item; + private int _min, _max; + + public RandomItem(ItemStack item, int amount, int minStackSize, int maxStackSize) + { + _amount = amount; + _item = item; + _min = minStackSize; + _max = maxStackSize; + } + + public RandomItem(ItemStack item, int amount) + { + this(item, amount, item.getAmount(), item.getAmount()); + } + + public RandomItem(Material material, int amount, int minStackSize, int maxStackSize) + { + _amount = amount; + _item = new ItemStack(material); + _min = minStackSize; + _max = maxStackSize; + } + + public RandomItem(Material material, int amount) + { + this(material, amount, 1, 1); + } + + public ItemStack getItemStack() + { + _item.setAmount((new Random().nextInt(Math.max(1, (_max - _min) + 1)) + _min)); + + return _item; + } + + public int getAmount() + { + return _amount; + } +} \ No newline at end of file