From 1d1f5940d788e93c5b3cf5e8e797474d9e49088b Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Wed, 28 Aug 2024 19:15:03 +0800 Subject: [PATCH] Implement anchor commands --- .../core/commands/AnchorDeleteCommand.java | 47 ++++++++ .../core/commands/AnchorListCommand.java | 103 ++++++++++++++++++ .../core/commands/AnchorSetCommand.java | 87 +++++++++++++++ 3 files changed, 237 insertions(+) create mode 100644 src/main/java/org/mvplugins/multiverse/core/commands/AnchorDeleteCommand.java create mode 100644 src/main/java/org/mvplugins/multiverse/core/commands/AnchorListCommand.java create mode 100644 src/main/java/org/mvplugins/multiverse/core/commands/AnchorSetCommand.java diff --git a/src/main/java/org/mvplugins/multiverse/core/commands/AnchorDeleteCommand.java b/src/main/java/org/mvplugins/multiverse/core/commands/AnchorDeleteCommand.java new file mode 100644 index 00000000..85e32dee --- /dev/null +++ b/src/main/java/org/mvplugins/multiverse/core/commands/AnchorDeleteCommand.java @@ -0,0 +1,47 @@ +package org.mvplugins.multiverse.core.commands; + +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Syntax; +import jakarta.inject.Inject; +import org.jetbrains.annotations.NotNull; +import org.jvnet.hk2.annotations.Service; + +import org.mvplugins.multiverse.core.anchor.AnchorManager; +import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer; +import org.mvplugins.multiverse.core.commandtools.MVCommandManager; +import org.mvplugins.multiverse.core.commandtools.MultiverseCommand; + +@Service +@CommandAlias("mv") +class AnchorDeleteCommand extends MultiverseCommand { + + private final AnchorManager anchorManager; + + @Inject + AnchorDeleteCommand(@NotNull MVCommandManager commandManager, @NotNull AnchorManager anchorManager) { + super(commandManager); + this.anchorManager = anchorManager; + } + + @Subcommand("anchor delete") + @CommandPermission("multiverse.core.anchor.delete") + @CommandCompletion("") + @Syntax("") + @Description("") + void onAnchorDeleteCommand( + MVCommandIssuer issuer, + + @Syntax("") + @Description("") + String anchorName) { + if (anchorManager.deleteAnchor(anchorName)) { + issuer.sendMessage("&aAnchor &f" + anchorName + "&a deleted."); + } else { + issuer.sendMessage("&cFailed to delete anchor."); + } + } +} diff --git a/src/main/java/org/mvplugins/multiverse/core/commands/AnchorListCommand.java b/src/main/java/org/mvplugins/multiverse/core/commands/AnchorListCommand.java new file mode 100644 index 00000000..e96981b6 --- /dev/null +++ b/src/main/java/org/mvplugins/multiverse/core/commands/AnchorListCommand.java @@ -0,0 +1,103 @@ +package org.mvplugins.multiverse.core.commands; + +import java.util.List; + +import co.aikar.commands.InvalidCommandArgument; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Optional; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Syntax; +import jakarta.inject.Inject; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jvnet.hk2.annotations.Service; + +import org.mvplugins.multiverse.core.anchor.AnchorManager; +import org.mvplugins.multiverse.core.api.LocationManipulation; +import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer; +import org.mvplugins.multiverse.core.commandtools.MVCommandManager; +import org.mvplugins.multiverse.core.commandtools.MultiverseCommand; +import org.mvplugins.multiverse.core.commandtools.flags.CommandValueFlag; +import org.mvplugins.multiverse.core.commandtools.flags.ParsedCommandFlags; +import org.mvplugins.multiverse.core.display.ContentDisplay; +import org.mvplugins.multiverse.core.display.filters.ContentFilter; +import org.mvplugins.multiverse.core.display.filters.DefaultContentFilter; +import org.mvplugins.multiverse.core.display.filters.RegexContentFilter; +import org.mvplugins.multiverse.core.display.handlers.PagedSendHandler; +import org.mvplugins.multiverse.core.display.parsers.ListContentProvider; + +@Service +@CommandAlias("mv") +class AnchorListCommand extends MultiverseCommand { + + private final AnchorManager anchorManager; + private final LocationManipulation locationManipulation; + + private final CommandValueFlag PAGE_FLAG = flag(CommandValueFlag + .builder("--page", Integer.class) + .addAlias("-p") + .context(value -> { + try { + return Integer.parseInt(value); + } catch (NumberFormatException e) { + throw new InvalidCommandArgument("Invalid page number: " + value); + } + }) + .build()); + + private final CommandValueFlag FILTER_FLAG = flag(CommandValueFlag + .builder("--filter", ContentFilter.class) + .addAlias("-f") + .context(value -> { + try { + return RegexContentFilter.fromString(value); + } catch (IllegalArgumentException e) { + throw new InvalidCommandArgument("Invalid filter: " + value); + } + }) + .build()); + + @Inject + AnchorListCommand( + @NotNull MVCommandManager commandManager, + @NotNull AnchorManager anchorManager, + @NotNull LocationManipulation locationManipulation) { + super(commandManager); + this.anchorManager = anchorManager; + this.locationManipulation = locationManipulation; + } + + @Subcommand("anchor list") + @CommandPermission("multiverse.core.anchor.list") + @CommandCompletion("@flags:groupName=mvanchorlistcommand") + @Syntax("[--page ] [--filter ]") + @Description("") + void onAnchorListCommand( + MVCommandIssuer issuer, + + @Optional + @Syntax("[--page ] [--filter ]") + @Description("") + String[] flags) { + ParsedCommandFlags parsedFlags = parseFlags(flags); + ContentDisplay.create() + .addContent(ListContentProvider.forContent(getAnchors(issuer.getPlayer()))) + .withSendHandler(PagedSendHandler.create() + .withHeader("&3==== [ Multiverse Anchors ] ====") + .doPagination(true) + .withTargetPage(parsedFlags.flagValue(PAGE_FLAG, 1)) + .withFilter(parsedFlags.flagValue(FILTER_FLAG, DefaultContentFilter.get()))) + .send(issuer); + } + + private List getAnchors(Player player) { + return anchorManager.getAnchors(player).stream().map(anchorName -> { + Location anchorLocation = anchorManager.getAnchorLocation(anchorName); + return "&a" + anchorName + "&7 - &f" + locationManipulation.locationToString(anchorLocation); + }).toList(); + } +} diff --git a/src/main/java/org/mvplugins/multiverse/core/commands/AnchorSetCommand.java b/src/main/java/org/mvplugins/multiverse/core/commands/AnchorSetCommand.java new file mode 100644 index 00000000..0947b01d --- /dev/null +++ b/src/main/java/org/mvplugins/multiverse/core/commands/AnchorSetCommand.java @@ -0,0 +1,87 @@ +package org.mvplugins.multiverse.core.commands; + +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Flags; +import co.aikar.commands.annotation.Single; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Syntax; +import jakarta.inject.Inject; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jvnet.hk2.annotations.Service; + +import org.mvplugins.multiverse.core.anchor.AnchorManager; +import org.mvplugins.multiverse.core.api.LocationManipulation; +import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer; +import org.mvplugins.multiverse.core.commandtools.MVCommandManager; +import org.mvplugins.multiverse.core.commandtools.MultiverseCommand; + +@Service +@CommandAlias("mv") +class AnchorSetCommand extends MultiverseCommand { + + private final AnchorManager anchorManager; + private final LocationManipulation locationManipulation; + + @Inject + AnchorSetCommand( + @NotNull MVCommandManager commandManager, + @NotNull AnchorManager anchorManager, + @NotNull LocationManipulation locationManipulation) { + super(commandManager); + this.anchorManager = anchorManager; + this.locationManipulation = locationManipulation; + } + + @Subcommand("anchor set") + @CommandPermission("multiverse.core.anchor.create") + @CommandCompletion("@anchornames") + @Syntax(" [location]") + @Description("") + void onAnchorSetCommand( + MVCommandIssuer issuer, + + @Flags("resolve=issuerOnly") + Player player, + + @Single + @Syntax("") + @Description("") + //TODO: Check if anchor name is valid or exists already. + String anchorName) { + Location anchorLocation = player.getLocation(); + if (anchorManager.saveAnchorLocation(anchorName, anchorLocation)) { + issuer.sendMessage("&aAnchor &f" + anchorName + "&a set to &f" + + locationManipulation.locationToString(anchorLocation)); + } else { + issuer.sendMessage("&cFailed to set anchor &f" + anchorName + "."); + } + } + + @Subcommand("anchor set") + @CommandPermission("multiverse.core.anchor.create") + @CommandCompletion("@anchornames @locations") + @Syntax(" [location]") + @Description("") + void onAnchorSetCommand( + MVCommandIssuer issuer, + + @Syntax("") + @Description("") + String anchorName, + + @Single + @Syntax("[location]") + @Description("") + String locationString) { + if (anchorManager.saveAnchorLocation(anchorName, locationString)) { + issuer.sendMessage("&aAnchor &f" + anchorName + "&a set to &f" + locationString); + } else { + issuer.sendMessage("&cFailed to set anchor &f" + anchorName + "."); + } + } +}