More aggressive checks to ensure server name is fetched and cached on first player join

This commit is contained in:
William committed 2022-09-21 17:41:18 +01:00
1 parent 18a06b5a98
commit 26d59cce97
2 files changed
+36 -22

No files matched your search

@@ -51,7 +51,9 @@ import org.jetbrains.annotations.Nullable;
import java.io.File; import java.io.File;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -470,19 +472,31 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
} }
public CompletableFuture<Void> fetchServer(@NotNull OnlineUser requester) { public CompletableFuture<Void> fetchServer(@NotNull OnlineUser requester) {
if (!getSettings().crossServer) { if (!getSettings().crossServer || server != null) {
return CompletableFuture.completedFuture(null); return CompletableFuture.completedFuture(null);
} }
if (server == null) { assert networkMessenger != null;
return getNetworkMessenger().getServerName(requester).thenAcceptAsync(serverName -> { getLoggingAdapter().log(Level.INFO, "Fetching server information from the proxy...");
// Set the server name final AtomicInteger attempts = new AtomicInteger(0);
this.server = new Server(serverName); return CompletableFuture.runAsync(() -> {
String serverName = null;
// Cache the server to the server.yml file while (serverName == null) {
Annotaml.save(new CachedServer(serverName), new File(getDataFolder(), "server.yml")); attempts.getAndIncrement();
}); serverName = networkMessenger.getServerName(requester)
} .orTimeout(1, TimeUnit.SECONDS)
return CompletableFuture.completedFuture(null); .exceptionally(throwable -> null).join();
}
getLoggingAdapter().log(Level.INFO, "Successfully fetched server information from the proxy (name: "
+ serverName + ", attempts: " + attempts.get() + ")");
server = new Server(serverName);
Annotaml.save(new CachedServer(serverName), new File(getDataFolder(), "server.yml"));
})
.orTimeout(5, TimeUnit.SECONDS)
.exceptionally(throwable -> {
getLoggingAdapter().log(Level.SEVERE, "Failed to fetch server name from the proxy (attempts: "
+ attempts.get() + ")", throwable);
return null;
});
} }
@Override @Override
@@ -37,7 +37,7 @@ public class EventListener {
protected final void handlePlayerJoin(@NotNull OnlineUser onlineUser) { protected final void handlePlayerJoin(@NotNull OnlineUser onlineUser) {
// Handle the first player joining the server // Handle the first player joining the server
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
if (firstUser) { if (plugin.getSettings().crossServer && firstUser) {
firstUser = false; firstUser = false;
plugin.fetchServer(onlineUser).join(); plugin.fetchServer(onlineUser).join();
} }
@@ -65,16 +65,16 @@ public class EventListener {
// Teleport the player locally // Teleport the player locally
onlineUser.teleport(activeTeleport.target, plugin.getSettings().asynchronousTeleports).thenAccept(teleportResult -> { onlineUser.teleport(activeTeleport.target, plugin.getSettings().asynchronousTeleports).thenAccept(teleportResult -> {
if (!teleportResult.successful) { if (!teleportResult.successful) {
plugin.getLocales().getLocale("error_invalid_world") plugin.getLocales().getLocale("error_invalid_world")
.ifPresent(onlineUser::sendMessage); .ifPresent(onlineUser::sendMessage);
} else { } else {
plugin.getLocales().getLocale("teleporting_complete") plugin.getLocales().getLocale("teleporting_complete")
.ifPresent(onlineUser::sendMessage); .ifPresent(onlineUser::sendMessage);
plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_COMPLETE) plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_COMPLETE)
.ifPresent(onlineUser::playSound); .ifPresent(onlineUser::playSound);
} }
}).thenRun(() -> plugin.getDatabase().setCurrentTeleport(onlineUser, null)); }).thenRun(() -> plugin.getDatabase().setCurrentTeleport(onlineUser, null));
})); }));
} }
}).thenRun(() -> { }).thenRun(() -> {