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,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})
*/