From bf5ba2a7a3d799e407e80de6898eb833c669ba10 Mon Sep 17 00:00:00 2001 From: Emibergo02 <36164338+Emibergo02@users.noreply.github.com> Date: Sun, 18 Feb 2024 15:02:39 +0100 Subject: [PATCH] fix: Add missing `other` permission check in teleport command logic (#567) * Permission for player tp only * Changed according to Docs huskhomes.command.tp.other instead of huskhomes.command.tp.player * Documentation update * Revert "Documentation update" This reverts commit 902703e16876bc57e88cc32840413534f14eb7d0. * Requested changes * Check if the target has username before checking his permissions --- .../huskhomes/command/TpCommand.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java index 53b4c138..d7d7daab 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java @@ -36,7 +36,7 @@ public class TpCommand extends Command implements TabProvider { protected TpCommand(@NotNull HuskHomes plugin) { super("tp", List.of("tpo"), "[] [target]", plugin); - addAdditionalPermissions(Map.of("coordinates", true)); + addAdditionalPermissions(Map.of("coordinates", true, "other", true)); setOperatorCommand(true); } @@ -49,7 +49,6 @@ public class TpCommand extends Command implements TabProvider { .ifPresent(executor::sendMessage); return; } - this.execute(executor, user, Target.username(args[0]), args); } case 2 -> this.execute(executor, Teleportable.username(args[0]), Target.username(args[1]), args); @@ -88,9 +87,24 @@ public class TpCommand extends Command implements TabProvider { final TeleportBuilder builder = Teleport.builder(plugin) .teleporter(teleportable) .target(target); - if (executor instanceof OnlineUser user) { - builder.executor(user); + + if (executor instanceof OnlineUser onlineUser) { + if (target instanceof Teleportable teleportableTarget) { + if (!onlineUser.hasPermission(getPermission("other"))) { + plugin.getLocales().getLocale("error_no_permission") + .ifPresent(executor::sendMessage); + return; + } + + if (onlineUser.getUsername().equals(teleportableTarget.getUsername())) { + plugin.getLocales().getLocale("error_cannot_teleport_self") + .ifPresent(onlineUser::sendMessage); + return; + } + } + builder.executor(onlineUser); } + builder.buildAndComplete(false, args); // Display a teleport completion message @@ -112,6 +126,7 @@ public class TpCommand extends Command implements TabProvider { public final List suggest(@NotNull CommandUser user, @NotNull String[] args) { final Position relative = getBasePosition(user); final boolean serveCoordinateCompletions = user.hasPermission(getPermission("coordinates")); + final boolean servePlayerCompletions = user.hasPermission(getPermission("other")); switch (args.length) { case 0, 1 -> { final ArrayList completions = new ArrayList<>(); @@ -121,7 +136,9 @@ public class TpCommand extends Command implements TabProvider { ((int) relative.getX() + " " + (int) relative.getY()), ((int) relative.getX() + " " + (int) relative.getY() + " " + (int) relative.getZ())) : List.of()); - completions.addAll(plugin.getPlayerList(false)); + if (servePlayerCompletions) { + completions.addAll(plugin.getPlayerList(false)); + } return completions.stream() .filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : "")) .sorted().collect(Collectors.toList()); @@ -140,7 +157,9 @@ public class TpCommand extends Command implements TabProvider { ((int) relative.getX() + " " + (int) relative.getY() + " " + (int) relative.getZ())) : List.of() ); - completions.addAll(plugin.getPlayerList(false)); + if (servePlayerCompletions) { + completions.addAll(plugin.getPlayerList(false)); + } } return completions.stream() .filter(s -> s.toLowerCase().startsWith(args[1].toLowerCase()))