From ff7569c77daa37664b15b64f70b3744898a465d3 Mon Sep 17 00:00:00 2001 From: MD <1917406+mdcfe@users.noreply.github.com> Date: Tue, 11 Apr 2023 14:04:52 +0100 Subject: [PATCH] Refactor tpaccept world permission checks for clarity When mdcfe in `world_nether` sends a `/tpa` to mdcfe3 in `world` and mdcfe3 accepts: ``` [13:57:02 INFO]: mdcfe3 issued server command: /tpaccept [...] [13:57:02 INFO]: [Essentials] checking if mdcfe3 has essentials.tpaccept - true [...] [13:57:02 INFO]: [Essentials] checking if mdcfe has essentials.tpa - true [13:57:02 INFO]: [Essentials] checking if mdcfe has essentials.worlds.world - true ``` When mdcfe in `world` sends a `/tpahere` to mdcfe3 in `world_nether` and mdcfe3 accepts: ``` [14:02:36 INFO]: mdcfe3 issued server command: /tpaccept [...] [14:02:36 INFO]: [Essentials] checking if mdcfe3 has essentials.tpaccept - true [...] [14:02:36 INFO]: [Essentials] checking if mdcfe has essentials.tpahere - true [14:02:36 INFO]: [Essentials] checking if mdcfe3 has essentials.worlds.world - true ``` --- .../essentials/commands/Commandtpaccept.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpaccept.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpaccept.java index 221ae2b4c..1fbc64d92 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpaccept.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpaccept.java @@ -8,6 +8,7 @@ import net.essentialsx.api.v2.events.TeleportRequestResponseEvent; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Server; +import org.bukkit.World; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import java.util.ArrayList; @@ -87,12 +88,15 @@ public class Commandtpaccept extends EssentialsCommand { throw new Exception(tl("noPendingRequest")); } - if (request.isHere() && ((!requester.isAuthorized("essentials.tpahere") && !requester.isAuthorized("essentials.tpaall")) || (user.getWorld() != requester.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + requester.getWorld().getName())))) { - throw new Exception(tl("noPendingRequest")); - } - - if (!request.isHere() && (!requester.isAuthorized("essentials.tpa") || (user.getWorld() != requester.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !requester.isAuthorized("essentials.worlds." + user.getWorld().getName())))) { - throw new Exception(tl("noPendingRequest")); + if (request.isHere()) { + final boolean isRequesterAuthorized = requester.isAuthorized("essentials.tpahere") || requester.isAuthorized("essentials.tpaall"); + if (!isRequesterAuthorized || !canTeleportToWorld(user, requester.getWorld())) { + throw new Exception(tl("noPendingRequest")); + } + } else { + if (!requester.isAuthorized("essentials.tpa") || !canTeleportToWorld(requester, user.getWorld())) { + throw new Exception(tl("noPendingRequest")); + } } final TeleportRequestResponseEvent event = new TeleportRequestResponseEvent(user, requester, request, true); @@ -129,4 +133,8 @@ public class Commandtpaccept extends EssentialsCommand { } user.removeTpaRequest(request.getName()); } + + private boolean canTeleportToWorld(final User user, final World destination) { + return user.getWorld() == destination || !ess.getSettings().isWorldTeleportPermissions() || user.isAuthorized("essentials.worlds." + destination.getName()); + } }