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>();

View File

@ -72,19 +72,33 @@ public class Trade
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)
{
if (dropItems)
{ {
final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack()); final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack());
for (ItemStack itemStack : leftOver.values()) for (ItemStack itemStack : leftOver.values())
{ {
InventoryWorkaround.dropItem(user.getLocation(), itemStack); 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
@ -228,7 +242,8 @@ public class Trade
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;