Implemented optional specification of teleport causes

This should have backwards-compatibility for any users of the API but would require an update from anything reimplementing (for whatever reason) the interfaces amended by this commit.
This commit is contained in:
zombachu 2020-01-02 18:29:12 -10:00
parent 0f39df0e0b
commit 3aa554c52b
18 changed files with 81 additions and 28 deletions

View File

@ -1,11 +1,14 @@
package com.github.intellectualsites.plotsquared.bukkit.object;
import com.github.intellectualsites.plotsquared.bukkit.util.BukkitEventUtil;
import com.github.intellectualsites.plotsquared.bukkit.util.BukkitUtil;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.object.Location;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.github.intellectualsites.plotsquared.plot.util.EconHandler;
import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import com.github.intellectualsites.plotsquared.plot.util.MathMan;
import com.github.intellectualsites.plotsquared.plot.util.PlotWeather;
import com.github.intellectualsites.plotsquared.plot.util.StringMan;
@ -193,13 +196,13 @@ public class BukkitPlayer extends PlotPlayer {
}
}
@Override public void teleport(@NotNull final Location location) {
@Override public void teleport(@NotNull final Location location, @NotNull final TeleportCause cause) {
if (Math.abs(location.getX()) >= 30000000 || Math.abs(location.getZ()) >= 30000000) {
return;
}
final org.bukkit.Location bukkitLocation = new org.bukkit.Location(BukkitUtil.getWorld(location.getWorld()), location.getX() + 0.5,
location.getY(), location.getZ() + 0.5, location.getYaw(), location.getPitch());
PaperLib.teleportAsync(player, bukkitLocation, PlayerTeleportEvent.TeleportCause.COMMAND);
PaperLib.teleportAsync(player, bukkitLocation, ((BukkitEventUtil) EventUtil.manager).getTeleportCause(cause));
}
@Override public String getName() {

View File

@ -25,11 +25,13 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.Rating;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -130,4 +132,14 @@ public final class BukkitEventUtil extends EventUtil {
}
return event.getRating();
}
public PlayerTeleportEvent.TeleportCause getTeleportCause(@NotNull final TeleportCause cause) {
switch (cause) {
case COMMAND:
return PlayerTeleportEvent.TeleportCause.COMMAND;
case PLUGIN:
return PlayerTeleportEvent.TeleportCause.PLUGIN;
default: return PlayerTeleportEvent.TeleportCause.UNKNOWN;
}
}
}

View File

@ -15,6 +15,7 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal3;
import com.github.intellectualsites.plotsquared.plot.object.SetupObject;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.github.intellectualsites.plotsquared.plot.util.ChunkManager;
import com.github.intellectualsites.plotsquared.plot.util.CmdConfirm;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
@ -134,7 +135,7 @@ import java.util.Set;
if (WorldUtil.IMP.isWorld(world)) {
PlotSquared.get().loadWorld(world, null);
Captions.SETUP_FINISHED.send(player);
player.teleport(WorldUtil.IMP.getSpawn(world));
player.teleport(WorldUtil.IMP.getSpawn(world), TeleportCause.COMMAND);
if (area.TERRAIN != 3) {
ChunkManager.largeRegionTask(world, region,
new RunnableVal<BlockVector2>() {
@ -257,7 +258,7 @@ import java.util.Set;
String world = SetupUtils.manager.setupWorld(object);
if (WorldUtil.IMP.isWorld(world)) {
Captions.SETUP_FINISHED.send(player);
player.teleport(WorldUtil.IMP.getSpawn(world));
player.teleport(WorldUtil.IMP.getSpawn(world), TeleportCause.COMMAND);
} else {
MainUtil.sendMessage(player,
"An error occurred while creating the world: "
@ -284,13 +285,13 @@ import java.util.Set;
}
if (WorldUtil.IMP.isWorld(pa.worldname)) {
if (!player.getLocation().getWorld().equals(pa.worldname)) {
player.teleport(WorldUtil.IMP.getSpawn(pa.worldname));
player.teleport(WorldUtil.IMP.getSpawn(pa.worldname), TeleportCause.COMMAND);
}
} else {
object.terrain = 0;
object.type = 0;
SetupUtils.manager.setupWorld(object);
player.teleport(WorldUtil.IMP.getSpawn(pa.worldname));
player.teleport(WorldUtil.IMP.getSpawn(pa.worldname), TeleportCause.COMMAND);
}
player.setMeta("area_create_area", pa);
MainUtil.sendMessage(player,
@ -468,7 +469,7 @@ import java.util.Set;
center.setY(1 + WorldUtil.IMP
.getHighestBlock(area.worldname, center.getX(), center.getZ()));
}
player.teleport(center);
player.teleport(center, TeleportCause.COMMAND);
return true;
case "delete":
case "remove":

View File

@ -12,6 +12,7 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.github.intellectualsites.plotsquared.plot.util.EconHandler;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.Permissions;
@ -82,7 +83,7 @@ public class Auto extends SubCommand {
final String schematic) {
Set<Plot> plots = player.getPlots();
if (!plots.isEmpty()) {
plots.iterator().next().teleportPlayer(player);
plots.iterator().next().teleportPlayer(player, TeleportCause.COMMAND);
} else {
autoClaimSafe(player, area, start, schematic);
}

View File

@ -12,6 +12,7 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotCluster;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.Permissions;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
@ -551,7 +552,7 @@ import java.util.UUID;
return false;
}
}
player.teleport(cluster.getHome());
player.teleport(cluster.getHome(), TeleportCause.COMMAND);
return MainUtil.sendMessage(player, Captions.CLUSTER_TELEPORTING);
}
case "i":

View File

@ -9,6 +9,7 @@ import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.Rating;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.Permissions;
@ -42,7 +43,7 @@ import java.util.UUID;
for (final Plot plot : plots) {
if ((!Settings.Done.REQUIRED_FOR_RATINGS || plot.hasFlag(Flags.DONE))
&& plot.isBasePlot() && (!plot.getLikes().containsKey(uuid))) {
plot.teleportPlayer(player);
plot.teleportPlayer(player, TeleportCause.COMMAND);
MainUtil.sendMessage(player, Captions.RATE_THIS);
return true;
}

View File

@ -5,6 +5,7 @@ import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.object.Location;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
/**
* @author manuelgu, altered by Citymonstret
@ -20,7 +21,7 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
if (plot == null) {
return sendMessage(player, Captions.NOT_IN_PLOT);
}
player.teleport(plot.getCenter());
player.teleport(plot.getCenter(), TeleportCause.COMMAND);
return true;
}
}

View File

@ -12,6 +12,7 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotInventory;
import com.github.intellectualsites.plotsquared.plot.object.PlotItemStack;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.Rating;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.MathMan;
@ -55,7 +56,7 @@ import java.util.UUID;
if ((!Settings.Done.REQUIRED_FOR_RATINGS || p.hasFlag(Flags.DONE)) && p
.isBasePlot() && (!p.getRatings().containsKey(uuid)) && !p
.isAdded(uuid)) {
p.teleportPlayer(player);
p.teleportPlayer(player, TeleportCause.COMMAND);
MainUtil.sendMessage(player, Captions.RATE_THIS);
return true;
}

View File

@ -11,6 +11,7 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import com.github.intellectualsites.plotsquared.plot.object.PlotMessage;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.SetupObject;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.SetupUtils;
import com.github.intellectualsites.plotsquared.plot.util.StringMan;
@ -325,7 +326,7 @@ import java.util.UUID;
world = object.setupManager.setupWorld(object);
}
try {
player.teleport(WorldUtil.IMP.getSpawn(world));
player.teleport(WorldUtil.IMP.getSpawn(world), TeleportCause.COMMAND);
} catch (Exception e) {
player.sendMessage("&cAn error occurred. See console for more information");
e.printStackTrace();

View File

@ -13,6 +13,7 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotManager;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.SetupObject;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.SetupUtils;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
@ -167,7 +168,7 @@ import java.util.zip.ZipOutputStream;
SetupUtils.manager.setupWorld(setup);
GlobalBlockQueue.IMP.addEmptyTask(() -> {
MainUtil.sendMessage(player, "Done!");
player.teleport(WorldUtil.IMP.getSpawn(world));
player.teleport(WorldUtil.IMP.getSpawn(world), TeleportCause.COMMAND);
});
return true;
}

View File

@ -11,6 +11,7 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal2;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal3;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.MathMan;
import com.github.intellectualsites.plotsquared.plot.util.Permissions;
@ -145,7 +146,7 @@ import java.util.concurrent.CompletableFuture;
}
}
confirm.run(this, () -> {
if (plot.teleportPlayer(player)) {
if (plot.teleportPlayer(player, TeleportCause.COMMAND)) {
whenDone.run(Visit.this, CommandResult.SUCCESS);
} else {
whenDone.run(Visit.this, CommandResult.FAILURE);

View File

@ -78,7 +78,7 @@ public class ConsolePlayer extends PlotPlayer {
PlotSquared.log(message);
}
@Override public void teleport(Location location) {
@Override public void teleport(Location location, TeleportCause cause) {
setMeta(PlotPlayer.META_LAST_PLOT, location.getPlot());
setMeta(PlotPlayer.META_LOCATION, location);
}

View File

@ -1524,7 +1524,7 @@ public class Plot {
setSign(player.getName());
MainUtil.sendMessage(player, Captions.CLAIMED);
if (teleport && Settings.Teleport.ON_CLAIM) {
teleportPlayer(player);
teleportPlayer(player, TeleportCause.COMMAND);
}
PlotArea plotworld = getArea();
if (plotworld.SCHEMATIC_ON_CLAIM) {
@ -2809,6 +2809,17 @@ public class Plot {
* @return if the teleport succeeded
*/
public boolean teleportPlayer(final PlotPlayer player) {
return teleportPlayer(player, TeleportCause.PLUGIN);
}
/**
* Teleport a player to a plot and send them the teleport message.
*
* @param player the player
* @param cause the cause of the teleport
* @return if the teleport succeeded
*/
public boolean teleportPlayer(final PlotPlayer player, TeleportCause cause) {
Plot plot = this.getBasePlot(false);
boolean result = EventUtil.manager.callTeleport(player, player.getLocation(), plot);
if (result) {
@ -2818,14 +2829,12 @@ public class Plot {
} else {
location = this.getDefaultHome(false);
}
if (Settings.Teleport.DELAY == 0 || Permissions
.hasPermission(player, "plots.teleport.delay.bypass")) {
if (Settings.Teleport.DELAY == 0 || Permissions.hasPermission(player, "plots.teleport.delay.bypass")) {
MainUtil.sendMessage(player, Captions.TELEPORTED_TO_PLOT);
player.teleport(location);
player.teleport(location, cause);
return true;
}
MainUtil
.sendMessage(player, Captions.TELEPORT_IN_SECONDS, Settings.Teleport.DELAY + "");
MainUtil.sendMessage(player, Captions.TELEPORT_IN_SECONDS, Settings.Teleport.DELAY + "");
final String name = player.getName();
TaskManager.TELEPORT_QUEUE.add(name);
TaskManager.runTaskLater(() -> {
@ -2836,7 +2845,7 @@ public class Plot {
TaskManager.TELEPORT_QUEUE.remove(name);
if (player.isOnline()) {
MainUtil.sendMessage(player, Captions.TELEPORTED_TO_PLOT);
player.teleport(location);
player.teleport(location, cause);
}
}, Settings.Teleport.DELAY * 20);
return true;

View File

@ -373,7 +373,17 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer {
*
* @param location the target location
*/
public abstract void teleport(Location location);
public void teleport(Location location) {
teleport(location, TeleportCause.PLUGIN);
}
/**
* Teleport this player to a location.
*
* @param location the target location
* @param cause the cause of the teleport
*/
public abstract void teleport(Location location, TeleportCause cause);
/**
* Kick this player to a location

View File

@ -0,0 +1,7 @@
package com.github.intellectualsites.plotsquared.plot.object;
public enum TeleportCause {
COMMAND,
PLUGIN,
UNKNOWN
}

View File

@ -8,6 +8,7 @@ import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import org.jetbrains.annotations.NotNull;
@ -50,9 +51,9 @@ public class SinglePlot extends Plot {
return (SinglePlotArea) super.getArea();
}
public boolean teleportPlayer(final PlotPlayer player) {
public boolean teleportPlayer(final PlotPlayer player, TeleportCause cause) {
if (isLoaded()) {
return super.teleportPlayer(player);
return super.teleportPlayer(player, cause);
} else {
Captions.NOT_LOADED.send(player);
return false;

View File

@ -10,6 +10,7 @@ import com.github.intellectualsites.plotsquared.nukkit.util.NukkitUtil;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.object.Location;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import java.util.Collections;
import java.util.UUID;
@ -107,7 +108,7 @@ public class NukkitPlayer extends PlotPlayer {
}
}
@Override public void teleport(Location to) {
@Override public void teleport(Location to, TeleportCause cause) {
if (Math.abs(to.getX()) >= 30000000 || Math.abs(to.getZ()) >= 30000000) {
return;
}

View File

@ -5,6 +5,7 @@ import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.commands.RequiredType;
import com.github.intellectualsites.plotsquared.plot.object.Location;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.TeleportCause;
import com.github.intellectualsites.plotsquared.plot.util.PlotGameMode;
import com.github.intellectualsites.plotsquared.plot.util.PlotWeather;
import com.github.intellectualsites.plotsquared.plot.util.StringMan;
@ -85,7 +86,7 @@ public class SpongePlayer extends PlotPlayer {
}
}
@Override public void teleport(Location location) {
@Override public void teleport(Location location, TeleportCause cause) {
if ((Math.abs(location.getX()) >= 30000000) || (Math.abs(location.getZ()) >= 30000000)) {
return;
}