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;
}
@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");
@@ -93,10 +93,8 @@ public class Cache {
*
* @param plugin the implementing plugin
*/
public CompletableFuture<List<String>> fetchAndCacheGlobalPlayerList(@NotNull HuskHomes plugin,
@NotNull OnlineUser requester) {
public CompletableFuture<List<String>> 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);
}
}
@@ -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
@@ -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<String> players = fetchedPlayers.stream()
.filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList();
if (players.isEmpty()) {
@@ -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()
@@ -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<String> players = plugin.getCache().players.stream()
.filter(userName -> !userName.equalsIgnoreCase(onlineUser.username)).toList();
if (players.isEmpty()) {
@@ -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());
@@ -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()) {
@@ -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<TeleportResult> 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 :