From dda2a50c70846d3974452182ffbe565cba91bf33 Mon Sep 17 00:00:00 2001 From: Edson Passos Date: Tue, 17 Sep 2019 23:19:30 -0300 Subject: [PATCH] trust change event (#623) --- .../GriefPrevention/GriefPrevention.java | 65 ++++++++--- .../events/TrustChangedEvent.java | 110 ++++++++++++++++++ 2 files changed, 161 insertions(+), 14 deletions(-) create mode 100644 src/main/java/me/ryanhamshire/GriefPrevention/events/TrustChangedEvent.java diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java b/src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java index 0807759..595f865 100644 --- a/src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java +++ b/src/main/java/me/ryanhamshire/GriefPrevention/GriefPrevention.java @@ -35,6 +35,7 @@ import java.util.regex.Pattern; import me.ryanhamshire.GriefPrevention.DataStore.NoTransferException; import me.ryanhamshire.GriefPrevention.events.PreventBlockBreakEvent; import me.ryanhamshire.GriefPrevention.events.SaveTrappedPlayerEvent; +import me.ryanhamshire.GriefPrevention.events.TrustChangedEvent; import me.ryanhamshire.GriefPrevention.metrics.MetricsHandler; import net.milkbowl.vault.economy.Economy; @@ -1587,6 +1588,22 @@ public class GriefPrevention extends JavaPlugin if(claim == null) { PlayerData playerData = this.dataStore.getPlayerData(player.getUniqueId()); + + String idToDrop = args[0]; + if(otherPlayer != null) + { + idToDrop = otherPlayer.getUniqueId().toString(); + } + + //calling event + TrustChangedEvent event = new TrustChangedEvent(player, playerData.getClaims(), null, false, idToDrop); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return true; + } + + //dropping permissions for(int i = 0; i < playerData.getClaims().size(); i++) { claim = playerData.getClaims().get(i); @@ -1600,11 +1617,6 @@ public class GriefPrevention extends JavaPlugin //otherwise drop individual permissions else { - String idToDrop = args[0]; - if(otherPlayer != null) - { - idToDrop = otherPlayer.getUniqueId().toString(); - } claim.dropPermission(idToDrop); claim.managers.remove(idToDrop); } @@ -1648,6 +1660,14 @@ public class GriefPrevention extends JavaPlugin return true; } + //calling the event + TrustChangedEvent event = new TrustChangedEvent(player, claim, null, false, args[0]); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return true; + } + claim.clearPermissions(); GriefPrevention.sendMessage(player, TextMode.Success, Messages.ClearPermissionsOneClaim); } @@ -1668,6 +1688,14 @@ public class GriefPrevention extends JavaPlugin } else { + //calling the event + TrustChangedEvent event = new TrustChangedEvent(player, claim, null, false, idToDrop); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return true; + } + claim.dropPermission(idToDrop); claim.managers.remove(idToDrop); @@ -3018,19 +3046,28 @@ public class GriefPrevention extends JavaPlugin return; } + String identifierToAdd = recipientName; + if(permission != null) + { + identifierToAdd = "[" + permission + "]"; + } + else if(recipientID != null) + { + identifierToAdd = recipientID.toString(); + } + + //calling the event + TrustChangedEvent event = new TrustChangedEvent(player, targetClaims, permissionLevel, true, identifierToAdd); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return; + } + //apply changes for(int i = 0; i < targetClaims.size(); i++) { Claim currentClaim = targetClaims.get(i); - String identifierToAdd = recipientName; - if(permission != null) - { - identifierToAdd = "[" + permission + "]"; - } - else if(recipientID != null) - { - identifierToAdd = recipientID.toString(); - } if(permissionLevel == null) { diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/events/TrustChangedEvent.java b/src/main/java/me/ryanhamshire/GriefPrevention/events/TrustChangedEvent.java new file mode 100644 index 0000000..567fc20 --- /dev/null +++ b/src/main/java/me/ryanhamshire/GriefPrevention/events/TrustChangedEvent.java @@ -0,0 +1,110 @@ +package me.ryanhamshire.GriefPrevention.events; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import me.ryanhamshire.GriefPrevention.Claim; +import me.ryanhamshire.GriefPrevention.ClaimPermission; + +/** + * This event is thrown when the trust is changed in one or more claims + * + * @author roinujnosde + * + */ +public class TrustChangedEvent extends Event implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + + private final Player changer; + private final List claims; + private final ClaimPermission claimPermission; + private final boolean given; + private final String identifier; + private boolean cancelled; + + public TrustChangedEvent(Player changer, List claims, ClaimPermission claimPermission, boolean given, + String identifier) { + super(); + this.changer = changer; + this.claims = claims; + this.claimPermission = claimPermission; + this.given = given; + this.identifier = identifier; + } + + public TrustChangedEvent(Player changer, Claim claim, ClaimPermission claimPermission, boolean given, String identifier) { + this.changer = changer; + claims = new ArrayList<>(); + claims.add(claim); + this.claimPermission = claimPermission; + this.given = given; + this.identifier = identifier; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + /** + * Gets who made the change + * + * @return the changer + */ + public Player getChanger() { + return changer; + } + + /** + * Gets the changed claims + * + * @return the changed claims + */ + public List getClaims() { + return claims; + } + + /** + * Gets the claim permission (null if the permission is being taken) + * + * @return the claim permission + */ + public ClaimPermission getClaimPermission() { + return claimPermission; + } + + /** + * Checks if the trust is being given + * + * @return true if given, false if taken + */ + public boolean isGiven() { + return given; + } + + /** + * Gets the identifier of the receiver of this action + * Can be: "public", "all", a UUID, a permission + * + * @return the identifier + */ + public String getIdentifier() { + return identifier; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } +}