diff options
Diffstat (limited to 'Essentials/src')
9 files changed, 36 insertions, 452 deletions
diff --git a/Essentials/src/com/earth2me/essentials/Kit.java b/Essentials/src/com/earth2me/essentials/Kit.java index 89965e589..fcd1b4932 100644 --- a/Essentials/src/com/earth2me/essentials/Kit.java +++ b/Essentials/src/com/earth2me/essentials/Kit.java @@ -158,11 +158,11 @@ public class Kit final Map<Integer, ItemStack> overfilled; if (user.isAuthorized("essentials.oversizedstacks")) { - overfilled = InventoryWorkaround.addItem(user.getInventory(), true, ess.getSettings().getOversizedStackSize(), stack); + overfilled = InventoryWorkaround.addOversizedItems(user.getInventory(), ess.getSettings().getOversizedStackSize(), stack); } else { - overfilled = InventoryWorkaround.addItem(user.getInventory(), true, 0, stack); + overfilled = InventoryWorkaround.addItems(user.getInventory(), stack); } for (ItemStack itemStack : overfilled.values()) { diff --git a/Essentials/src/com/earth2me/essentials/Trade.java b/Essentials/src/com/earth2me/essentials/Trade.java index 420525e63..1a03a0145 100644 --- a/Essentials/src/com/earth2me/essentials/Trade.java +++ b/Essentials/src/com/earth2me/essentials/Trade.java @@ -77,7 +77,7 @@ public class Trade } if (getItemStack() != null - && !InventoryWorkaround.containsItem(user.getInventory(), true, true, itemStack)) + && !user.getInventory().containsAtLeast(itemStack, itemStack.getAmount())) { throw new ChargeException(_("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " "))); } @@ -113,7 +113,7 @@ public class Trade { if (dropItems) { - final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack()); + final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItems(user.getInventory(), getItemStack()); final Location loc = user.getLocation(); for (ItemStack itemStack : leftOver.values()) { @@ -137,7 +137,7 @@ public class Trade } else { - success = InventoryWorkaround.addAllItems(user.getInventory(), true, getItemStack()); + success = InventoryWorkaround.addAllItems(user.getInventory(), getItemStack()); } user.updateInventory(); } @@ -165,11 +165,11 @@ public class Trade } if (getItemStack() != null) { - if (!InventoryWorkaround.containsItem(user.getInventory(), true, true, itemStack)) + if (!user.getInventory().containsAtLeast(itemStack, itemStack.getAmount())) { throw new ChargeException(_("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " "))); } - InventoryWorkaround.removeItem(user.getInventory(), true, true, getItemStack()); + user.getInventory().removeItem(getItemStack()); user.updateInventory(); } if (command != null) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java index c5c1a0b6b..c4e26d9a8 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java @@ -94,11 +94,11 @@ public class Commandgive extends EssentialsCommand sender.sendMessage(_("giveSpawn", stack.getAmount(), itemName, giveTo.getDisplayName())); if (giveTo.isAuthorized("essentials.oversizedstacks")) { - InventoryWorkaround.addItem(giveTo.getInventory(), true, ess.getSettings().getOversizedStackSize(), stack); + InventoryWorkaround.addOversizedItems(giveTo.getInventory(), ess.getSettings().getOversizedStackSize(), stack); } else { - InventoryWorkaround.addItem(giveTo.getInventory(), true, stack); + InventoryWorkaround.addItems(giveTo.getInventory(), stack); } giveTo.updateInventory(); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandhat.java b/Essentials/src/com/earth2me/essentials/commands/Commandhat.java index 5dd0fbb74..7ab9a446c 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandhat.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandhat.java @@ -31,7 +31,7 @@ public class Commandhat extends EssentialsCommand { final ItemStack air = new ItemStack(Material.AIR); inv.setHelmet(air); - InventoryWorkaround.addItem(user.getInventory(), true, head); + InventoryWorkaround.addItems(user.getInventory(), head); user.sendMessage(_("hatRemoved")); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java index 1a77d772d..d14b04d2a 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java @@ -87,11 +87,11 @@ public class Commanditem extends EssentialsCommand user.sendMessage(_("itemSpawn", stack.getAmount(), displayName)); if (user.isAuthorized("essentials.oversizedstacks")) { - InventoryWorkaround.addItem(user.getInventory(), true, ess.getSettings().getOversizedStackSize(), stack); + InventoryWorkaround.addOversizedItems(user.getInventory(), ess.getSettings().getOversizedStackSize(), stack); } else { - InventoryWorkaround.addItem(user.getInventory(), true, stack); + InventoryWorkaround.addItems(user.getInventory(), stack); } user.updateInventory(); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandsell.java b/Essentials/src/com/earth2me/essentials/commands/Commandsell.java index 904990e39..ec3262958 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandsell.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandsell.java @@ -166,7 +166,7 @@ public class Commandsell extends EssentialsCommand //TODO: Prices for Enchantments final ItemStack ris = is.clone(); ris.setAmount(amount); - InventoryWorkaround.removeItem(user.getInventory(), true, true, ris); + user.getInventory().removeItem(ris); user.updateInventory(); Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(worth * amount, ess), user.getLocation(), ess); user.giveMoney(worth * amount); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandunlimited.java b/Essentials/src/com/earth2me/essentials/commands/Commandunlimited.java index 38a8725ba..fd0f3be2c 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandunlimited.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandunlimited.java @@ -103,7 +103,7 @@ public class Commandunlimited extends EssentialsCommand { message = "enableUnlimited"; enableUnlimited = true; - if (!InventoryWorkaround.containsItem(target.getInventory(), true, true, stack)) + if (!target.getInventory().containsAtLeast(stack, stack.getAmount())) { target.getInventory().addItem(stack); } diff --git a/Essentials/src/com/earth2me/essentials/craftbukkit/FakeInventory.java b/Essentials/src/com/earth2me/essentials/craftbukkit/FakeInventory.java deleted file mode 100644 index 014fe2f65..000000000 --- a/Essentials/src/com/earth2me/essentials/craftbukkit/FakeInventory.java +++ /dev/null @@ -1,249 +0,0 @@ -package com.earth2me.essentials.craftbukkit; - -import java.util.HashMap; -import java.util.List; -import java.util.ListIterator; -import org.bukkit.Material; -import org.bukkit.entity.HumanEntity; -import org.bukkit.event.inventory.InventoryType; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; -import org.bukkit.inventory.ItemStack; - - -public class FakeInventory implements Inventory -{ - ItemStack[] items; - - public FakeInventory(ItemStack[] items) - { - this.items = new ItemStack[items.length]; - for (int i = 0; i < items.length; i++) - { - if (items[i] == null) - { - continue; - } - this.items[i] = items[i].clone(); - } - } - - @Override - public int getSize() - { - return items.length; - } - - @Override - public String getName() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public ItemStack getItem(int i) - { - return items[i]; - } - - @Override - public void setItem(int i, ItemStack is) - { - items[i] = is; - } - - @Override - public HashMap<Integer, ItemStack> addItem(ItemStack... iss) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public HashMap<Integer, ItemStack> removeItem(ItemStack... iss) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public ItemStack[] getContents() - { - return items; - } - - @Override - public void setContents(ItemStack[] iss) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean contains(int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean contains(Material mtrl) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean contains(ItemStack is) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean contains(int i, int i1) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean contains(Material mtrl, int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean contains(ItemStack is, int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public HashMap<Integer, ? extends ItemStack> all(int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public HashMap<Integer, ? extends ItemStack> all(Material mtrl) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public HashMap<Integer, ? extends ItemStack> all(ItemStack is) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int first(int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int first(Material mtrl) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int first(ItemStack is) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int firstEmpty() - { - for (int i = 0; i < items.length; i++) - { - if (items[i] == null || items[i].getTypeId() == 0) { - return i; - } - } - return -1; - } - - @Override - public void remove(int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void remove(Material mtrl) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void remove(ItemStack is) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void clear(int i) - { - items[i] = null; - } - - @Override - public void clear() - { - for (int i = 0; i < items.length; i++) - { - items[i] = null; - } - } - - @Override - public List<HumanEntity> getViewers() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public String getTitle() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public InventoryType getType() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public InventoryHolder getHolder() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public ListIterator<ItemStack> iterator() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public int getMaxStackSize() - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setMaxStackSize(int size) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public ListIterator<ItemStack> iterator(int index) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public boolean containsAtLeast(ItemStack is, int i) - { - throw new UnsupportedOperationException("Not supported yet."); - } -} diff --git a/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java b/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java index fc9cbb74b..76ce91138 100644 --- a/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java +++ b/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java @@ -2,6 +2,7 @@ package com.earth2me.essentials.craftbukkit; import java.util.HashMap; import java.util.Map; +import org.bukkit.Bukkit; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -15,49 +16,17 @@ public final class InventoryWorkaround { } - public static int first(final Inventory inventory, final ItemStack item, final boolean enforceDurability, final boolean enforceAmount, final boolean enforceEnchantments) - { - return next(inventory, item, 0, enforceDurability, enforceAmount, enforceEnchantments); - } - - public static int next(final Inventory cinventory, final ItemStack item, final int start, final boolean enforceDurability, final boolean enforceAmount, final boolean enforceEnchantments) - { - final ItemStack[] inventory = cinventory.getContents(); - for (int i = start; i < inventory.length; i++) - { - final ItemStack cItem = inventory[i]; - if (cItem == null) - { - continue; - } - if (item.getTypeId() == cItem.getTypeId() && (!enforceAmount || item.getAmount() == cItem.getAmount()) && (!enforceDurability || cItem.getDurability() == item.getDurability()) && (!enforceEnchantments || cItem.getEnchantments().equals(item.getEnchantments()))) - { - return i; - } - } - return -1; - } - - public static int firstPartial(final Inventory cinventory, final ItemStack item, final boolean enforceDurability) - { - return firstPartial(cinventory, item, enforceDurability, item.getType().getMaxStackSize()); - } - - public static int firstPartial(final Inventory cinventory, final ItemStack item, final boolean enforceDurability, final int maxAmount) + public static int firstPartial(final Inventory inventory, final ItemStack item, final int maxAmount) { if (item == null) { return -1; } - final ItemStack[] inventory = cinventory.getContents(); - for (int i = 0; i < inventory.length; i++) + final ItemStack[] stacks = inventory.getContents(); + for (int i = 0; i < stacks.length; i++) { - final ItemStack cItem = inventory[i]; - if (cItem == null) - { - continue; - } - if (item.getTypeId() == cItem.getTypeId() && cItem.getAmount() < maxAmount && (!enforceDurability || cItem.getDurability() == item.getDurability()) && cItem.getEnchantments().equals(item.getEnchantments())) + final ItemStack cItem = stacks[i]; + if (cItem != null && cItem.getAmount() < maxAmount && cItem.isSimilar(item)) { return i; } @@ -65,26 +34,24 @@ public final class InventoryWorkaround return -1; } - public static boolean addAllItems(final Inventory cinventory, final boolean enforceDurability, final ItemStack... items) + public static boolean addAllItems(final Inventory inventory, final ItemStack... items) { - final Inventory fake = new FakeInventory(cinventory.getContents()); - if (addItem(fake, enforceDurability, items).isEmpty()) + final Inventory fakeInventory = Bukkit.getServer().createInventory(null, inventory.getType()); + fakeInventory.setContents(inventory.getContents()); + if (fakeInventory.addItem(items).isEmpty()) { - addItem(cinventory, enforceDurability, items); + inventory.addItem(items); return true; } - else - { - return false; - } + return false; } - public static Map<Integer, ItemStack> addItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items) + public static Map<Integer, ItemStack> addItems(final Inventory inventory, final ItemStack... items) { - return addItem(cinventory, forceDurability, 0, items); + return addOversizedItems(inventory, 0, items); } - public static Map<Integer, ItemStack> addItem(final Inventory cinventory, final boolean enforceDurability, final int oversizedStacks, final ItemStack... items) + public static Map<Integer, ItemStack> addOversizedItems(final Inventory inventory, final int oversizedStacks, final ItemStack... items) { final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>(); @@ -95,7 +62,7 @@ public final class InventoryWorkaround // combine items - ItemStack[] combined = new ItemStack[items.length]; + final ItemStack[] combined = new ItemStack[items.length]; for (int i = 0; i < items.length; i++) { if (items[i] == null || items[i].getAmount() < 1) @@ -109,7 +76,7 @@ public final class InventoryWorkaround combined[j] = items[i].clone(); break; } - if (combined[j].getTypeId() == items[i].getTypeId() && (!enforceDurability || combined[j].getDurability() == items[i].getDurability()) && combined[j].getEnchantments().equals(items[i].getEnchantments())) + if (combined[j].isSimilar(items[i])) { combined[j].setAmount(combined[j].getAmount() + items[i].getAmount()); break; @@ -130,13 +97,13 @@ public final class InventoryWorkaround { // Do we already have a stack of it? final int maxAmount = oversizedStacks > item.getType().getMaxStackSize() ? oversizedStacks : item.getType().getMaxStackSize(); - final int firstPartial = firstPartial(cinventory, item, enforceDurability, maxAmount); + final int firstPartial = firstPartial(inventory, item, maxAmount); // Drat! no partial stack if (firstPartial == -1) { // Find a free spot! - final int firstFree = cinventory.firstEmpty(); + final int firstFree = inventory.firstEmpty(); if (firstFree == -1) { @@ -151,13 +118,13 @@ public final class InventoryWorkaround { final ItemStack stack = item.clone(); stack.setAmount(maxAmount); - cinventory.setItem(firstFree, stack); + inventory.setItem(firstFree, stack); item.setAmount(item.getAmount() - maxAmount); } else { // Just store it - cinventory.setItem(firstFree, item); + inventory.setItem(firstFree, item); break; } } @@ -165,7 +132,7 @@ public final class InventoryWorkaround else { // So, apparently it might only partially fit, well lets do just that - final ItemStack partialItem = cinventory.getItem(firstPartial); + final ItemStack partialItem = inventory.getItem(firstPartial); final int amount = item.getAmount(); final int partialAmount = partialItem.getAmount(); @@ -185,138 +152,4 @@ public final class InventoryWorkaround } return leftover; } - - public static Map<Integer, ItemStack> removeItem(final Inventory cinventory, final boolean enforceDurability, final boolean enforceEnchantments, final ItemStack... items) - { - final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>(); - - // TODO: optimization - - for (int i = 0; i < items.length; i++) - { - final ItemStack item = items[i]; - if (item == null) - { - continue; - } - int toDelete = item.getAmount(); - - while (true) - { - - // Bail when done - if (toDelete <= 0) - { - break; - } - - // get first Item, ignore the amount - final int first = first(cinventory, item, enforceDurability, false, enforceEnchantments); - - // Drat! we don't have this type in the inventory - if (first == -1) - { - item.setAmount(toDelete); - leftover.put(i, item); - break; - } - else - { - final ItemStack itemStack = cinventory.getItem(first); - final int amount = itemStack.getAmount(); - - if (amount <= toDelete) - { - toDelete -= amount; - // clear the slot, all used up - cinventory.clear(first); - } - else - { - // split the stack and store - itemStack.setAmount(amount - toDelete); - cinventory.setItem(first, itemStack); - toDelete = 0; - } - } - } - } - return leftover; - } - - public static boolean containsItem(final Inventory cinventory, final boolean enforceDurability, final boolean enforceEnchantments, final ItemStack... items) - { - final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>(); - - // TODO: optimization - - // combine items - - ItemStack[] combined = new ItemStack[items.length]; - for (int i = 0; i < items.length; i++) - { - if (items[i] == null) - { - continue; - } - for (int j = 0; j < combined.length; j++) - { - if (combined[j] == null) - { - combined[j] = items[i].clone(); - break; - } - if (combined[j].getTypeId() == items[i].getTypeId() && (!enforceDurability || combined[j].getDurability() == items[i].getDurability()) && (!enforceEnchantments || combined[j].getEnchantments().equals(items[i].getEnchantments()))) - { - combined[j].setAmount(combined[j].getAmount() + items[i].getAmount()); - break; - } - } - } - - for (int i = 0; i < combined.length; i++) - { - final ItemStack item = combined[i]; - if (item == null) - { - continue; - } - int mustHave = item.getAmount(); - int position = 0; - - while (true) - { - // Bail when done - if (mustHave <= 0) - { - break; - } - - final int slot = next(cinventory, item, position, enforceDurability, false, enforceEnchantments); - - // Drat! we don't have this type in the inventory - if (slot == -1) - { - leftover.put(i, item); - break; - } - else - { - final ItemStack itemStack = cinventory.getItem(slot); - final int amount = itemStack.getAmount(); - - if (amount <= mustHave) - { - mustHave -= amount; - } - else - { - mustHave = 0; - } - position = slot + 1; - } - } - } - return leftover.isEmpty(); - } } |