mirror of
https://github.com/plexusorg/Module-HTTPD.git
synced 2024-12-22 16:04:59 +00:00
Merge pull request #12 from plexusorg/commands-route
Add commands route
This commit is contained in:
commit
c83f8f1d8e
5 changed files with 129 additions and 0 deletions
|
@ -5,6 +5,7 @@ import dev.plex.config.ModuleConfig;
|
||||||
import dev.plex.module.PlexModule;
|
import dev.plex.module.PlexModule;
|
||||||
import dev.plex.request.AbstractServlet;
|
import dev.plex.request.AbstractServlet;
|
||||||
import dev.plex.request.SchematicUploadServlet;
|
import dev.plex.request.SchematicUploadServlet;
|
||||||
|
import dev.plex.request.impl.CommandsEndpoint;
|
||||||
import dev.plex.request.impl.IndefBansEndpoint;
|
import dev.plex.request.impl.IndefBansEndpoint;
|
||||||
import dev.plex.request.impl.IndexEndpoint;
|
import dev.plex.request.impl.IndexEndpoint;
|
||||||
import dev.plex.request.impl.ListEndpoint;
|
import dev.plex.request.impl.ListEndpoint;
|
||||||
|
@ -77,6 +78,7 @@ public class HTTPDModule extends PlexModule
|
||||||
new IndexEndpoint();
|
new IndexEndpoint();
|
||||||
new ListEndpoint();
|
new ListEndpoint();
|
||||||
new PunishmentsEndpoint();
|
new PunishmentsEndpoint();
|
||||||
|
new CommandsEndpoint();
|
||||||
new SchematicDownloadEndpoint();
|
new SchematicDownloadEndpoint();
|
||||||
new SchematicUploadEndpoint();
|
new SchematicUploadEndpoint();
|
||||||
|
|
||||||
|
|
|
@ -98,6 +98,7 @@ public class AbstractServlet extends HttpServlet
|
||||||
base = base.replace("${ACTIVE_ADMINS}", "");
|
base = base.replace("${ACTIVE_ADMINS}", "");
|
||||||
base = base.replace("${ACTIVE_INDEFBANS}", "");
|
base = base.replace("${ACTIVE_INDEFBANS}", "");
|
||||||
base = base.replace("${ACTIVE_LIST}", "");
|
base = base.replace("${ACTIVE_LIST}", "");
|
||||||
|
base = base.replace("${ACTIVE_COMMANDS}", "");
|
||||||
base = base.replace("${ACTIVE_PUNISHMENTS}", "");
|
base = base.replace("${ACTIVE_PUNISHMENTS}", "");
|
||||||
base = base.replace("${ACTIVE_SCHEMATICS}", "");
|
base = base.replace("${ACTIVE_SCHEMATICS}", "");
|
||||||
base = base.replace("${CONTENT}", info[2]);
|
base = base.replace("${CONTENT}", info[2]);
|
||||||
|
|
111
src/main/java/dev/plex/request/impl/CommandsEndpoint.java
Normal file
111
src/main/java/dev/plex/request/impl/CommandsEndpoint.java
Normal file
|
@ -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<String, List<Command>> 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<Command> 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 "<details id=\"" + pluginName + "\"><summary>" + pluginName + "</summary>\n"
|
||||||
|
+ "<table id=\"" + pluginName + "Table\" class=\"table table-striped table-bordered\">\n"
|
||||||
|
+ " <thead>\n <tr>\n <th scope=\"col\">Name (Aliases)</th>\n "
|
||||||
|
+ "<th scope=\"col\">Description</th>\n "
|
||||||
|
+ "<th scope=\"col\">Usage</th>\n "
|
||||||
|
+ "<th scope=\"col\">Permission</th>\n </tr>\n</thead>\n"
|
||||||
|
+ "<tbody>\n " + commandRows + "\n</tbody>\n</table>\n</details>";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String createRow(String name, List<String> aliases, String description, String usage, String permission)
|
||||||
|
{
|
||||||
|
return " <tr>\n <th scope=\"row\">" + name
|
||||||
|
+ (aliases.isEmpty() || aliases.toString().equals("[]") ? "" : " (" + String.join(", ", aliases) + ")") + "</th>\n"
|
||||||
|
+ " <th scope=\"row\">" + description + "</th>\n"
|
||||||
|
+ " <th scope=\"row\"><code>" + cleanUsage(usage) + "</code></th>\n"
|
||||||
|
+ " <th scope=\"row\">" + (permission != null ? permission.replaceAll(";", "<br>") : "N/A") + "</th>\n </tr>";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String cleanUsage(String usage)
|
||||||
|
{
|
||||||
|
usage = usage.replaceAll("<", "<").replaceAll(">", ">");
|
||||||
|
if (usage.isBlank())
|
||||||
|
{
|
||||||
|
usage = "Not Provided";
|
||||||
|
}
|
||||||
|
return usage.startsWith("/") || usage.equals("Not Provided") ? usage : "/" + usage;
|
||||||
|
}
|
||||||
|
}
|
12
src/main/resources/httpd/commands.html
Normal file
12
src/main/resources/httpd/commands.html
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
Commands
|
||||||
|
COMMANDS
|
||||||
|
<style>
|
||||||
|
summary {
|
||||||
|
font-size: 24px;
|
||||||
|
padding: 16px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<h2>Commands List</h2>
|
||||||
|
<h5>A list of commands is below.</h5>
|
||||||
|
<br><br>
|
||||||
|
${commands}
|
|
@ -35,6 +35,9 @@
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link ${ACTIVE_PUNISHMENTS}" href="/api/punishments/">Punishments</a>
|
<a class="nav-link ${ACTIVE_PUNISHMENTS}" href="/api/punishments/">Punishments</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li>
|
||||||
|
<a class="nav-link ${ACTIVE_COMMANDS}" href="/api/commands/">Commands</a>
|
||||||
|
</li>
|
||||||
<li class="nav-item dropdown">
|
<li class="nav-item dropdown">
|
||||||
<a class="nav-link dropdown-toggle ${ACTIVE_SCHEMATICS}" id="navbarDropdownMenuLink" role="button"
|
<a class="nav-link dropdown-toggle ${ACTIVE_SCHEMATICS}" id="navbarDropdownMenuLink" role="button"
|
||||||
data-bs-toggle="dropdown" aria-expanded="false">
|
data-bs-toggle="dropdown" aria-expanded="false">
|
||||||
|
|
Loading…
Reference in a new issue