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:
parent
4c035fdc59
commit
04f3314e8a
4 changed files with 76 additions and 9 deletions
|
|
@ -13,13 +13,12 @@ import org.bstats.bukkit.Metrics;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.*;
|
import org.bukkit.command.*;
|
||||||
import org.bukkit.command.defaults.PluginsCommand;
|
import org.bukkit.command.defaults.PluginsCommand;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Arrays;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.Map;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
||||||
|
|
@ -52,6 +51,14 @@ public class ServerUtils extends JavaPlugin implements CommandExecutor {
|
||||||
.map(File::getName)
|
.map(File::getName)
|
||||||
.collect(Collectors.toList()));
|
.collect(Collectors.toList()));
|
||||||
commandManager.getCommandCompletions().registerAsyncCompletion("supportedConfigs", context -> CommandServerUtils.getSupportedConfigs());
|
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();
|
reload();
|
||||||
|
|
||||||
Bukkit.getPluginManager().registerEvents(new MainListener(), this);
|
Bukkit.getPluginManager().registerEvents(new MainListener(), this);
|
||||||
|
|
|
||||||
|
|
@ -197,4 +197,46 @@ public class CommandServerUtils extends BaseCommand {
|
||||||
builder.sendTo(sender);
|
builder.sendTo(sender);
|
||||||
Messenger.sendMessage(sender, "serverutils.plugininfo.footer");
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,15 @@ public class Messenger {
|
||||||
"seperator", "&8, ",
|
"seperator", "&8, ",
|
||||||
"last_seperator", " &8and ",
|
"last_seperator", " &8and ",
|
||||||
"footer", "&8&m-------------------------------------------------"
|
"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."
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -107,17 +107,20 @@ public class PluginManager {
|
||||||
return enablePlugin(loadResult.getPlugin());
|
return enablePlugin(loadResult.getPlugin());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void unregisterCommands(Plugin plugin) {
|
public static Map<String, Command> getKnownCommands() {
|
||||||
SimpleCommandMap commandMap = RCraftServer.getCommandMap();
|
|
||||||
Map<String, Command> map;
|
|
||||||
try {
|
try {
|
||||||
map = RCommandMap.getKnownCommands(commandMap);
|
return RCommandMap.getKnownCommands(RCraftServer.getCommandMap());
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
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) {
|
if (c instanceof PluginCommand) {
|
||||||
PluginCommand pc = (PluginCommand) c;
|
PluginCommand pc = (PluginCommand) c;
|
||||||
if (pc.getPlugin() == plugin) {
|
if (pc.getPlugin() == plugin) {
|
||||||
|
|
@ -129,4 +132,10 @@ public class PluginManager {
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Command getCommand(String command) {
|
||||||
|
Map<String, Command> knownCommands = getKnownCommands();
|
||||||
|
if (knownCommands == null) return null;
|
||||||
|
return knownCommands.get(command);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue