Add new loot classes to manage loot

This commit is contained in:
libraryaddict 2015-03-17 18:08:47 +13:00
parent 41469b198a
commit ec30269a14
2 changed files with 114 additions and 0 deletions

View File

@ -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<RandomItem> _randomItems = new ArrayList<RandomItem>();
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;
}
}

View File

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