Skip to content

Commit

Permalink
Added safety mechanism for admins.
Browse files Browse the repository at this point in the history
  • Loading branch information
MFHKiwi committed Jul 24, 2021
1 parent 0605681 commit eb92c54
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 65 deletions.
51 changes: 46 additions & 5 deletions src/main/java/me/MFHKiwi/KiwiClaims/KiwiClaims.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Player> overrides = new ArrayList<Player>();
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...");
Expand Down Expand Up @@ -91,12 +100,44 @@ public ChatColor getColour(int id) {
else return null;
}

public List<Player> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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;
}

Expand Down
22 changes: 20 additions & 2 deletions src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KCommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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 + ".";
Expand All @@ -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) {
Expand Down Expand Up @@ -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<Player> 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;
}
Expand Down
20 changes: 3 additions & 17 deletions src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KEntityListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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]);
}
}

Expand All @@ -102,21 +92,17 @@ 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]);
}
}

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]);
}
}
}
28 changes: 7 additions & 21 deletions src/main/java/me/MFHKiwi/KiwiClaims/Listeners/KPlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class KPlayerListener extends PlayerListener {
private final List<KSelection> selections = new ArrayList<KSelection>();
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) {
Expand All @@ -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.";
Expand Down Expand Up @@ -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) &&
Expand All @@ -163,18 +157,16 @@ 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;
}
// Nor does it have one for soil trampling...
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;
Expand Down Expand Up @@ -232,32 +224,26 @@ 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]);
}
}

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]);
}
}

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]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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]);
}
}
}

0 comments on commit eb92c54

Please sign in to comment.