Check teleportation on command location prefix

This commit is contained in:
Jesse Boyd 2016-06-21 03:02:41 +10:00
parent 2ff4e07919
commit e08db3d12b
3 changed files with 59 additions and 7 deletions

View File

@ -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) {

View File

@ -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);
}

View File

@ -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