Adjust player list caching

This commit is contained in:
William committed 2022-08-25 20:07:26 +01:00
1 parent 7b07e45742
commit c22276908a
5 files changed
+59 -52

No files matched your search

@@ -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<List<String>> 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);
}
}
@@ -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<String> 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<String> 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));
});
}
@@ -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<String> 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<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))));
// 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<String> 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<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))));
// 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);
});
});
}
}
@@ -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
@@ -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<OnlineUser> localPlayer = plugin.findPlayer(playerName);
final Optional<OnlineUser> localPlayer = !playerName.equals("@s") ? plugin.findPlayer(playerName) : Optional.of(requester);
final Optional<Position> localPositionTarget = localPlayer.map(OnlineUser::getPosition);
if (localPositionTarget.isPresent()) {
return teleportPlayerByName(playerName, localPositionTarget.get(), requester, timed);