From d097a15122f72a4979aa1458aba5159cb83af2d7 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 23 Nov 2016 16:05:33 +0100 Subject: [PATCH] Added way to disable WorldGuard/Towny integration + Added permission to use shops in WorldGuard regions that deny shop use --- .../de/epiceric/shopchest/config/Config.java | 8 +++++ .../listeners/ChestProtectListener.java | 4 +-- .../listeners/ShopInteractListener.java | 16 ++++----- .../listeners/WorldGuardListener.java | 34 ++++++++++--------- .../epiceric/shopchest/utils/Permissions.java | 1 + src/main/resources/config.yml | 8 +++++ src/main/resources/plugin.yml | 4 +++ 7 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/config/Config.java b/src/main/java/de/epiceric/shopchest/config/Config.java index c9b3fa5..4d95f18 100644 --- a/src/main/java/de/epiceric/shopchest/config/Config.java +++ b/src/main/java/de/epiceric/shopchest/config/Config.java @@ -74,6 +74,12 @@ public class Config { /** Whether the debug log file should be created **/ public boolean enable_debug_log; + /** Whether WorldGuard integration should be enabled **/ + public boolean enable_worldguard_integration; + + /** Whether Towny integration should be enabled **/ + public boolean enable_towny_integration; + /** Whether admin shops should be excluded of the shop limits **/ public boolean exclude_admin_shops; @@ -282,6 +288,8 @@ public class Config { two_line_prices = plugin.getConfig().getBoolean("two-line-prices"); enable_hologram_interaction = plugin.getConfig().getBoolean("enable-hologram-interaction"); enable_debug_log = plugin.getConfig().getBoolean("enable-debug-log"); + enable_worldguard_integration = plugin.getConfig().getBoolean("enable-worldguard-integration"); + enable_towny_integration = plugin.getConfig().getBoolean("enable-towny-integration"); explosion_protection = plugin.getConfig().getBoolean("explosion-protection"); exclude_admin_shops = plugin.getConfig().getBoolean("shop-limits.exclude-admin-shops"); append_potion_level_to_item_name = plugin.getConfig().getBoolean("append-potion-level-to-item-name"); diff --git a/src/main/java/de/epiceric/shopchest/listeners/ChestProtectListener.java b/src/main/java/de/epiceric/shopchest/listeners/ChestProtectListener.java index d091fe5..2b3e42b 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ChestProtectListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ChestProtectListener.java @@ -135,13 +135,13 @@ public class ChestProtectListener implements Listener { boolean externalPluginsAllowed = true; - if (plugin.hasWorldGuard()) { + if (plugin.hasWorldGuard() && plugin.getShopChestConfig().enable_worldguard_integration) { RegionContainer container = worldGuard.getRegionContainer(); RegionQuery query = container.createQuery(); externalPluginsAllowed = query.testState(b.getLocation(), p, ShopFlag.CREATE_SHOP); } - if (plugin.hasTowny()) { + if (plugin.hasTowny() && plugin.getShopChestConfig().enable_towny_integration) { TownBlock townBlock = TownyUniverse.getTownBlock(b.getLocation()); externalPluginsAllowed &= (townBlock != null && townBlock.getType() == TownBlockType.COMMERCIAL); } diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java index 31269f2..93dc062 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java @@ -94,7 +94,7 @@ public class ShopInteractListener implements Listener { chestLocations[1] = ((Chest) dc.getRightSide()).getLocation(); } - if (plugin.hasWorldGuard()) { + if (plugin.hasWorldGuard() && config.enable_worldguard_integration) { RegionContainer container = worldGuard.getRegionContainer(); RegionQuery query = container.createQuery(); @@ -105,7 +105,7 @@ public class ShopInteractListener implements Listener { } } - if (plugin.hasTowny()) { + if (plugin.hasTowny() && config.enable_towny_integration) { for (Location loc : chestLocations) { if (loc != null) { TownBlock townBlock = TownyUniverse.getTownBlock(loc); @@ -237,26 +237,26 @@ public class ShopInteractListener implements Listener { boolean worldGuardAllowed = true; if (shop.getShopType() == ShopType.ADMIN) { - if (plugin.hasWorldGuard()) { + if (plugin.hasWorldGuard() && config.enable_worldguard_integration) { RegionContainer container = worldGuard.getRegionContainer(); RegionQuery query = container.createQuery(); worldGuardAllowed = query.testState(b.getLocation(), p, ShopFlag.USE_ADMIN_SHOP); } - if (worldGuardAllowed) { + if (worldGuardAllowed || p.hasPermission(Permissions.WORLDGUARD_BYPASS)) { buy(p, shop); } else { plugin.debug(p.getName() + " doesn't have worldguard permission"); p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_WG_BUY)); } } else { - if (plugin.hasWorldGuard()) { + if (plugin.hasWorldGuard() && config.enable_worldguard_integration) { RegionContainer container = worldGuard.getRegionContainer(); RegionQuery query = container.createQuery(); worldGuardAllowed = query.testState(b.getLocation(), p, ShopFlag.USE_SHOP); } - if (worldGuardAllowed) { + if (worldGuardAllowed || p.hasPermission(Permissions.WORLDGUARD_BYPASS)) { Chest c = (Chest) b.getState(); if (Utils.getAmount(c.getInventory(), shop.getProduct()) >= shop.getProduct().getAmount()) { buy(p, shop); @@ -307,7 +307,7 @@ public class ShopInteractListener implements Listener { if (p.hasPermission(Permissions.SELL)) { boolean worldGuardAllowed = true; - if (plugin.hasWorldGuard()) { + if (plugin.hasWorldGuard() && config.enable_worldguard_integration) { RegionContainer container = worldGuard.getRegionContainer(); RegionQuery query = container.createQuery(); @@ -315,7 +315,7 @@ public class ShopInteractListener implements Listener { worldGuardAllowed = query.testState(b.getLocation(), p, flag); } - if (worldGuardAllowed) { + if (worldGuardAllowed || p.hasPermission(Permissions.WORLDGUARD_BYPASS)) { if (Utils.getAmount(p.getInventory(), shop.getProduct()) >= shop.getProduct().getAmount()) { sell(p, shop); } else { diff --git a/src/main/java/de/epiceric/shopchest/listeners/WorldGuardListener.java b/src/main/java/de/epiceric/shopchest/listeners/WorldGuardListener.java index a2ad1dd..0e8fc87 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/WorldGuardListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/WorldGuardListener.java @@ -84,29 +84,31 @@ public class WorldGuardListener implements Listener { @EventHandler(priority = EventPriority.LOW) public void onUseBlock(UseBlockEvent event) { - if (event.getCause().getFirstPlayer() == null) return; + if (plugin.getShopChestConfig().enable_worldguard_integration) { + if (event.getCause().getFirstPlayer() == null) return; - if (event.getOriginalEvent() instanceof PlayerInteractEvent) { - PlayerInteractEvent orig = (PlayerInteractEvent) event.getOriginalEvent(); + if (event.getOriginalEvent() instanceof PlayerInteractEvent) { + PlayerInteractEvent orig = (PlayerInteractEvent) event.getOriginalEvent(); - if (orig.hasBlock()) { - Material type = orig.getClickedBlock().getType(); - if (type == Material.CHEST || type == Material.TRAPPED_CHEST) { - if (isAllowed(event, orig.getClickedBlock().getLocation(), orig.getAction())) { + if (orig.hasBlock()) { + Material type = orig.getClickedBlock().getType(); + if (type == Material.CHEST || type == Material.TRAPPED_CHEST) { + if (isAllowed(event, orig.getClickedBlock().getLocation(), orig.getAction())) { + event.setAllowed(true); + orig.setCancelled(false); + } + } + } + } else if (event.getOriginalEvent() instanceof InventoryOpenEvent) { + InventoryOpenEvent orig = (InventoryOpenEvent) event.getOriginalEvent(); + + if (orig.getInventory().getType() == InventoryType.CHEST) { + if (isAllowed(event, orig.getInventory().getLocation(), Action.RIGHT_CLICK_BLOCK)) { event.setAllowed(true); orig.setCancelled(false); } } } - } else if (event.getOriginalEvent() instanceof InventoryOpenEvent) { - InventoryOpenEvent orig = (InventoryOpenEvent) event.getOriginalEvent(); - - if (orig.getInventory().getType() == InventoryType.CHEST) { - if (isAllowed(event, orig.getInventory().getLocation(), Action.RIGHT_CLICK_BLOCK)) { - event.setAllowed(true); - orig.setCancelled(false); - } - } } } diff --git a/src/main/java/de/epiceric/shopchest/utils/Permissions.java b/src/main/java/de/epiceric/shopchest/utils/Permissions.java index c2e7bbd..300063c 100644 --- a/src/main/java/de/epiceric/shopchest/utils/Permissions.java +++ b/src/main/java/de/epiceric/shopchest/utils/Permissions.java @@ -16,5 +16,6 @@ public class Permissions { public static final String CONFIG = "shopchest.config"; public static final String EXTEND_OTHER = "shopchest.extend.other"; public static final String EXTEND_PROTECTED = "shopchest.extend.protected"; + public static final String WORLDGUARD_BYPASS = "shopchest.worldguard.bypass"; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e24dbc1..b654fa7 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -24,6 +24,14 @@ enable-hologram-interaction: true # The file may get large! Please enable this setting when reporting bugs. enable-debug-log: false +# Set whether WorldGuard integration should be enabled +# Of course, this only worls if WorldGuard is installed +enable-worldguard-integration: true + +# Set whether Towny integration should be enabled +# Of course, this only worls if Towny is installed +enable-towny-integration: true + # Set whether the buy- and sell price should be arranged below each other. # The first line will be the buy price with the message "message.hologram.only-buy", # the second line will be the sell price with the message "message.hologram.only-sell". diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index dc4b587..8bb7472 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -25,6 +25,7 @@ permissions: shopchest.config: true shopchest.extend.other: true shopchest.extend.protected: true + shopchest.worldguard.bypass: true shopchest.create: description: Allows you to create a shop. default: true @@ -70,3 +71,6 @@ permissions: shopchest.extend.protected: description: Allows you to extend shops into a protected region. default: op + shopchest.worlguard.bypass: + description: Allows you to to use shops in WorldGuard regions that deny shop use. + default: op