From 49bdf5719e3e61dfd309489e3cc51327405d4d71 Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 13 Jun 2011 15:05:31 +0200 Subject: [PATCH] Trade sign --- .../src/com/earth2me/essentials/IUser.java | 2 + .../src/com/earth2me/essentials/Teleport.java | 22 +- .../src/com/earth2me/essentials/Trade.java | 70 +++- .../essentials/signs/EssentialsSign.java | 361 +++++++++++------- .../earth2me/essentials/signs/SignBuy.java | 23 +- .../earth2me/essentials/signs/SignFree.java | 7 +- .../earth2me/essentials/signs/SignHeal.java | 6 +- .../earth2me/essentials/signs/SignSell.java | 17 +- .../earth2me/essentials/signs/SignTime.java | 6 +- .../earth2me/essentials/signs/SignTrade.java | 66 ++++ .../earth2me/essentials/signs/SignWarp.java | 6 +- 11 files changed, 382 insertions(+), 204 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/signs/SignTrade.java diff --git a/Essentials/src/com/earth2me/essentials/IUser.java b/Essentials/src/com/earth2me/essentials/IUser.java index 4cb69d7d7..cfed0915f 100644 --- a/Essentials/src/com/earth2me/essentials/IUser.java +++ b/Essentials/src/com/earth2me/essentials/IUser.java @@ -33,6 +33,8 @@ public interface IUser void takeMoney(double value); + void giveMoney(double value); + PlayerInventory getInventory(); void updateInventory(); diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java index 535e6e088..e63d1699e 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -48,11 +48,11 @@ public class Teleport implements Runnable private long initY; private long initZ; private Target teleportTarget; - private Charge chargeFor; + private Trade chargeFor; private final IEssentials ess; private static final Logger logger = Logger.getLogger("Minecraft"); - private void initTimer(long delay, Target target, Charge chargeFor) + private void initTimer(long delay, Target target, Trade chargeFor) { this.started = System.currentTimeMillis(); this.delay = delay; @@ -118,12 +118,12 @@ public class Teleport implements Runnable this.ess = ess; } - public void respawn(Spawn spawn, Charge chargeFor) throws Exception + public void respawn(Spawn spawn, Trade chargeFor) throws Exception { teleport(new Target(spawn.getSpawn(user.getGroup())), chargeFor); } - public void warp(String warp, Charge chargeFor) throws Exception + public void warp(String warp, Trade chargeFor) throws Exception { Location loc = ess.getWarps().getWarp(warp); teleport(new Target(loc), chargeFor); @@ -177,17 +177,17 @@ public class Teleport implements Runnable cancel(false); } - public void teleport(Location loc, Charge chargeFor) throws Exception + public void teleport(Location loc, Trade chargeFor) throws Exception { teleport(new Target(loc), chargeFor); } - public void teleport(Entity entity, Charge chargeFor) throws Exception + public void teleport(Entity entity, Trade chargeFor) throws Exception { teleport(new Target(entity), chargeFor); } - private void teleport(Target target, Charge chargeFor) throws Exception + private void teleport(Target target, Trade chargeFor) throws Exception { double delay = ess.getSettings().getTeleportDelay(); @@ -230,7 +230,7 @@ public class Teleport implements Runnable now(new Target(loc)); } - public void now(Location loc, Charge chargeFor) throws Exception + public void now(Location loc, Trade chargeFor) throws Exception { cooldown(false); chargeFor.charge(user); @@ -243,7 +243,7 @@ public class Teleport implements Runnable now(new Target(entity)); } - public void back(Charge chargeFor) throws Exception + public void back(Trade chargeFor) throws Exception { teleport(new Target(user.getLastLocation()), chargeFor); } @@ -253,12 +253,12 @@ public class Teleport implements Runnable back(null); } - public void home(Charge chargeFor) throws Exception + public void home(Trade chargeFor) throws Exception { home(user, chargeFor); } - public void home(IUser user, Charge chargeFor) throws Exception + public void home(IUser user, Trade chargeFor) throws Exception { Location loc = user.getHome(this.user.getLocation()); if (loc == null) diff --git a/Essentials/src/com/earth2me/essentials/Trade.java b/Essentials/src/com/earth2me/essentials/Trade.java index a84bfa918..b4570d780 100644 --- a/Essentials/src/com/earth2me/essentials/Trade.java +++ b/Essentials/src/com/earth2me/essentials/Trade.java @@ -1,52 +1,53 @@ package com.earth2me.essentials; +import java.util.Map; import org.bukkit.inventory.ItemStack; -public class Charge +public class Trade { private final transient String command; - private final transient Double costs; - private final transient ItemStack items; + private final transient Double money; + private final transient ItemStack itemStack; private final transient IEssentials ess; - public Charge(final String command, final IEssentials ess) + public Trade(final String command, final IEssentials ess) { this(command, null, null, ess); } - public Charge(final double money, final IEssentials ess) + public Trade(final double money, final IEssentials ess) { this(null, money, null, ess); } - public Charge(final ItemStack items, final IEssentials ess) + public Trade(final ItemStack items, final IEssentials ess) { this(null, null, items, ess); } - private Charge(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.costs = money; - this.items = item; + this.money = money; + this.itemStack = item; this.ess = ess; } public void isAffordableFor(final IUser user) throws ChargeException { final double mon = user.getMoney(); - if (costs != null - && mon < costs + if (getMoney() != null + && mon < getMoney() && !user.isAuthorized("essentials.eco.loan")) { throw new ChargeException(Util.i18n("notEnoughMoney")); } - if (items != null - && !InventoryWorkaround.containsItem(user.getInventory(), true, items)) + if (getItemStack() != null + && !InventoryWorkaround.containsItem(user.getInventory(), true, itemStack)) { - throw new ChargeException(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " "))); + throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase().replace("_", " "))); } if (command != null && !command.isEmpty() @@ -59,24 +60,41 @@ public class Charge } } + public void pay(final IUser user) + { + if (getMoney() != null) + { + user.giveMoney(getMoney()); + } + if (getItemStack() != null) + { + final Map leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack()); + for (ItemStack itemStack : leftOver.values()) + { + InventoryWorkaround.dropItem(user.getLocation(), itemStack); + } + user.updateInventory(); + } + } + public void charge(final IUser user) throws ChargeException { - if (costs != null) + if (getMoney() != null) { final double mon = user.getMoney(); - if (mon < costs && !user.isAuthorized("essentials.eco.loan")) + if (mon < getMoney() && !user.isAuthorized("essentials.eco.loan")) { throw new ChargeException(Util.i18n("notEnoughMoney")); } - user.takeMoney(costs); + user.takeMoney(getMoney()); } - if (items != null) + if (getItemStack() != null) { - if (!InventoryWorkaround.containsItem(user.getInventory(), true, items)) + if (!InventoryWorkaround.containsItem(user.getInventory(), true, itemStack)) { - throw new ChargeException(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " "))); + throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase().replace("_", " "))); } - InventoryWorkaround.removeItem(user.getInventory(), true, items); + InventoryWorkaround.removeItem(user.getInventory(), true, getItemStack()); user.updateInventory(); } if (command != null && !command.isEmpty() @@ -92,4 +110,14 @@ public class Charge user.takeMoney(cost); } } + + public Double getMoney() + { + return money; + } + + public ItemStack getItemStack() + { + return itemStack; + } } diff --git a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java index 83563973c..4fec56f04 100644 --- a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java +++ b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.signs; -import com.earth2me.essentials.Charge; +import com.earth2me.essentials.Trade; import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.ItemDb; @@ -36,21 +36,24 @@ public class EssentialsSign { return false; } - boolean ret; try { - ret = onSignCreate(sign, user, getUsername(user), ess); + final boolean ret = onSignCreate(sign, user, getUsername(user), ess); + if (ret) + { + sign.setLine(0, String.format(FORMAT_SUCCESS, this.signName)); + } + return ret; + } + catch (ChargeException ex) + { + ess.showError(user, ex, signName); } catch (SignException ex) { ess.showError(user, ex, signName); - ret = false; } - if (ret) - { - sign.setLine(0, String.format(FORMAT_SUCCESS, this.signName)); - } - return ret; + return false; } private String getUsername(final User user) @@ -97,7 +100,7 @@ public class EssentialsSign } } - protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException + protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException { return true; } @@ -112,50 +115,189 @@ public class EssentialsSign return true; } - protected final void validateCharge(final ISign sign, final int index) throws SignException + protected final void validateTrade(final ISign sign, final int index, final IEssentials ess) throws SignException { final String line = sign.getLine(index).trim(); if (line.isEmpty()) { return; } - - final boolean isMoney = line.matches("^[^0-9-][\\.0-9]+"); - if (isMoney) + final Trade trade = getTrade(sign, index, 0, ess); + final Double money = trade.getMoney(); + if (money != null) { - final double quantity = Double.parseDouble(line.substring(1)); - if (quantity <= 0) + sign.setLine(index, Util.formatCurrency(money)); + } + } + + 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, Util.formatCurrency(quantity)); + sign.setLine(index, amount + " " + split[1] + ":0"); + return; } - else + + if (split.length == 3 && amountNeeded) { - final String[] split = line.split("[ :-]+", 2); - if (split.length != 2) + 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("invalidCharge")); + throw new SignException(Util.i18n("moreThanZero")); } - try + 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) { - final int quantity = Integer.parseInt(split[0]); - if (quantity <= 1) - { - throw new SignException(Util.i18n("moreThanZero")); - } - final String item = split[1].toLowerCase(); - if (!item.equalsIgnoreCase("times")) - { - getItemStack(item); - } - sign.setLine(index, quantity + " " + item); - } - catch (NumberFormatException ex) - { - throw new SignException(Util.i18n("invalidCharge"), ex); + 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 + { + final Trade trade = getTrade(sign, amountIndex, itemIndex, player, ess); + final ItemStack item = trade.getItemStack(); + sign.setLine(amountIndex, Integer.toString(item.getAmount())); + sign.setLine(itemIndex, sign.getLine(itemIndex).trim()); + } + + protected final Trade getTrade(final ISign sign, final int amountIndex, final int itemIndex, + final User player, final IEssentials ess) throws SignException + { + + final ItemStack item = getItemStack(sign.getLine(itemIndex), 1); + final int amount = Math.min(getInteger(sign.getLine(amountIndex)), item.getType().getMaxStackSize() * player.getInventory().getSize()); + if (item.getTypeId() == 0 || amount < 1) + { + throw new SignException(Util.i18n("moreThanZero")); + } + item.setAmount(amount); + return new Trade(item, ess); } protected final void validateInteger(final ISign sign, final int index) throws SignException @@ -186,26 +328,13 @@ public class EssentialsSign } } - protected final void validateItem(final ISign sign, final int index, final boolean noair) throws SignException - { - final String line = sign.getLine(index).trim(); - if (line.isEmpty()) - { - throw new SignException("Empty line " + index); - } - ItemStack item = getItemStack(line); - if (noair && item.getTypeId() == 0) - { - throw new SignException("Don't sell air."); - } - sign.setLine(index, line); - } - - protected final ItemStack getItemStack(final String itemName) throws SignException + protected final ItemStack getItemStack(final String itemName, final int quantity) throws SignException { try { - return ItemDb.get(itemName); + final ItemStack item = ItemDb.get(itemName); + item.setAmount(quantity); + return item; } catch (Exception ex) { @@ -213,99 +342,69 @@ public class EssentialsSign } } - protected final void validateMoney(final ISign sign, final int index) throws SignException + private 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 + { + try + { + final double quantity = Double.parseDouble(line); + if (quantity <= 0.0) + { + throw new SignException(Util.i18n("moreThanZero")); + } + return quantity; + } + catch (NumberFormatException ex) + { + throw new SignException(ex.getMessage(), ex); + } + } + + protected final Trade getTrade(final ISign sign, final int index, final IEssentials ess) throws SignException + { + return getTrade(sign, index, 1, ess); + } + + protected final Trade getTrade(final ISign sign, final int index, final int decrement, final IEssentials ess) throws SignException { final String line = sign.getLine(index).trim(); if (line.isEmpty()) { - throw new SignException("Empty line " + index); - } - final double quantity = getMoney(line); - sign.setLine(index, Util.formatCurrency(quantity)); - } - - protected final double getMoney(final String line) throws SignException - { - final boolean isMoney = line.matches("^[^0-9-][\\.0-9]+"); - if (isMoney) - { - try - { - final double quantity = Double.parseDouble(line.substring(1)); - if (quantity <= 0) - { - throw new SignException(Util.i18n("moreThanZero")); - } - return quantity; - } - catch (NumberFormatException ex) - { - throw new SignException(ex.getMessage(), ex); - } - } - else - { - throw new SignException("Invalid money"); - } - } - - protected final Charge getCharge(final ISign sign, final int index, final IEssentials ess) throws SignException - { - final String line = sign.getLine(index).trim(); - if (line.isEmpty()) - { - return new Charge(signName.toLowerCase() + "sign", ess); + return new Trade(signName.toLowerCase() + "sign", ess); } - final boolean isMoney = line.matches("^[^0-9-][\\.0-9]+"); - if (isMoney) + final Double money = getMoney(line); + if (money == null) { - try - { - final double quantity = Double.parseDouble(line.substring(1)); - if (quantity <= 0) - { - throw new SignException(Util.i18n("moreThanZero")); - } - return new Charge(quantity, ess); - } - catch (NumberFormatException ex) - { - throw new SignException(ex.getMessage(), ex); - } - } - else - { - final String[] split = line.split("[ :-]+", 2); + final String[] split = line.split("[ :]+", 2); if (split.length != 2) { throw new SignException(Util.i18n("invalidCharge")); } - try + final int quantity = getInteger(split[0]); + + final String item = split[1].toLowerCase(); + if (item.equalsIgnoreCase("times")) { - final int quantity = Integer.parseInt(split[0]); - if (quantity <= 1) - { - throw new SignException(Util.i18n("moreThanZero")); - } - final String item = split[1].toLowerCase(); - if (item.equalsIgnoreCase("times")) - { - sign.setLine(index, (quantity - 1) + " times"); - return new Charge(signName.toLowerCase() + "sign", ess); - } - else - { - final ItemStack stack = getItemStack(item); - stack.setAmount(quantity); - return new Charge(quantity, ess); - } + sign.setLine(index, (quantity - decrement) + " times"); + return new Trade(signName.toLowerCase() + "sign", ess); } - catch (NumberFormatException ex) + else { - throw new SignException(Util.i18n("invalidCharge"), ex); + final ItemStack stack = getItemStack(item, quantity); + sign.setLine(index, quantity + " " + item); + return new Trade(quantity, ess); } } + else + { + return new Trade(money, ess); + } } diff --git a/Essentials/src/com/earth2me/essentials/signs/SignBuy.java b/Essentials/src/com/earth2me/essentials/signs/SignBuy.java index f0200cace..21dc7683e 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignBuy.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignBuy.java @@ -1,12 +1,9 @@ package com.earth2me.essentials.signs; -import com.earth2me.essentials.Charge; +import com.earth2me.essentials.Trade; import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.IEssentials; -import com.earth2me.essentials.InventoryWorkaround; import com.earth2me.essentials.User; -import java.util.Map; -import org.bukkit.inventory.ItemStack; public class SignBuy extends EssentialsSign @@ -19,26 +16,18 @@ public class SignBuy extends EssentialsSign @Override protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException { - validateInteger(sign, 1); - validateItem(sign, 2, true); - validateCharge(sign, 3); + validateTrade(sign, 1, 2, player, ess); + validateTrade(sign, 3, ess); return true; } @Override protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException { - final ItemStack item = getItemStack(sign.getLine(2)); - final int amount = Math.min(getInteger(sign.getLine(1)), item.getType().getMaxStackSize()*player.getInventory().getSize()); - item.setAmount(amount); - final Charge charge = getCharge(sign, 3, ess); + final Trade items = getTrade(sign, 1, 2, player, ess); + final Trade charge = getTrade(sign, 3, ess); charge.isAffordableFor(player); - final Map leftOver = InventoryWorkaround.addItem(player.getInventory(), true, item); - for (ItemStack itemStack : leftOver.values()) - { - InventoryWorkaround.dropItem(player.getLocation(), itemStack); - } - player.updateInventory(); + items.pay(player); charge.charge(player); return true; } diff --git a/Essentials/src/com/earth2me/essentials/signs/SignFree.java b/Essentials/src/com/earth2me/essentials/signs/SignFree.java index dfdb58bd5..6c45c8b07 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignFree.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignFree.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.signs; -import com.earth2me.essentials.Charge; +import com.earth2me.essentials.Trade; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.ItemDb; import com.earth2me.essentials.User; @@ -19,17 +19,16 @@ public class SignFree extends EssentialsSign @Override protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException { - getItemStack(sign.getLine(1)); + getItemStack(sign.getLine(1), 9 * 4 * 64); return true; } @Override protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException { - final ItemStack item = getItemStack(sign.getLine(1)); + final ItemStack item = getItemStack(sign.getLine(1), 9 * 4 * 64); final CraftInventoryPlayer inv = new CraftInventoryPlayer(new InventoryPlayer(player.getHandle())); inv.clear(); - item.setAmount(9 * 4 * 64); inv.addItem(item); player.showInventory(inv); return true; diff --git a/Essentials/src/com/earth2me/essentials/signs/SignHeal.java b/Essentials/src/com/earth2me/essentials/signs/SignHeal.java index 4426c9720..190dfe07c 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignHeal.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignHeal.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.signs; -import com.earth2me.essentials.Charge; +import com.earth2me.essentials.Trade; import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; @@ -17,14 +17,14 @@ public class SignHeal extends EssentialsSign @Override protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException { - validateCharge(sign, 1); + validateTrade(sign, 1, ess); return true; } @Override protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException { - final Charge charge = getCharge(sign, 1, ess); + final Trade charge = getTrade(sign, 1, ess); charge.isAffordableFor(player); player.setHealth(20); player.sendMessage(Util.i18n("youAreHealed")); diff --git a/Essentials/src/com/earth2me/essentials/signs/SignSell.java b/Essentials/src/com/earth2me/essentials/signs/SignSell.java index 54a6e9838..2b74a78e6 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignSell.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignSell.java @@ -1,10 +1,9 @@ package com.earth2me.essentials.signs; -import com.earth2me.essentials.Charge; +import com.earth2me.essentials.Trade; import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; -import org.bukkit.inventory.ItemStack; public class SignSell extends EssentialsSign @@ -17,22 +16,18 @@ public class SignSell extends EssentialsSign @Override protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException { - validateInteger(sign, 1); - validateItem(sign, 2, true); - validateMoney(sign, 3); + validateTrade(sign, 1, 2, player, ess); + validateTrade(sign, 3, ess); return true; } @Override protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException { - final int amount = getInteger(sign.getLine(1)); - final ItemStack item = getItemStack(sign.getLine(2)); - item.setAmount(amount); - final double money = getMoney(sign.getLine(3)); - final Charge charge = new Charge(item, ess); + final Trade charge = getTrade(sign, 1, 2, player, ess); + final Trade money = getTrade(sign, 3, ess); charge.isAffordableFor(player); - player.giveMoney(money); + money.pay(player); charge.charge(player); return true; } diff --git a/Essentials/src/com/earth2me/essentials/signs/SignTime.java b/Essentials/src/com/earth2me/essentials/signs/SignTime.java index 845cfbcca..120347467 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignTime.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignTime.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.signs; -import com.earth2me.essentials.Charge; +import com.earth2me.essentials.Trade; import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; @@ -17,7 +17,7 @@ public class SignTime extends EssentialsSign @Override protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException { - validateCharge(sign, 2); + validateTrade(sign, 2, ess); final String timeString = sign.getLine(1); if ("Day".equalsIgnoreCase(timeString)) { @@ -35,7 +35,7 @@ public class SignTime extends EssentialsSign @Override protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException { - final Charge charge = getCharge(sign, 2, ess); + final Trade charge = getTrade(sign, 2, ess); charge.isAffordableFor(player); final String timeString = sign.getLine(1); long time = player.getWorld().getTime(); diff --git a/Essentials/src/com/earth2me/essentials/signs/SignTrade.java b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java new file mode 100644 index 000000000..2d5f1fcb2 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/signs/SignTrade.java @@ -0,0 +1,66 @@ +package com.earth2me.essentials.signs; + +import com.earth2me.essentials.ChargeException; +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.Trade; +import com.earth2me.essentials.User; + + +public class SignTrade extends EssentialsSign +{ + public SignTrade() + { + super("Trade"); + } + + @Override + protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException + { + validateTrade(sign, 1, false, ess); + validateTrade(sign, 2, true, ess); + final Trade charge = getTrade(sign, 2, true, ess); + charge.isAffordableFor(player); + sign.setLine(3, "ยง8" + username); + charge.charge(player); + return true; + } + + @Override + protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException + { + if (sign.getLine(3).substring(2).equalsIgnoreCase(username)) + { + final Trade stored = getTrade(sign, 1, true, ess); + substractAmount(sign, 1, stored); + stored.pay(player); + } + else + { + final Trade charge = getTrade(sign, 1, false, ess); + final Trade trade = getTrade(sign, 2, false, ess); + charge.isAffordableFor(player); + substractAmount(sign, 2, trade); + trade.pay(player); + addAmount(sign, 1, charge); + charge.charge(player); + } + return true; + } + + @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)) + { + final Trade stored1 = getTrade(sign, 1, true, ess); + final Trade stored2 = getTrade(sign, 2, true, ess); + stored1.pay(player); + stored2.pay(player); + return true; + } + else + { + return false; + } + } +} diff --git a/Essentials/src/com/earth2me/essentials/signs/SignWarp.java b/Essentials/src/com/earth2me/essentials/signs/SignWarp.java index 0851991ab..776d97924 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignWarp.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignWarp.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.signs; -import com.earth2me.essentials.Charge; +import com.earth2me.essentials.Trade; import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; @@ -16,7 +16,7 @@ public class SignWarp extends EssentialsSign @Override protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException { - validateCharge(sign, 3); + validateTrade(sign, 3, ess); final String warpName = sign.getLine(1); if (warpName.isEmpty()) @@ -53,7 +53,7 @@ public class SignWarp extends EssentialsSign || player.inGroup(group))) || (!ess.getSettings().getPerWarpPermission() || player.isAuthorized("essentials.warp." + warpName))) { - final Charge charge = getCharge(sign, 3, ess); + final Trade charge = getTrade(sign, 3, ess); try { player.getTeleport().warp(warpName, charge);