diff --git a/src/main/java/org/dynmap/DynmapPlugin.java b/src/main/java/org/dynmap/DynmapPlugin.java index 5514dae4..5ab7d8d6 100644 --- a/src/main/java/org/dynmap/DynmapPlugin.java +++ b/src/main/java/org/dynmap/DynmapPlugin.java @@ -17,12 +17,15 @@ import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.util.config.Configuration; +import org.dynmap.Event.Listener; import org.dynmap.debug.Debug; import org.dynmap.debug.LogDebugger; import org.dynmap.web.HttpServer; import org.dynmap.web.handlers.ClientConfigurationHandler; import org.dynmap.web.handlers.ClientUpdateHandler; import org.dynmap.web.handlers.FilesystemHandler; +import org.dynmap.web.handlers.SendMessageHandler; +import org.dynmap.web.handlers.SendMessageHandler.Message; public class DynmapPlugin extends JavaPlugin { @@ -55,13 +58,13 @@ public class DynmapPlugin extends JavaPlugin { public void onEnable() { Debug.addDebugger(new LogDebugger()); - + configuration = new Configuration(new File(this.getDataFolder(), "configuration.txt")); configuration.load(); tilesDirectory = getFile(configuration.getString("tilespath", "web/tiles")); tilesDirectory.mkdirs(); - + playerList = new PlayerList(getServer()); playerList.load(); @@ -87,6 +90,16 @@ public class DynmapPlugin extends JavaPlugin { webServer.handlers.put("/up/", new ClientUpdateHandler(mapManager, playerList, getServer())); webServer.handlers.put("/up/configuration", new ClientConfigurationHandler((Map) configuration.getProperty("web"))); + SendMessageHandler messageHandler = new SendMessageHandler(); + messageHandler.onMessageReceived.addListener(new Listener() { + @Override + public void triggered(Message t) { + log.info("[WEB] " + t.name + ": " + t.message); + getServer().broadcastMessage("[WEB] " + t.name + ": " + t.message); + } + }); + webServer.handlers.put("/up/sendmessage", messageHandler); + try { webServer.startServer(); } catch (IOException e) { @@ -115,7 +128,7 @@ public class DynmapPlugin extends JavaPlugin { getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, playerListener, Priority.Normal, this); getServer().getPluginManager().registerEvent(Event.Type.PLAYER_CHAT, playerListener, Priority.Normal, this); } - + private static File combinePaths(File parent, String path) { return combinePaths(parent, new File(path)); } @@ -125,7 +138,7 @@ public class DynmapPlugin extends JavaPlugin { return path; return new File(parent, path.getPath()); } - + public File getFile(String path) { return combinePaths(DynmapPlugin.dataRoot, path); } diff --git a/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java b/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java new file mode 100644 index 00000000..a59a4907 --- /dev/null +++ b/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java @@ -0,0 +1,45 @@ +package org.dynmap.web.handlers; + +import java.io.InputStreamReader; +import java.nio.CharBuffer; +import java.util.logging.Logger; + +import org.dynmap.Event; +import org.dynmap.web.HttpErrorHandler; +import org.dynmap.web.HttpField; +import org.dynmap.web.HttpHandler; +import org.dynmap.web.HttpMethods; +import org.dynmap.web.HttpRequest; +import org.dynmap.web.HttpResponse; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; + +public class SendMessageHandler implements HttpHandler { + protected static final Logger log = Logger.getLogger("Minecraft"); + + private static final JSONParser parser = new JSONParser(); + public Event onMessageReceived = new Event(); + @Override + public void handle(String path, HttpRequest request, HttpResponse response) throws Exception { + if (!request.method.equals(HttpMethods.Post)) { + HttpErrorHandler.handleMethodNotAllowed(response); + return; + } + + InputStreamReader reader = new InputStreamReader(request.body); + + JSONObject o = (JSONObject)parser.parse(reader); + Message message = new Message(); + message.name = String.valueOf(o.get("name")); + message.message = String.valueOf(o.get("message")); + + onMessageReceived.trigger(message); + + response.fields.put(HttpField.contentLength, "0"); + response.getBody(); + } + public class Message { + public String name; + public String message; + } +}