From 7ef93e7bc16e0c482c167695eab4ba979dd79ea0 Mon Sep 17 00:00:00 2001 From: Acrobot Date: Sun, 2 Sep 2012 22:41:24 +0200 Subject: [PATCH] Fiiiixeees --- com/Acrobot/ChestShop/Config/Property.java | 4 +-- com/Acrobot/ChestShop/Economy/Economy.java | 17 +++++------- .../PostTransaction/EconomicModule.java | 26 +++++++++++++++++-- .../PostTransaction/EmptyShopDeleter.java | 24 ++++++++++++----- .../ChestShop/Signs/ChestShopSign.java | 2 +- plugin.yml | 2 +- 6 files changed, 52 insertions(+), 23 deletions(-) diff --git a/com/Acrobot/ChestShop/Config/Property.java b/com/Acrobot/ChestShop/Config/Property.java index b06b34e..2418c73 100644 --- a/com/Acrobot/ChestShop/Config/Property.java +++ b/com/Acrobot/ChestShop/Config/Property.java @@ -16,8 +16,8 @@ public enum Property { ALLOW_SIGN_CHEST_OPEN(true, "Can shop's chest be opened by owner with right-clicking a shop's sign?"), ALLOW_LEFT_CLICK_DESTROYING(true, "If true, if you left-click your own shop sign you won't open chest's inventory, but instead you will start destroying the sign.\n"), - REMOVE_EMPTY_SHOPS(false, "If true, if the shop is empty, the sign is destroyed and put into the chest, so the shop isn't usable anymore.\n"), - REMOVE_EMPTY_CHESTS(false, "If true, if the REMOVE_EMPTY_SHOPS option is turned on, the chest is also destroyed."), + REMOVE_EMPTY_SHOPS(false, "If true, if the shop is empty, the sign is destroyed and put into the chest, so the shop isn't usable anymore."), + REMOVE_EMPTY_CHESTS(false, "If true, if the REMOVE_EMPTY_SHOPS option is turned on, the chest is also destroyed.\n"), ADMIN_SHOP_NAME("Admin Shop", "First line of your Admin Shop's sign should look like this"), SERVER_ECONOMY_ACCOUNT("", "The economy account which Admin Shops should use and to which all taxes will go"), diff --git a/com/Acrobot/ChestShop/Economy/Economy.java b/com/Acrobot/ChestShop/Economy/Economy.java index 5d10dcd..f55ff76 100644 --- a/com/Acrobot/ChestShop/Economy/Economy.java +++ b/com/Acrobot/ChestShop/Economy/Economy.java @@ -1,7 +1,9 @@ package com.Acrobot.ChestShop.Economy; +import com.Acrobot.Breeze.Utils.NumberUtil; import com.Acrobot.ChestShop.Config.Config; import com.Acrobot.ChestShop.Config.Property; +import com.Acrobot.ChestShop.Signs.ChestShopSign; import com.Acrobot.ChestShop.Utils.uName; import static com.Acrobot.Breeze.Utils.NumberUtil.roundUp; @@ -20,9 +22,9 @@ public class Economy { public static String getServerAccountName() { return Config.getString(Property.SERVER_ECONOMY_ACCOUNT); } - + public static boolean isServerAccount(String acc) { - return getServerAccountName().equals(acc); + return ChestShopSign.isAdminShop(acc); } public static void add(String name, double amount) { @@ -30,29 +32,24 @@ public class Economy { double tax = getTax(taxAmount, amount); if (tax != 0) { - if (!serverAccount().isEmpty()) { + if (!getServerAccountName().isEmpty()) { economy.add(getServerAccountName(), tax); } amount -= tax; } - - if (name.isEmpty()) return; + economy.add(uName.getName(name), amount); } public static double getTax(Property tax, double price) { - return roundDown((Config.getFloat(tax) / 100F) * price); + return NumberUtil.roundDown((Config.getFloat(tax) / 100F) * price); } public static void subtract(String name, double amount) { - if (name.isEmpty()) return; economy.subtract(uName.getName(name), roundUp(amount)); } public static boolean hasEnough(String name, double amount) { - if (isServerAccount(name)) { - return true; - } return economy.hasEnough(uName.getName(name), roundUp(amount)); } diff --git a/com/Acrobot/ChestShop/Listeners/PostTransaction/EconomicModule.java b/com/Acrobot/ChestShop/Listeners/PostTransaction/EconomicModule.java index 4aa40a2..bc50ea3 100644 --- a/com/Acrobot/ChestShop/Listeners/PostTransaction/EconomicModule.java +++ b/com/Acrobot/ChestShop/Listeners/PostTransaction/EconomicModule.java @@ -1,9 +1,13 @@ package com.Acrobot.ChestShop.Listeners.PostTransaction; +import com.Acrobot.ChestShop.Config.Config; +import com.Acrobot.ChestShop.Config.Property; +import com.Acrobot.ChestShop.Containers.AdminInventory; import com.Acrobot.ChestShop.Economy.Economy; import com.Acrobot.ChestShop.Events.TransactionEvent; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.inventory.Inventory; import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY; import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL; @@ -18,8 +22,11 @@ public class EconomicModule implements Listener { return; } + if (isOwnerEconomicalyActive(event)) { + Economy.add(event.getOwner().getName(), event.getPrice()); + } + Economy.subtract(event.getClient().getName(), event.getPrice()); - Economy.add(event.getOwner().getName(), event.getPrice()); } @EventHandler @@ -28,7 +35,22 @@ public class EconomicModule implements Listener { return; } - Economy.subtract(event.getOwner().getName(), event.getPrice()); + if (isOwnerEconomicalyActive(event)) { + Economy.subtract(event.getOwner().getName(), event.getPrice()); + } + Economy.add(event.getClient().getName(), event.getPrice()); } + + public static String getServerAccountName() { + return Config.getString(Property.SERVER_ECONOMY_ACCOUNT); + } + + public static boolean isServerShop(Inventory inventory) { + return inventory instanceof AdminInventory; + } + + public static boolean isOwnerEconomicalyActive(TransactionEvent event) { + return !isServerShop(event.getOwnerInventory()) || !getServerAccountName().isEmpty(); + } } diff --git a/com/Acrobot/ChestShop/Listeners/PostTransaction/EmptyShopDeleter.java b/com/Acrobot/ChestShop/Listeners/PostTransaction/EmptyShopDeleter.java index 66c0f5b..b592b32 100644 --- a/com/Acrobot/ChestShop/Listeners/PostTransaction/EmptyShopDeleter.java +++ b/com/Acrobot/ChestShop/Listeners/PostTransaction/EmptyShopDeleter.java @@ -24,8 +24,8 @@ public class EmptyShopDeleter implements Listener { return; } - if (shopShouldBeRemoved(event.getOwnerInventory())) { - if (Config.getBoolean(REMOVE_EMPTY_CHESTS) && !ChestShopSign.isAdminShop(event.getSign())) { + if (shopShouldBeRemoved(event.getOwnerInventory(), event.getStock())) { + if (Config.getBoolean(REMOVE_EMPTY_CHESTS) && !ChestShopSign.isAdminShop(event.getSign()) && chestIsEmpty(event.getOwnerInventory())) { Chest connectedChest = uBlock.findConnectedChest(event.getSign()); connectedChest.getBlock().setType(Material.AIR); } @@ -35,11 +35,7 @@ public class EmptyShopDeleter implements Listener { } } - private static boolean shopShouldBeRemoved(Inventory inventory) { - return Config.getBoolean(REMOVE_EMPTY_SHOPS) && isEmpty(inventory); - } - - public static boolean isEmpty(Inventory inventory) { + private static boolean chestIsEmpty(Inventory inventory) { for (ItemStack item : inventory.getContents()) { if (item != null) { return false; @@ -48,4 +44,18 @@ public class EmptyShopDeleter implements Listener { return true; } + + private static boolean shopShouldBeRemoved(Inventory inventory, ItemStack[] stock) { + return Config.getBoolean(REMOVE_EMPTY_SHOPS) && !hasMoreStock(inventory, stock); + } + + private static boolean hasMoreStock(Inventory inventory, ItemStack[] stock) { + for (ItemStack stack : stock) { + if (!inventory.contains(stack, 1)) { + return false; + } + } + + return true; + } } diff --git a/com/Acrobot/ChestShop/Signs/ChestShopSign.java b/com/Acrobot/ChestShop/Signs/ChestShopSign.java index 0d3e84d..8cf42c7 100644 --- a/com/Acrobot/ChestShop/Signs/ChestShopSign.java +++ b/com/Acrobot/ChestShop/Signs/ChestShopSign.java @@ -23,7 +23,7 @@ public class ChestShopSign { public static final Pattern[] SHOP_SIGN_PATTERN = { Pattern.compile("^[\\w ]*$"), Pattern.compile("[0-9]+"), - Pattern.compile("^[\\dbs(free) :]+$"), + Pattern.compile("(?i)^[\\dbs(free) :]+$"), Pattern.compile("[\\w : -]+") }; diff --git a/plugin.yml b/plugin.yml index 796e1ac..ebd6015 100644 --- a/plugin.yml +++ b/plugin.yml @@ -2,7 +2,7 @@ name: ChestShop main: com.Acrobot.ChestShop.ChestShop -version: 3.50t0004 +version: 3.50t0005 #for CButD dev-url: http://dev.bukkit.org/server-mods/chestshop/