From 5f8c44ee6b231880e661923e30c69cc0802e4283 Mon Sep 17 00:00:00 2001 From: FrozenCow Date: Sat, 21 May 2011 14:53:49 +0200 Subject: [PATCH] Transfered chat/webchat functionality to their components. --- configuration.txt | 6 ++- .../dynmap/ClientConfigurationComponent.java | 7 +-- src/main/java/org/dynmap/DynmapPlugin.java | 33 -------------- .../dynmap/InternalClientUpdateComponent.java | 45 +++++++++++++++++-- .../dynmap/JsonFileClientUpdateComponent.java | 19 ++++++-- .../org/dynmap/SimpleWebChatComponent.java | 45 ++++++++++++++++++- .../org/dynmap/herochat/HeroChatHandler.java | 1 - .../dynmap/herochat/HeroWebChatComponent.java | 33 ++++++++++++++ 8 files changed, 142 insertions(+), 47 deletions(-) diff --git a/configuration.txt b/configuration.txt index 4c4b29b0..e3a12230 100644 --- a/configuration.txt +++ b/configuration.txt @@ -5,11 +5,15 @@ components: - class: org.dynmap.InternalClientUpdateComponent sendhealth: true + allowwebchat: true + webchat-interval: 5 #- class: org.dynmap.JsonFileClientUpdateComponent + # writeinterval: 1 # sendhealth: true - # interval: 1 + # allowwebchat: false - class: org.dynmap.SimpleWebChatComponent + allowchat: true #- class: org.dynmap.herochat.HeroWebChatComponent # # Control which HeroChat channel messages from web are directed to # herochatwebchannel: Global diff --git a/src/main/java/org/dynmap/ClientConfigurationComponent.java b/src/main/java/org/dynmap/ClientConfigurationComponent.java index 1ed62002..d8ff6ed0 100644 --- a/src/main/java/org/dynmap/ClientConfigurationComponent.java +++ b/src/main/java/org/dynmap/ClientConfigurationComponent.java @@ -15,13 +15,10 @@ public class ClientConfigurationComponent extends Component { public void triggered(JSONObject t) { ConfigurationNode c = plugin.configuration; s(t, "updaterate", c.getFloat("updaterate", 1.0f)); - s(t, "allowchat", c.getBoolean("allowchat", true)); - s(t, "allowwebchat", c.getBoolean("allowwebchat", true)); - s(t, "webchat-interval", c.getFloat("webchat-interval", 5.0f)); s(t, "showplayerfacesinmenu", c.getBoolean("showplayerfacesinmenu", true)); s(t, "joinmessage", c.getString("joinmessage", "%playername% joined")); - s(t, "quitmessage", c.getString("joinmessage", "%playername% quit")); - s(t, "spammessage", c.getString("joinmessage", "You may only chat once every %interval% seconds.")); + s(t, "quitmessage", c.getString("quitmessage", "%playername% quit")); + s(t, "spammessage", c.getString("spammessage", "You may only chat once every %interval% seconds.")); for(ConfigurationNode wn : plugin.configuration.getNodes("worlds")) { DynmapWorld world = plugin.mapManager.getWorld(wn.getString("name")); diff --git a/src/main/java/org/dynmap/DynmapPlugin.java b/src/main/java/org/dynmap/DynmapPlugin.java index b7622f21..3af5fce6 100644 --- a/src/main/java/org/dynmap/DynmapPlugin.java +++ b/src/main/java/org/dynmap/DynmapPlugin.java @@ -141,21 +141,6 @@ public class DynmapPlugin extends JavaPlugin { webServer.handlers.put("/", new FilesystemHandler(getFile(configuration.getString("webpath", "web")))); webServer.handlers.put("/tiles/", new FilesystemHandler(tilesDirectory)); webServer.handlers.put("/up/configuration", new ClientConfigurationHandler(this)); - - if (configuration.getBoolean("allowwebchat", false)) { - SendMessageHandler messageHandler = new SendMessageHandler() {{ - maximumMessageInterval = (configuration.getInteger("webchat-interval", 1) * 1000); - spamMessage = "\""+configuration.getString("spammessage", "You may only chat once every %interval% seconds.")+"\""; - onMessageReceived.addListener(new Listener() { - @Override - public void triggered(Message t) { - webChat(t.name, t.message); - } - }); - }}; - - webServer.handlers.put("/up/sendmessage", messageHandler); - } } public void startWebserver() { @@ -245,17 +230,6 @@ public class DynmapPlugin extends JavaPlugin { //if (isTrigger("chunkgenerated")) pm.registerEvent(Event.Type.CHUNK_GENERATED, renderTrigger, Priority.Monitor, this); } - // To announce when players have joined/quit/chatted. - if (configuration.getBoolean("allowchat", false)) { - // To handle webchat. - PlayerListener playerListener = new DynmapPlayerChatListener(this); - //getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, playerListener, Priority.Normal, this); - pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_CHAT, playerListener, org.bukkit.event.Event.Priority.Monitor, this); - pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_LOGIN, playerListener, org.bukkit.event.Event.Priority.Monitor, this); - pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_JOIN, playerListener, org.bukkit.event.Event.Priority.Monitor, this); - pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_QUIT, playerListener, org.bukkit.event.Event.Priority.Monitor, this); - } - // To link configuration to real loaded worlds. WorldListener worldListener = new WorldListener() { @Override @@ -381,11 +355,4 @@ public class DynmapPlugin extends JavaPlugin { } return true; } - - public void webChat(String name, String message) { - mapManager.pushUpdate(new Client.ChatMessage("web", null, name, message, null)); - Log.info("[WEB]" + name + ": " + message); - ChatEvent event = new ChatEvent("web", name, message); - events.trigger("webchat", event); - } } diff --git a/src/main/java/org/dynmap/InternalClientUpdateComponent.java b/src/main/java/org/dynmap/InternalClientUpdateComponent.java index d571aeef..b15953ec 100644 --- a/src/main/java/org/dynmap/InternalClientUpdateComponent.java +++ b/src/main/java/org/dynmap/InternalClientUpdateComponent.java @@ -1,12 +1,51 @@ package org.dynmap; +import org.dynmap.Event.Listener; import org.dynmap.web.handlers.ClientUpdateHandler; +import org.dynmap.web.handlers.SendMessageHandler; +import org.dynmap.web.handlers.SendMessageHandler.Message; +import org.json.simple.JSONObject; +import static org.dynmap.JSONUtils.*; public class InternalClientUpdateComponent extends ClientUpdateComponent { - public InternalClientUpdateComponent(DynmapPlugin plugin, ConfigurationNode configuration) { + public InternalClientUpdateComponent(DynmapPlugin plugin, final ConfigurationNode configuration) { super(plugin, configuration); - plugin.webServer.handlers.put("/up/", new ClientUpdateHandler(plugin)); - } + final Boolean allowwebchat = configuration.getBoolean("allowwebchat", false); + final float webchatInterval = configuration.getFloat("webchat-interval", 1); + final String spammessage = plugin.configuration.getString("spammessage", "You may only chat once every %interval% seconds."); + plugin.events.addListener("buildclientconfiguration", new Event.Listener() { + @Override + public void triggered(JSONObject t) { + s(t, "allowwebchat", allowwebchat); + s(t, "webchat-interval", webchatInterval); + } + }); + + plugin.webServer.handlers.put("/up/", new ClientUpdateHandler(plugin)); + + if (allowwebchat) { + SendMessageHandler messageHandler = new SendMessageHandler() {{ + maximumMessageInterval = (int)(webchatInterval * 1000); + spamMessage = "\""+spammessage+"\""; + onMessageReceived.addListener(new Listener() { + @Override + public void triggered(Message t) { + webChat(t.name, t.message); + } + }); + }}; + + plugin.webServer.handlers.put("/up/sendmessage", messageHandler); + } + } + + protected void webChat(String name, String message) { + // TODO: Change null to something meaningful. + plugin.mapManager.pushUpdate(new Client.ChatMessage("web", null, name, message, null)); + Log.info("[WEB]" + name + ": " + message); + ChatEvent event = new ChatEvent("web", name, message); + plugin.events.trigger("webchat", event); + } } diff --git a/src/main/java/org/dynmap/JsonFileClientUpdateComponent.java b/src/main/java/org/dynmap/JsonFileClientUpdateComponent.java index ef2d4d7f..55545efc 100644 --- a/src/main/java/org/dynmap/JsonFileClientUpdateComponent.java +++ b/src/main/java/org/dynmap/JsonFileClientUpdateComponent.java @@ -26,13 +26,16 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent { protected long jsonInterval; protected long lastTimestamp = 0; protected JSONParser parser = new JSONParser(); - public JsonFileClientUpdateComponent(final DynmapPlugin plugin, ConfigurationNode configuration) { + public JsonFileClientUpdateComponent(final DynmapPlugin plugin, final ConfigurationNode configuration) { super(plugin, configuration); - jsonInterval = (long)(configuration.getFloat("interval", 1) * 1000); + jsonInterval = (long)(configuration.getFloat("writeinterval", 1) * 1000); task = new TimerTask() { @Override public void run() { writeUpdates(); + if (configuration.getBoolean("allowwebchat", false)) { + handleWebChat(); + } } }; timer = new Timer(); @@ -132,10 +135,20 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent { while (iter.hasNext()) { JSONObject o = (JSONObject) iter.next(); if (Long.parseLong(String.valueOf(o.get("timestamp"))) >= (lastTimestamp)) { - plugin.webChat(String.valueOf(o.get("name")), String.valueOf(o.get("message"))); + String name = String.valueOf(o.get("name")); + String message = String.valueOf(o.get("message")); + webChat(name, message); } } } } } + + protected void webChat(String name, String message) { + // TODO: Change null to something meaningful. + plugin.mapManager.pushUpdate(new Client.ChatMessage("web", null, name, message, null)); + Log.info("[WEB]" + name + ": " + message); + ChatEvent event = new ChatEvent("web", name, message); + plugin.events.trigger("webchat", event); + } } diff --git a/src/main/java/org/dynmap/SimpleWebChatComponent.java b/src/main/java/org/dynmap/SimpleWebChatComponent.java index a8f7e145..2ea177d3 100644 --- a/src/main/java/org/dynmap/SimpleWebChatComponent.java +++ b/src/main/java/org/dynmap/SimpleWebChatComponent.java @@ -1,8 +1,17 @@ package org.dynmap; +import static org.dynmap.JSONUtils.s; + +import org.bukkit.event.player.PlayerChatEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerListener; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.plugin.PluginManager; +import org.json.simple.JSONObject; + public class SimpleWebChatComponent extends Component { - public SimpleWebChatComponent(final DynmapPlugin plugin, ConfigurationNode configuration) { + public SimpleWebChatComponent(final DynmapPlugin plugin, final ConfigurationNode configuration) { super(plugin, configuration); plugin.events.addListener("webchat", new Event.Listener() { @Override @@ -10,6 +19,40 @@ public class SimpleWebChatComponent extends Component { plugin.getServer().broadcastMessage("[WEB]" + t.name + ": " + t.message); } }); + + plugin.events.addListener("buildclientconfiguration", new Event.Listener() { + @Override + public void triggered(JSONObject t) { + s(t, "allowchat", configuration.getBoolean("allowchat", false)); + } + }); + + if (configuration.getBoolean("allowchat", false)) { + PlayerChatListener playerListener = new PlayerChatListener(); + PluginManager pm = plugin.getServer().getPluginManager(); + pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_CHAT, playerListener, org.bukkit.event.Event.Priority.Monitor, plugin); + pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_LOGIN, playerListener, org.bukkit.event.Event.Priority.Monitor, plugin); + pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_JOIN, playerListener, org.bukkit.event.Event.Priority.Monitor, plugin); + pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_QUIT, playerListener, org.bukkit.event.Event.Priority.Monitor, plugin); + } + } + + protected class PlayerChatListener extends PlayerListener { + @Override + public void onPlayerChat(PlayerChatEvent event) { + if(event.isCancelled()) return; + plugin.mapManager.pushUpdate(new Client.ChatMessage("player", "", event.getPlayer().getDisplayName(), event.getMessage(), event.getPlayer().getName())); + } + + @Override + public void onPlayerJoin(PlayerJoinEvent event) { + plugin.mapManager.pushUpdate(new Client.PlayerJoinMessage(event.getPlayer().getDisplayName(), event.getPlayer().getName())); + } + + @Override + public void onPlayerQuit(PlayerQuitEvent event) { + plugin.mapManager.pushUpdate(new Client.PlayerQuitMessage(event.getPlayer().getDisplayName(), event.getPlayer().getName())); + } } } diff --git a/src/main/java/org/dynmap/herochat/HeroChatHandler.java b/src/main/java/org/dynmap/herochat/HeroChatHandler.java index 8474221c..0b4d593f 100644 --- a/src/main/java/org/dynmap/herochat/HeroChatHandler.java +++ b/src/main/java/org/dynmap/herochat/HeroChatHandler.java @@ -15,7 +15,6 @@ import org.dynmap.Client; import org.dynmap.ConfigurationNode; import org.dynmap.DynmapPlugin; import org.dynmap.Log; -import org.dynmap.Client.ChatMessage; public class HeroChatHandler { private static final String DEF_CHANNEL = "Global"; diff --git a/src/main/java/org/dynmap/herochat/HeroWebChatComponent.java b/src/main/java/org/dynmap/herochat/HeroWebChatComponent.java index 071e44ef..ee53c8b2 100644 --- a/src/main/java/org/dynmap/herochat/HeroWebChatComponent.java +++ b/src/main/java/org/dynmap/herochat/HeroWebChatComponent.java @@ -1,10 +1,18 @@ package org.dynmap.herochat; +import static org.dynmap.JSONUtils.s; + +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerListener; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.plugin.PluginManager; import org.dynmap.ChatEvent; +import org.dynmap.Client; import org.dynmap.Component; import org.dynmap.ConfigurationNode; import org.dynmap.DynmapPlugin; import org.dynmap.Event; +import org.json.simple.JSONObject; public class HeroWebChatComponent extends Component { HeroChatHandler handler; @@ -20,6 +28,31 @@ public class HeroWebChatComponent extends Component { } } }); + + plugin.events.addListener("buildclientconfiguration", new Event.Listener() { + @Override + public void triggered(JSONObject t) { + s(t, "allowchat", true); + } + }); + + // Also make HeroChat announce joins and quits. + PlayerChatListener playerListener = new PlayerChatListener(); + PluginManager pm = plugin.getServer().getPluginManager(); + pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_LOGIN, playerListener, org.bukkit.event.Event.Priority.Monitor, plugin); + pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_JOIN, playerListener, org.bukkit.event.Event.Priority.Monitor, plugin); + pm.registerEvent(org.bukkit.event.Event.Type.PLAYER_QUIT, playerListener, org.bukkit.event.Event.Priority.Monitor, plugin); } + + protected class PlayerChatListener extends PlayerListener { + @Override + public void onPlayerJoin(PlayerJoinEvent event) { + plugin.mapManager.pushUpdate(new Client.PlayerJoinMessage(event.getPlayer().getDisplayName(), event.getPlayer().getName())); + } + @Override + public void onPlayerQuit(PlayerQuitEvent event) { + plugin.mapManager.pushUpdate(new Client.PlayerQuitMessage(event.getPlayer().getDisplayName(), event.getPlayer().getName())); + } + } }