Web help roughly implemented.

This commit is contained in:
StevenLawson 2013-08-27 12:39:28 -04:00
parent 0f31ea2953
commit ada803cd7d
4 changed files with 133 additions and 13 deletions

View file

@ -1,6 +1,7 @@
package me.StevenLawson.TotalFreedomMod.HTTPD;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.apache.commons.lang3.StringEscapeUtils.*;
@ -17,17 +18,33 @@ public class HTMLGenerationTools
return "<p>" + escapeHtml4(data) + "</p>\r\n";
}
public static String mapToHTMLList(Map<String, String> map)
public static <K, V> String list(Map<K, V> map)
{
StringBuilder output = new StringBuilder();
output.append("<ul>\r\n");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
while (it.hasNext())
{
Map.Entry<String, String> entry = it.next();
output.append("<li>").append(escapeHtml4(entry.getKey() + " = " + entry.getValue())).append("</li>\r\n");
Map.Entry<K, V> entry = it.next();
output.append("<li>").append(escapeHtml4(entry.getKey().toString() + " = " + entry.getValue().toString())).append("</li>\r\n");
}
output.append("</ul>\r\n");
return output.toString();
}
public static <T> String list(List<T> list)
{
StringBuilder output = new StringBuilder();
output.append("<ul>\r\n");
for (T entry : list)
{
output.append("<li>").append(escapeHtml4(entry.toString())).append("</li>\r\n");
}
output.append("</ul>\r\n");

View file

@ -24,11 +24,11 @@ public class Module_dump extends TFM_HTTPD_Module
.append(paragraph("args (Length: " + args.length + "): " + StringUtils.join(args, ",")))
.append(paragraph("Method: " + method.toString()))
.append(paragraph("Headers:"))
.append(mapToHTMLList(headers))
.append(list(headers))
.append(paragraph("Params:"))
.append(mapToHTMLList(params))
.append(list(params))
.append(paragraph("Files:"))
.append(mapToHTMLList(files));
.append(list(files));
return responseBody.toString();
}

View file

@ -0,0 +1,96 @@
package me.StevenLawson.TotalFreedomMod.HTTPD;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.PluginIdentifiableCommand;
import static me.StevenLawson.TotalFreedomMod.HTTPD.HTMLGenerationTools.*;
public class Module_helpNew extends TFM_HTTPD_Module
{
public Module_helpNew(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files)
{
super(uri, method, headers, params, files);
}
@Override
public String getBody()
{
StringBuilder responseBody = new StringBuilder();
CommandMap commandMap;
HashMap<String, Command> knownCommands;
if ((commandMap = TFM_CommandLoader.getInstance().getCommandMap()) == null
|| (knownCommands = TFM_CommandLoader.getInstance().getKnownCommands(commandMap)) == null)
{
return paragraph("Error loading commands.");
}
final Map<String, List<Command>> commandsByPlugin = new HashMap<String, List<Command>>();
final Iterator<Map.Entry<String, Command>> itKnownCommands = knownCommands.entrySet().iterator();
while (itKnownCommands.hasNext())
{
final Map.Entry<String, Command> entry = itKnownCommands.next();
final String name = entry.getKey();
final Command command = entry.getValue();
if (name.equalsIgnoreCase(command.getName()))
{
String pluginName = "Bukkit";
if (command instanceof PluginIdentifiableCommand)
{
pluginName = ((PluginIdentifiableCommand) command).getPlugin().getName();
}
List<Command> pluginCommands = commandsByPlugin.get(pluginName);
if (pluginCommands == null)
{
commandsByPlugin.put(pluginName, pluginCommands = new ArrayList<Command>());
}
pluginCommands.add(command);
}
}
final Iterator<Map.Entry<String, List<Command>>> itCommandsByPlugin = commandsByPlugin.entrySet().iterator();
while (itCommandsByPlugin.hasNext())
{
final Map.Entry<String, List<Command>> entry = itCommandsByPlugin.next();
final String pluginName = entry.getKey();
final List<Command> commands = entry.getValue();
Collections.sort(commands, new Comparator<Command>()
{
@Override
public int compare(Command a, Command b)
{
return a.getName().compareTo(b.getName());
}
});
List<String> descriptions = new ArrayList<String>();
for (Command command : commands)
{
descriptions.add(command.getName() + " (" + command.getUsage().replace("<command>", command.getName()).trim() + "): " + command.getDescription());
}
responseBody
.append(paragraph(pluginName))
.append(list(descriptions));
}
return responseBody.toString();
}
@Override
public String getTitle()
{
return "TotalFreedomMod :: WebHelp";
}
}

View file

@ -11,7 +11,7 @@ import org.bukkit.Bukkit;
public class TFM_HTTPD_Manager
{
public static final int PORT = 28966;
public static final int PORT = 8748;
//
private final TFM_HTTPD httpd = new TFM_HTTPD(PORT);
@ -24,6 +24,15 @@ public class TFM_HTTPD_Manager
try
{
httpd.start();
if (httpd.isAlive())
{
TFM_Log.info("TFM HTTPd started. Listening on port: " + httpd.getListeningPort());
}
else
{
TFM_Log.info("Error starting TFM HTTPd.");
}
}
catch (IOException ex)
{
@ -34,6 +43,8 @@ public class TFM_HTTPD_Manager
public void stop()
{
httpd.stop();
TFM_Log.info("TFM HTTPd stopped.");
}
private static class TFM_HTTPD extends NanoHTTPD
@ -56,7 +67,6 @@ public class TFM_HTTPD_Manager
final String[] args = StringUtils.split(uri, "/");
if (args.length >= 1)
{
// Hop onto the Bukkit thread, so we're safe to access the Bukkit API
Future<Response> responseCall = Bukkit.getScheduler().callSyncMethod(TotalFreedomMod.plugin, new Callable<Response>()
{
@Override
@ -72,11 +82,8 @@ public class TFM_HTTPD_Manager
}
else if ("help".equalsIgnoreCase(args[0]))
{
//The issue is that plugin.getDescription().getCommands() only shows commands in the plugin.yml file.
//I need to make another version of this that uses the CommandMap.
return new Module_help(uri, method, headers, params, files).getResponse();
return new Module_helpNew(uri, method, headers, params, files).getResponse();
}
return null;
}
});