Add /su commandinfo command

This command will output information about the specified command, like which plugin it belongs to, which usages, aliases, names, description and permissions it has. Permission to use the command is `serverutils.commandinfo`.
This commit is contained in:
Frank van der Heijden 2020-06-09 20:51:01 +02:00
parent 4c035fdc59
commit 04f3314e8a
No known key found for this signature in database
GPG key ID: 26DA56488D314D11
4 changed files with 76 additions and 9 deletions

View file

@ -13,13 +13,12 @@ import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.command.*;
import org.bukkit.command.defaults.PluginsCommand;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.Arrays;
import java.util.Map;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;
public class ServerUtils extends JavaPlugin implements CommandExecutor {
@ -52,6 +51,14 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
.map(File::getName)
.collect(Collectors.toList()));
commandManager.getCommandCompletions().registerAsyncCompletion("supportedConfigs", context -> CommandServerUtils.getSupportedConfigs());
commandManager.getCommandCompletions().registerAsyncCompletion("commands", context -> {
try {
return RCommandMap.getKnownCommands((SimpleCommandMap) Bukkit.getCommandMap()).keySet();
} catch (IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
}
return Collections.emptyList();
});
reload();
Bukkit.getPluginManager().registerEvents(new MainListener(), this);

View file

@ -197,4 +197,46 @@ public class CommandServerUtils extends BaseCommand {
builder.sendTo(sender);
Messenger.sendMessage(sender, "serverutils.plugininfo.footer");
}
@Subcommand("commandinfo")
@CommandCompletion("@commands")
@CommandPermission("serverutils.commandinfo")
@Description("Shows information about the specified command.")
public void onCommandInfo(CommandSender sender, String command) {
Command cmd = PluginManager.getCommand(command);
if (cmd == null) {
Messenger.sendMessage(sender, "serverutils.commandinfo.not_exists");
return;
}
String format = Messenger.getMessage("serverutils.commandinfo.format");
String listFormatString = Messenger.getMessage("serverutils.commandinfo.list_format");
String seperator = Messenger.getMessage("serverutils.commandinfo.seperator");
String lastSeperator = Messenger.getMessage("serverutils.commandinfo.last_seperator");
ListFormat<String> listFormat = str -> listFormatString.replace("%value%", str);
Messenger.sendMessage(sender, "serverutils.commandinfo.header");
FormatBuilder builder = FormatBuilder.create(format)
.orderedKeys("%key%", "%value%")
.add("Name", cmd.getName());
if (cmd instanceof PluginIdentifiableCommand) {
PluginIdentifiableCommand pc = (PluginIdentifiableCommand) cmd;
builder.add("Plugin", pc.getPlugin().getName());
}
builder.add("Usage", cmd.getUsage())
.add("Description", cmd.getDescription())
.add("Aliases", ListBuilder.create(cmd.getAliases())
.format(listFormat)
.seperator(seperator)
.lastSeperator(lastSeperator)
.toString())
.add("Label", cmd.getLabel())
.add("Timings Name", cmd.getTimingName())
.add("Permission", cmd.getPermission())
.add("Permission Message", cmd.getPermissionMessage());
builder.sendTo(sender);
Messenger.sendMessage(sender, "serverutils.commandinfo.footer");
}
}

View file

@ -54,6 +54,15 @@ public class Messenger {
"seperator", "&8, ",
"last_seperator", " &8and ",
"footer", "&8&m-------------------------------------------------"
),
"commandinfo", Defaults.of(
"header", "&8&m-----------=&r&8[ &b&lServerUtils CommandInfo&r &8]&m=----------",
"format", " &3%key%&8: &b%value%",
"list_format", "&b%value%",
"seperator", "&8, ",
"last_seperator", " &8and ",
"footer", "&8&m-------------------------------------------------",
"not_exists", "&cThat command is not a valid registered command."
)
)
);

View file

@ -107,17 +107,20 @@ public class PluginManager {
return enablePlugin(loadResult.getPlugin());
}
public static void unregisterCommands(Plugin plugin) {
SimpleCommandMap commandMap = RCraftServer.getCommandMap();
Map<String, Command> map;
public static Map<String, Command> getKnownCommands() {
try {
map = RCommandMap.getKnownCommands(commandMap);
return RCommandMap.getKnownCommands(RCraftServer.getCommandMap());
} catch (Exception ex) {
ex.printStackTrace();
return;
return null;
}
}
map.values().removeIf(c -> {
public static void unregisterCommands(Plugin plugin) {
Map<String, Command> knownCommands = getKnownCommands();
if (knownCommands == null) return;
knownCommands.values().removeIf(c -> {
if (c instanceof PluginCommand) {
PluginCommand pc = (PluginCommand) c;
if (pc.getPlugin() == plugin) {
@ -129,4 +132,10 @@ public class PluginManager {
return false;
});
}
public static Command getCommand(String command) {
Map<String, Command> knownCommands = getKnownCommands();
if (knownCommands == null) return null;
return knownCommands.get(command);
}
}