From c22276908abee0af07fb7bf3df1bf84c8ee065c0 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Aug 2022 20:07:26 +0100 Subject: [PATCH] Adjust player list caching --- .../java/net/william278/huskhomes/Cache.java | 9 ++-- .../huskhomes/command/TpAllCommand.java | 31 ++++++------ .../huskhomes/command/TpaAllCommand.java | 50 +++++++++---------- .../huskhomes/listener/EventListener.java | 13 ++--- .../huskhomes/teleport/TeleportManager.java | 8 ++- 5 files changed, 59 insertions(+), 52 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/Cache.java b/common/src/main/java/net/william278/huskhomes/Cache.java index 3a27e187..424e20e8 100644 --- a/common/src/main/java/net/william278/huskhomes/Cache.java +++ b/common/src/main/java/net/william278/huskhomes/Cache.java @@ -89,16 +89,18 @@ public class Cache { } /** - * Updates the cached list of online players + * Updates the cached list of online players and returns it * * @param plugin the implementing plugin */ - public void updateOnlinePlayerList(@NotNull HuskHomes plugin, @NotNull OnlineUser requester) { + public CompletableFuture> fetchAndCacheGlobalPlayerList(@NotNull HuskHomes plugin, + @NotNull OnlineUser requester) { if (plugin.getSettings().crossServer) { assert plugin.getNetworkMessenger() != null; - plugin.getNetworkMessenger().getOnlinePlayerNames(requester).thenAccept(returnedPlayerList -> { + return plugin.getNetworkMessenger().getOnlinePlayerNames(requester).thenApply(returnedPlayerList -> { players.clear(); players.addAll(List.of(returnedPlayerList)); + return players; }); } else { players.clear(); @@ -107,6 +109,7 @@ public class Cache { .filter(player -> !player.isVanished()) .map(onlineUser -> onlineUser.username) .toList()); + return CompletableFuture.completedFuture(players); } } 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 24fb0c34..248d0d22 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpAllCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpAllCommand.java @@ -21,24 +21,23 @@ public class TpAllCommand extends CommandBase { return; } - // Update cached players - plugin.getCache().updateOnlinePlayerList(plugin, onlineUser); + // Determine players to teleport and teleport them + plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, onlineUser).thenAccept(fetchedPlayers -> { + final List players = fetchedPlayers.stream() + .filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList(); + if (players.isEmpty()) { + plugin.getLocales().getLocale("error_no_players_online").ifPresent(onlineUser::sendMessage); + return; + } - // Determine players to teleport - final List players = plugin.getCache().players.stream() - .filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList(); - if (players.isEmpty()) { - plugin.getLocales().getLocale("error_no_players_online").ifPresent(onlineUser::sendMessage); - return; - } + // Send a message + plugin.getLocales().getLocale("teleporting_all_players", Integer.toString(players.size())) + .ifPresent(onlineUser::sendMessage); - // 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, false)); + // Teleport every player + players.forEach(playerName -> plugin.getTeleportManager() + .teleportPlayerByName(playerName, onlineUser.getPosition(), onlineUser, false)); + }); } 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 f4f04d83..88005c53 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpaAllCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpaAllCommand.java @@ -25,35 +25,33 @@ public class TpaAllCommand extends CommandBase { return; } - // Update cached players - plugin.getCache().updateOnlinePlayerList(plugin, onlineUser); - - // Determine players to send teleport requests to - final List players = plugin.getCache().players.stream() - .filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList(); - if (players.isEmpty()) { - plugin.getLocales().getLocale("error_no_players_online").ifPresent(onlineUser::sendMessage); - return; - } - - // Send a teleport request to every player - final AtomicInteger counter = new AtomicInteger(0); - 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)))); - - // Send a message when all requests have been sent - CompletableFuture.allOf(sentRequestsFuture.toArray(new CompletableFuture[0])).thenRun(() -> { - if (counter.get() == 0) { - plugin.getLocales().getLocale("error_no_players_online") - .ifPresent(onlineUser::sendMessage); + // Determine players to teleport and teleport them + plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, onlineUser).thenAccept(playerList -> { + final List players = plugin.getCache().players.stream() + .filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList(); + if (players.isEmpty()) { + plugin.getLocales().getLocale("error_no_players_online").ifPresent(onlineUser::sendMessage); return; } - plugin.getLocales().getLocale("tpaall_request_sent", Integer.toString(counter.get())) - .ifPresent(onlineUser::sendMessage); - }); + // Send a teleport request to every player + final AtomicInteger counter = new AtomicInteger(0); + 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)))); + + // Send a message when all requests have been sent + CompletableFuture.allOf(sentRequestsFuture.toArray(new CompletableFuture[0])).thenRun(() -> { + if (counter.get() == 0) { + plugin.getLocales().getLocale("error_no_players_online") + .ifPresent(onlineUser::sendMessage); + return; + } + plugin.getLocales().getLocale("tpaall_request_sent", Integer.toString(counter.get())) + .ifPresent(onlineUser::sendMessage); + }); + }); } } diff --git a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java index ad2b055b..411b1712 100644 --- a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java +++ b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java @@ -81,7 +81,7 @@ public class EventListener { // Update the player list assert plugin.getNetworkMessenger() != null; - plugin.getCache().updateOnlinePlayerList(plugin, onlineUser); + plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, onlineUser); } }).thenRun(() -> { // Get this user's tp-ignore state and set locally @@ -116,13 +116,14 @@ public class EventListener { // Remove this user's home cache plugin.getCache().homes.remove(onlineUser.uuid); - // Update the player list + // Update the player list using another online player if possible if (plugin.getSettings().crossServer) { assert plugin.getNetworkMessenger() != null; - plugin.getOnlinePlayers().stream().filter( - onlinePlayer -> !onlinePlayer.uuid.equals(onlineUser.uuid)) - .findAny().ifPresent(updater -> - plugin.getCache().updateOnlinePlayerList(plugin, updater)); + plugin.getOnlinePlayers() + .stream() + .filter(onlinePlayer -> !onlinePlayer.uuid.equals(onlineUser.uuid)) + .findAny() + .ifPresent(updater -> plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, updater)); } // Set offline position 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 d1e0d6a2..fba0951f 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java @@ -161,6 +161,12 @@ public class TeleportManager { .thenApply(Optional::of); } if (plugin.getSettings().crossServer && plugin.getNetworkMessenger() != null) { + // If the player is not online, cancel + if (plugin.getCache().players.stream().noneMatch(player -> player.equalsIgnoreCase(playerName))) { + return CompletableFuture.completedFuture(Optional.empty()); + } + + // Dispatch the message cross-server return plugin.getNetworkMessenger().sendMessage(requester, new Message(Message.MessageType.TELEPORT_TO_POSITION_REQUEST, requester.username, @@ -193,7 +199,7 @@ public class TeleportManager { @NotNull String targetPlayer, @NotNull OnlineUser requester, final boolean timed) { - final Optional localPlayer = plugin.findPlayer(playerName); + final Optional localPlayer = !playerName.equals("@s") ? plugin.findPlayer(playerName) : Optional.of(requester); final Optional localPositionTarget = localPlayer.map(OnlineUser::getPosition); if (localPositionTarget.isPresent()) { return teleportPlayerByName(playerName, localPositionTarget.get(), requester, timed);