diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index 32e6595d..75c404d9 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -354,13 +354,18 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { return savedPositionManager; } + @NotNull @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; } + @NotNull @Override - public @NotNull RandomTeleportEngine getRandomTeleportEngine() { + public RandomTeleportEngine getRandomTeleportEngine() { return randomTeleportEngine; } @@ -447,7 +452,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { if (server != null) { return server; } - if (getSettings().crossServer && getNetworkMessenger() != null) { + if (getSettings().crossServer) { server = getNetworkMessenger().getServerName(requester).thenApply(Server::new).join(); } else { server = new Server("server"); diff --git a/common/src/main/java/net/william278/huskhomes/Cache.java b/common/src/main/java/net/william278/huskhomes/Cache.java index 424e20e8..94b12961 100644 --- a/common/src/main/java/net/william278/huskhomes/Cache.java +++ b/common/src/main/java/net/william278/huskhomes/Cache.java @@ -93,10 +93,8 @@ public class Cache { * * @param plugin the implementing plugin */ - public CompletableFuture> fetchAndCacheGlobalPlayerList(@NotNull HuskHomes plugin, - @NotNull OnlineUser requester) { + public CompletableFuture> updatePlayerListCache(@NotNull HuskHomes plugin, @NotNull OnlineUser requester) { if (plugin.getSettings().crossServer) { - assert plugin.getNetworkMessenger() != null; return plugin.getNetworkMessenger().getOnlinePlayerNames(requester).thenApply(returnedPlayerList -> { players.clear(); players.addAll(List.of(returnedPlayerList)); @@ -109,6 +107,7 @@ public class Cache { .filter(player -> !player.isVanished()) .map(onlineUser -> onlineUser.username) .toList()); + System.out.println(String.join(", ", players)); return CompletableFuture.completedFuture(players); } } diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomes.java b/common/src/main/java/net/william278/huskhomes/HuskHomes.java index 860b65ba..5f46cc84 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomes.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomes.java @@ -131,8 +131,8 @@ public interface HuskHomes { * * @return the {@link NetworkMessenger} implementation */ - @Nullable - NetworkMessenger getNetworkMessenger(); + @NotNull + NetworkMessenger getNetworkMessenger() throws HuskHomesException; /** * The {@link RandomTeleportEngine} that manages random teleports 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 248d0d22..10c98d51 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpAllCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpAllCommand.java @@ -22,7 +22,7 @@ public class TpAllCommand extends CommandBase { } // Determine players to teleport and teleport them - plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, onlineUser).thenAccept(fetchedPlayers -> { + plugin.getCache().updatePlayerListCache(plugin, onlineUser).thenAccept(fetchedPlayers -> { final List players = fetchedPlayers.stream() .filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList(); if (players.isEmpty()) { 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 bb0f60df..11424127 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java @@ -38,7 +38,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe : onlineUser.username : onlineUser.username); // 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() .filter(user -> user.equalsIgnoreCase(targetPlayerToTeleport)).findFirst() .or(() -> plugin.getCache().players.stream() 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 aab76336..5c39eb4e 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpaAllCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpaAllCommand.java @@ -26,7 +26,7 @@ public class TpaAllCommand extends CommandBase { } // Determine players to teleport and teleport them - plugin.getCache().fetchAndCacheGlobalPlayerList(plugin, onlineUser).thenAccept(playerList -> { + plugin.getCache().updatePlayerListCache(plugin, onlineUser).thenAccept(playerList -> { final List players = plugin.getCache().players.stream() .filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList(); if (players.isEmpty()) { 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 b33767bc..80b8ca94 100644 --- a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java +++ b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java @@ -78,11 +78,10 @@ public class EventListener { }).thenRun(() -> plugin.getDatabase().setCurrentTeleport(onlineUser, null)) .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(() -> { // Get this user's tp-ignore state and set locally plugin.getDatabase().getUserData(onlineUser.uuid).thenAccept(userData -> { @@ -116,15 +115,13 @@ public class EventListener { // Remove this user's home cache plugin.getCache().homes.remove(onlineUser.uuid); - // 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().fetchAndCacheGlobalPlayerList(plugin, updater)); - } + // Update the cached player list using another online player if possible + plugin.getOnlinePlayers() + .stream() + .filter(onlinePlayer -> !onlinePlayer.uuid.equals(onlineUser.uuid)) + .findAny() + .ifPresent(updater -> plugin.getCache().updatePlayerListCache(plugin, updater)); + // Set offline position plugin.getDatabase().setOfflinePosition(onlineUser, onlineUser.getPosition()); diff --git a/common/src/main/java/net/william278/huskhomes/request/RequestManager.java b/common/src/main/java/net/william278/huskhomes/request/RequestManager.java index 235dda36..33eaba6e 100644 --- a/common/src/main/java/net/william278/huskhomes/request/RequestManager.java +++ b/common/src/main/java/net/william278/huskhomes/request/RequestManager.java @@ -92,7 +92,7 @@ public class RequestManager { // If the player couldn't be found locally, send the request cross-server if (plugin.getSettings().crossServer) { - assert plugin.getNetworkMessenger() != null; + // Find the matching networked target return plugin.getNetworkMessenger().findPlayer(requester, targetUser).thenApply(networkedTarget -> { @@ -213,7 +213,7 @@ public class RequestManager { if (localRequester.isPresent()) { handleLocalRequestResponse(localRequester.get(), request); } else if (plugin.getSettings().crossServer) { - assert plugin.getNetworkMessenger() != null; + // Ensure the sender is still online if (!plugin.getNetworkMessenger().findPlayer(recipient, request.requesterName).thenApply(networkedTarget -> { if (networkedTarget.isEmpty()) { 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 45507bb4..32d503b5 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java @@ -126,7 +126,7 @@ public class TeleportManager { (timed ? timedTeleport(onlineUser, localPlayer.get().getPosition()) : teleport(onlineUser, localPlayer.get().getPosition())) .thenAccept(result -> finishTeleport(onlineUser, result)); } else if (plugin.getSettings().crossServer) { - assert plugin.getNetworkMessenger() != null; + getPlayerPositionByName(onlineUser, targetPlayer).thenAccept(optionalPosition -> { if (optionalPosition.isPresent()) { (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)) .thenApply(Optional::of); } - if (plugin.getSettings().crossServer && plugin.getNetworkMessenger() != null) { + if (plugin.getSettings().crossServer) { // If the player is not online, cancel if (plugin.getCache().players.stream().noneMatch(player -> player.equalsIgnoreCase(playerName))) { return CompletableFuture.completedFuture(Optional.empty()); @@ -219,7 +219,7 @@ public class TeleportManager { return CompletableFuture.supplyAsync(() -> Optional.of(localPlayer.get().getPosition())); } if (plugin.getSettings().crossServer) { - assert plugin.getNetworkMessenger() != null; + return plugin.getNetworkMessenger().findPlayer(requester, playerName).thenApply(foundPlayer -> { if (foundPlayer.isEmpty()) { return Optional.empty(); @@ -372,7 +372,7 @@ public class TeleportManager { * target server is not online */ private CompletableFuture teleportCrossServer(@NotNull OnlineUser onlineUser, @NotNull Teleport teleport) { - assert plugin.getNetworkMessenger() != null; + return plugin.getDatabase().setCurrentTeleport(teleport.player, teleport) .thenApply(ignored -> plugin.getNetworkMessenger().sendPlayer(onlineUser, teleport.target.server) .thenApply(completed -> completed ? TeleportResult.COMPLETED_CROSS_SERVER :