From 260db50cc3c65306f81b7cf1d37c2f415b2734cb Mon Sep 17 00:00:00 2001 From: ryanhamshire Date: Sun, 31 Jan 2016 09:02:27 -0800 Subject: [PATCH] API - GPFlags Support Updated the API to support two new GriefPreventionFlags flags. --- .../GriefPrevention/EntityEventHandler.java | 31 ++++++---- .../GriefPrevention/GriefPrevention.java | 9 ++- .../GriefPrevention/PlayerRescueTask.java | 19 ++++-- .../GriefPrevention/TextMode.java | 2 +- .../events/ProtectDeathDropsEvent.java | 49 +++++++++++++++ .../events/SaveTrappedPlayerEvent.java | 61 +++++++++++++++++++ 6 files changed, 152 insertions(+), 19 deletions(-) create mode 100644 src/me/ryanhamshire/GriefPrevention/events/ProtectDeathDropsEvent.java create mode 100644 src/me/ryanhamshire/GriefPrevention/events/SaveTrappedPlayerEvent.java diff --git a/src/me/ryanhamshire/GriefPrevention/EntityEventHandler.java b/src/me/ryanhamshire/GriefPrevention/EntityEventHandler.java index 3a912ad..debac18 100644 --- a/src/me/ryanhamshire/GriefPrevention/EntityEventHandler.java +++ b/src/me/ryanhamshire/GriefPrevention/EntityEventHandler.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.UUID; import me.ryanhamshire.GriefPrevention.events.PreventPvPEvent; +import me.ryanhamshire.GriefPrevention.events.ProtectDeathDropsEvent; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -442,20 +443,26 @@ public class EntityEventHandler implements Listener if((isPvPWorld && GriefPrevention.instance.config_lockDeathDropsInPvpWorlds) || (!isPvPWorld && GriefPrevention.instance.config_lockDeathDropsInNonPvpWorlds)) { - //remember information about these drops so that they can be marked when they spawn as items - long expirationTime = System.currentTimeMillis() + 3000; //now + 3 seconds - Location deathLocation = player.getLocation(); - UUID playerID = player.getUniqueId(); - List drops = event.getDrops(); - for(ItemStack stack : drops) + Claim claim = this.dataStore.getClaimAt(player.getLocation(), false, playerData.lastClaim); + ProtectDeathDropsEvent protectionEvent = new ProtectDeathDropsEvent(claim); + Bukkit.getPluginManager().callEvent(protectionEvent); + if(!protectionEvent.isCancelled()) { - GriefPrevention.instance.pendingItemWatchList.add( - new PendingItemProtection(deathLocation, playerID, expirationTime, stack)); + //remember information about these drops so that they can be marked when they spawn as items + long expirationTime = System.currentTimeMillis() + 3000; //now + 3 seconds + Location deathLocation = player.getLocation(); + UUID playerID = player.getUniqueId(); + List drops = event.getDrops(); + for(ItemStack stack : drops) + { + GriefPrevention.instance.pendingItemWatchList.add( + new PendingItemProtection(deathLocation, playerID, expirationTime, stack)); + } + + //allow the player to receive a message about how to unlock any drops + playerData.dropsAreUnlocked = false; + playerData.receivedDropUnlockAdvertisement = false; } - - //allow the player to receive a message about how to unlock any drops - playerData.dropsAreUnlocked = false; - playerData.receivedDropUnlockAdvertisement = false; } } diff --git a/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java b/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java index 57a4955..b52c4b1 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.SaveTrappedPlayerEvent; import net.milkbowl.vault.economy.Economy; import org.bukkit.Achievement; @@ -2194,8 +2195,12 @@ public class GriefPrevention extends JavaPlugin return true; } + //rescue destination may be set by GPFlags, ask to find out + SaveTrappedPlayerEvent event = new SaveTrappedPlayerEvent(claim); + Bukkit.getPluginManager().callEvent(event); + //if the player is in an administrative claim, he should contact an admin - if(claim.isAdminClaim()) + if(claim.isAdminClaim() && event.getDestination() != null) { GriefPrevention.sendMessage(player, TextMode.Err, Messages.TrappedWontWorkHere); return true; @@ -2205,7 +2210,7 @@ public class GriefPrevention extends JavaPlugin GriefPrevention.sendMessage(player, TextMode.Instr, Messages.RescuePending); //create a task to rescue this player in a little while - PlayerRescueTask task = new PlayerRescueTask(player, player.getLocation()); + PlayerRescueTask task = new PlayerRescueTask(player, player.getLocation(), event.getDestination()); this.getServer().getScheduler().scheduleSyncDelayedTask(this, task, 200L); //20L ~ 1 second return true; diff --git a/src/me/ryanhamshire/GriefPrevention/PlayerRescueTask.java b/src/me/ryanhamshire/GriefPrevention/PlayerRescueTask.java index 0cdfe14..44a6802 100644 --- a/src/me/ryanhamshire/GriefPrevention/PlayerRescueTask.java +++ b/src/me/ryanhamshire/GriefPrevention/PlayerRescueTask.java @@ -29,13 +29,17 @@ class PlayerRescueTask implements Runnable //original location where /trapped was used private Location location; + //rescue destination, may be decided at instantiation or at execution + private Location destination; + //player data private Player player; - public PlayerRescueTask(Player player, Location location) + public PlayerRescueTask(Player player, Location location, Location destination) { this.player = player; - this.location = location; + this.location = location; + this.destination = destination; } @Override @@ -56,9 +60,16 @@ class PlayerRescueTask implements Runnable } //otherwise find a place to teleport him - Location destination = GriefPrevention.instance.ejectPlayer(this.player); + if(this.destination == null) + { + this.destination = GriefPrevention.instance.ejectPlayer(this.player); + } + else + { + player.teleport(this.destination); + } //log entry, in case admins want to investigate the "trap" - GriefPrevention.AddLogEntry("Rescued trapped player " + player.getName() + " from " + GriefPrevention.getfriendlyLocationString(this.location) + " to " + GriefPrevention.getfriendlyLocationString(destination) + "."); + GriefPrevention.AddLogEntry("Rescued trapped player " + player.getName() + " from " + GriefPrevention.getfriendlyLocationString(this.location) + " to " + GriefPrevention.getfriendlyLocationString(this.destination) + "."); } } diff --git a/src/me/ryanhamshire/GriefPrevention/TextMode.java b/src/me/ryanhamshire/GriefPrevention/TextMode.java index 2a0daee..1fd288f 100644 --- a/src/me/ryanhamshire/GriefPrevention/TextMode.java +++ b/src/me/ryanhamshire/GriefPrevention/TextMode.java @@ -21,7 +21,7 @@ package me.ryanhamshire.GriefPrevention; import org.bukkit.ChatColor; //just a few constants for chat color codes -class TextMode +public class TextMode { final static ChatColor Info = ChatColor.AQUA; final static ChatColor Instr = ChatColor.YELLOW; diff --git a/src/me/ryanhamshire/GriefPrevention/events/ProtectDeathDropsEvent.java b/src/me/ryanhamshire/GriefPrevention/events/ProtectDeathDropsEvent.java new file mode 100644 index 0000000..cfba989 --- /dev/null +++ b/src/me/ryanhamshire/GriefPrevention/events/ProtectDeathDropsEvent.java @@ -0,0 +1,49 @@ +package me.ryanhamshire.GriefPrevention.events; + +import me.ryanhamshire.GriefPrevention.Claim; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +//if cancelled, GriefPrevention will not protect items dropped by a player on death +public class ProtectDeathDropsEvent extends Event implements Cancellable +{ + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled = false; + + public static HandlerList getHandlerList() + { + return handlers; + } + + Claim claim; + + public ProtectDeathDropsEvent(Claim claim) + { + this.claim = claim; + } + + public Claim getClaim() + { + return this.claim; + } + + @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 diff --git a/src/me/ryanhamshire/GriefPrevention/events/SaveTrappedPlayerEvent.java b/src/me/ryanhamshire/GriefPrevention/events/SaveTrappedPlayerEvent.java new file mode 100644 index 0000000..46305f5 --- /dev/null +++ b/src/me/ryanhamshire/GriefPrevention/events/SaveTrappedPlayerEvent.java @@ -0,0 +1,61 @@ +package me.ryanhamshire.GriefPrevention.events; + +import me.ryanhamshire.GriefPrevention.Claim; + +import org.bukkit.Location; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +//if destination field is set, then GriefPrevention will send the player to that location instead of searching for one +public class SaveTrappedPlayerEvent extends Event implements Cancellable +{ + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled = false; + private Location destination = null; + + public static HandlerList getHandlerList() + { + return handlers; + } + + Claim claim; + + public SaveTrappedPlayerEvent(Claim claim) + { + this.claim = claim; + } + + public Location getDestination() + { + return destination; + } + + public void setDestination(Location destination) + { + this.destination = destination; + } + + public Claim getClaim() + { + return this.claim; + } + + @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