From 98931c19006a1c4e29c16e25df672d5081e031d3 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 24 Aug 2022 22:24:35 +0100 Subject: [PATCH] Prevent sending teleport requests to self, send message on /tpall --- .../huskhomes/command/TpAllCommand.java | 4 ++++ .../william278/huskhomes/command/TpCommand.java | 11 ++++++----- .../william278/huskhomes/command/TpaCommand.java | 11 +++++++++-- .../huskhomes/command/TpaHereCommand.java | 11 +++++++++-- .../huskhomes/request/RequestManager.java | 3 +++ .../huskhomes/teleport/TeleportManager.java | 3 ++- common/src/main/resources/locales/en-gb.yml | 16 ++++++++-------- 7 files changed, 41 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/command/TpAllCommand.java b/common/src/main/java/net/william278/huskhomes/command/TpAllCommand.java index 879fbfce..df6e7905 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpAllCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpAllCommand.java @@ -32,6 +32,10 @@ public class TpAllCommand extends CommandBase { return; } + // Send a message + plugin.getLocales().getLocale("teleporting_all_players", Integer.toString(players.size())) + .ifPresent(onlineUser::sendMessage); + // Teleport every player players.forEach(playerName -> plugin.getTeleportManager() .teleportPlayerByName(playerName, onlineUser.getPosition(), onlineUser)); 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 aa618a81..d5481ffb 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java @@ -130,10 +130,11 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe @Override public @NotNull List onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) { + final boolean serveCoordinateCompletions = user != null && user.hasPermission(Permission.COMMAND_TP_TO_COORDINATES.node); switch (args.length) { case 0, 1 -> { final ArrayList completions = new ArrayList<>(); - completions.addAll(user != null + completions.addAll(serveCoordinateCompletions ? List.of("~", "~ ~", "~ ~ ~", Integer.toString((int) user.getPosition().x), ((int) user.getPosition().x + " " + (int) user.getPosition().y), @@ -153,7 +154,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe completions.addAll(List.of("~", Integer.toString((int) user.getPosition().y))); completions.addAll(List.of("~ ~", (int) user.getPosition().y + " " + (int) user.getPosition().z)); } else { - completions.addAll(user != null + completions.addAll(serveCoordinateCompletions ? List.of("~", "~ ~", "~ ~ ~", Integer.toString((int) user.getPosition().x), ((int) user.getPosition().x + " " + (int) user.getPosition().y), @@ -168,12 +169,12 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe case 3 -> { final ArrayList completions = new ArrayList<>(); if (isCoordinate(args[0]) && isCoordinate(args[1])) { - if (user == null) { + if (!serveCoordinateCompletions) { return completions; } completions.addAll(List.of("~", Integer.toString((int) user.getPosition().z))); } else if (isCoordinate(args[1])) { - if (user == null) { + if (!serveCoordinateCompletions) { return completions; } completions.addAll(List.of("~", Integer.toString((int) user.getPosition().y))); @@ -186,7 +187,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe case 4 -> { final ArrayList completions = new ArrayList<>(); if (isCoordinate(args[1]) && isCoordinate(args[2]) && !isCoordinate(args[0])) { - if (user == null) { + if (!serveCoordinateCompletions) { return completions; } completions.addAll(List.of("~", Integer.toString((int) user.getPosition().z))); diff --git a/common/src/main/java/net/william278/huskhomes/command/TpaCommand.java b/common/src/main/java/net/william278/huskhomes/command/TpaCommand.java index 7710f59a..c531c6c1 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpaCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpaCommand.java @@ -22,10 +22,17 @@ public class TpaCommand extends CommandBase implements TabCompletable { if (args.length == 1) { plugin.getRequestManager().sendTeleportRequest(onlineUser, args[0], TeleportRequest.RequestType.TPA).thenAccept(sent -> { if (!sent) { - plugin.getLocales().getLocale("error_player_not_found", args[0]).ifPresent(onlineUser::sendMessage); + if (plugin.findPlayer(args[0]).isPresent()) { + plugin.getLocales().getLocale("error_teleport_request_self") + .ifPresent(onlineUser::sendMessage); + return; + } + plugin.getLocales().getLocale("error_player_not_found", args[0]) + .ifPresent(onlineUser::sendMessage); return; } - plugin.getLocales().getLocale("tpa_request_sent", args[0]).ifPresent(onlineUser::sendMessage); + plugin.getLocales().getLocale("tpa_request_sent", args[0]) + .ifPresent(onlineUser::sendMessage); }); } else { plugin.getLocales().getLocale("error_invalid_syntax", "/tpa ") diff --git a/common/src/main/java/net/william278/huskhomes/command/TpaHereCommand.java b/common/src/main/java/net/william278/huskhomes/command/TpaHereCommand.java index 428c6a6d..3006519d 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpaHereCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpaHereCommand.java @@ -22,10 +22,17 @@ public class TpaHereCommand extends CommandBase implements TabCompletable { if (args.length == 1) { plugin.getRequestManager().sendTeleportRequest(onlineUser, args[0], TeleportRequest.RequestType.TPA_HERE).thenAccept(sent -> { if (!sent) { - plugin.getLocales().getLocale("error_player_not_found", args[0]).ifPresent(onlineUser::sendMessage); + if (plugin.findPlayer(args[0]).isPresent()) { + plugin.getLocales().getLocale("error_teleport_request_self") + .ifPresent(onlineUser::sendMessage); + return; + } + plugin.getLocales().getLocale("error_player_not_found", args[0]) + .ifPresent(onlineUser::sendMessage); return; } - plugin.getLocales().getLocale("tpahere_request_sent", args[0]).ifPresent(onlineUser::sendMessage); + plugin.getLocales().getLocale("tpahere_request_sent", args[0]) + .ifPresent(onlineUser::sendMessage); }); } else { plugin.getLocales().getLocale("error_invalid_syntax", "/tpahere ") diff --git a/common/src/main/java/net/william278/huskhomes/request/RequestManager.java b/common/src/main/java/net/william278/huskhomes/request/RequestManager.java index d4f9df68..2c14facc 100644 --- a/common/src/main/java/net/william278/huskhomes/request/RequestManager.java +++ b/common/src/main/java/net/william278/huskhomes/request/RequestManager.java @@ -83,6 +83,9 @@ public class RequestManager { Instant.now().getEpochSecond() + plugin.getSettings().teleportRequestExpiryTime); final Optional localTarget = plugin.findPlayer(targetUser); if (localTarget.isPresent()) { + if (localTarget.get().uuid.equals(requester.uuid)) { + return CompletableFuture.completedFuture(false); + } request.recipientName = localTarget.get().username; return CompletableFuture.supplyAsync(() -> sendLocalTeleportRequest(request, localTarget.get())); } diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java index 1eb04e74..ffb2d5c7 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java @@ -186,7 +186,8 @@ public class TeleportManager { public CompletableFuture> teleportPlayerToPlayerByName(@NotNull String playerName, @NotNull String targetPlayer, @NotNull OnlineUser requester) { - final Optional localPositionTarget = plugin.findPlayer(targetPlayer).map(OnlineUser::getPosition); + final Optional localPlayer = plugin.findPlayer(playerName); + final Optional localPositionTarget = localPlayer.map(OnlineUser::getPosition); if (localPositionTarget.isPresent()) { return teleportPlayerByName(playerName, localPositionTarget.get(), requester); } diff --git a/common/src/main/resources/locales/en-gb.yml b/common/src/main/resources/locales/en-gb.yml index 018015e6..7bfc5f24 100644 --- a/common/src/main/resources/locales/en-gb.yml +++ b/common/src/main/resources/locales/en-gb.yml @@ -8,6 +8,7 @@ teleporting_action_bar_processing: '[Teleporting…](#00fb9a)' teleporting_action_bar_cancelled: '[Teleportation cancelled!](#ff7e5e)' teleporting_other_complete: '[Teleported %1% to %2%.](#00fb9a)' teleporting_other_complete_position: '[Teleported %1% to ⚐ x: %2%, y: %3%, z: %4%.](#00fb9a)' +teleporting_all_players: '[Teleporting %1% players to your position…](#00fb9a)' edit_home_menu_title: '[Viewing details for your home,](#00fb9a) [%1%](#00fb9a bold)[:](#00fb9a)' edit_home_menu_title_other: '[Viewing details for %1%''s home,](#00fb9a) [%2%](#00fb9a bold)[:](#00fb9a)' @@ -83,6 +84,12 @@ teleport_request_declined_confirmation: '[You declined %1%''s teleport request.] teleport_request_accepted: '[%1% has accepted your teleport request!](#00fb9a)' teleport_request_declined: '[%1% has declined your teleport request.](#ff7e5e)' +tpignore_toggle_on: '[You are now](#00fb9a) [ignoring](#00fb9a bold) [incoming teleport requests.](#00fb9a) %1%' +tpignore_toggle_off: '[You are now](#00fb9a) [listening](#00fb9a bold) [to incoming teleport requests.](#00fb9a) %1%' +tpignore_on_notification: '[You are currently ignoring incoming teleport requests!](gray) %1%' +tpignore_toggle_button: '[[🔔 Toggle…]](#ffc43b show_text=&#ffc43b&Click to toggle:\n&7Toggle ignoring/listening to incoming teleport requests run_command=/huskhomes:tpignore)' + + set_home_used_free_slots: '[You''ve used all of your free set home slots (%1%); setting additional homes will cost %2% each.](gray)' set_home_success: '[Successfully set a new home called](#00fb9a) [%1%](#00fb9a bold)' set_warp_success: '[Successfully set a new warp called](#00fb9a) [%1%](#00fb9a bold)' @@ -97,13 +104,6 @@ teleporting_complete_console: '[Console teleported you to %1%.](gray)' spawn_warp_default_description: 'The /spawn position of the server' item_no_description: '(No description)' -tpignore_toggle_on: '[You are now](#00fb9a) [ignoring](#00fb9a bold) [incoming teleport requests.](#00fb9a) %1%' -tpignore_toggle_off: '[You are now](#00fb9a) [listening](#00fb9a bold) [to incoming teleport requests.](#00fb9a) %1%' -tpignore_on_notification: '[You are currently ignoring incoming teleport requests!](gray) %1%' -tpignore_toggle_button: '[[🔔 Toggle…]](#ffc43b show_text=&#ffc43b&Click to toggle:\n&7Toggle ignoring/listening to incoming teleport requests run_command=/huskhomes:tpignore)' - -teleporting_all_players: '[Teleporting all players to your position…](#00fb9a)' - economy_action_additional_home_slot: '[ℹ Spent %1% to set an additional home.](gray)' economy_action_make_home_public: '[ℹ Spent %1% to make your home public.](gray)' economy_action_random_teleport: '[ℹ Spent %1% to randomly teleport.](gray)' @@ -144,8 +144,8 @@ error_teleport_request_expired: '[Error:](#ff3300) [Your pending teleport reques error_teleport_request_sender_not_online: '[Error:](#ff3300) [The sender of your pending teleport request is no longer online.](#ff7e5e)' error_ignoring_teleport_requests: '[Error:](#ff3300) [You are currently ignoring teleport requests.](#ff7e5e) [[Stop Ignoring…]](#ff7e5e show_text=&#ff7e5e&Click to listen for incoming teleport requests run_command=/huskhomes:tpignore)' error_invalid_teleport_request: '[Error:](#ff3300) [You do not have a pending teleport request from %1%.](#ff7e5e)' +error_teleport_request_self: '[Error:](#ff3300) [You cannot send a teleport request to yourself.](#ff7e5e)' error_player_not_found: '[Error:](#ff3300) [Could not find the player %1%.](#ff7e5e)' -error_teleport_to_self: '[Error:](#ff3300) [You cannot teleport to yourself!](#ff7e5e)' # todo error_rtp_restricted_world: '[Error:](#ff3300) [You cannot randomly teleport in this world.](#ff7e5e)' error_rtp_cooldown: '[Error:](#ff3300) [You must wait %1% minutes before randomly teleporting again.](#ff7e5e)' error_command_disabled: '[Error:](#ff3300) [That command is disabled!](#ff7e5e)'