This commit is contained in:
FrozenCow 2011-01-25 23:06:48 +01:00
commit 36d1a7676e
6 changed files with 124 additions and 2 deletions

View File

@ -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<ChatMessage> messageQueue;
/* remember up to this old chat messages (ms) */
private static final int maxChatAge = 120000;
public ChatQueue() {
messageQueue = new LinkedList<ChatMessage>();
}
/* 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<ChatMessage> queue = new ArrayList<ChatMessage>();
ArrayList<ChatMessage> updateList = new ArrayList<ChatMessage>();
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;
}
}

View File

@ -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);
}
} }

View File

@ -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.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_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); //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.COMMAND, listener, this, PluginListener.Priority.MEDIUM);
etc.getLoader().addListener(PluginLoader.Hook.BLOCK_CREATED, listener, this, PluginListener.Priority.MEDIUM); etc.getLoader().addListener(PluginLoader.Hook.BLOCK_CREATED, listener, this, PluginListener.Priority.MEDIUM);

View File

@ -5,6 +5,7 @@ import java.util.logging.Logger;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.util.config.ConfigurationNode; import org.bukkit.util.config.ConfigurationNode;
import org.dynmap.debug.Debugger; import org.dynmap.debug.Debugger;
import org.dynmap.kzedmap.KzedMap; import org.dynmap.kzedmap.KzedMap;
@ -16,6 +17,7 @@ public class MapManager extends Thread {
private Debugger debugger; private Debugger debugger;
private MapType map; private MapType map;
public StaleQueue staleQueue; public StaleQueue staleQueue;
public ChatQueue chatQueue;
public PlayerList playerList; public PlayerList playerList;
/* lock for our data structures */ /* lock for our data structures */
@ -56,6 +58,7 @@ public class MapManager extends Thread {
this.world = world; this.world = world;
this.debugger = debugger; this.debugger = debugger;
this.staleQueue = new StaleQueue(); this.staleQueue = new StaleQueue();
this.chatQueue = new ChatQueue();
tileDirectory = combinePaths(DynmapPlugin.dataRoot, configuration.getString("tilespath", "web/tiles")); tileDirectory = combinePaths(DynmapPlugin.dataRoot, configuration.getString("tilespath", "web/tiles"));
webDirectory = combinePaths(DynmapPlugin.dataRoot, configuration.getString("webpath", "web")); webDirectory = combinePaths(DynmapPlugin.dataRoot, configuration.getString("webpath", "web"));
@ -146,4 +149,9 @@ public class MapManager extends Thread {
debugger.debug("invalidating tile " + tile.getName()); debugger.debug("invalidating tile " + tile.getName());
staleQueue.pushStaleTile(tile); staleQueue.pushStaleTile(tile);
} }
public void addChatEvent(PlayerChatEvent event)
{
chatQueue.pushChatMessage(event);
}
} }

View File

@ -123,8 +123,13 @@ public class WebServerRequest extends Thread {
for(TileUpdate tu : tileUpdates) { for(TileUpdate tu : tileUpdates) {
sb.append("tile " + tu.tile.getName() + "\n"); 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(); byte[] bytes = sb.toString().getBytes();

View File

@ -210,7 +210,21 @@ DynMap.prototype = {
tile: function() { tile: function() {
me.onTileUpdated(row.name); 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 = { var mi = {
id: row.type + '_' + row.name, id: row.type + '_' + row.name,
text: 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) { onTileUpdated: function(tileName) {
var me = this; var me = this;
var tile = this.registeredTiles[tileName]; var tile = this.registeredTiles[tileName];