feat: Implement help command

This commit is contained in:
Ben Woo 2023-02-18 14:19:25 +08:00
parent d819153690
commit 974afb4751
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
5 changed files with 106 additions and 10 deletions

View File

@ -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.
*/

View File

@ -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.");
}
}

View File

@ -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);
}
}

View File

@ -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<String> 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<String> suggestDestinations(BukkitCommandCompletionContext context) {
if (context.hasConfig("playerOnly") && !context.getIssuer().isPlayer()) {
return Collections.emptyList();

View File

@ -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<HelpEntry> entries = help.getHelpEntries();
if (entries.size() == 1) {
this.plugin.getMVCommandManager().getHelpFormatter().showDetailedHelp(help, entries.get(0));
return;
}
help.showHelp();
}
}