From 34f650e270e4418109347a6b716a3c191c446a6b Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 13 Jun 2011 15:15:19 +0200 Subject: [PATCH] Moved some code from EssentialsSign to SignTrade, because it's only needed there. --- .../essentials/signs/EssentialsSign.java | 149 +---------------- .../earth2me/essentials/signs/SignTrade.java | 156 +++++++++++++++++- 2 files changed, 157 insertions(+), 148 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java index 4fec56f04..a6f17c8bb 100644 --- a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java +++ b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java @@ -130,152 +130,7 @@ public class EssentialsSign } } - protected final void validateTrade(final ISign sign, final int index, final boolean amountNeeded, final IEssentials ess) throws SignException - { - final String line = sign.getLine(index).trim(); - if (line.isEmpty()) - { - throw new SignException("Empty line"); - } - final String[] split = line.split("[ :]+"); - - if (split.length == 1 && !amountNeeded) - { - final Double money = getMoney(split[0]); - if (money != null) - { - sign.setLine(index, Util.formatCurrency(money) + ":0"); - return; - } - } - - if (split.length == 2 && amountNeeded) - { - final Double money = getMoney(split[0]); - final Double amount = getDouble(split[1]); - if (money != null && amount != null) - { - sign.setLine(index, Util.formatCurrency(money) + ":" + Util.formatCurrency(amount).substring(1)); - return; - } - } - - if (split.length == 2 && !amountNeeded) - { - final int amount = getInteger(split[0]); - final ItemStack item = getItemStack(split[1], amount); - if (amount < 1 || item.getTypeId() == 0) - { - throw new SignException(Util.i18n("moreThanZero")); - } - sign.setLine(index, amount + " " + split[1] + ":0"); - return; - } - - if (split.length == 3 && amountNeeded) - { - final int stackamount = getInteger(split[0]); - final ItemStack item = getItemStack(split[1], stackamount); - int amount = getInteger(split[2]); - amount -= amount % stackamount; - if (amount < 1 || stackamount < 1 || item.getTypeId() == 0) - { - throw new SignException(Util.i18n("moreThanZero")); - } - sign.setLine(index, stackamount + " " + split[1] + ":" + amount); - return; - } - throw new SignException(Util.format("invalidSignLine", index)); - } - - protected final Trade getTrade(final ISign sign, final int index, final boolean fullAmount, final IEssentials ess) throws SignException - { - final String line = sign.getLine(index).trim(); - if (line.isEmpty()) - { - throw new SignException("Empty line"); - } - final String[] split = line.split("[ :]+"); - - if (split.length == 2) - { - final Double money = getMoney(split[0]); - final Double amount = getDouble(split[1]); - if (money != null && amount != null) - { - return new Trade(fullAmount ? amount : money, ess); - } - } - - if (split.length == 3) - { - final int stackamount = getInteger(split[0]); - final ItemStack item = getItemStack(split[1], stackamount); - int amount = getInteger(split[2]); - amount -= amount % stackamount; - if (amount < 1 || stackamount < 1 || item.getTypeId() == 0) - { - throw new SignException(Util.i18n("moreThanZero")); - } - item.setAmount(fullAmount ? amount : stackamount); - return new Trade(item, ess); - } - throw new SignException(Util.format("invalidSignLine", index)); - } - protected final void substractAmount(final ISign sign, final int index, final Trade trade) throws SignException - { - final Double money = trade.getMoney(); - if (money != null) { - changeAmount(sign, index, -money); - } - final ItemStack item = trade.getItemStack(); - if (item != null) { - changeAmount(sign, index, -item.getAmount()); - } - } - protected final void addAmount(final ISign sign, final int index, final Trade trade) throws SignException - { - final Double money = trade.getMoney(); - if (money != null) { - changeAmount(sign, index, money); - } - final ItemStack item = trade.getItemStack(); - if (item != null) { - changeAmount(sign, index, item.getAmount()); - } - } - - private void changeAmount(final ISign sign, final int index, final double value) throws SignException - { - final String line = sign.getLine(index).trim(); - if (line.isEmpty()) - { - throw new SignException("Empty line"); - } - final String[] split = line.split("[ :]+"); - - if (split.length == 2) - { - final Double money = getMoney(split[0]); - final Double amount = getDouble(split[1]); - if (money != null && amount != null) - { - sign.setLine(index, Util.formatCurrency(money) + ":" + Util.formatCurrency(amount+value).substring(1)); - return; - } - } - - if (split.length == 3) - { - final int stackamount = getInteger(split[0]); - final ItemStack item = getItemStack(split[1], stackamount); - int amount = getInteger(split[2]); - sign.setLine(index, stackamount + " " + split[1] + ":" + (amount+Math.round(value))); - return; - } - throw new SignException(Util.format("invalidSignLine", index)); - } protected final void validateTrade(final ISign sign, final int amountIndex, final int itemIndex, final User player, final IEssentials ess) throws SignException @@ -342,13 +197,13 @@ public class EssentialsSign } } - private final Double getMoney(final String line) throws SignException + protected final Double getMoney(final String line) throws SignException { final boolean isMoney = line.matches("^[^0-9-\\.][\\.0-9]+"); return isMoney ? getDouble(line.substring(1)) : null; } - private final Double getDouble(final String line) throws SignException + protected final Double getDouble(final String line) throws SignException { try { diff --git a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java index 2d5f1fcb2..dd82bd09d 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java @@ -4,6 +4,8 @@ import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; +import com.earth2me.essentials.Util; +import org.bukkit.inventory.ItemStack; public class SignTrade extends EssentialsSign @@ -50,7 +52,7 @@ public class SignTrade extends EssentialsSign @Override protected boolean onSignBreak(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException { - if (sign.getLine(3).substring(2).equalsIgnoreCase(username)) + if (sign.getLine(3).length() > 3 && sign.getLine(3).substring(2).equalsIgnoreCase(username)) { final Trade stored1 = getTrade(sign, 1, true, ess); final Trade stored2 = getTrade(sign, 2, true, ess); @@ -63,4 +65,156 @@ public class SignTrade extends EssentialsSign return false; } } + + protected final void validateTrade(final ISign sign, final int index, final boolean amountNeeded, final IEssentials ess) throws SignException + { + final String line = sign.getLine(index).trim(); + if (line.isEmpty()) + { + throw new SignException("Empty line"); + } + final String[] split = line.split("[ :]+"); + + if (split.length == 1 && !amountNeeded) + { + final Double money = getMoney(split[0]); + if (money != null) + { + sign.setLine(index, Util.formatCurrency(money) + ":0"); + return; + } + } + + if (split.length == 2 && amountNeeded) + { + final Double money = getMoney(split[0]); + final Double amount = getDouble(split[1]); + if (money != null && amount != null) + { + sign.setLine(index, Util.formatCurrency(money) + ":" + Util.formatCurrency(amount).substring(1)); + return; + } + } + + if (split.length == 2 && !amountNeeded) + { + final int amount = getInteger(split[0]); + final ItemStack item = getItemStack(split[1], amount); + if (amount < 1 || item.getTypeId() == 0) + { + throw new SignException(Util.i18n("moreThanZero")); + } + sign.setLine(index, amount + " " + split[1] + ":0"); + return; + } + + if (split.length == 3 && amountNeeded) + { + final int stackamount = getInteger(split[0]); + final ItemStack item = getItemStack(split[1], stackamount); + int amount = getInteger(split[2]); + amount -= amount % stackamount; + if (amount < 1 || stackamount < 1 || item.getTypeId() == 0) + { + throw new SignException(Util.i18n("moreThanZero")); + } + sign.setLine(index, stackamount + " " + split[1] + ":" + amount); + return; + } + throw new SignException(Util.format("invalidSignLine", index)); + } + + protected final Trade getTrade(final ISign sign, final int index, final boolean fullAmount, final IEssentials ess) throws SignException + { + final String line = sign.getLine(index).trim(); + if (line.isEmpty()) + { + throw new SignException("Empty line"); + } + final String[] split = line.split("[ :]+"); + + if (split.length == 2) + { + final Double money = getMoney(split[0]); + final Double amount = getDouble(split[1]); + if (money != null && amount != null) + { + return new Trade(fullAmount ? amount : money, ess); + } + } + + if (split.length == 3) + { + final int stackamount = getInteger(split[0]); + final ItemStack item = getItemStack(split[1], stackamount); + int amount = getInteger(split[2]); + amount -= amount % stackamount; + if (amount < 1 || stackamount < 1 || item.getTypeId() == 0) + { + throw new SignException(Util.i18n("moreThanZero")); + } + item.setAmount(fullAmount ? amount : stackamount); + return new Trade(item, ess); + } + throw new SignException(Util.format("invalidSignLine", index)); + } + + protected final void substractAmount(final ISign sign, final int index, final Trade trade) throws SignException + { + final Double money = trade.getMoney(); + if (money != null) + { + changeAmount(sign, index, -money); + } + final ItemStack item = trade.getItemStack(); + if (item != null) + { + changeAmount(sign, index, -item.getAmount()); + } + } + + protected final void addAmount(final ISign sign, final int index, final Trade trade) throws SignException + { + final Double money = trade.getMoney(); + if (money != null) + { + changeAmount(sign, index, money); + } + final ItemStack item = trade.getItemStack(); + if (item != null) + { + changeAmount(sign, index, item.getAmount()); + } + } + + private void changeAmount(final ISign sign, final int index, final double value) throws SignException + { + final String line = sign.getLine(index).trim(); + if (line.isEmpty()) + { + throw new SignException("Empty line"); + } + final String[] split = line.split("[ :]+"); + + if (split.length == 2) + { + final Double money = getMoney(split[0]); + final Double amount = getDouble(split[1]); + if (money != null && amount != null) + { + sign.setLine(index, Util.formatCurrency(money) + ":" + Util.formatCurrency(amount + value).substring(1)); + return; + } + } + + if (split.length == 3) + { + final int stackamount = getInteger(split[0]); + final ItemStack item = getItemStack(split[1], stackamount); + int amount = getInteger(split[2]); + sign.setLine(index, stackamount + " " + split[1] + ":" + (amount + Math.round(value))); + return; + } + throw new SignException(Util.format("invalidSignLine", index)); + } }