From 3e061dda8ddf52c5d3a12cfbf7946f3f6a1ad136 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Thu, 18 Jun 2020 17:01:52 +0100 Subject: [PATCH] Add config option to limit trading with shops via protections --- .../Acrobot/ChestShop/Configuration/Messages.java | 1 + .../ChestShop/Configuration/Properties.java | 3 +++ .../Listeners/Player/PlayerInteract.java | 5 +++++ .../ChestShop/Plugins/WorldGuardProtection.java | 15 ++++++++++++--- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java b/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java index 5ab892c..25b3c4c 100644 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java +++ b/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java @@ -18,6 +18,7 @@ public class Messages { @PrecededBySpace public static String ACCESS_DENIED = "You don't have permission to access that shop's storage container!"; + public static String TRADE_DENIED = "You don't have permission to trade with that shop!"; @PrecededBySpace public static String NOT_ENOUGH_MONEY = "You don't have enough money!"; diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java index b1a83d0..b3d5d59 100644 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java +++ b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java @@ -249,6 +249,9 @@ public class Properties { @ConfigurationComment("Do you want to disable the hopper protection, which prevents Hopper-Minecarts from taking items out of shops?") public static boolean TURN_OFF_HOPPER_PROTECTION = false; + @ConfigurationComment("Only allow users to buy/sell that have access to the sign's protection? (E.g. LWC protection)") + public static boolean CHECK_ACCESS_FOR_SHOP_USE = false; + @ConfigurationComment("Do you want to protect shop chests with LWC?") public static boolean PROTECT_CHEST_WITH_LWC = false; 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 159dadc..dc18afc 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java @@ -134,6 +134,11 @@ public class PlayerInteract implements Listener { event.setCancelled(true); } + if (Properties.CHECK_ACCESS_FOR_SHOP_USE && !Security.canAccess(player, block, true)) { + player.sendMessage(Messages.prefix(Messages.TRADE_DENIED)); + return; + } + //Bukkit.getLogger().info("ChestShop - DEBUG - "+block.getWorld().getName()+": "+block.getLocation().getBlockX()+", "+block.getLocation().getBlockY()+", "+block.getLocation().getBlockZ()); PreTransactionEvent pEvent = preparePreTransactionEvent(sign, player, action); if (pEvent == null) diff --git a/src/main/java/com/Acrobot/ChestShop/Plugins/WorldGuardProtection.java b/src/main/java/com/Acrobot/ChestShop/Plugins/WorldGuardProtection.java index 121e3c2..52db541 100644 --- a/src/main/java/com/Acrobot/ChestShop/Plugins/WorldGuardProtection.java +++ b/src/main/java/com/Acrobot/ChestShop/Plugins/WorldGuardProtection.java @@ -1,5 +1,6 @@ package com.Acrobot.ChestShop.Plugins; +import com.Acrobot.Breeze.Utils.BlockUtil; import com.Acrobot.ChestShop.Events.Protection.ProtectionCheckEvent; import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.util.Location; @@ -12,8 +13,11 @@ import com.sk89q.worldguard.internal.permission.RegionPermissionModel; import com.sk89q.worldguard.internal.platform.WorldGuardPlatform; import com.sk89q.worldguard.protection.ApplicableRegionSet; import com.sk89q.worldguard.protection.flags.Flags; +import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.protection.managers.RegionManager; import org.bukkit.block.Block; +import org.bukkit.block.data.type.Sign; +import org.bukkit.block.data.type.WallSign; import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.EventHandler; @@ -54,7 +58,12 @@ public class WorldGuardProtection implements Listener { } ApplicableRegionSet set = manager.getApplicableRegions(location.toVector().toBlockPoint()); - if (!canAccess(localPlayer, (World) location.getExtent(), set)) { + StateFlag flag = Flags.CHEST_ACCESS; + if (BlockUtil.isSign(block)) { + flag = Flags.USE; + } + + if (!canAccess(localPlayer, (World) location.getExtent(), set, flag)) { event.setResult(Event.Result.DENY); } } @@ -66,9 +75,9 @@ public class WorldGuardProtection implements Listener { || !wcfg.getChestProtection().isProtected(location, player); } - private boolean canAccess(LocalPlayer player, World world, ApplicableRegionSet set) { + private boolean canAccess(LocalPlayer player, World world, ApplicableRegionSet set, StateFlag flag) { return new RegionPermissionModel(player).mayIgnoreRegionProtection(world) || set.testState(player, Flags.BUILD) - || set.testState(player, Flags.CHEST_ACCESS); + || set.testState(player, flag); } }