diff options
Diffstat (limited to 'Essentials/src/com/earth2me/essentials/signs/SignTrade.java')
-rw-r--r-- | Essentials/src/com/earth2me/essentials/signs/SignTrade.java | 78 |
1 files changed, 63 insertions, 15 deletions
diff --git a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java index bf85d827c..6c956165a 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java @@ -10,7 +10,6 @@ import org.bukkit.inventory.ItemStack; public class SignTrade extends EssentialsSign { - public SignTrade() { super("Trade"); @@ -34,25 +33,33 @@ public class SignTrade extends EssentialsSign { if (sign.getLine(3).substring(2).equalsIgnoreCase(username)) { + final Trade store = rechargeSign(sign, ess, player); + Trade stored = null; try { - final Trade stored = getTrade(sign, 1, true, true, ess); + stored = getTrade(sign, 1, true, true, ess); substractAmount(sign, 1, stored, ess); stored.pay(player); - Trade.log("Sign", "Trade", "OwnerInteract", username, null, username, stored, sign.getBlock().getLocation(), ess); - } + } catch (SignException e) { - throw new SignException(Util.i18n("tradeSignEmptyOwner")); - } + if (store == null) + { + throw new SignException(Util.i18n("tradeSignEmptyOwner"), e); + } + } + Trade.log("Sign", "Trade", "OwnerInteract", username, store, username, stored, sign.getBlock().getLocation(), ess); } else { final Trade charge = getTrade(sign, 1, false, false, ess); final Trade trade = getTrade(sign, 2, false, true, ess); charge.isAffordableFor(player); + if (!trade.pay(player, false)) + { + throw new ChargeException("Full inventory"); + } substractAmount(sign, 2, trade, ess); - trade.pay(player); addAmount(sign, 1, charge, ess); charge.charge(player); Trade.log("Sign", "Trade", "Interact", sign.getLine(3), charge, username, trade, sign.getBlock().getLocation(), ess); @@ -61,17 +68,48 @@ public class SignTrade extends EssentialsSign return true; } + private Trade rechargeSign(final ISign sign, final IEssentials ess, final User player) throws SignException, ChargeException + { + final Trade trade = getTrade(sign, 2, false, false, ess); + if (trade.getItemStack() != null && player.getItemInHand() != null + && trade.getItemStack().getTypeId() == player.getItemInHand().getTypeId() + && trade.getItemStack().getDurability() == player.getItemInHand().getDurability()) + { + int amount = player.getItemInHand().getAmount(); + amount -= amount % trade.getItemStack().getAmount(); + if (amount > 0) + { + final Trade store = new Trade(new ItemStack(player.getItemInHand().getTypeId(), amount, player.getItemInHand().getDurability()), ess); + addAmount(sign, 2, store, ess); + store.charge(player); + return store; + } + } + return null; + } + @Override protected boolean onSignBreak(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException { if ((sign.getLine(3).length() > 3 && sign.getLine(3).substring(2).equalsIgnoreCase(username)) || player.isAuthorized("essentials.signs.trade.override")) { - final Trade stored1 = getTrade(sign, 1, true, false, ess); - final Trade stored2 = getTrade(sign, 2, true, false, ess); - stored1.pay(player); - stored2.pay(player); - Trade.log("Sign", "Trade", "Break", username, stored2, username, stored1, sign.getBlock().getLocation(), ess); + try + { + final Trade stored1 = getTrade(sign, 1, true, false, ess); + final Trade stored2 = getTrade(sign, 2, true, false, ess); + stored1.pay(player); + stored2.pay(player); + Trade.log("Sign", "Trade", "Break", username, stored2, username, stored1, sign.getBlock().getLocation(), ess); + } + catch (SignException e) + { + if (player.isAuthorized("essentials.signs.trade.override")) + { + return true; + } + throw e; + } return true; } else @@ -128,7 +166,7 @@ public class SignTrade extends EssentialsSign throw new SignException(Util.i18n("moreThanZero")); } String newline = amount + " " + split[1] + ":0"; - if ((newline + amount).length() > 16) + if ((newline + amount).length() > 15) { throw new SignException("Line can be too long!"); } @@ -237,7 +275,12 @@ public class SignTrade extends EssentialsSign final Double amount = getDouble(split[1]); if (money != null && amount != null) { - sign.setLine(index, Util.formatCurrency(money, ess) + ":" + Util.formatCurrency(amount + value, ess).substring(1)); + final String newline = Util.formatCurrency(money, ess) + ":" + Util.formatCurrency(amount + value, ess).substring(1); + if (newline.length() > 15) + { + throw new SignException("Line too long!"); + } + sign.setLine(index, newline); return; } } @@ -247,7 +290,12 @@ public class SignTrade extends EssentialsSign final int stackamount = getIntegerPositive(split[0]); final ItemStack item = getItemStack(split[1], stackamount, ess); final int amount = getInteger(split[2]); - sign.setLine(index, stackamount + " " + split[1] + ":" + (amount + Math.round(value))); + final String newline = stackamount + " " + split[1] + ":" + (amount + Math.round(value)); + if (newline.length() > 15) + { + throw new SignException("Line too long!"); + } + sign.setLine(index, newline); return; } throw new SignException(Util.format("invalidSignLine", index + 1)); |