From 7a9480d4668973ed501930ee16e9f6b687836ac7 Mon Sep 17 00:00:00 2001 From: RoboMWM Date: Sat, 22 Jul 2017 00:19:09 -0700 Subject: [PATCH] Implement #114 --- .../CheckForPortalTrapTask.java | 15 ++++----- .../GriefPrevention/GriefPrevention.java | 4 +-- .../GriefPrevention/PlayerEventHandler.java | 31 ++++++++++++------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/me/ryanhamshire/GriefPrevention/CheckForPortalTrapTask.java b/src/me/ryanhamshire/GriefPrevention/CheckForPortalTrapTask.java index e8b5b4cd9..d10c37deb 100644 --- a/src/me/ryanhamshire/GriefPrevention/CheckForPortalTrapTask.java +++ b/src/me/ryanhamshire/GriefPrevention/CheckForPortalTrapTask.java @@ -23,6 +23,7 @@ import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; +import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.scheduler.BukkitRunnable; //players can be "trapped" in a portal frame if they don't have permission to break @@ -35,24 +36,24 @@ class CheckForPortalTrapTask extends BukkitRunnable private Player player; //where to send the player back to if he hasn't left the portal frame - //private Location returnLocation; + private Location returnLocation; - public CheckForPortalTrapTask(Player player, GriefPrevention plugin) + public CheckForPortalTrapTask(Player player, GriefPrevention plugin, Location locationToReturn) { this.player = player; this.instance = plugin; + this.returnLocation = locationToReturn; + player.setMetadata("GP_PORTALRESCUE", new FixedMetadataValue(instance, locationToReturn)); } @Override public void run() { - //if player has logged out, do nothing - if(!player.isOnline()) + if(player.isOnline() && player.getPortalCooldown() >= 10) { - instance.portalReturnTaskMap.remove(player.getUniqueId()); - return; + player.teleport(returnLocation); + player.removeMetadata("GP_PORTALRESCUE", instance); } - player.setPortalCooldown(0); instance.portalReturnTaskMap.remove(player.getUniqueId()); } } diff --git a/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java b/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java index e739ff388..058e43418 100644 --- a/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java +++ b/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java @@ -3704,10 +3704,10 @@ public void run() //Track scheduled "rescues" so we can cancel them if the player happens to teleport elsewhere so we can cancel it. ConcurrentHashMap portalReturnTaskMap = new ConcurrentHashMap(); - public void startRescueTask(Player player) + public void startRescueTask(Player player, Location location) { //Schedule task to reset player's portal cooldown after 20 seconds - BukkitTask task = new CheckForPortalTrapTask(player, this).runTaskLater(GriefPrevention.instance, 400L); + BukkitTask task = new CheckForPortalTrapTask(player, this, location).runTaskLater(GriefPrevention.instance, 400L); //Cancel existing rescue task if (portalReturnTaskMap.containsKey(player.getUniqueId())) diff --git a/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java b/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java index 1bce18ad9..e81dfcd32 100644 --- a/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java +++ b/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java @@ -766,17 +766,18 @@ else if(address.equals(playerData.ipAddress.toString())) //create a thread to load ignore information new IgnoreLoaderThread(playerID, playerData.ignoredPlayers).start(); - //is he possibly stuck in a portal frame? - //Because people can't read update notes, this try-catch will be here for a while - try + //is he stuck in a portal frame? + if (player.hasMetadata("GP_PORTALRESCUE")) { - player.setPortalCooldown(0); - } - catch (NoSuchMethodError e) - { - instance.getLogger().severe("Nether portal trap rescues will not function and you will receive a nice stack trace every time a player uses a nether portal."); - instance.getLogger().severe("Please update your server mod (Craftbukkit/Spigot/Paper), as mentioned in the update notes."); - instance.getServer().dispatchCommand(instance.getServer().getConsoleSender(), "version"); + new BukkitRunnable() + { + @Override + public void run() + { + player.teleport((Location)player.getMetadata("GP_PORTALRESCUE").get(0).value()); + player.removeMetadata("GP_PORTALRESCUE", instance); + } + }.runTaskLater(instance, 1L); } @@ -857,6 +858,14 @@ void onPlayerQuit(PlayerQuitEvent event) UUID playerID = player.getUniqueId(); PlayerData playerData = this.dataStore.getPlayerData(playerID); boolean isBanned; + + //If player is not trapped in a portal and has a pending rescue task, remove the associated metadata + //Why 9? No idea why, but this is decremented by 1 when the player disconnects. + if (player.getPortalCooldown() < 9) + { + player.removeMetadata("GP_PORTALRESCUE", instance); + } + if(playerData.wasKicked) { isBanned = player.isBanned(); @@ -997,7 +1006,7 @@ void onPlayerPortal(PlayerPortalEvent event) if(event.getCause() == TeleportCause.NETHER_PORTAL) { //FEATURE: when players get trapped in a nether portal, send them back through to the other side - instance.startRescueTask(player); + instance.startRescueTask(player, player.getLocation()); //don't track in worlds where claims are not enabled if(!instance.claimsEnabledForWorld(event.getTo().getWorld())) return;