From 86a18e2f049588b685045133c3898190d93e3581 Mon Sep 17 00:00:00 2001 From: ryanhamshire Date: Wed, 17 Aug 2016 11:09:05 -0700 Subject: [PATCH] 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. --- .../GriefPrevention/BlockEventHandler.java | 2 +- .../GriefPrevention/GriefPrevention.java | 88 +++++++++++-------- .../events/PreventBlockBreakEvent.java | 47 ++++++++++ 3 files changed, 101 insertions(+), 36 deletions(-) create mode 100644 src/me/ryanhamshire/GriefPrevention/events/PreventBlockBreakEvent.java diff --git a/src/me/ryanhamshire/GriefPrevention/BlockEventHandler.java b/src/me/ryanhamshire/GriefPrevention/BlockEventHandler.java index 51bd483..2af16af 100644 --- a/src/me/ryanhamshire/GriefPrevention/BlockEventHandler.java +++ b/src/me/ryanhamshire/GriefPrevention/BlockEventHandler.java @@ -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); diff --git a/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java b/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java index 5846661..c9e7a89 100644 --- a/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java +++ b/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java @@ -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; @@ -3320,42 +3322,58 @@ public class GriefPrevention extends JavaPlugin public String allowBreak(Player player, Block block, Location location) { - if(!GriefPrevention.instance.claimsEnabledForWorld(location.getWorld())) return null; - - PlayerData playerData = this.dataStore.getPlayerData(player.getUniqueId()); - Claim claim = this.dataStore.getClaimAt(location, false, playerData.lastClaim); - - //exception: administrators in ignore claims mode, and special player accounts created by server mods - if(playerData.ignoreClaims || GriefPrevention.instance.config_mods_ignoreClaimsAccounts.contains(player.getName())) return null; - - //wilderness rules - if(claim == null) - { - //no building in the wilderness in creative mode - if(this.creativeRulesApply(location) || this.config_claims_worldModes.get(location.getWorld()) == ClaimsMode.SurvivalRequiringClaims) - { - String reason = this.dataStore.getMessage(Messages.NoBuildOutsideClaims); - if(player.hasPermission("griefprevention.ignoreclaims")) - reason += " " + this.dataStore.getMessage(Messages.IgnoreClaimsAdvertisement); - reason += " " + this.dataStore.getMessage(Messages.CreativeBasicsVideo2, DataStore.CREATIVE_VIDEO_URL); - return reason; - } - - //but it's fine in survival mode - else - { - return null; - } - } - else - { - //cache the claim for later reference - playerData.lastClaim = claim; - - //if not in the wilderness, then apply claim rules (permissions, etc) - return claim.allowBreak(player, block.getType()); - } + 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; + + PlayerData playerData = this.dataStore.getPlayerData(player.getUniqueId()); + Claim claim = this.dataStore.getClaimAt(location, false, playerData.lastClaim); + + //exception: administrators in ignore claims mode, and special player accounts created by server mods + if(playerData.ignoreClaims || GriefPrevention.instance.config_mods_ignoreClaimsAccounts.contains(player.getName())) return null; + + //wilderness rules + if(claim == null) + { + //no building in the wilderness in creative mode + if(this.creativeRulesApply(location) || this.config_claims_worldModes.get(location.getWorld()) == ClaimsMode.SurvivalRequiringClaims) + { + String reason = this.dataStore.getMessage(Messages.NoBuildOutsideClaims); + if(player.hasPermission("griefprevention.ignoreclaims")) + reason += " " + this.dataStore.getMessage(Messages.IgnoreClaimsAdvertisement); + reason += " " + this.dataStore.getMessage(Messages.CreativeBasicsVideo2, DataStore.CREATIVE_VIDEO_URL); + return reason; + } + + //but it's fine in survival mode + else + { + return null; + } + } + else + { + //cache the claim for later reference + playerData.lastClaim = claim; + + //if not in the wilderness, then apply claim rules (permissions, etc) + 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; + } + } //restores nature in multiple chunks, as described by a claim instance //this restores all chunks which have ANY number of claim blocks from this claim in them diff --git a/src/me/ryanhamshire/GriefPrevention/events/PreventBlockBreakEvent.java b/src/me/ryanhamshire/GriefPrevention/events/PreventBlockBreakEvent.java new file mode 100644 index 0000000..bc14d1d --- /dev/null +++ b/src/me/ryanhamshire/GriefPrevention/events/PreventBlockBreakEvent.java @@ -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; + } +} \ No newline at end of file