Make the /tpahere and /tpa request sent confirmation message accurately report who the request was sent to

This commit is contained in:
William committed 2022-08-29 23:30:33 +01:00
1 parent c0b6b45617
commit f32082a8bf
5 files changed
+33 -25

No files matched your search

@@ -39,7 +39,7 @@ public class TpaAllCommand extends CommandBase {
final List<CompletableFuture<Void>> sentRequestsFuture = new ArrayList<>(); final List<CompletableFuture<Void>> sentRequestsFuture = new ArrayList<>();
players.forEach(playerName -> sentRequestsFuture.add(plugin.getRequestManager() players.forEach(playerName -> sentRequestsFuture.add(plugin.getRequestManager()
.sendTeleportRequest(onlineUser, playerName, TeleportRequest.RequestType.TPA_HERE) .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 // Send a message when all requests have been sent
CompletableFuture.allOf(sentRequestsFuture.toArray(new CompletableFuture[0])).thenRun(() -> { CompletableFuture.allOf(sentRequestsFuture.toArray(new CompletableFuture[0])).thenRun(() -> {
@@ -21,7 +21,10 @@ public class TpaCommand extends CommandBase implements TabCompletable {
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
if (args.length == 1) { if (args.length == 1) {
plugin.getRequestManager().sendTeleportRequest(onlineUser, args[0], TeleportRequest.RequestType.TPA).thenAccept(sent -> { 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()) { if (plugin.findPlayer(args[0]).isPresent()) {
plugin.getLocales().getLocale("error_teleport_request_self") plugin.getLocales().getLocale("error_teleport_request_self")
.ifPresent(onlineUser::sendMessage); .ifPresent(onlineUser::sendMessage);
@@ -29,10 +32,7 @@ public class TpaCommand extends CommandBase implements TabCompletable {
} }
plugin.getLocales().getLocale("error_player_not_found", args[0]) plugin.getLocales().getLocale("error_player_not_found", args[0])
.ifPresent(onlineUser::sendMessage); .ifPresent(onlineUser::sendMessage);
return;
} }
plugin.getLocales().getLocale("tpa_request_sent", args[0])
.ifPresent(onlineUser::sendMessage);
}); });
} else { } else {
plugin.getLocales().getLocale("error_invalid_syntax", "/tpa <player>") plugin.getLocales().getLocale("error_invalid_syntax", "/tpa <player>")
@@ -21,7 +21,7 @@ public class TpaHereCommand extends CommandBase implements TabCompletable {
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
if (args.length == 1) { if (args.length == 1) {
plugin.getRequestManager().sendTeleportRequest(onlineUser, args[0], TeleportRequest.RequestType.TPA_HERE).thenAccept(sent -> { plugin.getRequestManager().sendTeleportRequest(onlineUser, args[0], TeleportRequest.RequestType.TPA_HERE).thenAccept(sent -> {
if (!sent) { if (sent.isEmpty()) {
if (plugin.findPlayer(args[0]).isPresent()) { if (plugin.findPlayer(args[0]).isPresent()) {
plugin.getLocales().getLocale("error_teleport_request_self") plugin.getLocales().getLocale("error_teleport_request_self")
.ifPresent(onlineUser::sendMessage); .ifPresent(onlineUser::sendMessage);
@@ -31,7 +31,7 @@ public class TpaHereCommand extends CommandBase implements TabCompletable {
.ifPresent(onlineUser::sendMessage); .ifPresent(onlineUser::sendMessage);
return; return;
} }
plugin.getLocales().getLocale("tpahere_request_sent", args[0]) plugin.getLocales().getLocale("tpahere_request_sent", sent.get().getRecipientName())
.ifPresent(onlineUser::sendMessage); .ifPresent(onlineUser::sendMessage);
}); });
} else { } else {
@@ -74,20 +74,20 @@ public class RequestManager {
* @param requester The user making the request * @param requester The user making the request
* @param targetUser The user to send the request to * @param targetUser The user to send the request to
* @param requestType The type of request to send * @param requestType The type of request to send
* @return A {@link CompletableFuture} that will return {@code true} if the request was successfully sent, or * @return A {@link CompletableFuture} that will return the request that was sent if it was sent successfully,
* {@code false} if it was not as the user was not found * or an empty {@link Optional} if the request was not sent
*/ */
public CompletableFuture<Boolean> sendTeleportRequest(@NotNull OnlineUser requester, @NotNull String targetUser, public CompletableFuture<Optional<TeleportRequest>> sendTeleportRequest(@NotNull OnlineUser requester, @NotNull String targetUser,
@NotNull TeleportRequest.RequestType requestType) { @NotNull TeleportRequest.RequestType requestType) {
final TeleportRequest request = new TeleportRequest(requester, requestType, final TeleportRequest request = new TeleportRequest(requester, requestType,
Instant.now().getEpochSecond() + plugin.getSettings().teleportRequestExpiryTime); Instant.now().getEpochSecond() + plugin.getSettings().teleportRequestExpiryTime);
final Optional<OnlineUser> localTarget = plugin.findPlayer(targetUser); final Optional<OnlineUser> localTarget = plugin.findPlayer(targetUser);
if (localTarget.isPresent()) { if (localTarget.isPresent()) {
if (localTarget.get().uuid.equals(requester.uuid)) { if (localTarget.get().uuid.equals(requester.uuid)) {
return CompletableFuture.completedFuture(false); return CompletableFuture.completedFuture(Optional.empty());
} }
request.recipientName = localTarget.get().username; 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 // 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 // Find the matching networked target
return plugin.getNetworkMessenger().findPlayer(requester, targetUser).thenApply(networkedTarget -> { return plugin.getNetworkMessenger().findPlayer(requester, targetUser).thenApply(networkedTarget -> {
if (networkedTarget.isEmpty()) { if (networkedTarget.isEmpty()) {
return false; return Optional.empty();
} }
// Use the network messenger to send the request // Use the network messenger to send the request
request.recipientName = networkedTarget.get(); request.recipientName = networkedTarget.get();
return plugin.getNetworkMessenger().sendMessage(requester, return Optional.ofNullable(plugin.getNetworkMessenger().sendMessage(requester,
new Message(Message.MessageType.TELEPORT_REQUEST, new Message(Message.MessageType.TELEPORT_REQUEST,
requester.username, requester.username,
networkedTarget.get(), networkedTarget.get(),
@@ -113,15 +113,18 @@ public class RequestManager {
.exceptionally(throwable -> null) .exceptionally(throwable -> null)
.thenApply(reply -> { .thenApply(reply -> {
if (reply == null || reply.payload.teleportRequest == null) { if (reply == null || reply.payload.teleportRequest == null) {
return false; return null;
} }
// If the message was ignored by the recipient, return false // If the message was ignored by the recipient, return false
return reply.payload.teleportRequest.status == TeleportRequest.RequestStatus.PENDING; if (reply.payload.teleportRequest.status == TeleportRequest.RequestStatus.PENDING) {
}).join(); 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 request The {@link TeleportRequest} to send
* @param recipient The online recipient of the request * @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 * @return the {@link TeleportRequest} that was sent if it could be sent, otherwise an empty optional if it was not
* is ignoring requests or is vanished ({@link OnlineUser#isVanished()}) * sent because the recipient is ignoring requests or is vanished ({@link OnlineUser#isVanished()})
*/ */
public boolean sendLocalTeleportRequest(@NotNull TeleportRequest request, @NotNull OnlineUser recipient) { public Optional<TeleportRequest> sendLocalTeleportRequest(@NotNull TeleportRequest request, @NotNull OnlineUser recipient) {
if (isIgnoringRequests(recipient) || recipient.isVanished()) { if (isIgnoringRequests(recipient) || recipient.isVanished()) {
request.status = TeleportRequest.RequestStatus.IGNORED; 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 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) if (getTeleportRequest(request.requesterName, recipient).map(existingRequest -> existingRequest.type)
.orElse(null) == request.type) { .orElse(null) == request.type) {
return true; return Optional.of(request);
} }
// Add the request and display a message to the recipient // Add the request and display a message to the recipient
@@ -151,7 +154,7 @@ public class RequestManager {
.ifPresent(recipient::sendMessage); .ifPresent(recipient::sendMessage);
plugin.getLocales().getLocale("teleport_request_buttons", request.requesterName) plugin.getLocales().getLocale("teleport_request_buttons", request.requesterName)
.ifPresent(recipient::sendMessage); .ifPresent(recipient::sendMessage);
return true; return Optional.of(request);
} }
public void respondToTeleportRequestBySenderName(@NotNull OnlineUser recipient, @NotNull String senderName, public void respondToTeleportRequestBySenderName(@NotNull OnlineUser recipient, @NotNull String senderName,
@@ -73,6 +73,11 @@ public class TeleportRequest {
return Instant.now().isAfter(Instant.ofEpochSecond(expiryTime)); return Instant.now().isAfter(Instant.ofEpochSecond(expiryTime));
} }
@NotNull
public String getRecipientName() {
return recipientName;
}
public enum RequestType { public enum RequestType {
TPA, TPA,
TPA_HERE TPA_HERE