Add /setspawn and /spawn

This commit is contained in:
William committed 2022-08-22 16:41:22 +01:00
1 parent 560d701113
commit 8adbc49a61
10 files changed
+198 -17

No files matched your search

@@ -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<PluginHook> pluginHooks;
@Nullable
@@ -284,6 +285,18 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
return rtpEngine;
}
@Override
public Optional<Spawn> 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<PluginHook> getPluginHooks() {
return pluginHooks;
@@ -360,30 +373,33 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
@Override
public CompletableFuture<Boolean> 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);
@@ -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;
@@ -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);
}
@@ -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<Spawn> 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
*
@@ -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> 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));
}
}
@@ -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<? extends Position> 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();
});
}
}
@@ -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";
@@ -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<Position> 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() {
}
}
@@ -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);
@@ -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})
*/