diff --git a/src/main/java/dev/plex/HTTPDModule.java b/src/main/java/dev/plex/HTTPDModule.java index 0c4d151..08f64eb 100644 --- a/src/main/java/dev/plex/HTTPDModule.java +++ b/src/main/java/dev/plex/HTTPDModule.java @@ -5,6 +5,7 @@ import dev.plex.config.ModuleConfig; import dev.plex.module.PlexModule; import dev.plex.request.AbstractServlet; import dev.plex.request.SchematicUploadServlet; +import dev.plex.request.impl.CommandsEndpoint; import dev.plex.request.impl.IndefBansEndpoint; import dev.plex.request.impl.IndexEndpoint; import dev.plex.request.impl.ListEndpoint; @@ -77,6 +78,7 @@ public class HTTPDModule extends PlexModule new IndexEndpoint(); new ListEndpoint(); new PunishmentsEndpoint(); + new CommandsEndpoint(); new SchematicDownloadEndpoint(); new SchematicUploadEndpoint(); diff --git a/src/main/java/dev/plex/request/AbstractServlet.java b/src/main/java/dev/plex/request/AbstractServlet.java index a1554ee..34951b3 100644 --- a/src/main/java/dev/plex/request/AbstractServlet.java +++ b/src/main/java/dev/plex/request/AbstractServlet.java @@ -98,6 +98,7 @@ public class AbstractServlet extends HttpServlet base = base.replace("${ACTIVE_ADMINS}", ""); base = base.replace("${ACTIVE_INDEFBANS}", ""); base = base.replace("${ACTIVE_LIST}", ""); + base = base.replace("${ACTIVE_COMMANDS}", ""); base = base.replace("${ACTIVE_PUNISHMENTS}", ""); base = base.replace("${ACTIVE_SCHEMATICS}", ""); base = base.replace("${CONTENT}", info[2]); diff --git a/src/main/java/dev/plex/request/impl/CommandsEndpoint.java b/src/main/java/dev/plex/request/impl/CommandsEndpoint.java new file mode 100644 index 0000000..43fd5e4 --- /dev/null +++ b/src/main/java/dev/plex/request/impl/CommandsEndpoint.java @@ -0,0 +1,111 @@ +package dev.plex.request.impl; + +import dev.plex.command.PlexCommand; +import dev.plex.command.annotation.CommandPermissions; +import dev.plex.request.AbstractServlet; +import dev.plex.request.GetMapping; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.SortedMap; +import java.util.TreeMap; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.PluginIdentifiableCommand; + +public class CommandsEndpoint extends AbstractServlet +{ + + private final StringBuilder list = new StringBuilder(); + private boolean loadedCommands = false; + + @GetMapping(endpoint = "/commands/") + public String getCommands(HttpServletRequest request, HttpServletResponse response) + { + if (!loadedCommands) + { + final SortedMap> commandMap = new TreeMap<>(); + final CommandMap map = Bukkit.getCommandMap(); + for (Command command : map.getKnownCommands().values()) + { + String plugin = "Bukkit"; + if (command instanceof PluginIdentifiableCommand) + { + plugin = ((PluginIdentifiableCommand) command).getPlugin().getName(); + } + + List pluginCommands = commandMap.computeIfAbsent(plugin, k -> new ArrayList<>()); + if (!pluginCommands.contains(command)) + { + pluginCommands.add(command); + } + } + + for (String key : commandMap.keySet()) + { + commandMap.get(key).sort(Comparator.comparing(Command::getName)); + StringBuilder rows = new StringBuilder(); + for (Command command : commandMap.get(key)) + { + String permission = command.getPermission(); + if (command instanceof PlexCommand plexCmd) + { + CommandPermissions perms = plexCmd.getClass().getAnnotation(CommandPermissions.class); + if (perms != null) + { + permission = (perms.permission().isBlank() ? "N/A" : perms.permission()); + } + } + + rows.append(createRow(command.getName(), command.getAliases(), command.getDescription(), command.getUsage(), permission)); + } + + list.append(createTable(key, rows.toString())).append("\n"); + } + + loadedCommands = true; + } + + return commandsHTML(list.toString()); + } + + private String commandsHTML(String commandsList) + { + String file = readFile(this.getClass().getResourceAsStream("/httpd/commands.html")); + file = file.replace("${commands}", commandsList); + return file; + } + + private String createTable(String pluginName, String commandRows) + { + return "
" + pluginName + "\n" + + "\n" + + " \n \n \n " + + "\n " + + "\n " + + "\n \n\n" + + "\n " + commandRows + "\n\n
Name (Aliases)DescriptionUsagePermission
\n
"; + } + + private String createRow(String name, List aliases, String description, String usage, String permission) + { + return " \n " + name + + (aliases.isEmpty() || aliases.toString().equals("[]") ? "" : " (" + String.join(", ", aliases) + ")") + "\n" + + " " + description + "\n" + + " " + cleanUsage(usage) + "\n" + + " " + (permission != null ? permission.replaceAll(";", "
") : "N/A") + "\n "; + } + + private String cleanUsage(String usage) + { + usage = usage.replaceAll("<", "<").replaceAll(">", ">"); + if (usage.isBlank()) + { + usage = "Not Provided"; + } + return usage.startsWith("/") || usage.equals("Not Provided") ? usage : "/" + usage; + } +} diff --git a/src/main/resources/httpd/commands.html b/src/main/resources/httpd/commands.html new file mode 100644 index 0000000..f052b87 --- /dev/null +++ b/src/main/resources/httpd/commands.html @@ -0,0 +1,12 @@ +Commands +COMMANDS + +

Commands List

+
A list of commands is below.
+

+${commands} \ No newline at end of file diff --git a/src/main/resources/httpd/template.html b/src/main/resources/httpd/template.html index 7883b59..4746e63 100644 --- a/src/main/resources/httpd/template.html +++ b/src/main/resources/httpd/template.html @@ -35,6 +35,9 @@ +
  • + Commands +