From 974afb4751be01d1266c015fa1b003e13e072905 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Sat, 18 Feb 2023 14:19:25 +0800 Subject: [PATCH] feat: Implement help command --- .../MultiverseCore/MultiverseCore.java | 12 ++----- .../MultiverseCore/commands/RootCommand.java | 21 ++++++++++++ .../MultiverseCore/commands/UsageCommand.java | 33 +++++++++++++++++++ .../commandtools/MVCommandCompletions.java | 32 ++++++++++++++++++ .../commandtools/MVCommandManager.java | 18 ++++++++++ 5 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/commands/RootCommand.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/commands/UsageCommand.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index a6faa8b2..a9e3c2b1 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -38,8 +38,10 @@ import com.onarandombox.MultiverseCore.commands.LoadCommand; import com.onarandombox.MultiverseCore.commands.RegenCommand; import com.onarandombox.MultiverseCore.commands.ReloadCommand; import com.onarandombox.MultiverseCore.commands.RemoveCommand; +import com.onarandombox.MultiverseCore.commands.RootCommand; import com.onarandombox.MultiverseCore.commands.TeleportCommand; import com.onarandombox.MultiverseCore.commands.UnloadCommand; +import com.onarandombox.MultiverseCore.commands.UsageCommand; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.destination.core.AnchorDestination; @@ -164,7 +166,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore { this.anchorManager.loadAnchors(); this.registerEvents(); this.registerCommands(); - this.setUpLocales(); this.registerDestinations(); this.setupMetrics(); this.saveMVConfig(); @@ -216,15 +217,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore { this.commandManager.registerCommand(new UnloadCommand(this)); } - /** - * Resgister locales - */ - private void setUpLocales() { - this.commandManager.usePerIssuerLocale(true, true); - this.commandManager.getLocales().addFileResClassLoader(this); - this.commandManager.getLocales().addMessageBundles("multiverse-core"); - } - /** * Register all the destinations. */ diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/RootCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/RootCommand.java new file mode 100644 index 00000000..8e9f9518 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/RootCommand.java @@ -0,0 +1,21 @@ +package com.onarandombox.MultiverseCore.commands; + +import co.aikar.commands.CommandIssuer; +import co.aikar.commands.annotation.CommandAlias; +import com.onarandombox.MultiverseCore.MultiverseCore; +import org.bukkit.ChatColor; +import org.bukkit.plugin.PluginDescriptionFile; +import org.jetbrains.annotations.NotNull; + +public class RootCommand extends MultiverseCoreCommand { + public RootCommand(@NotNull MultiverseCore plugin) { + super(plugin); + } + + @CommandAlias("mv") + public void onRootCommand(CommandIssuer issuer) { + PluginDescriptionFile description = this.plugin.getDescription(); + issuer.sendMessage(ChatColor.GREEN + description.getName() + " version " + description.getVersion()); + issuer.sendMessage(ChatColor.GREEN + "See " + ChatColor.WHITE + "/mv help" + ChatColor.GREEN + " for commands available."); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/UsageCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/UsageCommand.java new file mode 100644 index 00000000..e0c63b6e --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/UsageCommand.java @@ -0,0 +1,33 @@ +package com.onarandombox.MultiverseCore.commands; + +import co.aikar.commands.CommandHelp; +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.HelpCommand; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Syntax; +import com.onarandombox.MultiverseCore.MultiverseCore; +import org.jetbrains.annotations.NotNull; + +@CommandAlias("mv") +public class UsageCommand extends MultiverseCoreCommand { + public UsageCommand(@NotNull MultiverseCore plugin) { + super(plugin); + } + + @HelpCommand + @Subcommand("help") + @CommandPermission("multiverse.core.help") + @CommandCompletion("@commands:mv") + @Syntax("[filter] [page]") + @Description("Show Multiverse-Core Command usage.") + public void onUsageCommand(CommandHelp help) { + if (help.getIssuer().isPlayer()) { + // Prevent flooding the chat + help.setPerPage(4); + } + this.plugin.getMVCommandManager().showUsage(help); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java index 044dac90..ff5671d8 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java @@ -5,12 +5,16 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import co.aikar.commands.BukkitCommandCompletionContext; import co.aikar.commands.BukkitCommandIssuer; +import co.aikar.commands.CommandIssuer; import co.aikar.commands.PaperCommandCompletions; +import co.aikar.commands.RegisteredCommand; +import co.aikar.commands.RootCommand; import com.google.common.collect.Sets; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorld; @@ -30,6 +34,7 @@ public class MVCommandCompletions extends PaperCommandCompletions { this.plugin = plugin; this.worldManager = plugin.getMVWorldManager(); + registerAsyncCompletion("commands", this::suggestCommands); registerAsyncCompletion("destinations", this::suggestDestinations); registerAsyncCompletion("flags", this::suggestFlags); registerStaticCompletion("gamerules", this::suggestGamerules); @@ -41,6 +46,33 @@ public class MVCommandCompletions extends PaperCommandCompletions { setDefaultCompletion("mvworlds", MVWorld.class); } + private Collection suggestCommands(BukkitCommandCompletionContext context) { + String rootCmdName = context.getConfig(); + if (rootCmdName == null) { + return Collections.emptyList(); + } + + RootCommand rootCommand = this.plugin.getMVCommandManager().getRegisteredRootCommands().stream() + .unordered() + .filter(c -> c.getCommandName().equals(rootCmdName)) + .findFirst() + .orElse(null); + + if (rootCommand == null) { + return Collections.emptyList(); + } + + return rootCommand.getSubCommands().entries().stream() + .filter(entry -> checkPerms(context.getIssuer(), entry.getValue())) + .map(Map.Entry::getKey) + .filter(cmdName -> !cmdName.startsWith("__")) + .collect(Collectors.toList()); + } + + private boolean checkPerms(CommandIssuer issuer, RegisteredCommand command) { + return this.plugin.getMVCommandManager().hasPermission(issuer, command.getRequiredPermissions()); + } + private Collection suggestDestinations(BukkitCommandCompletionContext context) { if (context.hasConfig("playerOnly") && !context.getIssuer().isPlayer()) { return Collections.emptyList(); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandManager.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandManager.java index f217f3a0..8a1cd3a7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandManager.java @@ -1,9 +1,13 @@ package com.onarandombox.MultiverseCore.commandtools; +import java.util.List; + import co.aikar.commands.BukkitCommandCompletionContext; import co.aikar.commands.BukkitCommandExecutionContext; import co.aikar.commands.CommandCompletions; import co.aikar.commands.CommandContexts; +import co.aikar.commands.CommandHelp; +import co.aikar.commands.HelpEntry; import co.aikar.commands.PaperCommandManager; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlagsManager; @@ -92,4 +96,18 @@ public class MVCommandManager extends PaperCommandManager { } return this.completions; } + + /** + * Standardise usage command formatting for all mv modules. + * + * @param help The target {@link CommandHelp}. + */ + public void showUsage(@NotNull CommandHelp help) { + List entries = help.getHelpEntries(); + if (entries.size() == 1) { + this.plugin.getMVCommandManager().getHelpFormatter().showDetailedHelp(help, entries.get(0)); + return; + } + help.showHelp(); + } }