Don't spill out items, if inventory is full on buy signs.

This commit is contained in:
snowleo 2011-10-09 22:25:15 +02:00
parent 9dde04e4b8
commit 75a0164ea0
4 changed files with 236 additions and 19 deletions

View File

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

View File

@ -64,6 +64,20 @@ public final class InventoryWorkaround
return -1; 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<Integer, ItemStack> addItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items) public static Map<Integer, ItemStack> addItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items)
{ {
final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>(); final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
@ -106,7 +120,7 @@ public final class InventoryWorkaround
{ {
continue; continue;
} }
while (true) while (true)
{ {
// Do we already have a stack of it? // Do we already have a stack of it?

View File

@ -18,22 +18,22 @@ public class Trade
private final transient Double money; private final transient Double money;
private final transient ItemStack itemStack; private final transient ItemStack itemStack;
private final transient IEssentials ess; private final transient IEssentials ess;
public Trade(final String command, final IEssentials ess) public Trade(final String command, final IEssentials ess)
{ {
this(command, null, null, ess); this(command, null, null, ess);
} }
public Trade(final double money, final IEssentials ess) public Trade(final double money, final IEssentials ess)
{ {
this(null, money, null, ess); this(null, money, null, ess);
} }
public Trade(final ItemStack items, final IEssentials ess) public Trade(final ItemStack items, final IEssentials ess)
{ {
this(null, null, items, ess); this(null, null, items, ess);
} }
private Trade(final String command, final Double money, final ItemStack item, final IEssentials ess) private Trade(final String command, final Double money, final ItemStack item, final IEssentials ess)
{ {
this.command = command; this.command = command;
@ -41,7 +41,7 @@ public class Trade
this.itemStack = item; this.itemStack = item;
this.ess = ess; this.ess = ess;
} }
public void isAffordableFor(final IUser user) throws ChargeException public void isAffordableFor(final IUser user) throws ChargeException
{ {
final double mon = user.getMoney(); final double mon = user.getMoney();
@ -52,13 +52,13 @@ public class Trade
{ {
throw new ChargeException(Util.i18n("notEnoughMoney")); throw new ChargeException(Util.i18n("notEnoughMoney"));
} }
if (getItemStack() != null if (getItemStack() != null
&& !InventoryWorkaround.containsItem(user.getInventory(), true, itemStack)) && !InventoryWorkaround.containsItem(user.getInventory(), true, itemStack))
{ {
throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase().replace("_", " "))); throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase().replace("_", " ")));
} }
if (command != null && !command.isEmpty() if (command != null && !command.isEmpty()
&& !user.isAuthorized("essentials.nocommandcost.all") && !user.isAuthorized("essentials.nocommandcost.all")
&& !user.isAuthorized("essentials.nocommandcost." + command) && !user.isAuthorized("essentials.nocommandcost." + command)
@ -69,24 +69,38 @@ public class Trade
throw new ChargeException(Util.i18n("notEnoughMoney")); throw new ChargeException(Util.i18n("notEnoughMoney"));
} }
} }
public void pay(final IUser user) 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) if (getMoney() != null && getMoney() > 0)
{ {
user.giveMoney(getMoney()); user.giveMoney(getMoney());
} }
if (getItemStack() != null) if (getItemStack() != null)
{ {
final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack()); if (dropItems)
for (ItemStack itemStack : leftOver.values())
{ {
InventoryWorkaround.dropItem(user.getLocation(), itemStack); final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack());
for (ItemStack itemStack : leftOver.values())
{
InventoryWorkaround.dropItem(user.getLocation(), itemStack);
}
}
else
{
success = InventoryWorkaround.addAllItems(user.getInventory(), true, getItemStack());
} }
user.updateInventory(); user.updateInventory();
} }
return success;
} }
public void charge(final IUser user) throws ChargeException public void charge(final IUser user) throws ChargeException
{ {
if (getMoney() != null) if (getMoney() != null)
@ -120,18 +134,18 @@ public class Trade
user.takeMoney(cost); user.takeMoney(cost);
} }
} }
public Double getMoney() public Double getMoney()
{ {
return money; return money;
} }
public ItemStack getItemStack() public ItemStack getItemStack()
{ {
return itemStack; return itemStack;
} }
private static FileWriter fw = null; 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) 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()) if (!ess.getSettings().isEcoLogEnabled())
@ -225,10 +239,11 @@ public class Trade
Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex); Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
} }
} }
public static void closeLog() public static void closeLog()
{ {
if (fw != null) { if (fw != null)
{
try try
{ {
fw.close(); fw.close();

View File

@ -27,7 +27,9 @@ public class SignBuy extends EssentialsSign
final Trade items = getTrade(sign, 1, 2, player, ess); final Trade items = getTrade(sign, 1, 2, player, ess);
final Trade charge = getTrade(sign, 3, ess); final Trade charge = getTrade(sign, 3, ess);
charge.isAffordableFor(player); charge.isAffordableFor(player);
items.pay(player); if (!items.pay(player, false)) {
throw new ChargeException("Inventory full");
}
charge.charge(player); charge.charge(player);
Trade.log("Sign", "Buy", "Interact", username, charge, username, items, sign.getBlock().getLocation(), ess); Trade.log("Sign", "Buy", "Interact", username, charge, username, items, sign.getBlock().getLocation(), ess);
return true; return true;