Skip to content
This repository has been archived by the owner on Apr 20, 2019. It is now read-only.

Commit

Permalink
Implement Cancellable in ClaimModifiedEvent (#942)
Browse files Browse the repository at this point in the history
- If canceled, GP stops processing the resize command (no error printed or potential overlapping claim displayed)
  • Loading branch information
FrankHeijden authored and RoboMWM committed Aug 7, 2020
1 parent a3d0aec commit 4b3852d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
18 changes: 18 additions & 0 deletions src/main/java/me/ryanhamshire/GriefPrevention/Claim.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/me/ryanhamshire/GriefPrevention/DataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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;
}

Expand All @@ -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;
}

/**
Expand All @@ -55,4 +71,16 @@ public CommandSender getModifier()
{
return modifier;
}

@Override
public boolean isCancelled()
{
return cancelled;
}

@Override
public void setCancelled(boolean cancelled)
{
this.cancelled = cancelled;
}
}

0 comments on commit 4b3852d

Please sign in to comment.