Implement anchor commands

This commit is contained in:
Ben Woo 2023-09-21 17:32:58 +08:00
parent 913ceb3006
commit 5337bc75cd
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
3 changed files with 232 additions and 0 deletions

View File

@ -0,0 +1,45 @@
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("<name>")
@Description("")
void onAnchorDeleteCommand(
MVCommandIssuer issuer,
@Syntax("<name>")
@Description("")
String anchorName) {
anchorManager.deleteAnchor(anchorName)
.onSuccess(ignore -> issuer.sendMessage("&aAnchor &f'" + anchorName + "&a deleted."))
.onFailure(e -> issuer.sendMessage("&cFailed to delete anchor. " + e.getMessage()));
}
}

View File

@ -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<Integer> 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<ContentFilter> 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 <page>] [--filter <filter>]")
@Description("")
void onAnchorListCommand(
MVCommandIssuer issuer,
@Optional
@Syntax("[--page <page>] [--filter <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<String> getAnchors(Player player) {
return anchorManager.getAnchors(player).stream().map(anchorName -> {
Location anchorLocation = anchorManager.getAnchorLocation(anchorName);
return "&a" + anchorName + "&7 - &f" + locationManipulation.locationToString(anchorLocation);
}).toList();
}
}

View File

@ -0,0 +1,84 @@
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("<name> [location]")
@Description("")
void onAnchorSetCommand(
MVCommandIssuer issuer,
@Flags("resolve=issuerOnly")
Player player,
@Single
@Syntax("<name>")
@Description("")
//TODO: Check if anchor name is valid or exists already.
String anchorName) {
Location anchorLocation = player.getLocation();
anchorManager.saveAnchorLocation(anchorName, anchorLocation)
.onSuccess(ignore -> issuer.sendMessage("&aAnchor &f" + anchorName + "&a set to &f"
+ locationManipulation.locationToString(anchorLocation)))
.onFailure(e -> issuer.sendMessage("&cFailed to set anchor &f" + anchorName + "&c." + e.getMessage()));
}
@Subcommand("anchor set")
@CommandPermission("multiverse.core.anchor.create")
@CommandCompletion("@anchornames @locations")
@Syntax("<name> [location]")
@Description("")
void onAnchorSetCommand(
MVCommandIssuer issuer,
@Syntax("<name>")
@Description("")
String anchorName,
@Single
@Syntax("[location]")
@Description("")
String locationString) {
anchorManager.saveAnchorLocation(anchorName, locationString)
.onSuccess(anchorLocation -> issuer.sendMessage("&aAnchor &f" + anchorName + "&a set to &f"
+ locationManipulation.locationToString(anchorLocation)))
.onFailure(e -> issuer.sendMessage("&cFailed to set anchor '" + anchorName + "'&c. " + e.getMessage()));
}
}