Added PreventBlockBreak event.

Supports the new SpleefArena flag in GriefPrevention flags.  Fired when
a player is prevented from breaking a block.  If cancelled by another
plugin, the break will be allowed.
This commit is contained in:
ryanhamshire 2016-08-17 11:09:05 -07:00
parent 98d610d668
commit 86a18e2f04
3 changed files with 101 additions and 36 deletions

View File

@ -89,7 +89,7 @@ public class BlockEventHandler implements Listener
Block block = breakEvent.getBlock();
//make sure the player is allowed to break at the location
String noBuildReason = GriefPrevention.instance.allowBreak(player, block, block.getLocation());
String noBuildReason = GriefPrevention.instance.allowBreak(player, block, block.getLocation(), breakEvent);
if(noBuildReason != null)
{
GriefPrevention.sendMessage(player, TextMode.Err, noBuildReason);

View File

@ -33,6 +33,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import me.ryanhamshire.GriefPrevention.DataStore.NoTransferException;
import me.ryanhamshire.GriefPrevention.events.PreventBlockBreakEvent;
import me.ryanhamshire.GriefPrevention.events.SaveTrappedPlayerEvent;
import net.milkbowl.vault.economy.Economy;
@ -55,6 +56,7 @@ import org.bukkit.command.*;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
@ -3319,6 +3321,11 @@ public class GriefPrevention extends JavaPlugin
}
public String allowBreak(Player player, Block block, Location location)
{
return this.allowBreak(player, block, location, null);
}
public String allowBreak(Player player, Block block, Location location, BlockBreakEvent breakEvent)
{
if(!GriefPrevention.instance.claimsEnabledForWorld(location.getWorld())) return null;
@ -3353,7 +3360,18 @@ public class GriefPrevention extends JavaPlugin
playerData.lastClaim = claim;
//if not in the wilderness, then apply claim rules (permissions, etc)
return claim.allowBreak(player, block.getType());
String cancel = claim.allowBreak(player, block.getType());
if(cancel != null && breakEvent != null)
{
PreventBlockBreakEvent preventionEvent = new PreventBlockBreakEvent(breakEvent);
Bukkit.getPluginManager().callEvent(preventionEvent);
if(preventionEvent.isCancelled())
{
cancel = null;
}
}
return cancel;
}
}

View File

@ -0,0 +1,47 @@
package me.ryanhamshire.GriefPrevention.events;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.block.BlockBreakEvent;
//if cancelled, GriefPrevention will allow a block to be broken which it would not have otherwise
public class PreventBlockBreakEvent extends Event implements Cancellable
{
private static final HandlerList handlers = new HandlerList();
private boolean cancelled = false;
private BlockBreakEvent innerEvent;
public static HandlerList getHandlerList()
{
return handlers;
}
public PreventBlockBreakEvent(BlockBreakEvent innerEvent)
{
this.innerEvent = innerEvent;
}
public BlockBreakEvent getInnerEvent()
{
return this.innerEvent;
}
@Override
public HandlerList getHandlers()
{
return handlers;
}
@Override
public boolean isCancelled()
{
return this.cancelled;
}
@Override
public void setCancelled(boolean cancelled)
{
this.cancelled = cancelled;
}
}