summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsnowleo <schneeleo@gmail.com>2011-06-12 22:33:47 +0200
committersnowleo <schneeleo@gmail.com>2011-06-12 22:33:47 +0200
commit0c4d4688f07d47196424542cc9bc9b0b13479c99 (patch)
treee5603c797b8ff38538e280368f3fd230d41d1abd
parente1a624b3e7b6b24638185df21ad8b7fc986e354c (diff)
downloadEssentials-0c4d4688f07d47196424542cc9bc9b0b13479c99.tar
Essentials-0c4d4688f07d47196424542cc9bc9b0b13479c99.tar.gz
Essentials-0c4d4688f07d47196424542cc9bc9b0b13479c99.tar.lz
Essentials-0c4d4688f07d47196424542cc9bc9b0b13479c99.tar.xz
Essentials-0c4d4688f07d47196424542cc9bc9b0b13479c99.zip
Sell & Buy Sign
-rw-r--r--Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java98
-rw-r--r--Essentials/src/com/earth2me/essentials/signs/SignBuy.java45
-rw-r--r--Essentials/src/com/earth2me/essentials/signs/SignSell.java39
3 files changed, 176 insertions, 6 deletions
diff --git a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java
index 3f87658e1..83563973c 100644
--- a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java
+++ b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java
@@ -114,7 +114,7 @@ public class EssentialsSign
protected final void validateCharge(final ISign sign, final int index) throws SignException
{
- final String line = sign.getLine(index);
+ final String line = sign.getLine(index).trim();
if (line.isEmpty())
{
return;
@@ -158,6 +158,49 @@ public class EssentialsSign
}
}
+ protected final void validateInteger(final ISign sign, final int index) throws SignException
+ {
+ final String line = sign.getLine(index).trim();
+ if (line.isEmpty())
+ {
+ throw new SignException("Empty line " + index);
+ }
+ final int quantity = getInteger(line);
+ sign.setLine(index, Integer.toString(quantity));
+ }
+
+ protected final int getInteger(final String line) throws SignException
+ {
+ try
+ {
+ final int quantity = Integer.parseInt(line);
+ if (quantity <= 1)
+ {
+ throw new SignException(Util.i18n("moreThanZero"));
+ }
+ return quantity;
+ }
+ catch (NumberFormatException ex)
+ {
+ throw new SignException("Invalid sign", ex);
+ }
+ }
+
+ 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
{
try
@@ -170,9 +213,45 @@ public class EssentialsSign
}
}
+ protected final void validateMoney(final ISign sign, final int index) 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);
+ final String line = sign.getLine(index).trim();
if (line.isEmpty())
{
return new Charge(signName.toLowerCase() + "sign", ess);
@@ -181,12 +260,19 @@ public class EssentialsSign
final boolean isMoney = line.matches("^[^0-9-][\\.0-9]+");
if (isMoney)
{
- final double quantity = Double.parseDouble(line.substring(1));
- if (quantity <= 0)
+ try
{
- throw new SignException(Util.i18n("moreThanZero"));
+ 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);
}
- return new Charge(quantity, ess);
}
else
{
diff --git a/Essentials/src/com/earth2me/essentials/signs/SignBuy.java b/Essentials/src/com/earth2me/essentials/signs/SignBuy.java
new file mode 100644
index 000000000..24e4cbade
--- /dev/null
+++ b/Essentials/src/com/earth2me/essentials/signs/SignBuy.java
@@ -0,0 +1,45 @@
+package com.earth2me.essentials.signs;
+
+import com.earth2me.essentials.Charge;
+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
+{
+ public SignBuy()
+ {
+ super("Buy");
+ }
+
+ @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);
+ 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 Charge charge = getCharge(sign, 3, ess);
+ charge.isAffordableFor(player);
+ final Map<Integer, ItemStack> leftOver = player.getInventory().addItem(item);
+ for (ItemStack itemStack : leftOver.values())
+ {
+ InventoryWorkaround.dropItem(player.getLocation(), itemStack);
+ }
+ player.updateInventory();
+ charge.charge(player);
+ return true;
+ }
+}
diff --git a/Essentials/src/com/earth2me/essentials/signs/SignSell.java b/Essentials/src/com/earth2me/essentials/signs/SignSell.java
new file mode 100644
index 000000000..54a6e9838
--- /dev/null
+++ b/Essentials/src/com/earth2me/essentials/signs/SignSell.java
@@ -0,0 +1,39 @@
+package com.earth2me.essentials.signs;
+
+import com.earth2me.essentials.Charge;
+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
+{
+ public SignSell()
+ {
+ super("Sell");
+ }
+
+ @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);
+ 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);
+ charge.isAffordableFor(player);
+ player.giveMoney(money);
+ charge.charge(player);
+ return true;
+ }
+}