diff options
author | Chris Ward <chris@chrisgward.com> | 2013-01-08 23:56:23 +1100 |
---|---|---|
committer | Chris Ward <chris@chrisgward.com> | 2013-01-08 23:56:23 +1100 |
commit | bd4247b342d82ea5dd3f2e522b72df87fe312281 (patch) | |
tree | 01b60faa21e05680d7492f32870cb36b2f359997 | |
parent | a50eb2c2c4e536969d016279b9df8c7c138eb019 (diff) | |
download | Essentials-bd4247b342d82ea5dd3f2e522b72df87fe312281.tar Essentials-bd4247b342d82ea5dd3f2e522b72df87fe312281.tar.gz Essentials-bd4247b342d82ea5dd3f2e522b72df87fe312281.tar.lz Essentials-bd4247b342d82ea5dd3f2e522b72df87fe312281.tar.xz Essentials-bd4247b342d82ea5dd3f2e522b72df87fe312281.zip |
Update Inventory Workaround to latest version
2a88a314ce3003598f8b94c427b4f88a6aeb16e6
-rw-r--r-- | Essentials/src/net/ess3/commands/Commandhat.java | 4 | ||||
-rw-r--r-- | Essentials/src/net/ess3/commands/Commandsell.java | 3 | ||||
-rw-r--r-- | Essentials/src/net/ess3/craftbukkit/FakeInventory.java | 249 | ||||
-rw-r--r-- | Essentials/src/net/ess3/craftbukkit/InventoryWorkaround.java | 213 | ||||
-rw-r--r-- | Essentials/src/net/ess3/economy/Trade.java | 10 |
5 files changed, 29 insertions, 450 deletions
diff --git a/Essentials/src/net/ess3/commands/Commandhat.java b/Essentials/src/net/ess3/commands/Commandhat.java index 5a0d5bd19..ce1ead49f 100644 --- a/Essentials/src/net/ess3/commands/Commandhat.java +++ b/Essentials/src/net/ess3/commands/Commandhat.java @@ -26,7 +26,7 @@ public class Commandhat extends EssentialsCommand { final ItemStack air = new ItemStack(Material.AIR); inv.setHelmet(air); - InventoryWorkaround.addItem(user.getPlayer().getInventory(), true, head); + InventoryWorkaround.addItems(user.getPlayer().getInventory(), head); user.sendMessage(_("hatRemoved")); } } @@ -41,7 +41,7 @@ public class Commandhat extends EssentialsCommand final PlayerInventory inv = player.getInventory(); final ItemStack head = inv.getHelmet(); hand.setAmount(1); - InventoryWorkaround.removeItem(inv, true, true, hand); + inv.remove(hand); inv.setHelmet(hand); inv.setItemInHand(head); user.sendMessage(_("hatPlaced")); diff --git a/Essentials/src/net/ess3/commands/Commandsell.java b/Essentials/src/net/ess3/commands/Commandsell.java index 8e1989bad..d36f82ee1 100644 --- a/Essentials/src/net/ess3/commands/Commandsell.java +++ b/Essentials/src/net/ess3/commands/Commandsell.java @@ -7,7 +7,6 @@ import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import net.ess3.api.IUser; -import net.ess3.craftbukkit.InventoryWorkaround; import net.ess3.economy.Trade; import net.ess3.utils.FormatUtil; @@ -143,7 +142,7 @@ public class Commandsell extends EssentialsCommand //TODO: Prices for Enchantments final ItemStack ris = is.clone(); ris.setAmount(amount); - InventoryWorkaround.removeItem(player.getInventory(), true, true, ris); + player.getInventory().removeItem(ris); player.updateInventory(); Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(worth * amount, ess), player.getLocation(), ess); user.giveMoney(worth * amount); diff --git a/Essentials/src/net/ess3/craftbukkit/FakeInventory.java b/Essentials/src/net/ess3/craftbukkit/FakeInventory.java deleted file mode 100644 index a874f8b46..000000000 --- a/Essentials/src/net/ess3/craftbukkit/FakeInventory.java +++ /dev/null @@ -1,249 +0,0 @@ -package net.ess3.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."); - } - - public boolean containsAtLeast(ItemStack arg0, int arg1) - { - throw new UnsupportedOperationException("Not supported yet."); - } -} diff --git a/Essentials/src/net/ess3/craftbukkit/InventoryWorkaround.java b/Essentials/src/net/ess3/craftbukkit/InventoryWorkaround.java index 3c2fa910f..2a40a6c1c 100644 --- a/Essentials/src/net/ess3/craftbukkit/InventoryWorkaround.java +++ b/Essentials/src/net/ess3/craftbukkit/InventoryWorkaround.java @@ -2,6 +2,7 @@ package net.ess3.craftbukkit; import java.util.HashMap; import java.util.Map; +import org.bukkit.Bukkit; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -16,51 +17,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; } @@ -68,26 +35,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>(); @@ -112,8 +77,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; @@ -134,13 +98,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) { @@ -155,13 +119,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; } } @@ -169,7 +133,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(); @@ -189,139 +153,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(); - } } diff --git a/Essentials/src/net/ess3/economy/Trade.java b/Essentials/src/net/ess3/economy/Trade.java index 8ccc6f205..8d5bd5ad6 100644 --- a/Essentials/src/net/ess3/economy/Trade.java +++ b/Essentials/src/net/ess3/economy/Trade.java @@ -71,7 +71,7 @@ public class Trade throw new ChargeException(_("notEnoughMoney")); } - if (getItemStack() != null && InventoryWorkaround.containsItem(user.getPlayer().getInventory(), true, true, itemStack)) + if (getItemStack() != null && !user.getPlayer().getInventory().containsAtLeast(itemStack, itemStack.getAmount())) { throw new ChargeException( _("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " "))); @@ -105,7 +105,7 @@ public class Trade { if (dropItems) { - final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getPlayer().getInventory(), true, getItemStack()); + final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItems(user.getPlayer().getInventory(), getItemStack()); final Location loc = user.getPlayer().getLocation(); for (ItemStack dropStack : leftOver.values()) { @@ -129,7 +129,7 @@ public class Trade } else { - success = InventoryWorkaround.addAllItems(user.getPlayer().getInventory(), true, getItemStack()); + success = InventoryWorkaround.addAllItems(user.getPlayer().getInventory(), getItemStack()); } user.getPlayer().updateInventory(); } @@ -152,12 +152,12 @@ public class Trade } if (getItemStack() != null) { - if (!InventoryWorkaround.containsItem(user.getPlayer().getInventory(), true, true, itemStack)) + if (!user.getPlayer().getInventory().containsAtLeast(itemStack, itemStack.getAmount())) { throw new ChargeException( _("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " "))); } - InventoryWorkaround.removeItem(user.getPlayer().getInventory(), true, true, getItemStack()); + user.getPlayer().getInventory().removeItem(getItemStack()); user.getPlayer().updateInventory(); } if (command != null && !command.isEmpty() && !Permissions.NOCOMMANDCOST.isAuthorized(user, command)) |