2011-05-21 19:55:55 +02:00
|
|
|
package com.Acrobot.ChestShop.Utils;
|
|
|
|
|
2011-09-06 19:01:57 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Config;
|
|
|
|
import com.Acrobot.ChestShop.Config.Property;
|
2011-05-21 19:55:55 +02:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.inventory.Inventory;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
2012-02-16 19:09:37 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2011-05-21 19:55:55 +02:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
2011-07-05 19:08:55 +02:00
|
|
|
public class uInventory {
|
2011-09-06 19:01:57 +02:00
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static int remove(Inventory inv, ItemStack item, int amount, short durability) {
|
2011-06-23 23:25:34 +02:00
|
|
|
amount = (amount > 0 ? amount : 1);
|
2011-05-21 19:55:55 +02:00
|
|
|
Material itemMaterial = item.getType();
|
|
|
|
|
2011-06-23 23:25:34 +02:00
|
|
|
int first = inv.first(itemMaterial);
|
2011-07-23 21:00:47 +02:00
|
|
|
if (first == -1) return amount;
|
2011-06-23 23:25:34 +02:00
|
|
|
|
|
|
|
for (int slot = first; slot < inv.getSize(); slot++) {
|
2011-07-23 21:00:47 +02:00
|
|
|
if (amount <= 0) return 0;
|
2011-05-21 19:55:55 +02:00
|
|
|
|
|
|
|
ItemStack currentItem = inv.getItem(slot);
|
2011-07-23 21:00:47 +02:00
|
|
|
if (currentItem == null || currentItem.getType() == Material.AIR) continue;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
if (equals(currentItem, item, durability)) {
|
2011-05-21 19:55:55 +02:00
|
|
|
int currentAmount = currentItem.getAmount();
|
2011-06-23 23:25:34 +02:00
|
|
|
if (amount == currentAmount) {
|
2011-05-21 19:55:55 +02:00
|
|
|
currentItem = null;
|
2011-06-23 23:25:34 +02:00
|
|
|
amount = 0;
|
|
|
|
} else if (amount < currentAmount) {
|
|
|
|
currentItem.setAmount(currentAmount - amount);
|
|
|
|
amount = 0;
|
2011-05-29 13:25:25 +02:00
|
|
|
} else {
|
2011-05-21 19:55:55 +02:00
|
|
|
currentItem = null;
|
2011-06-23 23:25:34 +02:00
|
|
|
amount -= currentAmount;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
inv.setItem(slot, currentItem);
|
|
|
|
}
|
|
|
|
}
|
2011-06-23 23:25:34 +02:00
|
|
|
return amount;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static int add(Inventory inv, ItemStack item, int amount) {
|
2011-06-23 23:25:34 +02:00
|
|
|
amount = (amount > 0 ? amount : 1);
|
2011-09-06 19:01:57 +02:00
|
|
|
if (Config.getBoolean(Property.STACK_UNSTACKABLES)) return addAndStackTo64(inv, item, amount);
|
2012-02-16 19:09:37 +01:00
|
|
|
ItemStack itemstack = new ItemStack(item.getType(), amount, item.getDurability());
|
2011-12-16 17:20:09 +01:00
|
|
|
itemstack.addEnchantments(item.getEnchantments());
|
2012-02-16 19:09:37 +01:00
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
HashMap<Integer, ItemStack> items = inv.addItem(itemstack);
|
2011-06-09 22:54:01 +02:00
|
|
|
amount = 0;
|
2011-09-06 19:01:57 +02:00
|
|
|
for (ItemStack toAdd : items.values()) amount += toAdd.getAmount();
|
|
|
|
|
2012-02-16 19:09:37 +01:00
|
|
|
return amount;
|
2011-09-06 19:01:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static int addAndStackTo64(Inventory inv, ItemStack item, int amount) {
|
2011-12-16 17:20:09 +01:00
|
|
|
return addManually(inv, item, amount, 64);
|
|
|
|
}
|
2012-02-16 19:09:37 +01:00
|
|
|
|
|
|
|
public static int addManually(Inventory inv, ItemStack item, int amount, int max) {
|
2011-12-16 17:20:09 +01:00
|
|
|
if (amount <= 0) return 0;
|
|
|
|
|
2012-02-16 19:09:37 +01:00
|
|
|
for (int slot = 0; slot < inv.getSize() && amount > 0; slot++) {
|
2011-09-06 19:01:57 +02:00
|
|
|
ItemStack curItem = inv.getItem(slot);
|
2011-12-16 17:20:09 +01:00
|
|
|
ItemStack dupe = item.clone();
|
|
|
|
|
2011-09-06 19:01:57 +02:00
|
|
|
if (curItem == null || curItem.getType() == Material.AIR) {
|
2011-12-16 17:20:09 +01:00
|
|
|
dupe.setAmount((amount > max ? max : amount));
|
|
|
|
dupe.addEnchantments(item.getEnchantments());
|
|
|
|
amount -= dupe.getAmount();
|
|
|
|
inv.setItem(slot, dupe);
|
2011-12-20 21:39:45 +01:00
|
|
|
} else if (equals(item, curItem, curItem.getDurability()) && curItem.getAmount() != max) {
|
2011-12-16 17:20:09 +01:00
|
|
|
int cA = curItem.getAmount();
|
|
|
|
int amountAdded = amount > max - cA ? max - cA : amount;
|
|
|
|
dupe.setAmount(cA + amountAdded);
|
|
|
|
amount -= amountAdded;
|
|
|
|
dupe.addEnchantments(item.getEnchantments());
|
|
|
|
inv.setItem(slot, dupe);
|
2011-09-06 19:01:57 +02:00
|
|
|
}
|
2011-08-26 23:12:32 +02:00
|
|
|
}
|
2011-12-16 17:20:09 +01:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
return amount;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static int amount(Inventory inv, ItemStack item, short durability) {
|
2011-07-23 21:00:47 +02:00
|
|
|
if (!inv.contains(item.getType())) return 0;
|
|
|
|
|
2011-05-21 19:55:55 +02:00
|
|
|
int amount = 0;
|
2011-07-23 21:00:47 +02:00
|
|
|
for (ItemStack i : inv.getContents()) {
|
2011-12-16 17:20:09 +01:00
|
|
|
if (equals(i, item, durability)) amount += i.getAmount();
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static int fits(Inventory inv, ItemStack item, int amount, short durability) {
|
2011-12-16 17:20:09 +01:00
|
|
|
int maxStackSize = (Config.getBoolean(Property.STACK_UNSTACKABLES) ? 64 : item.getType().getMaxStackSize());
|
2012-01-09 22:39:38 +01:00
|
|
|
|
2011-05-21 19:55:55 +02:00
|
|
|
int amountLeft = amount;
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
for (ItemStack currentItem : inv.getContents()) {
|
2011-07-23 21:00:47 +02:00
|
|
|
if (amountLeft <= 0) return 0;
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (currentItem == null || currentItem.getType() == Material.AIR) {
|
2011-05-21 19:55:55 +02:00
|
|
|
amountLeft -= maxStackSize;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int currentAmount = currentItem.getAmount();
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-12-16 17:20:09 +01:00
|
|
|
if (currentAmount != maxStackSize && equals(currentItem, item, durability)) {
|
2011-08-13 12:08:34 +02:00
|
|
|
amountLeft = currentAmount + amountLeft <= maxStackSize ? 0 : amountLeft - (maxStackSize - currentAmount);
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return amountLeft;
|
|
|
|
}
|
2012-02-16 19:09:37 +01:00
|
|
|
|
|
|
|
private static boolean equals(ItemStack i, ItemStack item, short durability) {
|
2011-12-16 17:20:09 +01:00
|
|
|
return i != null
|
|
|
|
&& i.getType() == item.getType()
|
|
|
|
&& i.getEnchantments().equals(item.getEnchantments())
|
|
|
|
&& (durability == -1 || i.getDurability() == durability);
|
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|