Added sendmessage handler (for chatting back).

This commit is contained in:
FrozenCow 2011-02-15 21:00:36 +01:00
parent 2d693b1ebf
commit ada9637d72
2 changed files with 62 additions and 4 deletions

View File

@ -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<SendMessageHandler.Message>() {
@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);
}

View File

@ -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<Message> onMessageReceived = new Event<SendMessageHandler.Message>();
@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;
}
}