From 5f9dce1a075a51786cc2b8f49d76573bd16c6f01 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Wed, 11 Feb 2015 17:08:14 +1100 Subject: [PATCH] Cancel teleport on damage or move --- .../com/intellectualcrafters/plot/PlotMain.java | 7 +++++++ .../com/intellectualcrafters/plot/config/C.java | 2 +- .../plot/listeners/PlayerEvents.java | 16 ++++++++++++++++ .../plot/util/TaskManager.java | 5 +++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java index 8db49821d..3bda9ba8f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java @@ -598,9 +598,16 @@ public class PlotMain extends JavaPlugin implements Listener { final World world = player.getWorld(); final int x = loc.getBlockX(); final int z = loc.getBlockZ(); + final String name = player.getName(); + TaskManager.TELEPORT_QUEUE.add(name); TaskManager.runTaskLater(new Runnable() { @Override public void run() { + if (!TaskManager.TELEPORT_QUEUE.contains(name)) { + PlayerFunctions.sendMessage(player, C.TELEPORT_FAILED); + return; + } + TaskManager.TELEPORT_QUEUE.remove(name); if (!player.isOnline()) { return; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java index 8f0b5fe7c..08099f3de 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -261,7 +261,7 @@ public enum C { TELEPORTED_TO_PLOT("&6You have been teleported"), TELEPORTED_TO_ROAD("&cYou got teleported to the road"), TELEPORT_IN_SECONDS("&6Teleporting in %s seconds. Do not move..."), - TELEPORT_FAILED("&cTeleportation cancelled due to movement"), + TELEPORT_FAILED("&cTeleportation cancelled due to movement or damage"), /* * Set Block */ diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java index fcbfc6c7c..fc69ebf97 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -99,6 +99,7 @@ import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.object.StringWrapper; import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.PlotHelper; +import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.UUIDHandler; /** @@ -161,6 +162,12 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi final Location q = new Location(t.getWorld(), t.getBlockX(), 0, t.getZ()); if ((f.getBlockX() != q.getBlockX()) || (f.getBlockZ() != q.getBlockZ())) { + if (Settings.TELEPORT_DELAY != 0 && TaskManager.TELEPORT_QUEUE.size() > 0) { + String name = player.getName(); + if (TaskManager.TELEPORT_QUEUE.contains(name)) { + TaskManager.TELEPORT_QUEUE.remove(name); + } + } if (!isPlotWorld(player.getWorld())) { return; } @@ -916,6 +923,15 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi final Location l = e.getEntity().getLocation(); final Entity d = e.getDamager(); final Entity a = e.getEntity(); + + if (Settings.TELEPORT_DELAY != 0 && TaskManager.TELEPORT_QUEUE.size() > 0 && a instanceof Player) { + Player player = (Player) a; + String name = player.getName(); + if (TaskManager.TELEPORT_QUEUE.contains(name)) { + TaskManager.TELEPORT_QUEUE.remove(name); + } + } + if (isPlotWorld(l)) { if (d instanceof Player) { final Player p = (Player) d; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/TaskManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/TaskManager.java index daa40b6eb..6cfa25031 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/TaskManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/TaskManager.java @@ -1,8 +1,13 @@ package com.intellectualcrafters.plot.util; +import java.util.HashSet; + import com.intellectualcrafters.plot.PlotMain; public class TaskManager { + + public static HashSet TELEPORT_QUEUE = new HashSet<>(); + public static void runTask(final Runnable r) { PlotMain.getMain().getServer().getScheduler().runTaskAsynchronously(PlotMain.getMain(), r); }