Skip to content

Commit e2fb3fb

Browse files
committed
Brigadier
1 parent 582084b commit e2fb3fb

1 file changed

Lines changed: 39 additions & 26 deletions

File tree

src/main/java/dev/plex/request/impl/CommandsEndpoint.java

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.plex.request.impl;
22

3+
import dev.plex.HTTPDModule;
34
import dev.plex.command.PlexCommand;
4-
import dev.plex.command.annotation.CommandPermissions;
55
import dev.plex.request.AbstractServlet;
66
import dev.plex.request.GetMapping;
77
import jakarta.servlet.http.HttpServletRequest;
@@ -36,7 +36,14 @@ public String getCommands(HttpServletRequest request, HttpServletResponse respon
3636

3737
private static String buildSections()
3838
{
39-
final SortedMap<String, List<Command>> commandMap = new TreeMap<>();
39+
final SortedMap<String, List<CommandInfo>> commandMap = new TreeMap<>();
40+
41+
List<CommandInfo> plexCommands = commandMap.computeIfAbsent("Plex", k -> new ArrayList<>());
42+
for (PlexCommand command : HTTPDModule.plexApi().commands().registeredCommands())
43+
{
44+
plexCommands.add(CommandInfo.from(command));
45+
}
46+
4047
final CommandMap map = Bukkit.getCommandMap();
4148
for (Command command : map.getKnownCommands().values())
4249
{
@@ -45,29 +52,30 @@ private static String buildSections()
4552
{
4653
plugin = pic.getPlugin().getName();
4754
}
48-
List<Command> pluginCommands = commandMap.computeIfAbsent(plugin, k -> new ArrayList<>());
49-
if (!pluginCommands.contains(command))
55+
List<CommandInfo> pluginCommands = commandMap.computeIfAbsent(plugin, k -> new ArrayList<>());
56+
CommandInfo commandInfo = CommandInfo.from(command);
57+
if (!pluginCommands.contains(commandInfo))
5058
{
51-
pluginCommands.add(command);
59+
pluginCommands.add(commandInfo);
5260
}
5361
}
5462

5563
StringBuilder sb = new StringBuilder();
5664
for (String key : commandMap.keySet())
5765
{
58-
List<Command> commands = commandMap.get(key);
59-
commands.sort(Comparator.comparing(Command::getName));
66+
List<CommandInfo> commands = commandMap.get(key);
67+
commands.sort(Comparator.comparing(CommandInfo::name));
6068
sb.append(renderSection(key, commands));
6169
}
6270
return sb.toString();
6371
}
6472

65-
private static String renderSection(String plugin, List<Command> commands)
73+
private static String renderSection(String plugin, List<CommandInfo> commands)
6674
{
6775
StringBuilder cards = new StringBuilder();
68-
for (Command c : commands)
76+
for (CommandInfo command : commands)
6977
{
70-
cards.append(renderCard(c));
78+
cards.append(renderCard(command));
7179
}
7280
String name = escapeHtml(plugin);
7381
return """
@@ -88,13 +96,13 @@ private static String renderSection(String plugin, List<Command> commands)
8896
""".formatted(name, name, commands.size(), commands.size() == 1 ? "command" : "commands", cards);
8997
}
9098

91-
private static String renderCard(Command c)
99+
private static String renderCard(CommandInfo command)
92100
{
93-
String name = escapeHtml(c.getName());
94-
String aliases = c.getAliases() == null || c.getAliases().isEmpty() ? "" : String.join(", ", c.getAliases());
95-
String description = c.getDescription() == null || c.getDescription().isBlank() ? "" : escapeHtml(c.getDescription());
96-
String usage = cleanUsage(c.getUsage());
97-
String permission = resolvePermission(c);
101+
String name = escapeHtml(command.name());
102+
String aliases = command.aliases().isEmpty() ? "" : String.join(", ", command.aliases());
103+
String description = command.description().isBlank() ? "" : escapeHtml(command.description());
104+
String usage = cleanUsage(command.usage());
105+
String permission = cleanPermission(command.permission());
98106

99107
String aliasMarkup = aliases.isEmpty()
100108
? ""
@@ -123,17 +131,8 @@ private static String renderCard(Command c)
123131
""".formatted(searchBlob, name, aliasMarkup, descMarkup, usage, permission);
124132
}
125133

126-
private static String resolvePermission(Command c)
134+
private static String cleanPermission(String permission)
127135
{
128-
String permission = c.getPermission();
129-
if (c instanceof PlexCommand plexCmd)
130-
{
131-
CommandPermissions perms = plexCmd.getClass().getAnnotation(CommandPermissions.class);
132-
if (perms != null)
133-
{
134-
permission = perms.permission().isBlank() ? "N/A" : perms.permission();
135-
}
136-
}
137136
if (permission == null || permission.isBlank()) return "N/A";
138137
return escapeHtml(permission).replace(";", "<br>");
139138
}
@@ -153,4 +152,18 @@ private static String escapeHtml(String s)
153152
.replace(">", "&gt;")
154153
.replace("\"", "&quot;");
155154
}
155+
156+
private record CommandInfo(String name, List<String> aliases, String description, String usage, String permission)
157+
{
158+
private static CommandInfo from(PlexCommand command)
159+
{
160+
return new CommandInfo(command.getName(), command.getAliases(), command.getDescription(), command.getUsage(), command.getPermission());
161+
}
162+
163+
private static CommandInfo from(Command command)
164+
{
165+
List<String> aliases = command.getAliases() == null ? List.of() : command.getAliases();
166+
return new CommandInfo(command.getName(), aliases, command.getDescription(), command.getUsage(), command.getPermission());
167+
}
168+
}
156169
}

0 commit comments

Comments
 (0)