From e08db3d12b5fe5b1caa7c6bc1ac5b2118f61db1e Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Tue, 21 Jun 2016 03:02:41 +1000 Subject: [PATCH] Check teleportation on command location prefix --- .../bukkit/object/BukkitPlayer.java | 36 +++++++++++++++++++ .../plot/commands/MainCommand.java | 19 ++++++---- .../plot/object/PlotPlayer.java | 11 ++++++ 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java b/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java index a6661c541..18db247fd 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitPlayer.java @@ -12,9 +12,12 @@ import org.bukkit.Effect; import org.bukkit.GameMode; import org.bukkit.WeatherType; import org.bukkit.entity.Player; +import org.bukkit.event.EventException; +import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import java.util.UUID; +import org.bukkit.plugin.RegisteredListener; public class BukkitPlayer extends PlotPlayer { @@ -57,6 +60,39 @@ public class BukkitPlayer extends PlotPlayer { return this.player.getLastPlayed(); } + @Override + public boolean canTeleport(Location loc) { + org.bukkit.Location to = BukkitUtil.getLocation(loc); + org.bukkit.Location from = player.getLocation(); + PlayerTeleportEvent event = new PlayerTeleportEvent(player, from, to); + RegisteredListener[] listeners = event.getHandlers().getRegisteredListeners(); + for (RegisteredListener listener : listeners) { + if (listener.getPlugin().getName().equals("PlotSquared")) { + continue; + } + try { + listener.callEvent(event); + } catch (EventException e) { + e.printStackTrace(); + } + } + if (event.isCancelled() || !event.getTo().equals(to)) { + return false; + } + event = new PlayerTeleportEvent(player, to, from); + for (RegisteredListener listener : listeners) { + if (listener.getPlugin().getName().equals("PlotSquared")) { + continue; + } + try { + listener.callEvent(event); + } catch (EventException e) { + e.printStackTrace(); + } + } + return true; + } + @Override public boolean hasPermission(String permission) { if (this.offline && EconHandler.manager != null) { diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java index 15949419e..05890fe3a 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java @@ -206,13 +206,18 @@ public class MainCommand extends Command { PlotArea area = player.getApplicablePlotArea(); Plot newPlot = Plot.fromString(area, args[0]); if (newPlot != null && (player instanceof ConsolePlayer || newPlot.getArea().equals(area) || Permissions.hasPermission(player, C.PERMISSION_ADMIN)) && !newPlot.isDenied(player.getUUID())) { - // Save meta - loc = player.getMeta("location"); - plot = player.getMeta("lastplot"); - tp = true; - // Set loc - player.setMeta("location", newPlot.getBottomAbs()); - player.setMeta("lastplot", newPlot); + Location newLoc = newPlot.getCenter(); + if (player.canTeleport(newLoc)) { + // Save meta + loc = player.getMeta("location"); + plot = player.getMeta("lastplot"); + tp = true; + // Set loc + player.setMeta("location", newLoc); + player.setMeta("lastplot", newPlot); + } else { + C.BORDER.send(player); + } // Trim command args = Arrays.copyOfRange(args, 1, args.length); } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java index 4b7725827..920960ac0 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java @@ -250,6 +250,17 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer { */ public abstract UUID getUUID(); + public boolean canTeleport(Location loc) { + Location current = getLocationFull(); + teleport(loc); + boolean result = true; + if (!getLocation().equals(loc)) { + result = false; + } + teleport(current); + return result; + } + /** * Teleport the player to a location. * @param location the target location