Fix BuildPermissionEvent logic

This makes it possible for other plugins to force allow placing of shops
and avoid potential issues in the check if multiple plugins are changing
the allow settings.

ALso while the allow counters were a nice idea they might not have even
worked correctly because in the case of only one plugin disallowing it
they would've still returned true at the end. Only if one listener
allowed it and another disallowed it the end result would be disallow.

This also makes the BuildPermissionEvent implement Cancellable (which is
an inverted allowed) in order to easily ignore events that are already
disallowed by another listener.
This commit is contained in:
Phoenix616 2020-01-16 00:03:21 +01:00
parent bd8f2dfc19
commit d0919e78d7
4 changed files with 20 additions and 15 deletions

View File

@ -2,20 +2,20 @@ package com.Acrobot.ChestShop.Events.Protection;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* @author Acrobot
*/
public class BuildPermissionEvent extends Event {
public class BuildPermissionEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private Player player;
private Location chest, sign;
private int disallowed = 0;
private int received = 0;
private boolean allowed = true;
public BuildPermissionEvent(Player player, Location chest, Location sign) {
this.player = player;
@ -36,24 +36,19 @@ public class BuildPermissionEvent extends Event {
}
public void allow() {
received++;
allowed = true;
}
public boolean isAllowed() {
return disallowed != received || received == 0;
return allowed;
}
public void allow(boolean yesOrNot) {
if (yesOrNot) {
allow();
} else {
disallow();
}
allowed = yesOrNot;
}
public void disallow() {
received++;
disallowed++;
allowed = false;
}
public HandlerList getHandlers() {
@ -63,4 +58,14 @@ public class BuildPermissionEvent extends Event {
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public boolean isCancelled() {
return !isAllowed();
}
@Override
public void setCancelled(boolean cancel) {
allow(!cancel);
}
}

View File

@ -16,7 +16,7 @@ public class GriefPrevenentionBuilding implements Listener {
this.griefPrevention = (GriefPrevention) plugin;
}
@EventHandler
@EventHandler(ignoreCancelled = true)
public void canBuild(BuildPermissionEvent event) {
event.allow(griefPrevention.dataStore.getClaimAt(event.getSign(), false, null) != null);
}

View File

@ -18,7 +18,7 @@ public class RedProtectBuilding implements Listener {
this.redProtect = (RedProtect) plugin;
}
@EventHandler
@EventHandler(ignoreCancelled = true)
public void canBuild(BuildPermissionEvent event) {
Region region = redProtect.getAPI().getRegion(event.getSign());
event.allow(region != null && region.canBuild(event.getPlayer()));

View File

@ -26,7 +26,7 @@ public class WorldGuardBuilding implements Listener {
this.worldGuardPlatform = WorldGuard.getInstance().getPlatform();
}
@EventHandler
@EventHandler(ignoreCancelled = true)
public void canBuild(BuildPermissionEvent event) {
ApplicableRegionSet regions = getApplicableRegions(event.getSign().getBlock().getLocation());