From 9625680d97460d854bd2b4d79171d8223d357a4a Mon Sep 17 00:00:00 2001 From: Justin Kaeser Date: Wed, 9 Jan 2013 01:50:44 +0100 Subject: [PATCH 1/3] Make chestshop buying transactions fail if money deposit on seller's account fails. Some economy plugins, such as Gringotts, only allow a limited amount of money in an account. Thus, deposits to an account can fail. Chestshop would ignore the failure, which could cause a seller not to receive the payment for selling an item. This commit fixes the problem by canceling a transaction if the seller is not able to receive the money of a sale. --- .../ChestShop/Configuration/Messages.java | 3 +++ com/Acrobot/ChestShop/Economy/Economy.java | 8 ++++---- .../ChestShop/Economy/EconomyManager.java | 6 ++++-- com/Acrobot/ChestShop/Economy/Register.java | 8 ++++---- com/Acrobot/ChestShop/Economy/Vault.java | 8 ++++---- .../ChestShop/Events/PreTransactionEvent.java | 2 ++ .../PreTransaction/ErrorMessageSender.java | 17 ++++++++++++++++- .../PartialTransactionModule.java | 11 +++++++++++ 8 files changed, 48 insertions(+), 15 deletions(-) diff --git a/com/Acrobot/ChestShop/Configuration/Messages.java b/com/Acrobot/ChestShop/Configuration/Messages.java index 60baccd..2591187 100644 --- a/com/Acrobot/ChestShop/Configuration/Messages.java +++ b/com/Acrobot/ChestShop/Configuration/Messages.java @@ -17,6 +17,9 @@ public class Messages { public static String NOT_ENOUGH_MONEY = "You don't have enough money!"; public static String NOT_ENOUGH_MONEY_SHOP = "Shop owner doesn't have enough money!"; + + public static String DEPOSIT_FAILED = "Money deposit to shop owner failed!"; + public static String DEPOSIT_FAILED_OWNER = "Money deposit to your account failed, shop transaction aborted!"; public static byte NEWLINE_NOT_ENOUGH_MONEY_SHO; /////////////////////////////////////////////////// diff --git a/com/Acrobot/ChestShop/Economy/Economy.java b/com/Acrobot/ChestShop/Economy/Economy.java index b8b2ef5..cdebfb1 100644 --- a/com/Acrobot/ChestShop/Economy/Economy.java +++ b/com/Acrobot/ChestShop/Economy/Economy.java @@ -31,7 +31,7 @@ public class Economy { return ChestShopSign.isAdminShop(acc); } - public static void add(String name, double amount) { + public static boolean add(String name, double amount) { if (isServerAccount(name) && !getServerAccountName().isEmpty()) { name = getServerAccountName(); } @@ -46,19 +46,19 @@ public class Economy { amount -= tax; } - manager.add(uName.getName(name), amount); + return manager.add(uName.getName(name), amount); } public static double getTax(float tax, double price) { return NumberUtil.roundDown((tax / 100F) * price); } - public static void subtract(String name, double amount) { + public static boolean subtract(String name, double amount) { if (isServerAccount(name) && !getServerAccountName().isEmpty()) { name = getServerAccountName(); } - manager.subtract(uName.getName(name), roundUp(amount)); + return manager.subtract(uName.getName(name), roundUp(amount)); } public static boolean hasEnough(String name, double amount) { diff --git a/com/Acrobot/ChestShop/Economy/EconomyManager.java b/com/Acrobot/ChestShop/Economy/EconomyManager.java index 896b19d..e0056b4 100644 --- a/com/Acrobot/ChestShop/Economy/EconomyManager.java +++ b/com/Acrobot/ChestShop/Economy/EconomyManager.java @@ -11,12 +11,14 @@ public class EconomyManager { return false; } - public void add(String player, double amount) { + public boolean add(String player, double amount) { printError(); + return false; } - public void subtract(String player, double amount) { + public boolean subtract(String player, double amount) { printError(); + return false; } public boolean hasEnough(String player, double amount) { diff --git a/com/Acrobot/ChestShop/Economy/Register.java b/com/Acrobot/ChestShop/Economy/Register.java index ede7a15..8ac300e 100644 --- a/com/Acrobot/ChestShop/Economy/Register.java +++ b/com/Acrobot/ChestShop/Economy/Register.java @@ -16,12 +16,12 @@ public class Register extends EconomyManager { return method.hasAccount(player); } - public void add(String player, double amount) { - method.getAccount(player).add(amount); + public boolean add(String player, double amount) { + return method.getAccount(player).add(amount); } - public void subtract(String player, double amount) { - method.getAccount(player).subtract(amount); + public boolean subtract(String player, double amount) { + return method.getAccount(player).subtract(amount); } public boolean hasEnough(String player, double amount) { diff --git a/com/Acrobot/ChestShop/Economy/Vault.java b/com/Acrobot/ChestShop/Economy/Vault.java index 9094b42..3c491a4 100644 --- a/com/Acrobot/ChestShop/Economy/Vault.java +++ b/com/Acrobot/ChestShop/Economy/Vault.java @@ -13,12 +13,12 @@ public class Vault extends EconomyManager { return vaultPlugin.hasAccount(player); } - public void add(String player, double amount) { - vaultPlugin.depositPlayer(player, amount); + public boolean add(String player, double amount) { + return vaultPlugin.depositPlayer(player, amount).transactionSuccess(); } - public void subtract(String player, double amount) { - vaultPlugin.withdrawPlayer(player, amount); + public boolean subtract(String player, double amount) { + return vaultPlugin.withdrawPlayer(player, amount).transactionSuccess(); } public boolean hasEnough(String player, double amount) { diff --git a/com/Acrobot/ChestShop/Events/PreTransactionEvent.java b/com/Acrobot/ChestShop/Events/PreTransactionEvent.java index 416493d..b47330c 100644 --- a/com/Acrobot/ChestShop/Events/PreTransactionEvent.java +++ b/com/Acrobot/ChestShop/Events/PreTransactionEvent.java @@ -125,6 +125,8 @@ public class PreTransactionEvent extends Event { CLIENT_DOES_NOT_HAVE_ENOUGH_MONEY, SHOP_DOES_NOT_HAVE_ENOUGH_MONEY, + /** not enough space in shop's account for money deposit on sell */ + SHOP_DEPOSIT_FAILED, NOT_ENOUGH_SPACE_IN_CHEST, NOT_ENOUGH_SPACE_IN_INVENTORY, diff --git a/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java b/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java index a1a75a6..9c09f15 100644 --- a/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java +++ b/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java @@ -14,7 +14,7 @@ import org.bukkit.inventory.ItemStack; import java.util.LinkedList; import java.util.List; -import static com.Acrobot.ChestShop.Configuration.Messages.NOT_ENOUGH_STOCK_IN_YOUR_SHOP; +import static com.Acrobot.ChestShop.Configuration.Messages.*; /** * @author Acrobot @@ -58,12 +58,27 @@ public class ErrorMessageSender implements Listener { sendMessageToOwner(event.getOwner(), messageOutOfStock); message = Messages.NOT_ENOUGH_STOCK; break; + case SHOP_DEPOSIT_FAILED: + String messageDepositFailed = Messages.prefix(DEPOSIT_FAILED); + sendMessageToOwner(event.getOwner(), messageDepositFailed); + message = DEPOSIT_FAILED; + break; case SHOP_IS_RESTRICTED: message = Messages.ACCESS_DENIED; break; case INVALID_SHOP: message = Messages.INVALID_SHOP_DETECTED; break; + case CREATIVE_MODE_PROTECTION: + break; + case OTHER: + break; + case SPAM_CLICKING_PROTECTION: + break; + case TRANSACTION_SUCCESFUL: + break; + default: + break; } if (message != null) { diff --git a/com/Acrobot/ChestShop/Listeners/PreTransaction/PartialTransactionModule.java b/com/Acrobot/ChestShop/Listeners/PreTransaction/PartialTransactionModule.java index cf79747..eb64331 100644 --- a/com/Acrobot/ChestShop/Listeners/PreTransaction/PartialTransactionModule.java +++ b/com/Acrobot/ChestShop/Listeners/PreTransaction/PartialTransactionModule.java @@ -47,6 +47,17 @@ public class PartialTransactionModule implements Listener { event.setPrice(amountAffordable * pricePerItem); event.setStock(getCountedItemStack(stock, amountAffordable)); } + + String seller = event.getOwner().getName(); + boolean added = Economy.add(seller, price); + if (added) { + // added amount successfully, owner has enough space for deposit + // --> undo the add, for some other event to actually do. + Economy.subtract(seller, price); + } else { + event.setCancelled(SHOP_DEPOSIT_FAILED); + return; + } stock = event.getStock(); From e12adad4d024fe00f79cf5c61ea9053e8833872c Mon Sep 17 00:00:00 2001 From: Justin Kaeser Date: Mon, 14 Jan 2013 22:09:41 +0100 Subject: [PATCH 2/3] normalize whitespace --- .../PreTransaction/ErrorMessageSender.java | 100 +++++++++--------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java b/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java index 9c09f15..91e6941 100644 --- a/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java +++ b/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java @@ -29,56 +29,56 @@ public class ErrorMessageSender implements Listener { String message = null; switch (event.getTransactionOutcome()) { - case SHOP_DOES_NOT_BUY_THIS_ITEM: - message = Messages.NO_BUYING_HERE; - break; - case SHOP_DOES_NOT_SELL_THIS_ITEM: - message = Messages.NO_SELLING_HERE; - break; - case CLIENT_DOES_NOT_HAVE_PERMISSION: - message = Messages.NO_PERMISSION; - break; - case CLIENT_DOES_NOT_HAVE_ENOUGH_MONEY: - message = Messages.NOT_ENOUGH_MONEY; - break; - case SHOP_DOES_NOT_HAVE_ENOUGH_MONEY: - message = Messages.NOT_ENOUGH_MONEY_SHOP; - break; - case NOT_ENOUGH_SPACE_IN_CHEST: - message = Messages.NOT_ENOUGH_SPACE_IN_CHEST; - break; - case NOT_ENOUGH_SPACE_IN_INVENTORY: - message = Messages.NOT_ENOUGH_SPACE_IN_INVENTORY; - break; - case NOT_ENOUGH_STOCK_IN_INVENTORY: - message = Messages.NOT_ENOUGH_ITEMS_TO_SELL; - break; - case NOT_ENOUGH_STOCK_IN_CHEST: - String messageOutOfStock = Messages.prefix(NOT_ENOUGH_STOCK_IN_YOUR_SHOP).replace("%material", getItemNames(event.getStock())); - sendMessageToOwner(event.getOwner(), messageOutOfStock); - message = Messages.NOT_ENOUGH_STOCK; - break; - case SHOP_DEPOSIT_FAILED: - String messageDepositFailed = Messages.prefix(DEPOSIT_FAILED); - sendMessageToOwner(event.getOwner(), messageDepositFailed); - message = DEPOSIT_FAILED; - break; - case SHOP_IS_RESTRICTED: - message = Messages.ACCESS_DENIED; - break; - case INVALID_SHOP: - message = Messages.INVALID_SHOP_DETECTED; - break; - case CREATIVE_MODE_PROTECTION: - break; - case OTHER: - break; - case SPAM_CLICKING_PROTECTION: - break; - case TRANSACTION_SUCCESFUL: - break; - default: - break; + case SHOP_DOES_NOT_BUY_THIS_ITEM: + message = Messages.NO_BUYING_HERE; + break; + case SHOP_DOES_NOT_SELL_THIS_ITEM: + message = Messages.NO_SELLING_HERE; + break; + case CLIENT_DOES_NOT_HAVE_PERMISSION: + message = Messages.NO_PERMISSION; + break; + case CLIENT_DOES_NOT_HAVE_ENOUGH_MONEY: + message = Messages.NOT_ENOUGH_MONEY; + break; + case SHOP_DOES_NOT_HAVE_ENOUGH_MONEY: + message = Messages.NOT_ENOUGH_MONEY_SHOP; + break; + case NOT_ENOUGH_SPACE_IN_CHEST: + message = Messages.NOT_ENOUGH_SPACE_IN_CHEST; + break; + case NOT_ENOUGH_SPACE_IN_INVENTORY: + message = Messages.NOT_ENOUGH_SPACE_IN_INVENTORY; + break; + case NOT_ENOUGH_STOCK_IN_INVENTORY: + message = Messages.NOT_ENOUGH_ITEMS_TO_SELL; + break; + case NOT_ENOUGH_STOCK_IN_CHEST: + String messageOutOfStock = Messages.prefix(NOT_ENOUGH_STOCK_IN_YOUR_SHOP).replace("%material", getItemNames(event.getStock())); + sendMessageToOwner(event.getOwner(), messageOutOfStock); + message = Messages.NOT_ENOUGH_STOCK; + break; + case SHOP_DEPOSIT_FAILED: + String messageDepositFailed = Messages.prefix(DEPOSIT_FAILED); + sendMessageToOwner(event.getOwner(), messageDepositFailed); + message = DEPOSIT_FAILED; + break; + case SHOP_IS_RESTRICTED: + message = Messages.ACCESS_DENIED; + break; + case INVALID_SHOP: + message = Messages.INVALID_SHOP_DETECTED; + break; + case CREATIVE_MODE_PROTECTION: + break; + case OTHER: + break; + case SPAM_CLICKING_PROTECTION: + break; + case TRANSACTION_SUCCESFUL: + break; + default: + break; } if (message != null) { From 8b4ed86c76ba58a2ce9ea1cd73aa47ea0c2a9e39 Mon Sep 17 00:00:00 2001 From: Justin Kaeser Date: Mon, 14 Jan 2013 22:18:32 +0100 Subject: [PATCH 3/3] change mixed tab/space to space only --- .../PreTransaction/ErrorMessageSender.java | 100 +++++++++--------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java b/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java index 91e6941..50b24e4 100644 --- a/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java +++ b/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java @@ -29,56 +29,56 @@ public class ErrorMessageSender implements Listener { String message = null; switch (event.getTransactionOutcome()) { - case SHOP_DOES_NOT_BUY_THIS_ITEM: - message = Messages.NO_BUYING_HERE; - break; - case SHOP_DOES_NOT_SELL_THIS_ITEM: - message = Messages.NO_SELLING_HERE; - break; - case CLIENT_DOES_NOT_HAVE_PERMISSION: - message = Messages.NO_PERMISSION; - break; - case CLIENT_DOES_NOT_HAVE_ENOUGH_MONEY: - message = Messages.NOT_ENOUGH_MONEY; - break; - case SHOP_DOES_NOT_HAVE_ENOUGH_MONEY: - message = Messages.NOT_ENOUGH_MONEY_SHOP; - break; - case NOT_ENOUGH_SPACE_IN_CHEST: - message = Messages.NOT_ENOUGH_SPACE_IN_CHEST; - break; - case NOT_ENOUGH_SPACE_IN_INVENTORY: - message = Messages.NOT_ENOUGH_SPACE_IN_INVENTORY; - break; - case NOT_ENOUGH_STOCK_IN_INVENTORY: - message = Messages.NOT_ENOUGH_ITEMS_TO_SELL; - break; - case NOT_ENOUGH_STOCK_IN_CHEST: - String messageOutOfStock = Messages.prefix(NOT_ENOUGH_STOCK_IN_YOUR_SHOP).replace("%material", getItemNames(event.getStock())); - sendMessageToOwner(event.getOwner(), messageOutOfStock); - message = Messages.NOT_ENOUGH_STOCK; - break; - case SHOP_DEPOSIT_FAILED: - String messageDepositFailed = Messages.prefix(DEPOSIT_FAILED); - sendMessageToOwner(event.getOwner(), messageDepositFailed); - message = DEPOSIT_FAILED; - break; - case SHOP_IS_RESTRICTED: - message = Messages.ACCESS_DENIED; - break; - case INVALID_SHOP: - message = Messages.INVALID_SHOP_DETECTED; - break; - case CREATIVE_MODE_PROTECTION: - break; - case OTHER: - break; - case SPAM_CLICKING_PROTECTION: - break; - case TRANSACTION_SUCCESFUL: - break; - default: - break; + case SHOP_DOES_NOT_BUY_THIS_ITEM: + message = Messages.NO_BUYING_HERE; + break; + case SHOP_DOES_NOT_SELL_THIS_ITEM: + message = Messages.NO_SELLING_HERE; + break; + case CLIENT_DOES_NOT_HAVE_PERMISSION: + message = Messages.NO_PERMISSION; + break; + case CLIENT_DOES_NOT_HAVE_ENOUGH_MONEY: + message = Messages.NOT_ENOUGH_MONEY; + break; + case SHOP_DOES_NOT_HAVE_ENOUGH_MONEY: + message = Messages.NOT_ENOUGH_MONEY_SHOP; + break; + case NOT_ENOUGH_SPACE_IN_CHEST: + message = Messages.NOT_ENOUGH_SPACE_IN_CHEST; + break; + case NOT_ENOUGH_SPACE_IN_INVENTORY: + message = Messages.NOT_ENOUGH_SPACE_IN_INVENTORY; + break; + case NOT_ENOUGH_STOCK_IN_INVENTORY: + message = Messages.NOT_ENOUGH_ITEMS_TO_SELL; + break; + case NOT_ENOUGH_STOCK_IN_CHEST: + String messageOutOfStock = Messages.prefix(NOT_ENOUGH_STOCK_IN_YOUR_SHOP).replace("%material", getItemNames(event.getStock())); + sendMessageToOwner(event.getOwner(), messageOutOfStock); + message = Messages.NOT_ENOUGH_STOCK; + break; + case SHOP_DEPOSIT_FAILED: + String messageDepositFailed = Messages.prefix(DEPOSIT_FAILED); + sendMessageToOwner(event.getOwner(), messageDepositFailed); + message = DEPOSIT_FAILED; + break; + case SHOP_IS_RESTRICTED: + message = Messages.ACCESS_DENIED; + break; + case INVALID_SHOP: + message = Messages.INVALID_SHOP_DETECTED; + break; + case CREATIVE_MODE_PROTECTION: + break; + case OTHER: + break; + case SPAM_CLICKING_PROTECTION: + break; + case TRANSACTION_SUCCESFUL: + break; + default: + break; } if (message != null) {