Implement Cancellable in ClaimModifiedEvent (#942)

- If canceled, GP stops processing the resize command (no error printed or potential overlapping claim displayed)
This commit is contained in:
Frank van der Heijden 2020-08-07 19:26:32 +02:00 committed by RoboMWM
parent a3d0aecaf0
commit 4b3852d624
3 changed files with 64 additions and 16 deletions

View File

@ -250,6 +250,24 @@ public class Claim
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()
{

View File

@ -1051,8 +1051,6 @@ public abstract class DataStore
//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 @@ public abstract class DataStore
//save those changes
this.saveClaim(result.claim);
ClaimModifiedEvent event = new ClaimModifiedEvent(result.claim, resizingPlayer);
Bukkit.getPluginManager().callEvent(event);
}
return result;
@ -1362,18 +1358,24 @@ public abstract class DataStore
}
}
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<String>(), new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>(), null);
//if the new claim is smaller
if (!newClaim.contains(oldClaim.getLesserBoundaryCorner(), true, false) || !newClaim.contains(oldClaim.getGreaterBoundaryCorner(), true, false))
{

View File

@ -3,6 +3,7 @@ package me.ryanhamshire.GriefPrevention.events;
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 @@ import org.bukkit.event.HandlerList;
* 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 class ClaimModifiedEvent extends Event
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 class ClaimModifiedEvent extends Event
* 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 class ClaimModifiedEvent extends Event
{
return modifier;
}
@Override
public boolean isCancelled()
{
return cancelled;
}
@Override
public void setCancelled(boolean cancelled)
{
this.cancelled = cancelled;
}
}