More command refactoring

This commit is contained in:
William278 committed 2023-03-20 00:01:09 +00:00
1 parent 3bde5305d7
commit 34eb2c1f54
17 files changed
+382 -540

No files matched your search

@@ -1,46 +1,45 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.PositionMeta;
import net.william278.huskhomes.util.Permission;
import net.william278.huskhomes.user.CommandUser;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.util.ValidationException;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class SetSpawnCommand extends Command {
protected SetSpawnCommand(@NotNull HuskHomes implementor) {
super("setspawn", Permission.COMMAND_SET_SPAWN, implementor);
protected SetSpawnCommand(@NotNull HuskHomes plugin) {
super("setspawn", List.of(), "", plugin);
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
if (args.length > 0) {
plugin.getLocales().getLocale("error_invalid_syntax", "/setspawn")
.ifPresent(onlineUser::sendMessage);
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
if (!(executor instanceof OnlineUser user)) {
plugin.getLocales().getLocale("error_in_game_only")
.ifPresent(executor::sendMessage);
return;
}
final Position position = onlineUser.getPosition();
if (plugin.getSettings().isCrossServer() && plugin.getSettings().isGlobalSpawn()) {
plugin.getDatabase().getWarp(plugin.getSettings().getGlobalSpawnName()).thenApply(warp -> {
if (warp.isPresent()) {
return plugin.getManager().updateWarpPosition(warp.get(), position);
} else {
return plugin.getManager().setWarp(new PositionMeta(plugin.getSettings().getGlobalSpawnName(),
plugin.getLocales().getRawLocale("spawn_warp_default_description").orElse("")), position)
.thenApply(result -> result.resultType().successful);
}
}).thenAccept(result -> result.thenAccept(successful -> {
if (successful) {
plugin.getLocales().getLocale("set_spawn_success")
.ifPresent(onlineUser::sendMessage);
}
}));
} else {
plugin.setServerSpawn(position);
plugin.getLocales().getLocale("set_spawn_success")
.ifPresent(onlineUser::sendMessage);
final Position position = user.getPosition();
try {
if (plugin.getSettings().isCrossServer() && plugin.getSettings().isGlobalSpawn()) {
final String warpName = plugin.getSettings().getGlobalSpawnName();
plugin.getManager().warps().createWarp(warpName, position, true);
plugin.getLocales().getRawLocale("spawn_warp_default_description")
.ifPresent(description -> plugin.getManager().warps().setWarpDescription(warpName, description));
} else {
plugin.setServerSpawn(position);
}
} catch (ValidationException e) {
e.dispatchWarpError(executor, plugin, plugin.getSettings().getGlobalSpawnName());
return;
}
plugin.getLocales().getLocale("set_spawn_success")
.ifPresent(executor::sendMessage);
}
}