diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomes.java b/common/src/main/java/net/william278/huskhomes/HuskHomes.java index 86fd7509..e20dd324 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomes.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomes.java @@ -53,13 +53,13 @@ public interface HuskHomes { List getOnlinePlayers(); /** - * Finds a {@link OnlineUser} by their name. Auto-completes partially typed names for the closest match + * Finds a local {@link OnlineUser} by their name. Auto-completes partially typed names for the closest match * * @param playerName the name of the player to find * @return an {@link Optional} containing the {@link OnlineUser} if found, or an empty {@link Optional} if not found */ @NotNull - default Optional findPlayer(@NotNull String playerName) { + default Optional findOnlinePlayer(@NotNull String playerName) { return getOnlinePlayers().stream() .filter(user -> user.username.equalsIgnoreCase(playerName)) .findFirst() @@ -68,6 +68,29 @@ public interface HuskHomes { .findFirst()); } + /** + * Looks for a player logged in either on this server or the network of connected servers, by approximate name. + * Auto-completes partially typed names for the closest match. + * + * @param requester the player requesting the player to be found + * @param targetPlayerName the name of the player to find + * @return an {@link Optional} containing the {@link OnlineUser} if found, or an empty {@link Optional} if not found + */ + @NotNull + default CompletableFuture> findPlayer(@NotNull OnlineUser requester, @NotNull String targetPlayerName) { + if (requester.username.equalsIgnoreCase(targetPlayerName)) { + return CompletableFuture.completedFuture(Optional.of(requester.username)); + } + final Optional localPlayer = findOnlinePlayer(targetPlayerName); + if (localPlayer.isPresent()) { + return CompletableFuture.completedFuture(Optional.of(localPlayer.get().username)); + } + if (getSettings().crossServer) { + return getNetworkMessenger().findPlayer(requester, targetPlayerName); + } + return CompletableFuture.completedFuture(Optional.empty()); + } + /** * The plugin {@link Settings} loaded from file * diff --git a/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java index e2635865..baddc6c6 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java @@ -90,7 +90,7 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE return; } CompletableFuture.runAsync(() -> { - final OnlineUser playerToTeleport = plugin.findPlayer(args[0]).orElse(null); + final OnlineUser playerToTeleport = plugin.findOnlinePlayer(args[0]).orElse(null); if (playerToTeleport == null) { plugin.getLoggingAdapter().log(Level.WARNING, "Player not found: " + args[0]); return; diff --git a/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java index 2d4263b8..b5634598 100644 --- a/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/PublicHomeCommand.java @@ -102,7 +102,7 @@ public class PublicHomeCommand extends CommandBase implements TabCompletable, Co return; } CompletableFuture.runAsync(() -> { - final OnlineUser playerToTeleport = plugin.findPlayer(args[0]).orElse(null); + final OnlineUser playerToTeleport = plugin.findOnlinePlayer(args[0]).orElse(null); if (playerToTeleport == null) { plugin.getLoggingAdapter().log(Level.WARNING, "Player not found: " + args[0]); return; diff --git a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java index dadb03ae..839c35ce 100644 --- a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java @@ -31,7 +31,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable { plugin.getLocales().getLocale("error_no_permission").ifPresent(onlineUser::sendMessage); return; } - Optional foundUser = plugin.findPlayer(args[0]); + final Optional foundUser = plugin.findOnlinePlayer(args[0]); if (foundUser.isEmpty()) { plugin.getLocales().getLocale("error_player_not_found", args[0]) .ifPresent(onlineUser::sendMessage); @@ -102,7 +102,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable { plugin.getLoggingAdapter().log(Level.WARNING, "Invalid syntax. Usage: rtp [player]"); return; } - final Optional foundUser = plugin.findPlayer(args[0]); + final Optional foundUser = plugin.findOnlinePlayer(args[0]); if (foundUser.isEmpty()) { plugin.getLoggingAdapter().log(Level.WARNING, "Player not found: " + args[0]); return; 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 cc8a5ef9..ad4e4eff 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java @@ -165,7 +165,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe plugin.getLoggingAdapter().log(Level.WARNING, "Invalid syntax. Usage: tp "); return; } - final OnlineUser playerToTeleport = plugin.findPlayer(args[0]).orElse(null); + final OnlineUser playerToTeleport = plugin.findOnlinePlayer(args[0]).orElse(null); if (playerToTeleport == null) { plugin.getLoggingAdapter().log(Level.WARNING, "Player not found: " + args[0]); return; diff --git a/common/src/main/java/net/william278/huskhomes/command/TpHereCommand.java b/common/src/main/java/net/william278/huskhomes/command/TpHereCommand.java index 97d08fc9..ec51dd63 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpHereCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpHereCommand.java @@ -27,18 +27,27 @@ public class TpHereCommand extends CommandBase implements TabCompletable { return; } final String targetPlayerName = args[0]; - Teleport.builder(plugin, onlineUser) - .setTeleporter(targetPlayerName) - .setTarget(onlineUser.getPosition()) - .toTeleport() - .thenAccept(teleport -> teleport.execute().thenAccept(result -> { - if (result.successful()) { - result.getTeleporter() - .flatMap(teleporter -> plugin.getLocales().getLocale("teleporting_other_complete", - teleporter.username, onlineUser.username)) - .ifPresent(onlineUser::sendMessage); - } - })); + plugin.findPlayer(onlineUser, targetPlayerName).thenAccept(teleporterName -> { + if (teleporterName.isEmpty()) { + plugin.getLocales().getLocale("error_player_not_found", targetPlayerName) + .ifPresent(onlineUser::sendMessage); + return; + } + + Teleport.builder(plugin, onlineUser) + .setTeleporter(teleporterName.get()) + .setTarget(onlineUser.getPosition()) + .toTeleport() + .thenAccept(teleport -> teleport.execute().thenAccept(result -> { + if (result.successful()) { + result.getTeleporter() + .flatMap(teleporter -> plugin.getLocales().getLocale("teleporting_other_complete", + teleporter.username, onlineUser.username)) + .ifPresent(onlineUser::sendMessage); + } + })); + }); + }); } diff --git a/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java index ab3315f6..0219da05 100644 --- a/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/WarpCommand.java @@ -77,7 +77,7 @@ public class WarpCommand extends CommandBase implements TabCompletable, ConsoleE plugin.getLoggingAdapter().log(Level.WARNING, "Invalid syntax. Usage: warp "); return; } - final OnlineUser playerToTeleport = plugin.findPlayer(args[0]).orElse(null); + final OnlineUser playerToTeleport = plugin.findOnlinePlayer(args[0]).orElse(null); if (playerToTeleport == null) { plugin.getLoggingAdapter().log(Level.WARNING, "Player not found: " + args[0]); return; 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 37f3e3a7..4ad86237 100644 --- a/common/src/main/java/net/william278/huskhomes/request/RequestManager.java +++ b/common/src/main/java/net/william278/huskhomes/request/RequestManager.java @@ -128,7 +128,7 @@ public class RequestManager { @NotNull TeleportRequest.RequestType requestType) { final TeleportRequest request = new TeleportRequest(requester, requestType, Instant.now().getEpochSecond() + plugin.getSettings().teleportRequestExpiryTime); - final Optional localTarget = plugin.findPlayer(targetUser); + final Optional localTarget = plugin.findOnlinePlayer(targetUser); if (localTarget.isPresent()) { if (localTarget.get().uuid.equals(requester.uuid)) { return CompletableFuture.completedFuture(Optional.empty()); @@ -278,7 +278,7 @@ public class RequestManager { request.status = accepted ? TeleportRequest.RequestStatus.ACCEPTED : TeleportRequest.RequestStatus.DECLINED; // Send request response to the sender - final Optional localRequester = plugin.findPlayer(request.requesterName); + final Optional localRequester = plugin.findOnlinePlayer(request.requesterName); if (localRequester.isPresent()) { handleLocalRequestResponse(localRequester.get(), request); } else if (plugin.getSettings().crossServer) { diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportBuilder.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportBuilder.java index e08432de..95f88794 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportBuilder.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportBuilder.java @@ -87,7 +87,7 @@ public class TeleportBuilder { */ public TeleportBuilder setTeleporter(@NotNull String teleporterUsername) { this.teleporter = CompletableFuture.supplyAsync(() -> plugin - .findPlayer(teleporterUsername) + .findOnlinePlayer(teleporterUsername) .map(onlineUser -> (User) onlineUser) .or(() -> { if (plugin.getSettings().crossServer) { @@ -229,7 +229,7 @@ public class TeleportBuilder { * @return future optionally supplying the player's position, if the player could be found */ private CompletableFuture> getPlayerPosition(@NotNull String playerName) { - final Optional localPlayer = plugin.findPlayer(playerName); + final Optional localPlayer = plugin.findOnlinePlayer(playerName); if (localPlayer.isPresent()) { return CompletableFuture.supplyAsync(() -> Optional.of(localPlayer.get().getPosition())); }