diff --git a/bukkit/src/main/java/net/william278/huskhomes/HuskHomesBukkit.java b/bukkit/src/main/java/net/william278/huskhomes/HuskHomesBukkit.java index b34be72a..c38eeb89 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/HuskHomesBukkit.java +++ b/bukkit/src/main/java/net/william278/huskhomes/HuskHomesBukkit.java @@ -177,8 +177,8 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes { // Register commands - todo add all here getLogger().log(Level.INFO, "Registering commands..."); final CommandBase[] commands = new CommandBase[]{ - new HomeCommand(this), new SetHomeCommand(this), new HomeListCommand(this), - new WarpCommand(this), new SetWarpCommand(this), new WarpListCommand(this), + new HomeCommand(this), new SetHomeCommand(this), new HomeListCommand(this), new DelHomeCommand(this), + new WarpCommand(this), new SetWarpCommand(this), new WarpListCommand(this), new DelWarpCommand(this), new BackCommand(this)}; for (CommandBase commandBase : commands) { final PluginCommand pluginCommand = getCommand(commandBase.command); diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index cadc4bde..3d548f4f 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -11,5 +11,15 @@ commands: usage: /sethome homelist: usage: /homelist [page] + delhome: + usage: /delhome + warp: + usage: /warp + setwarp: + usage: /setwarp + warplist: + usage: /warplist [page] + delwarp: + usage: /delwarp back: usage: /back \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java new file mode 100644 index 00000000..b9e138d5 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java @@ -0,0 +1,69 @@ +package net.william278.huskhomes.command; + +import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.player.Player; +import net.william278.huskhomes.player.User; +import net.william278.huskhomes.util.Permission; +import net.william278.huskhomes.util.RegexUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class DelHomeCommand extends CommandBase implements TabCompletable, ConsoleExecutable { + + public DelHomeCommand(@NotNull HuskHomes implementor) { + super("delhome", Permission.COMMAND_DELETE_HOME, implementor); + } + + @Override + public void onExecute(@NotNull Player player, @NotNull String[] args) { + final User user = new User(player); + switch (args.length) { + case 0 -> plugin.getDatabase().getHomes(user).thenAccept(homes -> { + if (homes.size() == 1) { + homes.stream().findFirst().ifPresent(home -> plugin.getSavedPositionManager().deleteHome(user, home.meta.name)); + } else { + plugin.getLocales().getLocale("error_invalid_syntax", "/delhome ") + .ifPresent(player::sendMessage); + } + }); + case 1 -> { + final String homeName = args[0]; + if (RegexUtil.OWNER_NAME_PATTERN.matcher(homeName).matches()) { + final String ownerName = homeName.split("\\.")[0]; + final String ownersHomeName = homeName.split("\\.")[1]; + plugin.getDatabase().getUserByName(ownerName).thenAccept(optionalUserData -> optionalUserData + .ifPresentOrElse(userData -> { + if (!userData.uuid.equals(player.getUuid())) { + if (!player.hasPermission(Permission.COMMAND_DELETE_HOME_OTHER.node)) { + plugin.getLocales().getLocale("error_no_permission") + .ifPresent(player::sendMessage); + return; + } + } + plugin.getSavedPositionManager().deleteHome(userData, ownersHomeName); + }, + () -> plugin.getLocales().getLocale("error_home_invalid_other", ownerName, homeName) + .ifPresent(player::sendMessage))); + } else { + plugin.getSavedPositionManager().deleteHome(user, homeName); + } + } + default -> plugin.getLocales().getLocale("error_invalid_syntax", "/delhome ") + .ifPresent(player::sendMessage); + } + } + + @Override + public void onConsoleExecute(@NotNull String[] args) { + + } + + @Override + public List onTabComplete(@NotNull Player player, @NotNull String[] args) { + return plugin.getCache().homes.get(player.getUuid()).stream() + .filter(s -> s.startsWith(args.length >= 1 ? args[0] : "")) + .sorted().collect(Collectors.toList()); + } +} diff --git a/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java new file mode 100644 index 00000000..4520520b --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java @@ -0,0 +1,40 @@ +package net.william278.huskhomes.command; + +import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.player.Player; +import net.william278.huskhomes.player.User; +import net.william278.huskhomes.util.Permission; +import net.william278.huskhomes.util.RegexUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class DelWarpCommand extends CommandBase implements TabCompletable, ConsoleExecutable { + + public DelWarpCommand(@NotNull HuskHomes implementor) { + super("delwarp", Permission.COMMAND_DELETE_WARP, implementor); + } + + @Override + public void onExecute(@NotNull Player player, @NotNull String[] args) { + if (args.length == 1) { + plugin.getSavedPositionManager().deleteWarp(args[0]); + } else { + plugin.getLocales().getLocale("error_invalid_syntax", "/delwarp ") + .ifPresent(player::sendMessage); + } + } + + @Override + public void onConsoleExecute(@NotNull String[] args) { + + } + + @Override + public List onTabComplete(@NotNull Player player, @NotNull String[] args) { + return plugin.getCache().warps.stream() + .filter(s -> s.startsWith(args.length >= 1 ? args[0] : "")) + .sorted().collect(Collectors.toList()); + } +} diff --git a/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java index 11b853a1..49897efd 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HomeCommand.java @@ -51,15 +51,15 @@ public class HomeCommand extends CommandBase implements TabCompletable, ConsoleE } } + @Override + public void onConsoleExecute(@NotNull String[] args) { + //todo + } + @Override public List onTabComplete(@NotNull Player player, @NotNull String[] args) { return plugin.getCache().homes.get(player.getUuid()).stream() .filter(s -> s.startsWith(args.length >= 1 ? args[0] : "")) .sorted().collect(Collectors.toList()); } - - @Override - public void onConsoleExecute(@NotNull String[] args) { - //todo - } } diff --git a/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java b/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java index 368ceacf..789ea004 100644 --- a/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java +++ b/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java @@ -57,6 +57,30 @@ public class SavedPositionManager { }).join())); } + /** + * Tries to delete a {@link User}'s home by given name + * + * @param homeOwner the owner of the home + * @param homeName the name of the home + * @return a future supplying a boolean; {@code true} if the home was deleted, otherwise {@code false} if the home name was invalid. + */ + public CompletableFuture deleteHome(@NotNull User homeOwner, @NotNull String homeName) { + return CompletableFuture.supplyAsync(() -> database.getHome(homeOwner, homeName).thenApply(optionalHome -> { + if (optionalHome.isPresent()) { + final Home home = optionalHome.get(); + return database.deleteHome(home.uuid).thenApply(ignored -> { + cache.homes.computeIfPresent(home.owner.uuid, (ownerUUID, homeNames) -> { + homeNames.remove(home.meta.name); + return homeNames; + }); + cache.positionLists.clear(); + return true; + }).join(); + } + return false; + }).join()); + } + /** * Tries to create a server {@link Warp} at the specified {@link Position} from supplied * {@link PositionMeta} data and add it to the database @@ -80,6 +104,26 @@ public class SavedPositionManager { }).join())); } + /** + * Tries to delete a warp by given name + * + * @param warpName the name of the warp + * @return a future supplying a boolean; {@code true} if the warp was deleted, otherwise {@code false} if the warp name was invalid. + */ + public CompletableFuture deleteWarp(@NotNull String warpName) { + return CompletableFuture.supplyAsync(() -> database.getWarp(warpName).thenApply(optionalWarp -> { + if (optionalWarp.isPresent()) { + final Warp warp = optionalWarp.get(); + return database.deleteWarp(warp.uuid).thenApply(ignored -> { + cache.warps.remove(warp.meta.name); + cache.positionLists.clear(); + return true; + }).join(); + } + return false; + }).join()); + } + /** * Validates {@link PositionMeta} to ensure it can be added to the database *