From ba47b82ba13e06a5d321ab3ead0728f77f6d3257 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Sun, 24 Jan 2021 23:33:36 +0100 Subject: [PATCH] Make Admin Shops use containers if available (Resolves #402) Admin Shops can now be stocked the same way as player shops if a valid shop container is nearby. If no container is nearby it will just work as an unlimited shop. This behaviour can be disabled with the FORCE_UNLIMITED_ADMIN_SHOP config option so that an admin shop is unlimited even though a valid container is next to it. --- .../ChestShop/Configuration/Properties.java | 3 +++ .../Events/Protection/BuildPermissionEvent.java | 6 ++++-- .../ChestShop/Listeners/Block/Break/SignBreak.java | 6 +----- .../Listeners/Modules/StockCounterModule.java | 12 +++++------- .../ChestShop/Listeners/Player/PlayerInteract.java | 8 +++++--- .../PostTransaction/EmptyShopDeleter.java | 12 +++++++++--- .../Listeners/PreShopCreation/ChestChecker.java | 8 +++----- .../Listeners/PreShopCreation/TerrainChecker.java | 14 -------------- 8 files changed, 30 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java index bbb135c..6e1431a 100644 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java +++ b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java @@ -170,6 +170,9 @@ public class Properties { @ConfigurationComment("First line of your Admin Shop's sign should look like this:") public static String ADMIN_SHOP_NAME = "Admin Shop"; + @ConfigurationComment("Make all admin shops be unlimited even if they have a shop container at the sign") + public static boolean FORCE_UNLIMITED_ADMIN_SHOP = false; + @ConfigurationComment("The name of the economy account which Admin Shops should use and to which all taxes will go") public static String SERVER_ECONOMY_ACCOUNT = ""; diff --git a/src/main/java/com/Acrobot/ChestShop/Events/Protection/BuildPermissionEvent.java b/src/main/java/com/Acrobot/ChestShop/Events/Protection/BuildPermissionEvent.java index b1cd535..3cd13fd 100644 --- a/src/main/java/com/Acrobot/ChestShop/Events/Protection/BuildPermissionEvent.java +++ b/src/main/java/com/Acrobot/ChestShop/Events/Protection/BuildPermissionEvent.java @@ -6,6 +6,8 @@ import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import javax.annotation.Nullable; + /** * @author Acrobot */ @@ -17,7 +19,7 @@ public class BuildPermissionEvent extends Event implements Cancellable { private boolean allowed = true; - public BuildPermissionEvent(Player player, Location chest, Location sign) { + public BuildPermissionEvent(Player player, @Nullable Location chest, Location sign) { this.player = player; this.chest = chest; this.sign = sign; @@ -27,7 +29,7 @@ public class BuildPermissionEvent extends Event implements Cancellable { return player; } - public Location getChest() { + public @Nullable Location getChest() { return chest; } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java index 23c5398..2fb280f 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java @@ -165,11 +165,7 @@ public class SignBreak implements Listener { } public static void sendShopDestroyedEvent(Sign sign, Player player) { - Container connectedContainer = null; - - if (!ChestShopSign.isAdminShop(sign)) { - connectedContainer = uBlock.findConnectedContainer(sign.getBlock()); - } + Container connectedContainer = uBlock.findConnectedContainer(sign.getBlock()); Event event = new ShopDestroyedEvent(player, sign, connectedContainer); ChestShop.callEvent(event); diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java index 8490a66..a47abf0 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/StockCounterModule.java @@ -46,7 +46,8 @@ public class StockCounterModule implements Listener { event.setSignLine(QUANTITY_LINE, Integer.toString(quantity)); } - if (!Properties.USE_STOCK_COUNTER || ChestShopSign.isAdminShop(event.getSignLine(NAME_LINE))) { + if (!Properties.USE_STOCK_COUNTER + || (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(event.getSignLine(NAME_LINE)))) { return; } @@ -71,11 +72,8 @@ public class StockCounterModule implements Listener { } for (Sign shopSign : uBlock.findConnectedShopSigns(event.getInventory().getHolder())) { - if (ChestShopSign.isAdminShop(shopSign)) { - return; - } - - if (!Properties.USE_STOCK_COUNTER) { + if (!Properties.USE_STOCK_COUNTER + || (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(shopSign))) { if (QuantityUtil.quantityLineContainsCounter(shopSign.getLine(QUANTITY_LINE))) { removeCounterFromQuantityLine(shopSign); } @@ -111,7 +109,7 @@ public class StockCounterModule implements Listener { return; } - if (ChestShopSign.isAdminShop(event.getSign())) { + if (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(event.getSign())) { return; } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java index 840e207..c7a44d0 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java @@ -19,9 +19,7 @@ import com.Acrobot.ChestShop.Utils.uBlock; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; -import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.block.BlockState; import org.bukkit.block.Container; import org.bukkit.block.Sign; import org.bukkit.entity.Player; @@ -232,7 +230,11 @@ public class PlayerInteract implements Listener { ItemStack[] items = InventoryUtil.getItemsStacked(item); - if (adminShop) { + // Create virtual admin inventory if + // - it's an admin shop + // - there is no container for the shop sign + // - the config doesn't force unlimited admin shop stock + if (adminShop && (ownerInventory == null || Properties.FORCE_UNLIMITED_ADMIN_SHOP)) { ownerInventory = new AdminInventory(action == buy ? Arrays.stream(items).map(ItemStack::clone).toArray(ItemStack[]::new) : new ItemStack[0]); } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/EmptyShopDeleter.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/EmptyShopDeleter.java index bc4220b..befe03a 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/EmptyShopDeleter.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/EmptyShopDeleter.java @@ -27,9 +27,13 @@ public class EmptyShopDeleter implements Listener { return; } - Inventory ownerInventory = event.getOwnerInventory(); Sign sign = event.getSign(); - Container connectedContainer = uBlock.findConnectedContainer(sign); + + if (ChestShopSign.isAdminShop(sign)) { + return; + } + + Inventory ownerInventory = event.getOwnerInventory(); if (!shopShouldBeRemoved(ownerInventory, event.getStock())) { return; @@ -39,6 +43,8 @@ public class EmptyShopDeleter implements Listener { return; } + Container connectedContainer = uBlock.findConnectedContainer(sign); + ShopDestroyedEvent destroyedEvent = new ShopDestroyedEvent(null, event.getSign(), connectedContainer); ChestShop.callEvent(destroyedEvent); @@ -62,7 +68,7 @@ public class EmptyShopDeleter implements Listener { } private static boolean shopShouldBeRemoved(Inventory inventory, ItemStack[] stock) { - if (Properties.REMOVE_EMPTY_SHOPS && !ChestShopSign.isAdminShop(inventory)) { + if (Properties.REMOVE_EMPTY_SHOPS) { if (Properties.ALLOW_PARTIAL_TRANSACTIONS) { for (ItemStack itemStack : stock) { if (inventory.containsAtLeast(itemStack, 1)) { diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ChestChecker.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ChestChecker.java index 69b0073..00d28fd 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ChestChecker.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ChestChecker.java @@ -25,14 +25,12 @@ public class ChestChecker implements Listener { public static void onPreShopCreation(PreShopCreationEvent event) { String nameLine = event.getSignLine(NAME_LINE); - if (ChestShopSign.isAdminShop(nameLine)) { - return; - } - Container connectedContainer = uBlock.findConnectedContainer(event.getSign().getBlock()); if (connectedContainer == null) { - event.setOutcome(NO_CHEST); + if (!ChestShopSign.isAdminShop(nameLine)) { + event.setOutcome(NO_CHEST); + } return; } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/TerrainChecker.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/TerrainChecker.java index 9b4d65c..4f9b984 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/TerrainChecker.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/TerrainChecker.java @@ -3,9 +3,7 @@ package com.Acrobot.ChestShop.Listeners.PreShopCreation; import com.Acrobot.ChestShop.ChestShop; import com.Acrobot.ChestShop.Events.PreShopCreationEvent; import com.Acrobot.ChestShop.Events.Protection.BuildPermissionEvent; -import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.Security; -import com.Acrobot.ChestShop.Signs.ChestShopSign; import com.Acrobot.ChestShop.Utils.uBlock; import org.bukkit.Location; import org.bukkit.block.Container; @@ -14,8 +12,6 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.NO_PERMISSION_FOR_TERRAIN; -import static com.Acrobot.ChestShop.Permission.ADMIN; -import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE; /** * @author Acrobot @@ -24,18 +20,8 @@ public class TerrainChecker implements Listener { @EventHandler public static void onPreShopCreation(PreShopCreationEvent event) { - String nameLine = event.getSignLine(NAME_LINE); - - if (ChestShopSign.isAdminShop(nameLine)) { - return; - } - Player player = event.getPlayer(); - if (Permission.has(player, ADMIN)) { - return; - } - if (!Security.canPlaceSign(player, event.getSign())) { event.setOutcome(NO_PERMISSION_FOR_TERRAIN); return;