From 4b3852d624c081711fd2521bb69f587c06ce4f38 Mon Sep 17 00:00:00 2001 From: Frank van der Heijden <22407829+FrankHeijden@users.noreply.github.com> Date: Fri, 7 Aug 2020 19:26:32 +0200 Subject: [PATCH] Implement Cancellable in ClaimModifiedEvent (#942) - If canceled, GP stops processing the resize command (no error printed or potential overlapping claim displayed) --- .../ryanhamshire/GriefPrevention/Claim.java | 18 +++++++++ .../GriefPrevention/DataStore.java | 24 ++++++------ .../events/ClaimModifiedEvent.java | 38 ++++++++++++++++--- 3 files changed, 64 insertions(+), 16 deletions(-) diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/Claim.java b/src/main/java/me/ryanhamshire/GriefPrevention/Claim.java index 7c9219c43..196c443a7 100644 --- a/src/main/java/me/ryanhamshire/GriefPrevention/Claim.java +++ b/src/main/java/me/ryanhamshire/GriefPrevention/Claim.java @@ -250,6 +250,24 @@ boolean hasSurfaceFluids() this(lesserBoundaryCorner, greaterBoundaryCorner, ownerID, builderIDs, containerIDs, accessorIDs, managerIDs, false, id); } + //produces a copy of a claim. + public Claim(Claim claim) { + this.modifiedDate = claim.modifiedDate; + this.lesserBoundaryCorner = claim.greaterBoundaryCorner.clone(); + this.greaterBoundaryCorner = claim.greaterBoundaryCorner.clone(); + this.id = claim.id; + this.ownerID = claim.ownerID; + this.managers = new ArrayList<>(claim.managers); + this.playerIDToClaimPermissionMap = new HashMap<>(claim.playerIDToClaimPermissionMap); + this.inDataStore = false; //since it's a copy of a claim, not in datastore! + this.areExplosivesAllowed = claim.areExplosivesAllowed; + this.parent = claim.parent; + this.inheritNothing = claim.inheritNothing; + this.children = new ArrayList<>(claim.children); + this.siegeData = claim.siegeData; + this.doorsOpen = claim.doorsOpen; + } + //measurements. all measurements are in blocks public int getArea() { diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/DataStore.java b/src/main/java/me/ryanhamshire/GriefPrevention/DataStore.java index a23ca2fea..bd458ba7a 100644 --- a/src/main/java/me/ryanhamshire/GriefPrevention/DataStore.java +++ b/src/main/java/me/ryanhamshire/GriefPrevention/DataStore.java @@ -1051,8 +1051,6 @@ synchronized public void extendClaim(Claim claim, int newDepth) //save changes this.saveClaim(claim); - ClaimModifiedEvent event = new ClaimModifiedEvent(claim, null); - Bukkit.getPluginManager().callEvent(event); } //starts a siege on a claim @@ -1314,8 +1312,6 @@ synchronized public CreateClaimResult resizeClaim(Claim claim, int newx1, int ne //save those changes this.saveClaim(result.claim); - ClaimModifiedEvent event = new ClaimModifiedEvent(result.claim, resizingPlayer); - Bukkit.getPluginManager().callEvent(event); } return result; @@ -1362,18 +1358,24 @@ void resizeClaimWithChecks(Player player, PlayerData playerData, int newx1, int } } + Claim oldClaim = playerData.claimResizing; + Claim newClaim = new Claim(oldClaim); + World world = newClaim.getLesserBoundaryCorner().getWorld(); + newClaim.lesserBoundaryCorner = new Location(world, newx1, newy1, newz1); + newClaim.greaterBoundaryCorner = new Location(world, newx2, newy2, newz2); + + //call event here to check if it has been cancelled + ClaimModifiedEvent event = new ClaimModifiedEvent(oldClaim, newClaim, player); + Bukkit.getPluginManager().callEvent(event); + + //return here if event is cancelled + if (event.isCancelled()) return; + //special rule for making a top-level claim smaller. to check this, verifying the old claim's corners are inside the new claim's boundaries. //rule: in any mode, shrinking a claim removes any surface fluids - Claim oldClaim = playerData.claimResizing; boolean smaller = false; if (oldClaim.parent == null) { - //temporary claim instance, just for checking contains() - Claim newClaim = new Claim( - new Location(oldClaim.getLesserBoundaryCorner().getWorld(), newx1, newy1, newz1), - new Location(oldClaim.getLesserBoundaryCorner().getWorld(), newx2, newy2, newz2), - null, new ArrayList(), new ArrayList(), new ArrayList(), new ArrayList(), null); - //if the new claim is smaller if (!newClaim.contains(oldClaim.getLesserBoundaryCorner(), true, false) || !newClaim.contains(oldClaim.getGreaterBoundaryCorner(), true, false)) { diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/events/ClaimModifiedEvent.java b/src/main/java/me/ryanhamshire/GriefPrevention/events/ClaimModifiedEvent.java index f40ac751c..c04ec897b 100644 --- a/src/main/java/me/ryanhamshire/GriefPrevention/events/ClaimModifiedEvent.java +++ b/src/main/java/me/ryanhamshire/GriefPrevention/events/ClaimModifiedEvent.java @@ -3,6 +3,7 @@ import me.ryanhamshire.GriefPrevention.Claim; import org.bukkit.command.CommandSender; +import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; @@ -11,7 +12,7 @@ * a claim has changed. The CommandSender can be null in the event that the modification is called by the plugin itself. * Created by Narimm on 5/08/2018. */ -public class ClaimModifiedEvent extends Event +public class ClaimModifiedEvent extends Event implements Cancellable { private static final HandlerList handlers = new HandlerList(); @@ -21,12 +22,15 @@ public static HandlerList getHandlerList() return handlers; } - private final Claim claim; + private final Claim from; + private final Claim to; private CommandSender modifier; + private boolean cancelled; - public ClaimModifiedEvent(Claim claim, CommandSender modifier) + public ClaimModifiedEvent(Claim from, Claim to, CommandSender modifier) { - this.claim = claim; + this.from = from; + this.to = to; this.modifier = modifier; } @@ -40,10 +44,22 @@ public HandlerList getHandlers() * The claim * * @return the claim + * @deprecated Use the {@link #getTo() getTo} method. */ + @Deprecated public Claim getClaim() { - return claim; + return to; + } + + public Claim getFrom() + { + return from; + } + + public Claim getTo() + { + return to; } /** @@ -55,4 +71,16 @@ public CommandSender getModifier() { return modifier; } + + @Override + public boolean isCancelled() + { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) + { + this.cancelled = cancelled; + } }