From 74cb268426809fe2e1c404c0c1ebae2c622710b6 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 31 Aug 2022 11:55:47 +0100 Subject: [PATCH] Refactor #getServer() to be pre-cached from a CachedServer yaml file, no longer require fetcher, add #fetchServer(requester) --- .../william278/huskhomes/BukkitHuskHomes.java | 53 ++++++++++++------- .../huskhomes/api/HuskHomesAPI.java | 8 +-- .../listener/BukkitEventListener.java | 4 +- .../huskhomes/player/BukkitPlayer.java | 4 +- .../net/william278/huskhomes/HuskHomes.java | 29 ++++++---- .../huskhomes/command/SpawnCommand.java | 2 +- .../huskhomes/command/TpCommand.java | 2 +- .../huskhomes/config/CachedServer.java | 32 +++++++++++ .../config/{Spawn.java => CachedSpawn.java} | 8 +-- .../huskhomes/listener/EventListener.java | 6 +-- .../huskhomes/teleport/TeleportManager.java | 2 +- 11 files changed, 105 insertions(+), 45 deletions(-) create mode 100644 common/src/main/java/net/william278/huskhomes/config/CachedServer.java rename common/src/main/java/net/william278/huskhomes/config/{Spawn.java => CachedSpawn.java} (92%) diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index 75c404d9..3a56ad49 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -7,9 +7,10 @@ import net.william278.huskhomes.command.BukkitCommand; import net.william278.huskhomes.command.BukkitCommandType; import net.william278.huskhomes.command.CommandBase; import net.william278.huskhomes.command.DisabledCommand; +import net.william278.huskhomes.config.CachedServer; import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Settings; -import net.william278.huskhomes.config.Spawn; +import net.william278.huskhomes.config.CachedSpawn; import net.william278.huskhomes.database.Database; import net.william278.huskhomes.database.MySqlDatabase; import net.william278.huskhomes.database.SqLiteDatabase; @@ -69,7 +70,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { private SavedPositionManager savedPositionManager; private EventListener eventListener; private RandomTeleportEngine randomTeleportEngine; - private Spawn serverSpawn; + private CachedSpawn serverSpawn; private EventDispatcher eventDispatcher; private Set pluginHooks; private List registeredCommands; @@ -386,13 +387,13 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { } @Override - public Optional getServerSpawn() { + public Optional getServerSpawn() { return Optional.ofNullable(serverSpawn); } @Override public void setServerSpawn(@NotNull Location location) { - final Spawn newSpawn = new Spawn(location); + final CachedSpawn newSpawn = new CachedSpawn(location); this.serverSpawn = newSpawn; Annotaml.save(newSpawn, new File(getDataFolder(), "spawn.yml")); @@ -441,25 +442,31 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { return registeredCommands; } - /** - * Returns the {@link Server} the plugin is on - * - * @return The {@link Server} object - */ @Override @NotNull - public Server getServer(@NotNull OnlineUser requester) { - if (server != null) { - return server; - } - if (getSettings().crossServer) { - server = getNetworkMessenger().getServerName(requester).thenApply(Server::new).join(); - } else { - server = new Server("server"); + public Server getPluginServer() throws HuskHomesException { + if (server == null) { + throw new HuskHomesException("Attempted to access server when it was not initialized"); } return server; } + public CompletableFuture fetchServer(@NotNull OnlineUser requester) { + if (!getSettings().crossServer) { + return CompletableFuture.completedFuture(null); + } + if (server == null) { + return getNetworkMessenger().getServerName(requester).thenAccept(serverName -> { + // Set the server name + this.server = new Server(serverName); + + // Cache the server to the server.yml file + Annotaml.save(new CachedServer(serverName), new File(getDataFolder(), "server.yml")); + }); + } + return CompletableFuture.completedFuture(null); + } + @Override public @NotNull List getWorlds() { return getServer().getWorlds().stream().filter(world -> BukkitAdapter.adaptWorld(world).isPresent()) @@ -476,10 +483,20 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { Objects.requireNonNull(resourceReader.getResource("locales/" + settings.language + ".yml")), Locales.class, Annotaml.LoaderOptions.builder().copyDefaults(true)); + // Load cached server from file + if (settings.crossServer) { + final File serverFile = new File(getDataFolder(), "server.yml"); + if (serverFile.exists()) { + this.server = Annotaml.load(serverFile, CachedServer.class).getServer(); + } + } else { + this.server = new Server("server"); + } + // Load spawn location from file final File spawnFile = new File(getDataFolder(), "spawn.yml"); if (spawnFile.exists()) { - this.serverSpawn = Annotaml.load(spawnFile, Spawn.class); + this.serverSpawn = Annotaml.load(spawnFile, CachedSpawn.class); } return true; }).exceptionally(throwable -> { diff --git a/bukkit/src/main/java/net/william278/huskhomes/api/HuskHomesAPI.java b/bukkit/src/main/java/net/william278/huskhomes/api/HuskHomesAPI.java index 1040c550..f9e17218 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/api/HuskHomesAPI.java +++ b/bukkit/src/main/java/net/william278/huskhomes/api/HuskHomesAPI.java @@ -107,14 +107,14 @@ public class HuskHomesAPI extends BaseHuskHomesAPI { } /** - * Get this {@link Server}. + * Get the {@link Server}, containing the ID of the server the plugin is running on * - * @param requester a player; needed to send a plugin message requesting the server if it has not been cached * @return the {@link Server} + * @since 3.0 */ @NotNull - public Server getServer(@NotNull OnlineUser requester) { - return plugin.getServer(requester); + public Server getServer() { + return plugin.getPluginServer(); } } diff --git a/bukkit/src/main/java/net/william278/huskhomes/listener/BukkitEventListener.java b/bukkit/src/main/java/net/william278/huskhomes/listener/BukkitEventListener.java index e882e9c4..2098e8e4 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/listener/BukkitEventListener.java +++ b/bukkit/src/main/java/net/william278/huskhomes/listener/BukkitEventListener.java @@ -58,7 +58,7 @@ public class BukkitEventListener extends EventListener implements Listener { CompletableFuture.runAsync(() -> { final BukkitPlayer bukkitPlayer = BukkitPlayer.adapt(player); BukkitAdapter.adaptLocation(event.getFrom()).ifPresent(sourceLocation -> - handlePlayerTeleport(bukkitPlayer, new Position(sourceLocation, plugin.getServer(bukkitPlayer)))); + handlePlayerTeleport(bukkitPlayer, new Position(sourceLocation, plugin.getPluginServer()))); }); } @@ -80,7 +80,7 @@ public class BukkitEventListener extends EventListener implements Listener { super.handlePlayerUpdateSpawnPoint(onlineUser, new Position( adaptedLocation.x, adaptedLocation.y, adaptedLocation.z, adaptedLocation.yaw, adaptedLocation.pitch, - adaptedLocation.world, plugin.getServer(onlineUser))); + adaptedLocation.world, plugin.getPluginServer())); })); } diff --git a/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java b/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java index 0ea456ff..4516bcad 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java +++ b/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java @@ -65,14 +65,14 @@ public class BukkitPlayer extends OnlineUser { public Position getPosition() { return new Position(BukkitAdapter.adaptLocation(player.getLocation()) .orElseThrow(() -> new HuskHomesException("Failed to get the position of a BukkitPlayer (null)")), - BukkitHuskHomes.getInstance().getServer(this)); + BukkitHuskHomes.getInstance().getPluginServer()); } @Override public Optional getBedSpawnPosition() { return Optional.ofNullable(player.getBedSpawnLocation()).flatMap(BukkitAdapter::adaptLocation) - .map(location -> new Position(location, BukkitHuskHomes.getInstance().getServer(this))); + .map(location -> new Position(location, BukkitHuskHomes.getInstance().getPluginServer())); } @Override diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomes.java b/common/src/main/java/net/william278/huskhomes/HuskHomes.java index 5f46cc84..55faffae 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomes.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomes.java @@ -5,7 +5,7 @@ import net.william278.desertwell.Version; import net.william278.huskhomes.command.CommandBase; import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Settings; -import net.william278.huskhomes.config.Spawn; +import net.william278.huskhomes.config.CachedSpawn; import net.william278.huskhomes.database.Database; import net.william278.huskhomes.event.EventDispatcher; import net.william278.huskhomes.hook.EconomyHook; @@ -165,11 +165,11 @@ public interface HuskHomes { List getMigrators(); /** - * The {@link Spawn} location of this server + * The {@link CachedSpawn} location of this server * - * @return the {@link Spawn} location data + * @return the {@link CachedSpawn} location data */ - Optional getServerSpawn(); + Optional getServerSpawn(); /** * Returns a future returning the latest plugin {@link Version} if the plugin is out-of-date @@ -188,9 +188,9 @@ public interface HuskHomes { } /** - * Update the {@link Spawn} position to a location on the server + * Update the {@link CachedSpawn} position to a location on the server * - * @param location the new {@link Spawn} location + * @param location the new {@link CachedSpawn} location */ void setServerSpawn(@NotNull Location location); @@ -270,12 +270,23 @@ public interface HuskHomes { CompletableFuture> getSafeGroundLocation(@NotNull Location location); /** - * Get the {@link Server} this server is on + * Returns the {@link Server} the plugin is on * - * @return The server + * @return The {@link Server} object + * @throws HuskHomesException If the server has not been initialized */ @NotNull - Server getServer(@NotNull OnlineUser requester); + Server getPluginServer() throws HuskHomesException; + + /** + * Fetches the name of this server if {@link Server} is {@code null} by querying the proxy + * + * @param requester The {@link OnlineUser} to carry out the proxy request + * @return a future completing when the server has been fetched + * @implNote If cross-server mode is disabled, or the server has already been pulled from the server.yml cache file, + * the future will return immediately + */ + CompletableFuture fetchServer(@NotNull OnlineUser requester); /** * Returns a resource read from the plugin resources folder diff --git a/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java b/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java index 0de8a607..2d96bc2c 100644 --- a/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java @@ -26,7 +26,7 @@ public class SpawnCommand extends CommandBase { CompletableFuture.runAsync(() -> { final Optional position = (plugin.getSettings().crossServer && plugin.getSettings().globalSpawn ? plugin.getDatabase().getWarp(plugin.getSettings().globalSpawnName).join() - : plugin.getServerSpawn().flatMap(spawn -> spawn.getLocation(plugin.getServer(onlineUser)))); + : plugin.getServerSpawn().flatMap(spawn -> spawn.getLocation(plugin.getPluginServer()))); if (position.isEmpty()) { plugin.getLocales().getLocale("error_spawn_not_set").ifPresent(onlineUser::sendMessage); return; 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 11424127..1699546c 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java @@ -173,7 +173,7 @@ public class TpCommand extends CommandBase implements TabCompletable, ConsoleExe Double.parseDouble(args[3]), 0f, 0f, args.length >= 5 ? new World(args[4], UUID.randomUUID()) : plugin.getWorlds().get(0), - args.length == 6 ? new Server(args[5]) : plugin.getServer(playerToTeleport))); + args.length == 6 ? new Server(args[5]) : plugin.getPluginServer())); } catch (NumberFormatException e) { plugin.getLoggingAdapter().log(Level.WARNING, "Invalid syntax. Usage: tp [world] [server]"); return; diff --git a/common/src/main/java/net/william278/huskhomes/config/CachedServer.java b/common/src/main/java/net/william278/huskhomes/config/CachedServer.java new file mode 100644 index 00000000..e0d994a9 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/config/CachedServer.java @@ -0,0 +1,32 @@ +package net.william278.huskhomes.config; + +import net.william278.annotaml.YamlFile; +import net.william278.huskhomes.position.Server; +import org.jetbrains.annotations.NotNull; + +@YamlFile(header = """ + ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ + ┃ Server ID cache. Must match ┃ + ┃ server name in proxy config. ┃ + ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛""") +public class CachedServer { + + /** + * Name of the server (must match proxy config ID) + */ + public String serverName; + + public CachedServer(@NotNull String serverName) { + this.serverName = serverName; + } + + @SuppressWarnings("unused") + public CachedServer() { + } + + @NotNull + public Server getServer() { + return new Server(serverName); + } + +} \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/config/Spawn.java b/common/src/main/java/net/william278/huskhomes/config/CachedSpawn.java similarity index 92% rename from common/src/main/java/net/william278/huskhomes/config/Spawn.java rename to common/src/main/java/net/william278/huskhomes/config/CachedSpawn.java index bfe41450..e07bb96e 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Spawn.java +++ b/common/src/main/java/net/william278/huskhomes/config/CachedSpawn.java @@ -15,10 +15,10 @@ import java.util.UUID; */ @YamlFile(header = """ ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ - ┃ Server /spawn location ┃ + ┃ Server /spawn location cache ┃ ┃ Edit in-game using /setspawn ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛""") -public class Spawn { +public class CachedSpawn { public double x; public double y; @@ -47,7 +47,7 @@ public class Spawn { * * @param location The {@link Location} of the spawn */ - public Spawn(@NotNull Location location) { + public CachedSpawn(@NotNull Location location) { this.x = location.x; this.y = location.y; this.z = location.z; @@ -58,6 +58,6 @@ public class Spawn { } @SuppressWarnings("unused") - public Spawn() { + public CachedSpawn() { } } 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 80b8ca94..88d9c554 100644 --- a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java +++ b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java @@ -38,8 +38,8 @@ public class EventListener { // Handle the first player joining the server CompletableFuture.runAsync(() -> { if (firstUser) { - plugin.getServer(onlineUser); firstUser = false; + plugin.fetchServer(onlineUser).join(); } }).thenRun(() -> { // Ensure the player is present on the database first @@ -52,7 +52,7 @@ public class EventListener { if (activeTeleport.type == TeleportType.RESPAWN) { final Optional bedPosition = onlineUser.getBedSpawnPosition(); if (bedPosition.isEmpty()) { - plugin.getServerSpawn().flatMap(spawn -> spawn.getLocation(plugin.getServer(onlineUser))) + plugin.getServerSpawn().flatMap(spawn -> spawn.getLocation(plugin.getPluginServer())) .ifPresent(position -> onlineUser.teleport(position, plugin.getSettings().asynchronousTeleports)); onlineUser.sendMinecraftMessage("block.minecraft.spawn.not_valid"); } else { @@ -155,7 +155,7 @@ public class EventListener { // Respawn the player cross-server if needed if (plugin.getSettings().crossServer && plugin.getSettings().globalRespawning) { plugin.getDatabase().getRespawnPosition(onlineUser).thenAccept(position -> position.ifPresent(respawnPosition -> { - if (!respawnPosition.server.equals(plugin.getServer(onlineUser))) { + if (!respawnPosition.server.equals(plugin.getPluginServer())) { plugin.getTeleportManager().teleport(onlineUser, respawnPosition, TeleportType.RESPAWN).thenAccept( result -> plugin.getTeleportManager().finishTeleport(onlineUser, result)); } 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 32d503b5..d42b1f63 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java @@ -354,7 +354,7 @@ public class TeleportManager { } // Teleport player locally, or across server depending on need - if (position.server.equals(plugin.getServer(onlineUser))) { + if (position.server.equals(plugin.getPluginServer())) { return onlineUser.teleport(teleport.target, plugin.getSettings().asynchronousTeleports); } else { return teleportCrossServer(onlineUser, teleport);