Spawn: Simplify getting method, refactoring, add way of getting via API

This commit is contained in:
William278 committed 2022-09-05 16:09:20 +03:00
1 parent f94f9e82db
commit 4db869abf1
6 files changed
+36 -20

No files matched your search

@@ -387,7 +387,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
} }
@Override @Override
public Optional<CachedSpawn> getServerSpawn() { public Optional<CachedSpawn> getLocalCachedSpawn() {
return Optional.ofNullable(serverSpawn); return Optional.ofNullable(serverSpawn);
} }
@@ -14,10 +14,7 @@ import net.william278.huskhomes.hook.PluginHook;
import net.william278.huskhomes.messenger.NetworkMessenger; import net.william278.huskhomes.messenger.NetworkMessenger;
import net.william278.huskhomes.migrator.Migrator; import net.william278.huskhomes.migrator.Migrator;
import net.william278.huskhomes.player.OnlineUser; import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.*;
import net.william278.huskhomes.position.SavedPositionManager;
import net.william278.huskhomes.position.Server;
import net.william278.huskhomes.position.World;
import net.william278.huskhomes.random.RandomTeleportEngine; import net.william278.huskhomes.random.RandomTeleportEngine;
import net.william278.huskhomes.request.RequestManager; import net.william278.huskhomes.request.RequestManager;
import net.william278.huskhomes.teleport.TeleportManager; import net.william278.huskhomes.teleport.TeleportManager;
@@ -165,11 +162,23 @@ public interface HuskHomes {
List<Migrator> getMigrators(); List<Migrator> getMigrators();
/** /**
* The {@link CachedSpawn} location of this server * The local {@link CachedSpawn} location of this server, as cached to disk
* *
* @return the {@link CachedSpawn} location data * @return the {@link CachedSpawn} location data
* @see #getSpawn() for the canonical spawn point to use
*/ */
Optional<CachedSpawn> getServerSpawn(); Optional<CachedSpawn> getLocalCachedSpawn();
/**
* The canonical spawn {@link Position} of this server, if it has been set
*
* @return the {@link Position} of the spawn, or an empty {@link Optional} if it has not been set
*/
default Optional<? extends Position> getSpawn() {
return getSettings().crossServer && getSettings().globalSpawn
? getDatabase().getWarp(getSettings().globalSpawnName).join()
: getLocalCachedSpawn().flatMap(spawn -> spawn.getPosition(getPluginServer()));
}
/** /**
* Returns a future returning the latest plugin {@link Version} if the plugin is out-of-date * Returns a future returning the latest plugin {@link Version} if the plugin is out-of-date
@@ -196,6 +196,16 @@ public abstract class BaseHuskHomesAPI {
return plugin.getDatabase().getWarp(warpUuid); return plugin.getDatabase().getWarp(warpUuid);
} }
/**
* Get the canonical {@link Position} of the spawn point. Note that if cross-server and global spawn
* are enabled in the config, this may not return a position on this server.
*
* @return A {@link CompletableFuture} that will complete with the {@link Position} of the spawn point
*/
public final CompletableFuture<Optional<? extends Position>> getSpawn() {
return CompletableFuture.supplyAsync(plugin::getSpawn);
}
/** /**
* Update the {@link PositionMeta} of a {@link Home} * Update the {@link PositionMeta} of a {@link Home}
* *
@@ -21,16 +21,13 @@ public class SpawnCommand extends CommandBase {
return; return;
} }
CompletableFuture.supplyAsync(() -> plugin.getSettings().crossServer && plugin.getSettings().globalSpawn CompletableFuture.supplyAsync(plugin::getSpawn).thenAccept(position -> {
? plugin.getDatabase().getWarp(plugin.getSettings().globalSpawnName).join() if (position.isEmpty()) {
: plugin.getServerSpawn().flatMap(spawn -> spawn.getLocation(plugin.getPluginServer()))) plugin.getLocales().getLocale("error_spawn_not_set")
.thenAccept(position -> { .ifPresent(onlineUser::sendMessage);
if (position.isEmpty()) { return;
plugin.getLocales().getLocale("error_spawn_not_set") }
.ifPresent(onlineUser::sendMessage); plugin.getTeleportManager().timedTeleport(onlineUser, position.get());
return; });
}
plugin.getTeleportManager().timedTeleport(onlineUser, position.get());
});
} }
} }
@@ -33,7 +33,7 @@ public class CachedSpawn {
* *
* @return The {@link Position} of the spawn * @return The {@link Position} of the spawn
*/ */
public Optional<Position> getLocation(@NotNull Server server) { public Optional<Position> getPosition(@NotNull Server server) {
try { try {
return Optional.of(new Position(x, y, z, yaw, pitch, return Optional.of(new Position(x, y, z, yaw, pitch,
new World(worldName, UUID.fromString(worldUuid)), server)); new World(worldName, UUID.fromString(worldUuid)), server));
@@ -52,7 +52,7 @@ public class EventListener {
if (activeTeleport.type == TeleportType.RESPAWN) { if (activeTeleport.type == TeleportType.RESPAWN) {
final Optional<Position> bedPosition = onlineUser.getBedSpawnPosition(); final Optional<Position> bedPosition = onlineUser.getBedSpawnPosition();
if (bedPosition.isEmpty()) { if (bedPosition.isEmpty()) {
plugin.getServerSpawn().flatMap(spawn -> spawn.getLocation(plugin.getPluginServer())) plugin.getLocalCachedSpawn().flatMap(spawn -> spawn.getPosition(plugin.getPluginServer()))
.ifPresent(position -> onlineUser.teleport(position, plugin.getSettings().asynchronousTeleports)); .ifPresent(position -> onlineUser.teleport(position, plugin.getSettings().asynchronousTeleports));
onlineUser.sendMinecraftMessage("block.minecraft.spawn.not_valid"); onlineUser.sendMinecraftMessage("block.minecraft.spawn.not_valid");
} else { } else {