From eb92c546d15d6191e5a1d2593764672895e867e8 Mon Sep 17 00:00:00 2001 From: MFHKiwi Date: Sat, 24 Jul 2021 16:02:39 +1200 Subject: [PATCH] Added safety mechanism for admins. --- .../me/MFHKiwi/KiwiClaims/KiwiClaims.java | 51 +++++++++++++++++-- .../KiwiClaims/Listeners/KBlockListener.java | 11 +--- .../KiwiClaims/Listeners/KCommandHandler.java | 22 +++++++- .../KiwiClaims/Listeners/KEntityListener.java | 20 ++------ .../KiwiClaims/Listeners/KPlayerListener.java | 28 +++------- .../Listeners/KVehicleListener.java | 11 +--- 6 files changed, 78 insertions(+), 65 deletions(-) diff --git a/src/main/java/me/MFHKiwi/KiwiClaims/KiwiClaims.java b/src/main/java/me/MFHKiwi/KiwiClaims/KiwiClaims.java index 39fab06..d74fab1 100644 --- a/src/main/java/me/MFHKiwi/KiwiClaims/KiwiClaims.java +++ b/src/main/java/me/MFHKiwi/KiwiClaims/KiwiClaims.java @@ -18,6 +18,8 @@ package me.MFHKiwi.KiwiClaims; import java.io.File; +import java.util.ArrayList; +import java.util.List; import java.util.logging.Logger; import org.bukkit.ChatColor; @@ -40,6 +42,13 @@ public class KiwiClaims extends JavaPlugin { private final KPlayerListener player_listener = new KPlayerListener(this); private final KEntityListener entity_listener = new KEntityListener(this); private final KVehicleListener vehicle_listener = new KVehicleListener(this); + private final List overrides = new ArrayList(); + private final String[] not_allowed = { + colour1 + "You are not allowed to do that here!", + colour1 + "Ask the owner of this claim, " + colour2, + colour1 + ", for permission.", + colour1 + "Use " + colour2 + "/kc override" + colour1 + " to override this." + }; public void onEnable() { log("Plugin enabling..."); @@ -91,12 +100,44 @@ public ChatColor getColour(int id) { else return null; } + public List getOverrideList() { + return this.overrides; + } + + public boolean isOverriding(Player player) { + for (Player player_from_list : this.overrides) { + if (player == player_from_list) { + return true; + } + } + return false; + } + // Common methods that classes use. Not worth creating a class for, so I'm putting it in here. - public static boolean shouldPrevent(Player player, KClaim claim) { + public boolean shouldPrevent(Player player, KClaim claim, boolean silent) { String player_name = player.getName(); - if (!claim.ownerEquals(player_name) && - !claim.isTrusted(player_name) && - !player.hasPermission("kc.admin")) return true; - else return false; + boolean can_build = (claim.ownerEquals(player_name) || claim.isTrusted(player_name)); + boolean is_admin = player.hasPermission("kc.admin"); + boolean is_overriding = isOverriding(player); + if (!can_build && !is_admin) { + if (!silent) { + player.sendMessage(this.not_allowed[0]); + player.sendMessage(this.not_allowed[1] + claim.getOwnerName() + this.not_allowed[2]); + } + return true; + } + else if (!can_build && is_admin && !is_overriding) { + if (!silent) { + player.sendMessage(this.not_allowed[0]); + player.sendMessage(this.not_allowed[1] + claim.getOwnerName() + this.not_allowed[2]); + player.sendMessage(this.not_allowed[3]); + } + return true; + } + return false; + } + + public boolean shouldPrevent(Player player, KClaim claim) { + return shouldPrevent(player, claim, false); } } diff --git a/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KBlockListener.java b/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KBlockListener.java index dd49d05..94d2005 100644 --- a/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KBlockListener.java +++ b/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KBlockListener.java @@ -17,7 +17,6 @@ */ package me.MFHKiwi.KiwiClaims.Listeners; -import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -39,15 +38,9 @@ public class KBlockListener extends BlockListener { private final KiwiClaims plugin; - private final String[] not_allowed = new String[3]; public KBlockListener(KiwiClaims plugin) { this.plugin = plugin; - ChatColor colour1 = plugin.getColour(1); - ChatColor colour2 = plugin.getColour(2); - this.not_allowed[0] = colour1 + "You are not allowed to build here!"; - this.not_allowed[1] = colour1 + "Ask the owner of this claim, " + colour2; - this.not_allowed[2] = colour1 + ", for permission."; } public void registerEvents() { @@ -62,9 +55,7 @@ public void registerEvents() { public boolean commonHandler(Location block_location, Player player) { KClaim claim = plugin.getClaimSave().getClaimAt(block_location); if (claim == null) return false; - if (!KiwiClaims.shouldPrevent(player, claim)) return false; - player.sendMessage(this.not_allowed[0]); - player.sendMessage(this.not_allowed[1] + claim.getOwnerName() + this.not_allowed[2]); + if (!this.plugin.shouldPrevent(player, claim)) return false; return true; } diff --git a/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KCommandHandler.java b/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KCommandHandler.java index 8483bae..f0cb64a 100644 --- a/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KCommandHandler.java +++ b/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KCommandHandler.java @@ -17,6 +17,8 @@ */ package me.MFHKiwi.KiwiClaims.Listeners; +import java.util.Iterator; + import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -32,11 +34,11 @@ public class KCommandHandler implements CommandExecutor { private final KiwiClaims plugin; private final KPlayerListener listener; - private final String[] help_message = new String[11]; + private final String[] help_message = new String[12]; private final String[] owner_set = new String[2]; private final String[] plugin_info = new String[2]; private final String incorrect_usage, not_player, claim_message, not_in_claim, unclaim_message, not_allowed, - trust_message, already_trusted, untrust_message, already_untrusted, internal_error, no_permission, already_owner, selection_cancelled, no_selection; + trust_message, already_trusted, untrust_message, already_untrusted, internal_error, no_permission, already_owner, selection_cancelled, no_selection, overriding, not_overriding; public KCommandHandler(KiwiClaims plugin, KPlayerListener listener) { @@ -55,6 +57,7 @@ public KCommandHandler(KiwiClaims plugin, KPlayerListener listener) { this.help_message[8] = colour2 + " - " + colour1 + "/kc exclude" + colour2 + ": Create exclusion zone"; this.help_message[9] = colour2 + " - " + colour1 + "/kc unexclude" + colour2 + ": Remove exclusion zone"; this.help_message[10] = colour2 + " - " + colour1 + "/kc visualise/vis" + colour2 + ": Visualise claim corners"; + this.help_message[11] = colour2 + " - " + colour1 + "/kc override" + colour2 + ": Override claims"; this.incorrect_usage = colour1 + "Incorrect usage. See " + colour2 + "/kc help" + colour1 + "."; this.plugin_info[0] = colour1 + plugin.getDescription().getFullName() + colour2 + " by MFHKiwi"; this.plugin_info[1] = colour2 + "This plugin is licensed under the " + colour1 + "GNU GPL v3" + colour2 + "."; @@ -74,6 +77,8 @@ public KCommandHandler(KiwiClaims plugin, KPlayerListener listener) { this.owner_set[1] = colour2 + "."; this.selection_cancelled = colour2 + "Selection cancelled."; this.no_selection = colour1 + "You have not started a selection."; + this.overriding = colour2 + "Overriding claims."; + this.not_overriding = colour2 + "No longer overriding claims."; } private boolean shouldPrevent(Player player, KClaim claim) { @@ -236,6 +241,19 @@ else if (shouldPrevent(player, claim)) { } return true; } + if (subcommand.equalsIgnoreCase("override")) { + if (!plugin.isOverriding(player)) { + plugin.getOverrideList().add(player); + player.sendMessage(this.overriding); + } + else for (Iterator it = plugin.getOverrideList().iterator(); it.hasNext();) { + if (it.next() == player) { + it.remove(); + player.sendMessage(not_overriding); + } + } + return true; + } sender.sendMessage(this.incorrect_usage); return true; } diff --git a/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KEntityListener.java b/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KEntityListener.java index 742bd13..63a075c 100644 --- a/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KEntityListener.java +++ b/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KEntityListener.java @@ -19,7 +19,6 @@ import java.util.Iterator; -import org.bukkit.ChatColor; import org.bukkit.block.Block; import org.bukkit.entity.Animals; import org.bukkit.entity.Entity; @@ -42,16 +41,9 @@ public class KEntityListener extends EntityListener { private final KiwiClaims plugin; - private final String[] not_allowed = new String[4]; public KEntityListener(KiwiClaims plugin) { this.plugin = plugin; - ChatColor colour1 = plugin.getColour(1); - ChatColor colour2 = plugin.getColour(2); - this.not_allowed[0] = colour1 + "You are not allowed to hurt that here!"; - this.not_allowed[1] = colour1 + "You are not allowed to build here!"; - this.not_allowed[2] = colour1 + "Ask the owner of this claim, " + colour2; - this.not_allowed[3] = colour1 + ", for permission."; } public void registerEvents() { @@ -75,10 +67,8 @@ public void onEntityDamage(EntityDamageEvent event) { return; } Player player = (Player) event2.getDamager(); - if (KiwiClaims.shouldPrevent(player, claim)) { + if (this.plugin.shouldPrevent(player, claim)) { event.setCancelled(true); - player.sendMessage(this.not_allowed[0]); - player.sendMessage(this.not_allowed[2] + claim.getOwnerName() + this.not_allowed[3]); } } @@ -102,10 +92,8 @@ public void onPaintingBreak(PaintingBreakEvent event) { return; } Player player = (Player) event2.getRemover(); - if (KiwiClaims.shouldPrevent(player, claim)) { + if (this.plugin.shouldPrevent(player, claim)) { event.setCancelled(true); - player.sendMessage(this.not_allowed[1]); - player.sendMessage(this.not_allowed[2] + claim.getOwnerName() + this.not_allowed[3]); } } @@ -113,10 +101,8 @@ public void onPaintingPlace(PaintingPlaceEvent event) { Player player = event.getPlayer(); KClaim claim = plugin.getClaimSave().getClaimAt(event.getPainting().getLocation()); if (claim == null) return; - if (KiwiClaims.shouldPrevent(player, claim)) { + if (this.plugin.shouldPrevent(player, claim)) { event.setCancelled(true); - player.sendMessage(this.not_allowed[1]); - player.sendMessage(this.not_allowed[2] + claim.getOwnerName() + this.not_allowed[3]); } } } diff --git a/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KPlayerListener.java b/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KPlayerListener.java index fd0bad4..5abf8c4 100644 --- a/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KPlayerListener.java +++ b/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KPlayerListener.java @@ -51,7 +51,6 @@ public class KPlayerListener extends PlayerListener { private final List selections = new ArrayList(); private final String internal_error, world_mismatch, overlap, claim_create, exclusion_create; private final String[] pos_set = new String[4]; - private final String[] not_allowed = new String[3]; private final String[] claim_enter_leave = new String[3]; public KPlayerListener(KiwiClaims plugin) { @@ -67,9 +66,6 @@ public KPlayerListener(KiwiClaims plugin) { this.pos_set[1] = colour2 + " set (" + colour1 + "x: "; this.pos_set[2] = colour1 + ", z: "; this.pos_set[3] = colour2 + ")"; - this.not_allowed[0] = colour1 + "You are not allowed to use that here!"; - this.not_allowed[1] = colour1 + "Ask the owner of this claim, " + colour2; - this.not_allowed[2] = colour1 + ", for permission."; this.claim_enter_leave[0] = colour2 + "Entering " + colour1; this.claim_enter_leave[1] = colour2 + "Leaving " + colour1; this.claim_enter_leave[2] = colour2 + "'s claim."; @@ -147,14 +143,12 @@ public void onPlayerInteract(PlayerInteractEvent event) { if (event.getClickedBlock().getState() instanceof ContainerBlock && event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { KClaim claim = plugin.getClaimSave().getClaimAt(event.getClickedBlock().getLocation()); if (claim == null) return; - if (KiwiClaims.shouldPrevent(player, claim)) { + if (this.plugin.shouldPrevent(player, claim)) { event.setCancelled(true); - player.sendMessage(this.not_allowed[0]); - player.sendMessage(this.not_allowed[1] + claim.getOwnerName() + this.not_allowed[2]); } return; } - // And it doesn't have an adequate listener for vehicle placement either... + // And it doesn't have an adequate one for vehicle placement either... if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && player.getItemInHand().getType().equals(Material.BOAT) || (player.getItemInHand().getType().equals(Material.MINECART) && @@ -163,10 +157,8 @@ public void onPlayerInteract(PlayerInteractEvent event) { event.getClickedBlock().getType().equals(Material.POWERED_RAIL)))) { KClaim claim = plugin.getClaimSave().getClaimAt(event.getClickedBlock().getLocation()); if (claim == null) return; - if (KiwiClaims.shouldPrevent(player, claim)) { + if (this.plugin.shouldPrevent(player, claim)) { event.setCancelled(true); - player.sendMessage(this.not_allowed[0]); - player.sendMessage(this.not_allowed[1] + claim.getOwnerName() + this.not_allowed[2]); } return; } @@ -174,7 +166,7 @@ public void onPlayerInteract(PlayerInteractEvent event) { if (event.getClickedBlock().getType().equals(Material.SOIL) && event.getAction().equals(Action.PHYSICAL)) { KClaim claim = plugin.getClaimSave().getClaimAt(event.getClickedBlock().getLocation()); if (claim == null) return; - if (KiwiClaims.shouldPrevent(player, claim)) { + if (this.plugin.shouldPrevent(player, claim, true)) { event.setCancelled(true); } return; @@ -232,10 +224,8 @@ public void onPlayerBedEnter(PlayerBedEnterEvent event) { Player player = event.getPlayer(); KClaim claim = plugin.getClaimSave().getClaimAt(bed.getLocation()); if (claim == null) return; - if (KiwiClaims.shouldPrevent(player, claim)) { + if (this.plugin.shouldPrevent(player, claim)) { event.setCancelled(true); - player.sendMessage(this.not_allowed[0]); - player.sendMessage(this.not_allowed[1] + claim.getOwnerName() + this.not_allowed[2]); } } @@ -243,10 +233,8 @@ public void onPlayerBucketFill(PlayerBucketFillEvent event) { Player player = event.getPlayer(); KClaim claim = plugin.getClaimSave().getClaimAt(event.getBlockClicked().getLocation()); if (claim == null) return; - if (KiwiClaims.shouldPrevent(player, claim)) { + if (this.plugin.shouldPrevent(player, claim)) { event.setCancelled(true); - player.sendMessage(this.not_allowed[0]); - player.sendMessage(this.not_allowed[1] + claim.getOwnerName() + this.not_allowed[2]); } } @@ -254,10 +242,8 @@ public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) { Player player = event.getPlayer(); KClaim claim = plugin.getClaimSave().getClaimAt(event.getBlockClicked().getLocation()); if (claim == null) return; - if (KiwiClaims.shouldPrevent(player, claim)) { + if (this.plugin.shouldPrevent(player, claim)) { event.setCancelled(true); - player.sendMessage(this.not_allowed[0]); - player.sendMessage(this.not_allowed[1] + claim.getOwnerName() + this.not_allowed[2]); } } diff --git a/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KVehicleListener.java b/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KVehicleListener.java index 877495a..7d9a51c 100644 --- a/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KVehicleListener.java +++ b/src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KVehicleListener.java @@ -17,7 +17,6 @@ */ package me.MFHKiwi.KiwiClaims.Listeners; -import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.Listener; @@ -31,15 +30,9 @@ public class KVehicleListener extends VehicleListener { private final KiwiClaims plugin; - private final String[] not_allowed = new String[3]; public KVehicleListener(KiwiClaims plugin) { this.plugin = plugin; - ChatColor colour1 = plugin.getColour(1); - ChatColor colour2 = plugin.getColour(2); - this.not_allowed[0] = colour1 + "You are not allowed to break that here!"; - this.not_allowed[1] = colour1 + "Ask the owner of this claim, " + colour2; - this.not_allowed[2] = colour1 + ", for permission."; } public void registerEvents() { @@ -55,10 +48,8 @@ public void onVehicleDamage(VehicleDamageEvent event) { return; } Player player = (Player) event.getAttacker(); - if (KiwiClaims.shouldPrevent(player, claim)) { + if (this.plugin.shouldPrevent(player, claim)) { event.setCancelled(true); - player.sendMessage(this.not_allowed[0]); - player.sendMessage(this.not_allowed[1] + claim.getOwnerName() + this.not_allowed[2]); } } }