From 8adbc49a613f06adf72a42e20fcb4f7c1af9aa8c Mon Sep 17 00:00:00 2001 From: William Date: Mon, 22 Aug 2022 16:41:22 +0100 Subject: [PATCH] Add /setspawn and /spawn --- .../william278/huskhomes/BukkitHuskHomes.java | 44 ++++++++----- .../huskhomes/command/BukkitCommandType.java | 2 + .../huskhomes/util/BukkitLogger.java | 2 +- .../net/william278/huskhomes/HuskHomes.java | 15 +++++ .../huskhomes/command/SetSpawnCommand.java | 41 ++++++++++++ .../huskhomes/command/SpawnCommand.java | 38 +++++++++++ .../william278/huskhomes/config/Settings.java | 6 ++ .../william278/huskhomes/config/Spawn.java | 63 +++++++++++++++++++ .../net/william278/huskhomes/util/Logger.java | 2 +- .../william278/huskhomes/util/Permission.java | 2 +- 10 files changed, 198 insertions(+), 17 deletions(-) create mode 100644 common/src/main/java/net/william278/huskhomes/command/SetSpawnCommand.java create mode 100644 common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java create mode 100644 common/src/main/java/net/william278/huskhomes/config/Spawn.java diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index 54caf3c3..2317449e 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -2,11 +2,11 @@ package net.william278.huskhomes; import io.papermc.lib.PaperLib; import net.william278.annotaml.Annotaml; -import net.william278.annotaml.AnnotamlException; import net.william278.huskhomes.command.BukkitCommand; import net.william278.huskhomes.command.BukkitCommandType; import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Settings; +import net.william278.huskhomes.config.Spawn; import net.william278.huskhomes.database.Database; import net.william278.huskhomes.database.MySqlDatabase; import net.william278.huskhomes.database.SqLiteDatabase; @@ -59,6 +59,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { private SavedPositionManager savedPositionManager; private EventListener eventListener; private RtpEngine rtpEngine; + private Spawn spawn; private Set pluginHooks; @Nullable @@ -284,6 +285,18 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { return rtpEngine; } + @Override + public Optional getSpawn() { + return Optional.ofNullable(spawn); + } + + @Override + public void setSpawn(@NotNull Location location) { + final Spawn newSpawn = new Spawn(location); + this.spawn = newSpawn; + Annotaml.save(newSpawn, new File(getDataFolder(), "spawn.yml")); + } + @Override public @NotNull Set getPluginHooks() { return pluginHooks; @@ -360,30 +373,33 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { @Override public CompletableFuture reload() { return CompletableFuture.supplyAsync(() -> { - try { - this.settings = Annotaml.reload(new File(getDataFolder(), "config.yml"), - new Settings(), Annotaml.LoaderOptions.builder().copyDefaults(true)); + // Load settings and locales + this.settings = Annotaml.reload(new File(getDataFolder(), "config.yml"), + new Settings(), Annotaml.LoaderOptions.builder().copyDefaults(true)); + this.locales = Annotaml.reload(new File(getDataFolder(), "messages-" + settings.language + ".yml"), + Objects.requireNonNull(resourceReader.getResource("locales/" + settings.language + ".yml")), + Locales.class, Annotaml.LoaderOptions.builder().copyDefaults(true)); - this.locales = Annotaml.reload(new File(getDataFolder(), "messages-" + settings.language + ".yml"), - Objects.requireNonNull(resourceReader.getResource("locales/" + settings.language + ".yml")), - Locales.class, Annotaml.LoaderOptions.builder().copyDefaults(true)); - return true; - } catch (AnnotamlException e) { - getLoggingAdapter().log(Level.SEVERE, "Failed to load data from the config", e); - return false; - } catch (NullPointerException e) { - getLoggingAdapter().log(Level.SEVERE, "An unsupported language code was supplied, could not load locales", e); - return false; + // Load spawn location from file + final File spawnFile = new File(getDataFolder(), "spawn.yml"); + if (spawnFile.exists()) { + this.spawn = Annotaml.load(spawnFile, Spawn.class); } + return true; + }).exceptionally(throwable -> { + getLoggingAdapter().log(Level.SEVERE, "Failed to load data from the config", throwable); + return false; }); } // Default constructor + @SuppressWarnings("unused") public BukkitHuskHomes() { super(); } // Super constructor for unit testing + @SuppressWarnings("unused") protected BukkitHuskHomes(@NotNull JavaPluginLoader loader, @NotNull PluginDescriptionFile description, @NotNull File dataFolder, @NotNull File file) { super(loader, description, dataFolder, file); diff --git a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java index 8e49683c..74858daf 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java +++ b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java @@ -29,6 +29,8 @@ public enum BukkitCommandType { RTP_COMMAND(new RtpCommand(BukkitHuskHomes.getInstance())), TP_IGNORE_COMMAND(new TpIgnoreCommand(BukkitHuskHomes.getInstance())), TP_OFFLINE_COMMAND(new TpOfflineCommand(BukkitHuskHomes.getInstance())), + SPAWN_COMMAND(new SpawnCommand(BukkitHuskHomes.getInstance())), + SET_SPAWN_COMMAND(new SetSpawnCommand(BukkitHuskHomes.getInstance())), BACK_COMMAND(new BackCommand(BukkitHuskHomes.getInstance())); public final CommandBase commandBase; diff --git a/bukkit/src/main/java/net/william278/huskhomes/util/BukkitLogger.java b/bukkit/src/main/java/net/william278/huskhomes/util/BukkitLogger.java index 0293ef6b..dda95450 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/util/BukkitLogger.java +++ b/bukkit/src/main/java/net/william278/huskhomes/util/BukkitLogger.java @@ -15,7 +15,7 @@ public class BukkitLogger extends Logger { } @Override - public void log(@NotNull Level level, @NotNull String message, @NotNull Exception e) { + public void log(@NotNull Level level, @NotNull String message, @NotNull Throwable e) { logger.log(level, message, e); } diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomes.java b/common/src/main/java/net/william278/huskhomes/HuskHomes.java index acc8e4b4..2d981d14 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomes.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomes.java @@ -2,6 +2,7 @@ package net.william278.huskhomes; import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Settings; +import net.william278.huskhomes.config.Spawn; import net.william278.huskhomes.database.Database; import net.william278.huskhomes.hook.EconomyHook; import net.william278.huskhomes.hook.PluginHook; @@ -129,6 +130,20 @@ public interface HuskHomes { @NotNull RtpEngine getRtpEngine(); + /** + * The {@link Spawn} location of this server + * + * @return the {@link Spawn} location data + */ + Optional getSpawn(); + + /** + * Update the {@link Spawn} position to a location on the server + * + * @param location the new {@link Spawn} location + */ + void setSpawn(@NotNull Location location); + /** * Set of active {@link PluginHook}s running on the server * diff --git a/common/src/main/java/net/william278/huskhomes/command/SetSpawnCommand.java b/common/src/main/java/net/william278/huskhomes/command/SetSpawnCommand.java new file mode 100644 index 00000000..84f85c25 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/command/SetSpawnCommand.java @@ -0,0 +1,41 @@ +package net.william278.huskhomes.command; + +import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.player.OnlineUser; +import net.william278.huskhomes.position.PositionMeta; +import net.william278.huskhomes.position.Warp; +import net.william278.huskhomes.util.Permission; +import org.jetbrains.annotations.NotNull; + +import java.util.Optional; + +public class SetSpawnCommand extends CommandBase { + + protected SetSpawnCommand(@NotNull HuskHomes implementor) { + super("setspawn", Permission.COMMAND_SET_SPAWN, implementor); + } + + @Override + public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { + if (args.length > 0) { + plugin.getLocales().getLocale("error_invalid_syntax", "/setspawn") + .ifPresent(onlineUser::sendMessage); + return; + } + + onlineUser.getPosition().thenAccept(position -> { + if (plugin.getSettings().crossServer && plugin.getSettings().globalSpawn) { + final Optional warp = plugin.getDatabase().getWarp(plugin.getSettings().globalSpawnName).join(); + if (warp.isPresent()) { + plugin.getSavedPositionManager().updateWarpPosition(warp.get(), position).join(); + } else { + plugin.getSavedPositionManager().setWarp(new PositionMeta(plugin.getSettings().globalSpawnName, + plugin.getLocales().getRawLocale("spawn_warp_default_description") + .orElse("")), position).join(); + } + } else { + plugin.setSpawn(position); + } + }).thenRun(() -> plugin.getLocales().getLocale("set_spawn_success").ifPresent(onlineUser::sendMessage)); + } +} diff --git a/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java b/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java new file mode 100644 index 00000000..45bb2986 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java @@ -0,0 +1,38 @@ +package net.william278.huskhomes.command; + +import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.player.OnlineUser; +import net.william278.huskhomes.position.Position; +import net.william278.huskhomes.util.Permission; +import org.jetbrains.annotations.NotNull; + +import java.util.Optional; +import java.util.concurrent.CompletableFuture; + +public class SpawnCommand extends CommandBase { + + protected SpawnCommand(@NotNull HuskHomes implementor) { + super("spawn", Permission.COMMAND_SPAWN, implementor); + } + + @Override + public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) { + if (args.length > 0) { + plugin.getLocales().getLocale("error_invalid_syntax", "/spawn") + .ifPresent(onlineUser::sendMessage); + return; + } + + CompletableFuture.runAsync(() -> { + final Optional position = (plugin.getSettings().crossServer && plugin.getSettings().globalSpawn + ? plugin.getDatabase().getWarp(plugin.getSettings().globalSpawnName).join() + : plugin.getSpawn().flatMap(spawn -> spawn.getLocation(plugin.getServer(onlineUser).join()))); + if (position.isEmpty()) { + plugin.getLocales().getLocale("error_spawn_not_set").ifPresent(onlineUser::sendMessage); + return; + } + + plugin.getTeleportManager().timedTeleport(onlineUser, position.get()).join(); + }); + } +} diff --git a/common/src/main/java/net/william278/huskhomes/config/Settings.java b/common/src/main/java/net/william278/huskhomes/config/Settings.java index 6402c796..8d728fc2 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Settings.java +++ b/common/src/main/java/net/william278/huskhomes/config/Settings.java @@ -137,6 +137,12 @@ public class Settings { @KeyPath("cross_server.cluster_id") public String clusterId = ""; + @KeyPath("cross_server.global_spawn.enabled") + public boolean globalSpawn = false; + + @KeyPath("cross_server.global_spawn.warp_name") + public String globalSpawnName = "Spawn"; + @KeyPath("cross_server.redis_credentials.host") public String redisHost = "localhost"; diff --git a/common/src/main/java/net/william278/huskhomes/config/Spawn.java b/common/src/main/java/net/william278/huskhomes/config/Spawn.java new file mode 100644 index 00000000..b9e2d544 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/config/Spawn.java @@ -0,0 +1,63 @@ +package net.william278.huskhomes.config; + +import net.william278.annotaml.YamlFile; +import net.william278.huskhomes.position.Location; +import net.william278.huskhomes.position.Position; +import net.william278.huskhomes.position.Server; +import net.william278.huskhomes.position.World; +import org.jetbrains.annotations.NotNull; + +import java.util.Optional; +import java.util.UUID; + +/** + * Used to store the server spawn location + */ +@YamlFile(header = """ + ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ + ┃ Server /spawn location. ┃ + ┃ Edit in-game with /setspawn. ┃ + ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛""") +public class Spawn { + + public double x; + public double y; + public double z; + public float yaw; + public float pitch; + public String worldName; + public String worldUuid; + + /** + * Returns the {@link Position} of the spawn + * + * @return The {@link Position} of the spawn + */ + public Optional getLocation(@NotNull Server server) { + try { + return Optional.of(new Position(x, y, z, yaw, pitch, + new World(worldName, UUID.fromString(worldUuid)), server)); + } catch (IllegalArgumentException e) { + return Optional.empty(); + } + } + + /** + * Set the {@link Location} of the spawn + * + * @param location The {@link Location} of the spawn + */ + public Spawn(@NotNull Location location) { + this.x = location.x; + this.y = location.y; + this.z = location.z; + this.yaw = location.yaw; + this.pitch = location.pitch; + this.worldName = location.world.name; + this.worldUuid = location.world.uuid.toString(); + } + + @SuppressWarnings("unused") + public Spawn() { + } +} diff --git a/common/src/main/java/net/william278/huskhomes/util/Logger.java b/common/src/main/java/net/william278/huskhomes/util/Logger.java index b0479917..b4f259ad 100644 --- a/common/src/main/java/net/william278/huskhomes/util/Logger.java +++ b/common/src/main/java/net/william278/huskhomes/util/Logger.java @@ -12,7 +12,7 @@ public abstract class Logger { private boolean debug; - public abstract void log(@NotNull Level level, @NotNull String message, @NotNull Exception e); + public abstract void log(@NotNull Level level, @NotNull String message, @NotNull Throwable e); public abstract void log(@NotNull Level level, @NotNull String message); diff --git a/common/src/main/java/net/william278/huskhomes/util/Permission.java b/common/src/main/java/net/william278/huskhomes/util/Permission.java index 74f127cc..dcca2c73 100644 --- a/common/src/main/java/net/william278/huskhomes/util/Permission.java +++ b/common/src/main/java/net/william278/huskhomes/util/Permission.java @@ -128,7 +128,7 @@ public enum Permission { /** * Lets the user set the spawn position ({@code /setspawn}) */ - COMMAND_SETSPAWN("huskhomes.command.setspawn", DefaultAccess.OPERATORS), + COMMAND_SET_SPAWN("huskhomes.command.setspawn", DefaultAccess.OPERATORS), /** * Lets the user return to where they last teleported from ({@code /back}) */