From ccda814c881ce1951a3db983e22dc348c7d014d9 Mon Sep 17 00:00:00 2001 From: William Date: Sun, 18 Dec 2022 14:09:04 +0000 Subject: [PATCH] Adjust caching mechanism for player lists --- bukkit/build.gradle | 2 +- .../java/net/william278/huskhomes/Cache.java | 33 ++++++++++--------- .../huskhomes/command/TpCommand.java | 6 ++-- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/bukkit/build.gradle b/bukkit/build.gradle index 11f9b36c..5fbdb568 100644 --- a/bukkit/build.gradle +++ b/bukkit/build.gradle @@ -15,7 +15,7 @@ dependencies { compileOnly 'net.william278:Annotaml:2.0' compileOnly 'net.william278:DesertWell:1.1' compileOnly 'com.github.MilkBowl:VaultAPI:1.7.1' - compileOnly 'com.github.Emibergo02:RedisEconomy:2.1-SNAPSHOT' + compileOnly 'com.github.Emibergo02:RedisEconomy:9f4f9c86f4' compileOnly 'us.dynmap:dynmap-api:3.1' testImplementation 'com.github.seeseemelk:MockBukkit-v1.16:1.5.2' diff --git a/common/src/main/java/net/william278/huskhomes/Cache.java b/common/src/main/java/net/william278/huskhomes/Cache.java index bdec81ad..b6beb760 100644 --- a/common/src/main/java/net/william278/huskhomes/Cache.java +++ b/common/src/main/java/net/william278/huskhomes/Cache.java @@ -41,7 +41,7 @@ public class Cache { /** * Cached player list */ - public final List players; + public final Set players; /** * Cached lists of private homes for pagination, mapped to the username of the home owner @@ -75,7 +75,7 @@ public class Cache { this.homes = new HashMap<>(); this.publicHomes = new HashMap<>(); this.warps = new ArrayList<>(); - this.players = new ArrayList<>(); + this.players = new HashSet<>(); this.privateHomeLists = new HashMap<>(); this.publicHomeLists = new HashMap<>(); this.warpLists = new HashMap<>(); @@ -104,22 +104,23 @@ public class Cache { * * @param plugin the implementing plugin */ - public CompletableFuture> updatePlayerListCache(@NotNull HuskHomes plugin, @NotNull OnlineUser requester) { + public CompletableFuture> updatePlayerListCache(@NotNull HuskHomes plugin, @NotNull OnlineUser requester) { + players.clear(); + players.addAll(plugin.getOnlinePlayers() + .stream() + .filter(player -> !player.isVanished()) + .map(onlineUser -> onlineUser.username) + .toList()); + if (plugin.getSettings().crossServer) { - return plugin.getMessenger().getOnlinePlayerNames(requester).thenApply(returnedPlayerList -> { - players.clear(); - players.addAll(List.of(returnedPlayerList)); - return players; - }); - } else { - players.clear(); - players.addAll(plugin.getOnlinePlayers() - .stream() - .filter(player -> !player.isVanished()) - .map(onlineUser -> onlineUser.username) - .toList()); - return CompletableFuture.completedFuture(players); + return plugin.getMessenger() + .getOnlinePlayerNames(requester) + .thenApply(networkedPlayers -> { + players.addAll(Set.of(networkedPlayers)); + return players; + }); } + return CompletableFuture.completedFuture(players); } /** 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 ad4e4eff..de014e9a 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java @@ -40,10 +40,8 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe // Find the online user to teleport plugin.getCache().updatePlayerListCache(plugin, onlineUser).thenRun(() -> { // Get the list of potential teleports, filtered against vanished players, but ensuring the executor is in the list - final List players = plugin.getCache().players; - if (!players.contains(onlineUser.username)) { - players.add(onlineUser.username); - } + final Set players = plugin.getCache().players; + players.add(onlineUser.username); // Find the player to teleport final String playerToTeleport = players.stream()