trust change event (#623)

This commit is contained in:
Edson Passos 2019-09-17 23:19:30 -03:00 committed by RoboMWM
parent 4ceffa91b7
commit dda2a50c70
2 changed files with 161 additions and 14 deletions

View File

@ -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)
{

View File

@ -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<Claim> claims;
private final ClaimPermission claimPermission;
private final boolean given;
private final String identifier;
private boolean cancelled;
public TrustChangedEvent(Player changer, List<Claim> 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<Claim> 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;
}
}