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
This commit is contained in:
Emibergo02 authored and GitHub committed 2024-02-18 14:02:39 +00:00
1 parent 7ee4bae68a
commit bf5ba2a7a3
1 file changed
+25 -6
@@ -36,7 +36,7 @@ public class TpCommand extends Command implements TabProvider {
protected TpCommand(@NotNull HuskHomes plugin) { protected TpCommand(@NotNull HuskHomes plugin) {
super("tp", List.of("tpo"), "[<player|position>] [target]", plugin); super("tp", List.of("tpo"), "[<player|position>] [target]", plugin);
addAdditionalPermissions(Map.of("coordinates", true)); addAdditionalPermissions(Map.of("coordinates", true, "other", true));
setOperatorCommand(true); setOperatorCommand(true);
} }
@@ -49,7 +49,6 @@ public class TpCommand extends Command implements TabProvider {
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
return; return;
} }
this.execute(executor, user, Target.username(args[0]), args); this.execute(executor, user, Target.username(args[0]), args);
} }
case 2 -> this.execute(executor, Teleportable.username(args[0]), Target.username(args[1]), 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) final TeleportBuilder builder = Teleport.builder(plugin)
.teleporter(teleportable) .teleporter(teleportable)
.target(target); .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); builder.buildAndComplete(false, args);
// Display a teleport completion message // Display a teleport completion message
@@ -112,6 +126,7 @@ public class TpCommand extends Command implements TabProvider {
public final List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) { public final List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
final Position relative = getBasePosition(user); final Position relative = getBasePosition(user);
final boolean serveCoordinateCompletions = user.hasPermission(getPermission("coordinates")); final boolean serveCoordinateCompletions = user.hasPermission(getPermission("coordinates"));
final boolean servePlayerCompletions = user.hasPermission(getPermission("other"));
switch (args.length) { switch (args.length) {
case 0, 1 -> { case 0, 1 -> {
final ArrayList<String> completions = new ArrayList<>(); final ArrayList<String> 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.getX() + " " + (int) relative.getY() + " " + (int) relative.getZ())) ((int) relative.getX() + " " + (int) relative.getY() + " " + (int) relative.getZ()))
: List.of()); : List.of());
completions.addAll(plugin.getPlayerList(false)); if (servePlayerCompletions) {
completions.addAll(plugin.getPlayerList(false));
}
return completions.stream() return completions.stream()
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : "")) .filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
.sorted().collect(Collectors.toList()); .sorted().collect(Collectors.toList());
@@ -140,7 +157,9 @@ public class TpCommand extends Command implements TabProvider {
((int) relative.getX() + " " + (int) relative.getY() + " " + (int) relative.getZ())) ((int) relative.getX() + " " + (int) relative.getY() + " " + (int) relative.getZ()))
: List.of() : List.of()
); );
completions.addAll(plugin.getPlayerList(false)); if (servePlayerCompletions) {
completions.addAll(plugin.getPlayerList(false));
}
} }
return completions.stream() return completions.stream()
.filter(s -> s.toLowerCase().startsWith(args[1].toLowerCase())) .filter(s -> s.toLowerCase().startsWith(args[1].toLowerCase()))