diff --git a/src/main/java/org/dynmap/ChatQueue.java b/src/main/java/org/dynmap/ChatQueue.java new file mode 100644 index 00000000..1cee4fda --- /dev/null +++ b/src/main/java/org/dynmap/ChatQueue.java @@ -0,0 +1,70 @@ +package org.dynmap; + +import java.util.ArrayList; +import java.util.LinkedList; + +import org.bukkit.event.player.PlayerChatEvent; + +public class ChatQueue { + + public class ChatMessage + { + public long time; + public String playerName; + public String message; + + public ChatMessage(PlayerChatEvent event) + { + time = System.currentTimeMillis(); + playerName = event.getPlayer().getName(); + message = event.getMessage(); + } + } + + /* a list of recent chat message */ + private LinkedList messageQueue; + + /* remember up to this old chat messages (ms) */ + private static final int maxChatAge = 120000; + + public ChatQueue() { + messageQueue = new LinkedList(); + } + + /* put a chat message in the queue */ + public void pushChatMessage(PlayerChatEvent event) + { + synchronized(MapManager.lock) { + messageQueue.add(new ChatMessage(event)); + } + } + + public ChatMessage[] getChatMessages(long cutoff) { + + ArrayList queue = new ArrayList(); + ArrayList updateList = new ArrayList(); + queue.addAll(messageQueue); + + long now = System.currentTimeMillis(); + long deadline = now - maxChatAge; + + synchronized(MapManager.lock) { + + for (ChatMessage message : queue) + { + if (message.time < deadline) + { + messageQueue.remove(message); + } + else if (message.time >= cutoff) + { + updateList.add(message); + } + } + } + ChatMessage[] messages = new ChatMessage[updateList.size()]; + updateList.toArray(messages); + return messages; + } + +} diff --git a/src/main/java/org/dynmap/DynmapPlayerListener.java b/src/main/java/org/dynmap/DynmapPlayerListener.java index 3942ba88..54d47913 100644 --- a/src/main/java/org/dynmap/DynmapPlayerListener.java +++ b/src/main/java/org/dynmap/DynmapPlayerListener.java @@ -38,4 +38,14 @@ public class DynmapPlayerListener extends PlayerListener { } } } + + /** + * Called when a player sends a chat message + * + * @param event Relevant event details + */ + public void onPlayerChat(PlayerChatEvent event) + { + mgr.addChatEvent(event); + } } \ No newline at end of file diff --git a/src/main/java/org/dynmap/DynmapPlugin.java b/src/main/java/org/dynmap/DynmapPlugin.java index f9f07212..ad79e18c 100644 --- a/src/main/java/org/dynmap/DynmapPlugin.java +++ b/src/main/java/org/dynmap/DynmapPlugin.java @@ -78,6 +78,7 @@ public class DynmapPlugin extends JavaPlugin { getServer().getPluginManager().registerEvent(Event.Type.BLOCK_DAMAGED, blockListener, Priority.Normal, this); getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, new DynmapPlayerListener(mapManager, playerList), Priority.Normal, this); + getServer().getPluginManager().registerEvent(Event.Type.PLAYER_CHAT, new DynmapPlayerListener(mapManager, playerList), Priority.Normal, this); //getServer().getPluginManager().registerEvent(Event.Type.BLOCK_DESTROYED, listener, Priority.Normal, this); /* etc.getLoader().addListener(PluginLoader.Hook.COMMAND, listener, this, PluginListener.Priority.MEDIUM); etc.getLoader().addListener(PluginLoader.Hook.BLOCK_CREATED, listener, this, PluginListener.Priority.MEDIUM); diff --git a/src/main/java/org/dynmap/MapManager.java b/src/main/java/org/dynmap/MapManager.java index 90a0c7d0..bb5545ed 100644 --- a/src/main/java/org/dynmap/MapManager.java +++ b/src/main/java/org/dynmap/MapManager.java @@ -5,6 +5,7 @@ import java.util.logging.Logger; import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.util.config.ConfigurationNode; import org.dynmap.debug.Debugger; import org.dynmap.kzedmap.KzedMap; @@ -16,6 +17,7 @@ public class MapManager extends Thread { private Debugger debugger; private MapType map; public StaleQueue staleQueue; + public ChatQueue chatQueue; public PlayerList playerList; /* lock for our data structures */ @@ -56,6 +58,7 @@ public class MapManager extends Thread { this.world = world; this.debugger = debugger; this.staleQueue = new StaleQueue(); + this.chatQueue = new ChatQueue(); tileDirectory = combinePaths(DynmapPlugin.dataRoot, configuration.getString("tilespath", "web/tiles")); webDirectory = combinePaths(DynmapPlugin.dataRoot, configuration.getString("webpath", "web")); @@ -146,4 +149,9 @@ public class MapManager extends Thread { debugger.debug("invalidating tile " + tile.getName()); staleQueue.pushStaleTile(tile); } + + public void addChatEvent(PlayerChatEvent event) + { + chatQueue.pushChatMessage(event); + } } diff --git a/src/main/java/org/dynmap/WebServerRequest.java b/src/main/java/org/dynmap/WebServerRequest.java index 0d8bf962..2c072cd6 100644 --- a/src/main/java/org/dynmap/WebServerRequest.java +++ b/src/main/java/org/dynmap/WebServerRequest.java @@ -123,8 +123,13 @@ public class WebServerRequest extends Thread { for(TileUpdate tu : tileUpdates) { sb.append("tile " + tu.tile.getName() + "\n"); } + + ChatQueue.ChatMessage[] messages = mgr.chatQueue.getChatMessages(cutoff); + for(ChatQueue.ChatMessage cu : messages) { + sb.append("chat " + cu.playerName + " " + cu.message + "\n"); + } - debugger.debug("Sending " + players.length + " players and " + tileUpdates.length + " tile-updates. " + path + ";" + cutoff); + debugger.debug("Sending " + players.length + " players, " + tileUpdates.length + " tile-updates, and " + messages.length + " chats. "+ path + ";" + cutoff); byte[] bytes = sb.toString().getBytes(); diff --git a/web/map.js b/web/map.js index 1d7aeca5..cb8e2f53 100644 --- a/web/map.js +++ b/web/map.js @@ -210,7 +210,21 @@ DynMap.prototype = { tile: function() { me.onTileUpdated(row.name); } - }, function() { + , chat: function() { + var chats = line.split(' '); + var message = ''; + for (var chatIndex = 2; chatIndex < chats.length; chatIndex++) + { + if (chatIndex > 2) message = message + " "; + message = message + chats[chatIndex]; + } + if (message.length > 0) + { + me.onPlayerChat(row.name, message); + } + } + }, + function() { var mi = { id: row.type + '_' + row.name, text: row.name, @@ -244,6 +258,20 @@ DynMap.prototype = { } }); }, + onPlayerChat: function(playerName, message) { + var me = this; + var markers = me.markers; + var map = me.map; + var mid = "player_" + playerName; + var playerMarker = markers[mid]; + if (playerMarker) + { + var infowindow = new google.maps.InfoWindow({ + content: message + }); + infowindow.open(map,playerMarker); + } + }, onTileUpdated: function(tileName) { var me = this; var tile = this.registeredTiles[tileName];