Fix player cache not being updated on single-server mode, make #getNetworkMessenger @NotNull

This commit is contained in:
William committed 2022-08-30 18:15:26 +01:00
1 parent a952a372cc
commit 90291196f9
9 files changed
+31 -30

No files matched your search

@@ -354,13 +354,18 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
return savedPositionManager; return savedPositionManager;
} }
@NotNull
@Override @Override
public @Nullable NetworkMessenger getNetworkMessenger() { public NetworkMessenger getNetworkMessenger() {
if (networkMessenger == null) {
throw new HuskHomesException("Attempted to access network messenger when it was not initialized");
}
return networkMessenger; return networkMessenger;
} }
@NotNull
@Override @Override
public @NotNull RandomTeleportEngine getRandomTeleportEngine() { public RandomTeleportEngine getRandomTeleportEngine() {
return randomTeleportEngine; return randomTeleportEngine;
} }
@@ -447,7 +452,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
if (server != null) { if (server != null) {
return server; return server;
} }
if (getSettings().crossServer && getNetworkMessenger() != null) { if (getSettings().crossServer) {
server = getNetworkMessenger().getServerName(requester).thenApply(Server::new).join(); server = getNetworkMessenger().getServerName(requester).thenApply(Server::new).join();
} else { } else {
server = new Server("server"); server = new Server("server");
@@ -93,10 +93,8 @@ public class Cache {
* *
* @param plugin the implementing plugin * @param plugin the implementing plugin
*/ */
public CompletableFuture<List<String>> fetchAndCacheGlobalPlayerList(@NotNull HuskHomes plugin, public CompletableFuture<List<String>> updatePlayerListCache(@NotNull HuskHomes plugin, @NotNull OnlineUser requester) {
@NotNull OnlineUser requester) {
if (plugin.getSettings().crossServer) { if (plugin.getSettings().crossServer) {
assert plugin.getNetworkMessenger() != null;
return plugin.getNetworkMessenger().getOnlinePlayerNames(requester).thenApply(returnedPlayerList -> { return plugin.getNetworkMessenger().getOnlinePlayerNames(requester).thenApply(returnedPlayerList -> {
players.clear(); players.clear();
players.addAll(List.of(returnedPlayerList)); players.addAll(List.of(returnedPlayerList));
@@ -109,6 +107,7 @@ public class Cache {
.filter(player -> !player.isVanished()) .filter(player -> !player.isVanished())
.map(onlineUser -> onlineUser.username) .map(onlineUser -> onlineUser.username)
.toList()); .toList());
System.out.println(String.join(", ", players));
return CompletableFuture.completedFuture(players); return CompletableFuture.completedFuture(players);
} }
} }
@@ -131,8 +131,8 @@ public interface HuskHomes {
* *
* @return the {@link NetworkMessenger} implementation * @return the {@link NetworkMessenger} implementation
*/ */
@Nullable @NotNull
NetworkMessenger getNetworkMessenger(); NetworkMessenger getNetworkMessenger() throws HuskHomesException;
/** /**
* The {@link RandomTeleportEngine} that manages random teleports * The {@link RandomTeleportEngine} that manages random teleports
@@ -22,7 +22,7 @@ public class TpAllCommand extends CommandBase {
} }
// Determine players to teleport and teleport them // Determine players to teleport and teleport them
plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, onlineUser).thenAccept(fetchedPlayers -> { plugin.getCache().updatePlayerListCache(plugin, onlineUser).thenAccept(fetchedPlayers -> {
final List<String> players = fetchedPlayers.stream() final List<String> players = fetchedPlayers.stream()
.filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList(); .filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList();
if (players.isEmpty()) { if (players.isEmpty()) {
@@ -38,7 +38,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe
: onlineUser.username : onlineUser.username); : onlineUser.username : onlineUser.username);
// Find the online user to teleport // Find the online user to teleport
plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, onlineUser).join(); plugin.getCache().updatePlayerListCache(plugin, onlineUser).join();
final String playerToTeleport = plugin.getCache().players.stream() final String playerToTeleport = plugin.getCache().players.stream()
.filter(user -> user.equalsIgnoreCase(targetPlayerToTeleport)).findFirst() .filter(user -> user.equalsIgnoreCase(targetPlayerToTeleport)).findFirst()
.or(() -> plugin.getCache().players.stream() .or(() -> plugin.getCache().players.stream()
@@ -26,7 +26,7 @@ public class TpaAllCommand extends CommandBase {
} }
// Determine players to teleport and teleport them // Determine players to teleport and teleport them
plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, onlineUser).thenAccept(playerList -> { plugin.getCache().updatePlayerListCache(plugin, onlineUser).thenAccept(playerList -> {
final List<String> players = plugin.getCache().players.stream() final List<String> players = plugin.getCache().players.stream()
.filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList(); .filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList();
if (players.isEmpty()) { if (players.isEmpty()) {
@@ -78,11 +78,10 @@ public class EventListener {
}).thenRun(() -> plugin.getDatabase().setCurrentTeleport(onlineUser, null)) }).thenRun(() -> plugin.getDatabase().setCurrentTeleport(onlineUser, null))
.join(); .join();
})).join(); })).join();
// Update the player list
assert plugin.getNetworkMessenger() != null;
plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, onlineUser);
} }
}).thenRun(() -> {
// Update the cached player list
plugin.getCache().updatePlayerListCache(plugin, onlineUser);
}).thenRun(() -> { }).thenRun(() -> {
// Get this user's tp-ignore state and set locally // Get this user's tp-ignore state and set locally
plugin.getDatabase().getUserData(onlineUser.uuid).thenAccept(userData -> { plugin.getDatabase().getUserData(onlineUser.uuid).thenAccept(userData -> {
@@ -116,15 +115,13 @@ 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 using another online player if possible // Update the cached player list using another online player if possible
if (plugin.getSettings().crossServer) { plugin.getOnlinePlayers()
assert plugin.getNetworkMessenger() != null; .stream()
plugin.getOnlinePlayers() .filter(onlinePlayer -> !onlinePlayer.uuid.equals(onlineUser.uuid))
.stream() .findAny()
.filter(onlinePlayer -> !onlinePlayer.uuid.equals(onlineUser.uuid)) .ifPresent(updater -> plugin.getCache().updatePlayerListCache(plugin, updater));
.findAny()
.ifPresent(updater -> plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, updater));
}
// Set offline position // Set offline position
plugin.getDatabase().setOfflinePosition(onlineUser, onlineUser.getPosition()); plugin.getDatabase().setOfflinePosition(onlineUser, onlineUser.getPosition());
@@ -92,7 +92,7 @@ public class RequestManager {
// If the player couldn't be found locally, send the request cross-server // If the player couldn't be found locally, send the request cross-server
if (plugin.getSettings().crossServer) { if (plugin.getSettings().crossServer) {
assert plugin.getNetworkMessenger() != null;
// Find the matching networked target // Find the matching networked target
return plugin.getNetworkMessenger().findPlayer(requester, targetUser).thenApply(networkedTarget -> { return plugin.getNetworkMessenger().findPlayer(requester, targetUser).thenApply(networkedTarget -> {
@@ -213,7 +213,7 @@ public class RequestManager {
if (localRequester.isPresent()) { if (localRequester.isPresent()) {
handleLocalRequestResponse(localRequester.get(), request); handleLocalRequestResponse(localRequester.get(), request);
} else if (plugin.getSettings().crossServer) { } else if (plugin.getSettings().crossServer) {
assert plugin.getNetworkMessenger() != null;
// Ensure the sender is still online // Ensure the sender is still online
if (!plugin.getNetworkMessenger().findPlayer(recipient, request.requesterName).thenApply(networkedTarget -> { if (!plugin.getNetworkMessenger().findPlayer(recipient, request.requesterName).thenApply(networkedTarget -> {
if (networkedTarget.isEmpty()) { if (networkedTarget.isEmpty()) {
@@ -126,7 +126,7 @@ public class TeleportManager {
(timed ? timedTeleport(onlineUser, localPlayer.get().getPosition()) : teleport(onlineUser, localPlayer.get().getPosition())) (timed ? timedTeleport(onlineUser, localPlayer.get().getPosition()) : teleport(onlineUser, localPlayer.get().getPosition()))
.thenAccept(result -> finishTeleport(onlineUser, result)); .thenAccept(result -> finishTeleport(onlineUser, result));
} else if (plugin.getSettings().crossServer) { } else if (plugin.getSettings().crossServer) {
assert plugin.getNetworkMessenger() != null;
getPlayerPositionByName(onlineUser, targetPlayer).thenAccept(optionalPosition -> { getPlayerPositionByName(onlineUser, targetPlayer).thenAccept(optionalPosition -> {
if (optionalPosition.isPresent()) { if (optionalPosition.isPresent()) {
(timed ? timedTeleport(onlineUser, optionalPosition.get()) : teleport(onlineUser, optionalPosition.get())) (timed ? timedTeleport(onlineUser, optionalPosition.get()) : teleport(onlineUser, optionalPosition.get()))
@@ -160,7 +160,7 @@ public class TeleportManager {
return (timed ? timedTeleport(localPlayer.get(), position) : teleport(localPlayer.get(), position)) return (timed ? timedTeleport(localPlayer.get(), position) : teleport(localPlayer.get(), position))
.thenApply(Optional::of); .thenApply(Optional::of);
} }
if (plugin.getSettings().crossServer && plugin.getNetworkMessenger() != null) { if (plugin.getSettings().crossServer) {
// If the player is not online, cancel // If the player is not online, cancel
if (plugin.getCache().players.stream().noneMatch(player -> player.equalsIgnoreCase(playerName))) { if (plugin.getCache().players.stream().noneMatch(player -> player.equalsIgnoreCase(playerName))) {
return CompletableFuture.completedFuture(Optional.empty()); return CompletableFuture.completedFuture(Optional.empty());
@@ -219,7 +219,7 @@ public class TeleportManager {
return CompletableFuture.supplyAsync(() -> Optional.of(localPlayer.get().getPosition())); return CompletableFuture.supplyAsync(() -> Optional.of(localPlayer.get().getPosition()));
} }
if (plugin.getSettings().crossServer) { if (plugin.getSettings().crossServer) {
assert plugin.getNetworkMessenger() != null;
return plugin.getNetworkMessenger().findPlayer(requester, playerName).thenApply(foundPlayer -> { return plugin.getNetworkMessenger().findPlayer(requester, playerName).thenApply(foundPlayer -> {
if (foundPlayer.isEmpty()) { if (foundPlayer.isEmpty()) {
return Optional.empty(); return Optional.empty();
@@ -372,7 +372,7 @@ public class TeleportManager {
* target server is not online * target server is not online
*/ */
private CompletableFuture<TeleportResult> teleportCrossServer(@NotNull OnlineUser onlineUser, @NotNull Teleport teleport) { private CompletableFuture<TeleportResult> teleportCrossServer(@NotNull OnlineUser onlineUser, @NotNull Teleport teleport) {
assert plugin.getNetworkMessenger() != null;
return plugin.getDatabase().setCurrentTeleport(teleport.player, teleport) return plugin.getDatabase().setCurrentTeleport(teleport.player, teleport)
.thenApply(ignored -> plugin.getNetworkMessenger().sendPlayer(onlineUser, teleport.target.server) .thenApply(ignored -> plugin.getNetworkMessenger().sendPlayer(onlineUser, teleport.target.server)
.thenApply(completed -> completed ? TeleportResult.COMPLETED_CROSS_SERVER : .thenApply(completed -> completed ? TeleportResult.COMPLETED_CROSS_SERVER :