From 75a0164ea0dcd373e3a574faaea554fd4102ab9b Mon Sep 17 00:00:00 2001 From: snowleo Date: Sun, 9 Oct 2011 22:25:15 +0200 Subject: Don't spill out items, if inventory is full on buy signs. --- .../src/com/earth2me/essentials/FakeInventory.java | 186 +++++++++++++++++++++ .../earth2me/essentials/InventoryWorkaround.java | 16 +- Essentials/src/com/earth2me/essentials/Trade.java | 49 ++++-- .../src/com/earth2me/essentials/signs/SignBuy.java | 4 +- 4 files changed, 236 insertions(+), 19 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/FakeInventory.java diff --git a/Essentials/src/com/earth2me/essentials/FakeInventory.java b/Essentials/src/com/earth2me/essentials/FakeInventory.java new file mode 100644 index 000000000..e1c3321d2 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/FakeInventory.java @@ -0,0 +1,186 @@ +package com.earth2me.essentials; + +import java.util.HashMap; +import org.bukkit.Material; +import org.bukkit.inventory.Inventory; +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++) + { + this.items[i] = new ItemStack(items[i].getTypeId(), items[i].getAmount(), items[i].getDurability()); + } + } + + @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 addItem(ItemStack... iss) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public HashMap 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 all(int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public HashMap all(Material mtrl) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public HashMap 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; + } + } +} diff --git a/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java b/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java index 3f0f89af2..0470488b8 100644 --- a/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java +++ b/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java @@ -64,6 +64,20 @@ public final class InventoryWorkaround return -1; } + public static boolean addAllItems(final Inventory cinventory, final boolean forceDurability, final ItemStack... items) + { + final Inventory fake = new FakeInventory(cinventory.getContents()); + if (addItem(fake, forceDurability, items).isEmpty()) + { + addItem(cinventory, forceDurability, items); + return true; + } + else + { + return false; + } + } + public static Map addItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items) { final Map leftover = new HashMap(); @@ -106,7 +120,7 @@ public final class InventoryWorkaround { continue; } - + while (true) { // Do we already have a stack of it? diff --git a/Essentials/src/com/earth2me/essentials/Trade.java b/Essentials/src/com/earth2me/essentials/Trade.java index 45c3d49a3..8c082be3e 100644 --- a/Essentials/src/com/earth2me/essentials/Trade.java +++ b/Essentials/src/com/earth2me/essentials/Trade.java @@ -18,22 +18,22 @@ public class Trade private final transient Double money; private final transient ItemStack itemStack; private final transient IEssentials ess; - + public Trade(final String command, final IEssentials ess) { this(command, null, null, ess); } - + public Trade(final double money, final IEssentials ess) { this(null, money, null, ess); } - + public Trade(final ItemStack items, final IEssentials ess) { this(null, null, items, ess); } - + private Trade(final String command, final Double money, final ItemStack item, final IEssentials ess) { this.command = command; @@ -41,7 +41,7 @@ public class Trade this.itemStack = item; this.ess = ess; } - + public void isAffordableFor(final IUser user) throws ChargeException { final double mon = user.getMoney(); @@ -52,13 +52,13 @@ public class Trade { throw new ChargeException(Util.i18n("notEnoughMoney")); } - + if (getItemStack() != null && !InventoryWorkaround.containsItem(user.getInventory(), true, itemStack)) { throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase().replace("_", " "))); } - + if (command != null && !command.isEmpty() && !user.isAuthorized("essentials.nocommandcost.all") && !user.isAuthorized("essentials.nocommandcost." + command) @@ -69,24 +69,38 @@ public class Trade throw new ChargeException(Util.i18n("notEnoughMoney")); } } - + public void pay(final IUser user) { + pay(user, true); + } + + public boolean pay(final IUser user, final boolean dropItems) + { + boolean success = true; if (getMoney() != null && getMoney() > 0) { user.giveMoney(getMoney()); } if (getItemStack() != null) { - final Map leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack()); - for (ItemStack itemStack : leftOver.values()) + if (dropItems) + { + final Map leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack()); + for (ItemStack itemStack : leftOver.values()) + { + InventoryWorkaround.dropItem(user.getLocation(), itemStack); + } + } + else { - InventoryWorkaround.dropItem(user.getLocation(), itemStack); + success = InventoryWorkaround.addAllItems(user.getInventory(), true, getItemStack()); } user.updateInventory(); } + return success; } - + public void charge(final IUser user) throws ChargeException { if (getMoney() != null) @@ -120,18 +134,18 @@ public class Trade user.takeMoney(cost); } } - + public Double getMoney() { return money; } - + public ItemStack getItemStack() { return itemStack; } private static FileWriter fw = null; - + public static void log(String type, String subtype, String event, String sender, Trade charge, String receiver, Trade pay, Location loc, IEssentials ess) { if (!ess.getSettings().isEcoLogEnabled()) @@ -225,10 +239,11 @@ public class Trade Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex); } } - + public static void closeLog() { - if (fw != null) { + if (fw != null) + { try { fw.close(); diff --git a/Essentials/src/com/earth2me/essentials/signs/SignBuy.java b/Essentials/src/com/earth2me/essentials/signs/SignBuy.java index 6ee602ff5..39704ff0d 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignBuy.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignBuy.java @@ -27,7 +27,9 @@ public class SignBuy extends EssentialsSign final Trade items = getTrade(sign, 1, 2, player, ess); final Trade charge = getTrade(sign, 3, ess); charge.isAffordableFor(player); - items.pay(player); + if (!items.pay(player, false)) { + throw new ChargeException("Inventory full"); + } charge.charge(player); Trade.log("Sign", "Buy", "Interact", username, charge, username, items, sign.getBlock().getLocation(), ess); return true; -- cgit v1.2.3