mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 22:16:34 +00:00
Adjust player list caching
This commit is contained in:
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
|
* @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) {
|
if (plugin.getSettings().crossServer) {
|
||||||
assert plugin.getNetworkMessenger() != null;
|
assert plugin.getNetworkMessenger() != null;
|
||||||
plugin.getNetworkMessenger().getOnlinePlayerNames(requester).thenAccept(returnedPlayerList -> {
|
return plugin.getNetworkMessenger().getOnlinePlayerNames(requester).thenApply(returnedPlayerList -> {
|
||||||
players.clear();
|
players.clear();
|
||||||
players.addAll(List.of(returnedPlayerList));
|
players.addAll(List.of(returnedPlayerList));
|
||||||
|
return players;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
players.clear();
|
players.clear();
|
||||||
@@ -107,6 +109,7 @@ public class Cache {
|
|||||||
.filter(player -> !player.isVanished())
|
.filter(player -> !player.isVanished())
|
||||||
.map(onlineUser -> onlineUser.username)
|
.map(onlineUser -> onlineUser.username)
|
||||||
.toList());
|
.toList());
|
||||||
|
return CompletableFuture.completedFuture(players);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,24 +21,23 @@ public class TpAllCommand extends CommandBase {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update cached players
|
// Determine players to teleport and teleport them
|
||||||
plugin.getCache().updateOnlinePlayerList(plugin, onlineUser);
|
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
|
// Send a message
|
||||||
final List<String> players = plugin.getCache().players.stream()
|
plugin.getLocales().getLocale("teleporting_all_players", Integer.toString(players.size()))
|
||||||
.filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList();
|
.ifPresent(onlineUser::sendMessage);
|
||||||
if (players.isEmpty()) {
|
|
||||||
plugin.getLocales().getLocale("error_no_players_online").ifPresent(onlineUser::sendMessage);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send a message
|
// Teleport every player
|
||||||
plugin.getLocales().getLocale("teleporting_all_players", Integer.toString(players.size()))
|
players.forEach(playerName -> plugin.getTeleportManager()
|
||||||
.ifPresent(onlineUser::sendMessage);
|
.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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update cached players
|
// Determine players to teleport and teleport them
|
||||||
plugin.getCache().updateOnlinePlayerList(plugin, onlineUser);
|
plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, onlineUser).thenAccept(playerList -> {
|
||||||
|
final List<String> players = plugin.getCache().players.stream()
|
||||||
// Determine players to send teleport requests to
|
.filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList();
|
||||||
final List<String> players = plugin.getCache().players.stream()
|
if (players.isEmpty()) {
|
||||||
.filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList();
|
plugin.getLocales().getLocale("error_no_players_online").ifPresent(onlineUser::sendMessage);
|
||||||
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);
|
|
||||||
return;
|
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
|
// Update the player list
|
||||||
assert plugin.getNetworkMessenger() != null;
|
assert plugin.getNetworkMessenger() != null;
|
||||||
plugin.getCache().updateOnlinePlayerList(plugin, onlineUser);
|
plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, onlineUser);
|
||||||
}
|
}
|
||||||
}).thenRun(() -> {
|
}).thenRun(() -> {
|
||||||
// Get this user's tp-ignore state and set locally
|
// Get this user's tp-ignore state and set locally
|
||||||
@@ -116,13 +116,14 @@ public class EventListener {
|
|||||||
// Remove this user's home cache
|
// Remove this user's home cache
|
||||||
plugin.getCache().homes.remove(onlineUser.uuid);
|
plugin.getCache().homes.remove(onlineUser.uuid);
|
||||||
|
|
||||||
// Update the player list
|
// Update the player list using another online player if possible
|
||||||
if (plugin.getSettings().crossServer) {
|
if (plugin.getSettings().crossServer) {
|
||||||
assert plugin.getNetworkMessenger() != null;
|
assert plugin.getNetworkMessenger() != null;
|
||||||
plugin.getOnlinePlayers().stream().filter(
|
plugin.getOnlinePlayers()
|
||||||
onlinePlayer -> !onlinePlayer.uuid.equals(onlineUser.uuid))
|
.stream()
|
||||||
.findAny().ifPresent(updater ->
|
.filter(onlinePlayer -> !onlinePlayer.uuid.equals(onlineUser.uuid))
|
||||||
plugin.getCache().updateOnlinePlayerList(plugin, updater));
|
.findAny()
|
||||||
|
.ifPresent(updater -> plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, updater));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set offline position
|
// Set offline position
|
||||||
|
|||||||
@@ -161,6 +161,12 @@ public class TeleportManager {
|
|||||||
.thenApply(Optional::of);
|
.thenApply(Optional::of);
|
||||||
}
|
}
|
||||||
if (plugin.getSettings().crossServer && plugin.getNetworkMessenger() != null) {
|
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,
|
return plugin.getNetworkMessenger().sendMessage(requester,
|
||||||
new Message(Message.MessageType.TELEPORT_TO_POSITION_REQUEST,
|
new Message(Message.MessageType.TELEPORT_TO_POSITION_REQUEST,
|
||||||
requester.username,
|
requester.username,
|
||||||
@@ -193,7 +199,7 @@ public class TeleportManager {
|
|||||||
@NotNull String targetPlayer,
|
@NotNull String targetPlayer,
|
||||||
@NotNull OnlineUser requester,
|
@NotNull OnlineUser requester,
|
||||||
final boolean timed) {
|
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);
|
final Optional<Position> localPositionTarget = localPlayer.map(OnlineUser::getPosition);
|
||||||
if (localPositionTarget.isPresent()) {
|
if (localPositionTarget.isPresent()) {
|
||||||
return teleportPlayerByName(playerName, localPositionTarget.get(), requester, timed);
|
return teleportPlayerByName(playerName, localPositionTarget.get(), requester, timed);
|
||||||
|
|||||||
Reference in new issue
Block a user