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<>();
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(() -> {
@@ -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 <player>")
@@ -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 {
@@ -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<Boolean> sendTeleportRequest(@NotNull OnlineUser requester, @NotNull String targetUser,
@NotNull TeleportRequest.RequestType requestType) {
public CompletableFuture<Optional<TeleportRequest>> 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<OnlineUser> 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<TeleportRequest> 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,
@@ -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