From 29ef7f25aa3c2e54a3eedec5ccbd84a6de55b5ed Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Wed, 15 Nov 2017 21:13:07 +0100 Subject: [PATCH] Fix errors when server economy account is not defined or invalid --- .../Acrobot/ChestShop/Economy/Economy.java | 9 ++- .../Economy/ServerAccountCorrector.java | 64 ++++++------------- .../Listeners/Economy/TaxModule.java | 9 ++- .../PostShopCreation/CreationFeeGetter.java | 4 +- .../ShopRemoval/ShopRefundListener.java | 4 +- .../Acrobot/ChestShop/UUIDs/NameManager.java | 7 +- 6 files changed, 43 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/Acrobot/ChestShop/Economy/Economy.java b/src/main/java/com/Acrobot/ChestShop/Economy/Economy.java index 7ebaece..1a5b23a 100644 --- a/src/main/java/com/Acrobot/ChestShop/Economy/Economy.java +++ b/src/main/java/com/Acrobot/ChestShop/Economy/Economy.java @@ -7,6 +7,7 @@ import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent; import com.Acrobot.ChestShop.Events.Economy.CurrencyFormatEvent; import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent; import com.Acrobot.ChestShop.Signs.ChestShopSign; +import com.Acrobot.ChestShop.UUIDs.NameManager; import org.bukkit.World; import org.bukkit.inventory.Inventory; @@ -18,12 +19,18 @@ import java.util.UUID; * Economy management */ public class Economy { + /** + * Get the name of the server conomy account + * @return The username of te server economy account + * @deprecated Use {@link NameManager#getServerEconomyAccount()} or {@link Properties#SERVER_ECONOMY_ACCOUNT} + */ + @Deprecated public static String getServerAccountName() { return Properties.SERVER_ECONOMY_ACCOUNT; } public static boolean isOwnerEconomicallyActive(Inventory inventory) { - return !ChestShopSign.isAdminShop(inventory) || !getServerAccountName().isEmpty(); + return !ChestShopSign.isAdminShop(inventory) || NameManager.getServerEconomyAccount() != null; } public static boolean add(UUID name, World world, double amount) { diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/ServerAccountCorrector.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/ServerAccountCorrector.java index 618d3a8..4ab1773 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/ServerAccountCorrector.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/ServerAccountCorrector.java @@ -11,8 +11,6 @@ import org.bukkit.event.Listener; import java.math.BigDecimal; import java.util.UUID; -import static com.Acrobot.ChestShop.Configuration.Properties.SERVER_ECONOMY_ACCOUNT; - /** * @author Acrobot */ @@ -22,20 +20,12 @@ public class ServerAccountCorrector implements Listener { public static void onCurrencyAdd(CurrencyAddEvent event) { UUID target = event.getTarget(); - if (!NameManager.isAdminShop(target) || SERVER_ECONOMY_ACCOUNT.equals(NameManager.getUsername(target))) { + if (!NameManager.isAdminShop(target) || NameManager.isServerEconomyAccount(target)) { return; } - if (SERVER_ECONOMY_ACCOUNT.isEmpty()) { - event.setAdded(true); - return; - } else { - Account account = NameManager.getAccount(SERVER_ECONOMY_ACCOUNT); - if (account == null || account.getUuid() == null) { - return; - } - target = account.getUuid(); - } + Account account = NameManager.getServerEconomyAccount(); + target = account != null ? account.getUuid() : null; event.setAdded(true); if (target == null) { @@ -50,21 +40,12 @@ public class ServerAccountCorrector implements Listener { public static void onCurrencySubtract(CurrencySubtractEvent event) { UUID target = event.getTarget(); - if (!NameManager.isAdminShop(target) || SERVER_ECONOMY_ACCOUNT.equals(NameManager.getUsername(target))) { + if (!NameManager.isAdminShop(target) || NameManager.isServerEconomyAccount(target)) { return; } - - if (SERVER_ECONOMY_ACCOUNT.isEmpty()) { - event.setSubtracted(true); - return; - } else { - Account account = NameManager.getAccount(SERVER_ECONOMY_ACCOUNT); - if (account != null) { - target = account.getUuid(); - } else { - target = null; - } - } + + Account account = NameManager.getServerEconomyAccount(); + target = account != null ? account.getUuid() : null; event.setSubtracted(true); if (target == null) { @@ -78,21 +59,17 @@ public class ServerAccountCorrector implements Listener { @EventHandler(priority = EventPriority.LOWEST) public static void onCurrencyCheck(CurrencyCheckEvent event) { UUID target = event.getAccount(); - - if (!NameManager.isAdminShop(target) || SERVER_ECONOMY_ACCOUNT.equals(NameManager.getUsername(target))) { + + if (!NameManager.isAdminShop(target) || NameManager.isServerEconomyAccount(target)) { return; } - if (SERVER_ECONOMY_ACCOUNT.isEmpty()) { + Account account = NameManager.getServerEconomyAccount(); + target = account != null ? account.getUuid() : null; + + if (target == null) { event.hasEnough(true); return; - } else { - Account account = NameManager.getAccount(SERVER_ECONOMY_ACCOUNT); - if (account == null || account.getUuid() == null) { - event.hasEnough(true); - return; - } - target = account.getUuid(); } CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(event.getAmount(), target, event.getWorld()); @@ -104,8 +81,8 @@ public class ServerAccountCorrector implements Listener { @EventHandler(priority = EventPriority.LOWEST) public static void onCurrencyHoldCheck(CurrencyHoldEvent event) { UUID target = event.getAccount(); - - if (!NameManager.isAdminShop(target) || SERVER_ECONOMY_ACCOUNT.equals(NameManager.getUsername(target))) { + + if (!NameManager.isAdminShop(target) || NameManager.isServerEconomyAccount(target)) { return; } @@ -121,15 +98,12 @@ public class ServerAccountCorrector implements Listener { return; } - if (SERVER_ECONOMY_ACCOUNT.isEmpty()) { + Account account = NameManager.getServerEconomyAccount(); + target = account != null ? account.getUuid() : null; + + if (target == null) { event.setAmount(BigDecimal.valueOf(Double.MAX_VALUE)); return; - } else { - Account account = NameManager.getAccount(SERVER_ECONOMY_ACCOUNT); - if (account == null || account.getUuid() == null) { - return; - } - target = account.getUuid(); } CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(target, event.getWorld()); diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/TaxModule.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/TaxModule.java index 6e804aa..147735a 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/TaxModule.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/TaxModule.java @@ -33,7 +33,7 @@ public class TaxModule implements Listener { UUID target = event.getTarget(); - if (Economy.getServerAccountName().equals(NameManager.getUsername(target))) { + if (NameManager.isServerEconomyAccount(target)) { return; } @@ -45,8 +45,11 @@ public class TaxModule implements Listener { BigDecimal tax = getTax(event.getAmount(), taxAmount); - if (!Economy.getServerAccountName().isEmpty()) { - CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(tax, NameManager.getUUID(Economy.getServerAccountName()), event.getWorld()); + if (NameManager.getServerEconomyAccount() != null) { + CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent( + tax, + NameManager.getServerEconomyAccount().getUuid(), + event.getWorld()); ChestShop.callEvent(currencyAddEvent); } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PostShopCreation/CreationFeeGetter.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PostShopCreation/CreationFeeGetter.java index 5d1e793..8f234f5 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PostShopCreation/CreationFeeGetter.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PostShopCreation/CreationFeeGetter.java @@ -45,10 +45,10 @@ public class CreationFeeGetter implements Listener { CurrencySubtractEvent subtractionEvent = new CurrencySubtractEvent(BigDecimal.valueOf(shopCreationPrice), player); ChestShop.callEvent(subtractionEvent); - if (!Economy.getServerAccountName().isEmpty()) { + if (NameManager.getServerEconomyAccount() != null) { CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent( BigDecimal.valueOf(shopCreationPrice), - NameManager.getUUID(Economy.getServerAccountName()), + NameManager.getServerEconomyAccount().getUuid(), player.getWorld()); ChestShop.callEvent(currencyAddEvent); } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/ShopRemoval/ShopRefundListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/ShopRemoval/ShopRefundListener.java index cc1ae26..13921f8 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/ShopRemoval/ShopRefundListener.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/ShopRemoval/ShopRefundListener.java @@ -40,10 +40,10 @@ public class ShopRefundListener implements Listener { CurrencyAddEvent currencyEvent = new CurrencyAddEvent(BigDecimal.valueOf(refundPrice), account.getUuid(), event.getSign().getWorld()); ChestShop.callEvent(currencyEvent); - if (!Economy.getServerAccountName().isEmpty()) { + if (NameManager.getServerEconomyAccount() != null) { CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent( BigDecimal.valueOf(refundPrice), - NameManager.getUUID(Economy.getServerAccountName()), + NameManager.getServerEconomyAccount().getUuid(), event.getSign().getWorld()); ChestShop.callEvent(currencySubtractEvent); } diff --git a/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java b/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java index c2e4aeb..d2eccce 100644 --- a/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java +++ b/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java @@ -285,6 +285,7 @@ public class NameManager { if (!Properties.SERVER_ECONOMY_ACCOUNT.isEmpty()) { serverEconomyAccount = getAccount(Properties.SERVER_ECONOMY_ACCOUNT); if (serverEconomyAccount == null || serverEconomyAccount.getUuid() == null) { + serverEconomyAccount = null; ChestShop.getBukkitLogger().log(Level.WARNING, "Server economy account setting '" + Properties.SERVER_ECONOMY_ACCOUNT + "' doesn't seem to be the name of a known player! Please log in at least once in order for the server economy account to work."); } } @@ -292,7 +293,11 @@ public class NameManager { e.printStackTrace(); } } - + + public static Account getServerEconomyAccount() { + return serverEconomyAccount; + } + private static class SimpleLoadingCache { private final LinkedHashMap map;