mirror of
https://github.com/plexusorg/Module-HTTPD.git
synced 2026-06-04 00:56:54 +00:00
Brigadier
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
package dev.plex.request.impl;
|
package dev.plex.request.impl;
|
||||||
|
|
||||||
|
import dev.plex.HTTPDModule;
|
||||||
import dev.plex.command.PlexCommand;
|
import dev.plex.command.PlexCommand;
|
||||||
import dev.plex.command.annotation.CommandPermissions;
|
|
||||||
import dev.plex.request.AbstractServlet;
|
import dev.plex.request.AbstractServlet;
|
||||||
import dev.plex.request.GetMapping;
|
import dev.plex.request.GetMapping;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
@@ -36,7 +36,14 @@ public class CommandsEndpoint extends AbstractServlet
|
|||||||
|
|
||||||
private static String buildSections()
|
private static String buildSections()
|
||||||
{
|
{
|
||||||
final SortedMap<String, List<Command>> commandMap = new TreeMap<>();
|
final SortedMap<String, List<CommandInfo>> commandMap = new TreeMap<>();
|
||||||
|
|
||||||
|
List<CommandInfo> plexCommands = commandMap.computeIfAbsent("Plex", k -> new ArrayList<>());
|
||||||
|
for (PlexCommand command : HTTPDModule.plexApi().commands().registeredCommands())
|
||||||
|
{
|
||||||
|
plexCommands.add(CommandInfo.from(command));
|
||||||
|
}
|
||||||
|
|
||||||
final CommandMap map = Bukkit.getCommandMap();
|
final CommandMap map = Bukkit.getCommandMap();
|
||||||
for (Command command : map.getKnownCommands().values())
|
for (Command command : map.getKnownCommands().values())
|
||||||
{
|
{
|
||||||
@@ -45,29 +52,30 @@ public class CommandsEndpoint extends AbstractServlet
|
|||||||
{
|
{
|
||||||
plugin = pic.getPlugin().getName();
|
plugin = pic.getPlugin().getName();
|
||||||
}
|
}
|
||||||
List<Command> pluginCommands = commandMap.computeIfAbsent(plugin, k -> new ArrayList<>());
|
List<CommandInfo> pluginCommands = commandMap.computeIfAbsent(plugin, k -> new ArrayList<>());
|
||||||
if (!pluginCommands.contains(command))
|
CommandInfo commandInfo = CommandInfo.from(command);
|
||||||
|
if (!pluginCommands.contains(commandInfo))
|
||||||
{
|
{
|
||||||
pluginCommands.add(command);
|
pluginCommands.add(commandInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (String key : commandMap.keySet())
|
for (String key : commandMap.keySet())
|
||||||
{
|
{
|
||||||
List<Command> commands = commandMap.get(key);
|
List<CommandInfo> commands = commandMap.get(key);
|
||||||
commands.sort(Comparator.comparing(Command::getName));
|
commands.sort(Comparator.comparing(CommandInfo::name));
|
||||||
sb.append(renderSection(key, commands));
|
sb.append(renderSection(key, commands));
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String renderSection(String plugin, List<Command> commands)
|
private static String renderSection(String plugin, List<CommandInfo> commands)
|
||||||
{
|
{
|
||||||
StringBuilder cards = new StringBuilder();
|
StringBuilder cards = new StringBuilder();
|
||||||
for (Command c : commands)
|
for (CommandInfo command : commands)
|
||||||
{
|
{
|
||||||
cards.append(renderCard(c));
|
cards.append(renderCard(command));
|
||||||
}
|
}
|
||||||
String name = escapeHtml(plugin);
|
String name = escapeHtml(plugin);
|
||||||
return """
|
return """
|
||||||
@@ -88,13 +96,13 @@ public class CommandsEndpoint extends AbstractServlet
|
|||||||
""".formatted(name, name, commands.size(), commands.size() == 1 ? "command" : "commands", cards);
|
""".formatted(name, name, commands.size(), commands.size() == 1 ? "command" : "commands", cards);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String renderCard(Command c)
|
private static String renderCard(CommandInfo command)
|
||||||
{
|
{
|
||||||
String name = escapeHtml(c.getName());
|
String name = escapeHtml(command.name());
|
||||||
String aliases = c.getAliases() == null || c.getAliases().isEmpty() ? "" : String.join(", ", c.getAliases());
|
String aliases = command.aliases().isEmpty() ? "" : String.join(", ", command.aliases());
|
||||||
String description = c.getDescription() == null || c.getDescription().isBlank() ? "" : escapeHtml(c.getDescription());
|
String description = command.description().isBlank() ? "" : escapeHtml(command.description());
|
||||||
String usage = cleanUsage(c.getUsage());
|
String usage = cleanUsage(command.usage());
|
||||||
String permission = resolvePermission(c);
|
String permission = cleanPermission(command.permission());
|
||||||
|
|
||||||
String aliasMarkup = aliases.isEmpty()
|
String aliasMarkup = aliases.isEmpty()
|
||||||
? ""
|
? ""
|
||||||
@@ -123,17 +131,8 @@ public class CommandsEndpoint extends AbstractServlet
|
|||||||
""".formatted(searchBlob, name, aliasMarkup, descMarkup, usage, permission);
|
""".formatted(searchBlob, name, aliasMarkup, descMarkup, usage, permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String resolvePermission(Command c)
|
private static String cleanPermission(String permission)
|
||||||
{
|
{
|
||||||
String permission = c.getPermission();
|
|
||||||
if (c instanceof PlexCommand plexCmd)
|
|
||||||
{
|
|
||||||
CommandPermissions perms = plexCmd.getClass().getAnnotation(CommandPermissions.class);
|
|
||||||
if (perms != null)
|
|
||||||
{
|
|
||||||
permission = perms.permission().isBlank() ? "N/A" : perms.permission();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (permission == null || permission.isBlank()) return "N/A";
|
if (permission == null || permission.isBlank()) return "N/A";
|
||||||
return escapeHtml(permission).replace(";", "<br>");
|
return escapeHtml(permission).replace(";", "<br>");
|
||||||
}
|
}
|
||||||
@@ -153,4 +152,18 @@ public class CommandsEndpoint extends AbstractServlet
|
|||||||
.replace(">", ">")
|
.replace(">", ">")
|
||||||
.replace("\"", """);
|
.replace("\"", """);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private record CommandInfo(String name, List<String> aliases, String description, String usage, String permission)
|
||||||
|
{
|
||||||
|
private static CommandInfo from(PlexCommand command)
|
||||||
|
{
|
||||||
|
return new CommandInfo(command.getName(), command.getAliases(), command.getDescription(), command.getUsage(), command.getPermission());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static CommandInfo from(Command command)
|
||||||
|
{
|
||||||
|
List<String> aliases = command.getAliases() == null ? List.of() : command.getAliases();
|
||||||
|
return new CommandInfo(command.getName(), aliases, command.getDescription(), command.getUsage(), command.getPermission());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user