From 601e9e171ed5a8a4041009869e992e23d65f9d20 Mon Sep 17 00:00:00 2001 From: 1wair <1wairesd.com.industries@gmail.com> Date: Fri, 27 Nov 2026 16:23:57 +0100 Subject: [PATCH] Report the initiating user in PreTeleportEvent for /tp, /tpo, /tpohere, /tpall Add IAsyncTeleport#now overloads taking the teleporter (default methods, so the API stays compatible), pass the command sender from /tp , /tp , /tpo , /tpohere and /tpall, and report the warmup owner as teleporter when a timed teleport fires. --- .../earth2me/essentials/AsyncTeleport.java | 25 ++++++++++++++--- .../essentials/AsyncTimedTeleport.java | 3 +- .../essentials/api/IAsyncTeleport.java | 28 +++++++++++++++++++ .../essentials/commands/Commandtp.java | 4 +-- .../essentials/commands/Commandtpall.java | 3 +- .../essentials/commands/Commandtpo.java | 2 +- .../essentials/commands/Commandtpohere.java | 2 +- .../api/events/teleport/PreTeleportEvent.java | 4 +++ 8 files changed, 61 insertions(+), 10 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java b/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java index b5e687f255..cad476ed6e 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java +++ b/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java @@ -110,21 +110,31 @@ private void warnUser(final IUser user, final double delay) { @Override public void now(final Location loc, final boolean cooldown, final TeleportCause cause, final CompletableFuture future) { + now((IUser) null, loc, cooldown, cause, future); + } + + @Override + public void now(final IUser teleporter, final Location loc, final boolean cooldown, final TeleportCause cause, final CompletableFuture future) { if (cooldown && cooldown(false, future)) { return; } final ITarget target = new LocationTarget(loc); - nowAsync(teleportOwner, target, cause, future); + nowAsync(teleporter, teleportOwner, target, cause, future); } @Override public void now(final Player entity, final boolean cooldown, final TeleportCause cause, final CompletableFuture future) { + now((IUser) null, entity, cooldown, cause, future); + } + + @Override + public void now(final IUser teleporter, final Player entity, final boolean cooldown, final TeleportCause cause, final CompletableFuture future) { if (cooldown && cooldown(false, future)) { future.complete(false); return; } final ITarget target = new PlayerTarget(entity); - nowAsync(teleportOwner, target, cause, future); + nowAsync(teleporter, teleportOwner, target, cause, future); future.thenAccept(success -> { if (success) { teleportOwner.sendTl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ()); @@ -153,9 +163,16 @@ private void runOnMain(final Runnable runnable) throws ExecutionException, Inter } protected void nowAsync(final IUser teleportee, final ITarget target, final TeleportCause cause, final CompletableFuture future) { + nowAsync(null, teleportee, target, cause, future); + } + + /** + * @param teleporter the user who initiated the teleport, or null to report the teleportee + */ + protected void nowAsync(final IUser teleporter, final IUser teleportee, final ITarget target, final TeleportCause cause, final CompletableFuture future) { cancel(false); - final PreTeleportEvent event = new PreTeleportEvent(teleportee, cause, target); + final PreTeleportEvent event = new PreTeleportEvent(teleporter != null ? teleporter : teleportee, teleportee, cause, target); Bukkit.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { future.complete(false); @@ -339,7 +356,7 @@ private void teleportOther(final IUser teleporter, final IUser teleportee, final return; } - nowAsync(teleportee, target, cause, future); + nowAsync(teleporter, teleportee, target, cause, future); if (teleporter != null && cashCharge != null) { cashCharge.charge(teleporter, future); if (future.isCompletedExceptionally()) { diff --git a/Essentials/src/main/java/com/earth2me/essentials/AsyncTimedTeleport.java b/Essentials/src/main/java/com/earth2me/essentials/AsyncTimedTeleport.java index d1c37423c0..2589f82991 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/AsyncTimedTeleport.java +++ b/Essentials/src/main/java/com/earth2me/essentials/AsyncTimedTeleport.java @@ -123,7 +123,8 @@ public void run() { if (timer_respawn) { teleport.respawnNow(teleportUser, timer_cause, parentFuture); } else { - teleport.nowAsync(teleportUser, timer_teleportTarget, timer_cause, parentFuture); + // teleportOwner started the warmup (e.g. the /tphere sender), report it as the teleporter + teleport.nowAsync(teleportOwner, teleportUser, timer_teleportTarget, timer_cause, parentFuture); } parentFuture.thenAccept(success -> { if (timer_chargeFor != null) { diff --git a/Essentials/src/main/java/com/earth2me/essentials/api/IAsyncTeleport.java b/Essentials/src/main/java/com/earth2me/essentials/api/IAsyncTeleport.java index a6e6b82caf..03138b7d09 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/api/IAsyncTeleport.java +++ b/Essentials/src/main/java/com/earth2me/essentials/api/IAsyncTeleport.java @@ -34,6 +34,34 @@ public interface IAsyncTeleport { */ void now(Player entity, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture future); + /** + * Same as {@link #now(Location, boolean, PlayerTeleportEvent.TeleportCause, CompletableFuture)}, but reports + * {@code teleporter} as the user who initiated the teleport in {@link net.ess3.api.events.teleport.PreTeleportEvent}. + * + * @param teleporter - The user who initiated the teleport, or null if unknown (e.g. console) + * @param loc - Where should the player end up + * @param cooldown - If cooldown should be enforced + * @param cause - The reported teleportPlayer cause + * @param future - Future which is completed with the success status of the execution + */ + default void now(IUser teleporter, Location loc, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture future) { + now(loc, cooldown, cause, future); + } + + /** + * Same as {@link #now(Player, boolean, PlayerTeleportEvent.TeleportCause, CompletableFuture)}, but reports + * {@code teleporter} as the user who initiated the teleport in {@link net.ess3.api.events.teleport.PreTeleportEvent}. + * + * @param teleporter - The user who initiated the teleport, or null if unknown (e.g. console) + * @param entity - Where should the player end up + * @param cooldown - If cooldown should be enforced + * @param cause - The reported teleportPlayer cause + * @param future - Future which is completed with the success status of the execution + */ + default void now(IUser teleporter, Player entity, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture future) { + now(entity, cooldown, cause, future); + } + /** * Used to skip all safety checks while teleporting a player asynchronously. * diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtp.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtp.java index ad764a43f6..a2aefe0531 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtp.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtp.java @@ -82,7 +82,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new TranslatableException("teleportDisabled", target2.getDisplayName()); } user.sendTl("teleporting", locposother.getWorld().getName(), locposother.getBlockX(), locposother.getBlockY(), locposother.getBlockZ()); - target2.getAsyncTeleport().now(locposother, false, TeleportCause.COMMAND, future); + target2.getAsyncTeleport().now(user, locposother, false, TeleportCause.COMMAND, future); future.thenAccept(success -> { if (success) { target2.sendTl("teleporting", locposother.getWorld().getName(), locposother.getBlockX(), locposother.getBlockY(), locposother.getBlockZ()); @@ -106,7 +106,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new TranslatableException("noPerm", "essentials.worlds." + toPlayer.getWorld().getName()); } target.sendTl("teleportAtoB", user.getDisplayName(), toPlayer.getDisplayName()); - target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, future); + target.getAsyncTeleport().now(user, toPlayer.getBase(), false, TeleportCause.COMMAND, future); break; } } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpall.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpall.java index 5d18cb2bec..e31623d859 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpall.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpall.java @@ -31,6 +31,7 @@ public void run(final Server server, final CommandSource sender, final String co private void teleportAllPlayers(final Server server, final CommandSource sender, final User target, final String label) { sender.sendTl("teleportAll"); final Location loc = target.getLocation(); + final User teleporter = sender.isPlayer() ? ess.getUser(sender.getPlayer()) : null; for (final User player : ess.getOnlineUsers()) { if (target == player) { continue; @@ -38,7 +39,7 @@ private void teleportAllPlayers(final Server server, final CommandSource sender, if (sender.getSender().equals(target.getBase()) && target.getWorld() != player.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !target.isAuthorized("essentials.worlds." + target.getWorld().getName())) { continue; } - player.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, getNewExceptionFuture(sender, label)); + player.getAsyncTeleport().now(teleporter, loc, false, TeleportCause.COMMAND, getNewExceptionFuture(sender, label)); } } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpo.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpo.java index 8ff9884348..b6c554f108 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpo.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpo.java @@ -40,7 +40,7 @@ public void run(final Server server, final User user, final String commandLabel, } final CompletableFuture future = getNewExceptionFuture(user.getSource(), commandLabel); - target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, future); + target.getAsyncTeleport().now(user, toPlayer.getBase(), false, TeleportCause.COMMAND, future); future.thenAccept(success -> { if (success) { target.sendTl("teleportAtoB", user.getDisplayName(), toPlayer.getDisplayName()); diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpohere.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpohere.java index 5937a5b908..b78f167221 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpohere.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpohere.java @@ -26,7 +26,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new TranslatableException("noPerm", "essentials.worlds." + user.getWorld().getName()); } - player.getAsyncTeleport().now(user.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); + player.getAsyncTeleport().now(user, user.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); } @Override diff --git a/Essentials/src/main/java/net/ess3/api/events/teleport/PreTeleportEvent.java b/Essentials/src/main/java/net/ess3/api/events/teleport/PreTeleportEvent.java index 327b299324..01295fe523 100644 --- a/Essentials/src/main/java/net/ess3/api/events/teleport/PreTeleportEvent.java +++ b/Essentials/src/main/java/net/ess3/api/events/teleport/PreTeleportEvent.java @@ -19,6 +19,10 @@ public PreTeleportEvent(final IUser teleportee, final PlayerTeleportEvent.Telepo super(teleportee, cause, target); } + public PreTeleportEvent(final IUser teleporter, final IUser teleportee, final PlayerTeleportEvent.TeleportCause cause, final ITarget target) { + super(teleporter, teleportee, cause, target); + } + public static HandlerList getHandlerList() { return handlers; }