From f32082a8bf0dc64f2f4c00139916f0cda3fb4e21 Mon Sep 17 00:00:00 2001 From: William Date: Mon, 29 Aug 2022 23:30:33 +0100 Subject: [PATCH] Make the /tpahere and /tpa request sent confirmation message accurately report who the request was sent to --- .../huskhomes/command/TpaAllCommand.java | 2 +- .../huskhomes/command/TpaCommand.java | 8 ++-- .../huskhomes/command/TpaHereCommand.java | 4 +- .../huskhomes/request/RequestManager.java | 39 ++++++++++--------- .../huskhomes/request/TeleportRequest.java | 5 +++ 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/command/TpaAllCommand.java b/common/src/main/java/net/william278/huskhomes/command/TpaAllCommand.java index 88005c53..aab76336 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpaAllCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpaAllCommand.java @@ -39,7 +39,7 @@ public class TpaAllCommand extends CommandBase { final List> sentRequestsFuture = new ArrayList<>(); players.forEach(playerName -> sentRequestsFuture.add(plugin.getRequestManager() .sendTeleportRequest(onlineUser, playerName, TeleportRequest.RequestType.TPA_HERE) - .thenAccept(sent -> counter.addAndGet(sent ? 1 : 0)))); + .thenAccept(sent -> counter.addAndGet(sent.isPresent() ? 1 : 0)))); // Send a message when all requests have been sent CompletableFuture.allOf(sentRequestsFuture.toArray(new CompletableFuture[0])).thenRun(() -> { 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 c531c6c1..a0195edf 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpaCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpaCommand.java @@ -21,7 +21,10 @@ public class TpaCommand extends CommandBase implements TabCompletable { public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { if (args.length == 1) { plugin.getRequestManager().sendTeleportRequest(onlineUser, args[0], TeleportRequest.RequestType.TPA).thenAccept(sent -> { - if (!sent) { + if (sent.isPresent()) { + plugin.getLocales().getLocale("tpa_request_sent", sent.get().getRecipientName()) + .ifPresent(onlineUser::sendMessage); + } else { if (plugin.findPlayer(args[0]).isPresent()) { plugin.getLocales().getLocale("error_teleport_request_self") .ifPresent(onlineUser::sendMessage); @@ -29,10 +32,7 @@ public class TpaCommand extends CommandBase implements TabCompletable { } plugin.getLocales().getLocale("error_player_not_found", args[0]) .ifPresent(onlineUser::sendMessage); - return; } - 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 3006519d..75995854 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpaHereCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpaHereCommand.java @@ -21,7 +21,7 @@ public class TpaHereCommand extends CommandBase implements TabCompletable { public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { if (args.length == 1) { plugin.getRequestManager().sendTeleportRequest(onlineUser, args[0], TeleportRequest.RequestType.TPA_HERE).thenAccept(sent -> { - if (!sent) { + if (sent.isEmpty()) { if (plugin.findPlayer(args[0]).isPresent()) { plugin.getLocales().getLocale("error_teleport_request_self") .ifPresent(onlineUser::sendMessage); @@ -31,7 +31,7 @@ public class TpaHereCommand extends CommandBase implements TabCompletable { .ifPresent(onlineUser::sendMessage); return; } - plugin.getLocales().getLocale("tpahere_request_sent", args[0]) + plugin.getLocales().getLocale("tpahere_request_sent", sent.get().getRecipientName()) .ifPresent(onlineUser::sendMessage); }); } else { 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 4bfdda4c..235dda36 100644 --- a/common/src/main/java/net/william278/huskhomes/request/RequestManager.java +++ b/common/src/main/java/net/william278/huskhomes/request/RequestManager.java @@ -74,20 +74,20 @@ public class RequestManager { * @param requester The user making the request * @param targetUser The user to send the request to * @param requestType The type of request to send - * @return A {@link CompletableFuture} that will return {@code true} if the request was successfully sent, or - * {@code false} if it was not as the user was not found + * @return A {@link CompletableFuture} that will return the request that was sent if it was sent successfully, + * or an empty {@link Optional} if the request was not sent */ - public CompletableFuture sendTeleportRequest(@NotNull OnlineUser requester, @NotNull String targetUser, - @NotNull TeleportRequest.RequestType requestType) { + public CompletableFuture> sendTeleportRequest(@NotNull OnlineUser requester, @NotNull String targetUser, + @NotNull TeleportRequest.RequestType requestType) { final TeleportRequest request = new TeleportRequest(requester, requestType, 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); + return CompletableFuture.completedFuture(Optional.empty()); } request.recipientName = localTarget.get().username; - return CompletableFuture.supplyAsync(() -> sendLocalTeleportRequest(request, localTarget.get())); + return CompletableFuture.completedFuture(sendLocalTeleportRequest(request, localTarget.get())); } // If the player couldn't be found locally, send the request cross-server @@ -97,12 +97,12 @@ public class RequestManager { // Find the matching networked target return plugin.getNetworkMessenger().findPlayer(requester, targetUser).thenApply(networkedTarget -> { if (networkedTarget.isEmpty()) { - return false; + return Optional.empty(); } // Use the network messenger to send the request request.recipientName = networkedTarget.get(); - return plugin.getNetworkMessenger().sendMessage(requester, + return Optional.ofNullable(plugin.getNetworkMessenger().sendMessage(requester, new Message(Message.MessageType.TELEPORT_REQUEST, requester.username, networkedTarget.get(), @@ -113,15 +113,18 @@ public class RequestManager { .exceptionally(throwable -> null) .thenApply(reply -> { if (reply == null || reply.payload.teleportRequest == null) { - return false; + return null; } // If the message was ignored by the recipient, return false - return reply.payload.teleportRequest.status == TeleportRequest.RequestStatus.PENDING; - }).join(); + if (reply.payload.teleportRequest.status == TeleportRequest.RequestStatus.PENDING) { + return request; + } + return null; + }).join()); }); } - return CompletableFuture.supplyAsync(() -> false); + return CompletableFuture.completedFuture(Optional.empty()); } /** @@ -129,19 +132,19 @@ public class RequestManager { * * @param request The {@link TeleportRequest} to send * @param recipient The online recipient of the request - * @return {@code true} if the request was sent, {@code false} if it was not sent because the recipient - * is ignoring requests or is vanished ({@link OnlineUser#isVanished()}) + * @return the {@link TeleportRequest} that was sent if it could be sent, otherwise an empty optional if it was not + * sent because the recipient is ignoring requests or is vanished ({@link OnlineUser#isVanished()}) */ - public boolean sendLocalTeleportRequest(@NotNull TeleportRequest request, @NotNull OnlineUser recipient) { + public Optional sendLocalTeleportRequest(@NotNull TeleportRequest request, @NotNull OnlineUser recipient) { if (isIgnoringRequests(recipient) || recipient.isVanished()) { request.status = TeleportRequest.RequestStatus.IGNORED; - return false; + return Optional.empty(); } // If the person already has a request of the same type by this player, don't bother sending another one if (getTeleportRequest(request.requesterName, recipient).map(existingRequest -> existingRequest.type) .orElse(null) == request.type) { - return true; + return Optional.of(request); } // Add the request and display a message to the recipient @@ -151,7 +154,7 @@ public class RequestManager { .ifPresent(recipient::sendMessage); plugin.getLocales().getLocale("teleport_request_buttons", request.requesterName) .ifPresent(recipient::sendMessage); - return true; + return Optional.of(request); } public void respondToTeleportRequestBySenderName(@NotNull OnlineUser recipient, @NotNull String senderName, diff --git a/common/src/main/java/net/william278/huskhomes/request/TeleportRequest.java b/common/src/main/java/net/william278/huskhomes/request/TeleportRequest.java index 0fceacfa..3854f3a1 100644 --- a/common/src/main/java/net/william278/huskhomes/request/TeleportRequest.java +++ b/common/src/main/java/net/william278/huskhomes/request/TeleportRequest.java @@ -73,6 +73,11 @@ public class TeleportRequest { return Instant.now().isAfter(Instant.ofEpochSecond(expiryTime)); } + @NotNull + public String getRecipientName() { + return recipientName; + } + public enum RequestType { TPA, TPA_HERE