From 06785be5f33f746f6b9d394cfb6932cbcf60d907 Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Sat, 21 May 2011 23:58:48 -0500 Subject: [PATCH] Fixes for alias/nicknames in web UI (esp handling player icons), HeroChat fixes --- src/main/java/org/dynmap/Client.java | 15 +++++++++---- .../org/dynmap/DynmapPlayerChatListener.java | 8 ++++--- src/main/java/org/dynmap/DynmapPlugin.java | 2 +- src/main/java/org/dynmap/HeroChatHandler.java | 22 ++++++++++++++++--- src/main/java/org/dynmap/web/HttpRequest.java | 2 ++ .../org/dynmap/web/HttpServerConnection.java | 4 +++- .../web/handlers/SendMessageHandler.java | 3 ++- web/js/chat.js | 3 ++- web/js/chatbox.js | 15 ++++++++++--- 9 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/dynmap/Client.java b/src/main/java/org/dynmap/Client.java index 78d7de31..4a82ec08 100644 --- a/src/main/java/org/dynmap/Client.java +++ b/src/main/java/org/dynmap/Client.java @@ -40,27 +40,34 @@ public class Client { public String source; public String playerName; public String message; - - public ChatMessage(String source, String playerName, String message) { + public String account; + public String channel; + public ChatMessage(String source, String channel, String playerName, String message, String playeraccount) { this.source = source; this.playerName = ChatColor.stripColor(playerName); this.message = ChatColor.stripColor(message); + this.account = playeraccount; + this.channel = channel; } } public static class PlayerJoinMessage extends Stamped { public String type = "playerjoin"; public String playerName; - public PlayerJoinMessage(String playerName) { + public String account; + public PlayerJoinMessage(String playerName, String playeraccount) { this.playerName = ChatColor.stripColor(playerName); + this.account = playeraccount; } } public static class PlayerQuitMessage extends Stamped { public String type = "playerquit"; public String playerName; - public PlayerQuitMessage(String playerName) { + public String account; + public PlayerQuitMessage(String playerName, String playeraccount) { this.playerName = ChatColor.stripColor(playerName); + this.account = playeraccount; } } diff --git a/src/main/java/org/dynmap/DynmapPlayerChatListener.java b/src/main/java/org/dynmap/DynmapPlayerChatListener.java index 51a03d28..64d3fcb4 100644 --- a/src/main/java/org/dynmap/DynmapPlayerChatListener.java +++ b/src/main/java/org/dynmap/DynmapPlayerChatListener.java @@ -15,17 +15,19 @@ public class DynmapPlayerChatListener extends PlayerListener { @Override public void onPlayerChat(PlayerChatEvent event) { if(event.isCancelled()) return; - plugin.mapManager.pushUpdate(new Client.ChatMessage("player", event.getPlayer().getDisplayName(), event.getMessage())); + 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())); + 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())); + plugin.mapManager.pushUpdate(new Client.PlayerQuitMessage(event.getPlayer().getDisplayName(), event.getPlayer().getName())); } } diff --git a/src/main/java/org/dynmap/DynmapPlugin.java b/src/main/java/org/dynmap/DynmapPlugin.java index c95cb24f..a723de4e 100644 --- a/src/main/java/org/dynmap/DynmapPlugin.java +++ b/src/main/java/org/dynmap/DynmapPlugin.java @@ -411,7 +411,7 @@ public class DynmapPlugin extends JavaPlugin { } public void webChat(String name, String message) { - mapManager.pushUpdate(new Client.ChatMessage("web", name, message)); + mapManager.pushUpdate(new Client.ChatMessage("web", null, name, message, null)); Log.info("[WEB]" + name + ": " + message); /* Let HeroChat take a look - only broadcast to players if it doesn't handle it */ if(hchand.sendWebMessageToHeroChat(name, message) == false) diff --git a/src/main/java/org/dynmap/HeroChatHandler.java b/src/main/java/org/dynmap/HeroChatHandler.java index bd42cc85..1ca39a72 100644 --- a/src/main/java/org/dynmap/HeroChatHandler.java +++ b/src/main/java/org/dynmap/HeroChatHandler.java @@ -38,6 +38,7 @@ public class HeroChatHandler { private static Class channelchatevent; private static Method getsource; private static Method getmessage; + private static Method issentbyplayer; private static boolean isgood = false; private Event evt; @@ -48,6 +49,7 @@ public class HeroChatHandler { .forName("com.herocraftonline.dthielke.herochat.event.ChannelChatEvent"); getsource = channelchatevent.getMethod("getSource", new Class[0]); getmessage = channelchatevent.getMethod("getMessage", new Class[0]); + issentbyplayer = channelchatevent.getMethod("isSentByPlayer", new Class[0]); isgood = true; } catch (ClassNotFoundException cnfx) { } catch (NoSuchMethodException nsmx) { @@ -78,6 +80,14 @@ public class HeroChatHandler { return null; } } + + public boolean isSentByPlayer() { + try { + return (Boolean) issentbyplayer.invoke(evt); + } catch (Exception x) { + return true; + } + } } /* Reflection-based access wrapper for ChannelEvent from HeroChat */ @@ -213,9 +223,15 @@ public class HeroChatHandler { /* Match on name or nickname of channel */ if (hcchannels.contains(c.getName()) || hcchannels.contains(c.getNick())) { - plugin.mapManager.pushUpdate(new Client.ChatMessage( - "player", "[" + c.getNick() + "] " - + cce.getSource(), cce.getMessage())); + if(cce.isSentByPlayer()) { /* Player message? */ + org.bukkit.entity.Player p = plugin.getServer().getPlayer(cce.getSource()); + if(p != null) + plugin.mapManager.pushUpdate(new Client.ChatMessage("player", + c.getNick(), + p.getDisplayName(), + cce.getMessage(), + p.getName())); + } } } } diff --git a/src/main/java/org/dynmap/web/HttpRequest.java b/src/main/java/org/dynmap/web/HttpRequest.java index a2cb2746..dc8db7bb 100644 --- a/src/main/java/org/dynmap/web/HttpRequest.java +++ b/src/main/java/org/dynmap/web/HttpRequest.java @@ -3,6 +3,7 @@ package org.dynmap.web; import java.io.InputStream; import java.util.HashMap; import java.util.Map; +import java.net.InetSocketAddress; public class HttpRequest { public String method; @@ -10,4 +11,5 @@ public class HttpRequest { public String version; public Map fields = new HashMap(); public InputStream body; + public InetSocketAddress rmtaddr; } diff --git a/src/main/java/org/dynmap/web/HttpServerConnection.java b/src/main/java/org/dynmap/web/HttpServerConnection.java index e6d4eee5..0a87cb18 100644 --- a/src/main/java/org/dynmap/web/HttpServerConnection.java +++ b/src/main/java/org/dynmap/web/HttpServerConnection.java @@ -15,6 +15,7 @@ import java.util.regex.Pattern; import org.dynmap.Log; import org.dynmap.debug.Debug; +import java.net.InetSocketAddress; public class HttpServerConnection extends Thread { protected static final Logger log = Logger.getLogger("Minecraft"); @@ -120,13 +121,14 @@ public class HttpServerConnection extends Thread { if (socket == null) return; socket.setSoTimeout(5000); + InetSocketAddress rmtaddr = (InetSocketAddress)socket.getRemoteSocketAddress(); /* Get remote address */ InputStream in = socket.getInputStream(); BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream(), 40960); printOut = new PrintStream(out, false); while (true) { HttpRequest request = new HttpRequest(); - + request.rmtaddr = rmtaddr; if (!readRequestHeader(in, request)) { socket.close(); return; diff --git a/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java b/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java index 2f5ab721..2bf08925 100644 --- a/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java +++ b/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java @@ -40,7 +40,8 @@ public class SendMessageHandler implements HttpHandler { JSONObject o = (JSONObject)parser.parse(reader); final Message message = new Message(); - message.name = String.valueOf(o.get("name")); + //message.name = String.valueOf(o.get("name")); //Can't trust client....we don't need to on internal web server + message.name = request.rmtaddr.getAddress().getHostAddress(); message.message = String.valueOf(o.get("message")); final long now = System.currentTimeMillis(); diff --git a/web/js/chat.js b/web/js/chat.js index 68421ee3..8e29bc00 100644 --- a/web/js/chat.js +++ b/web/js/chat.js @@ -12,7 +12,8 @@ componentconstructors['chat'] = function(dynmap, configuration) { $(dynmap).bind('worldupdate', function(event, update) { swtch(update.type, { chat: function() { - $(dynmap).trigger('chat', [{source: update.source, name: update.playerName, text: update.message}]); + $(dynmap).trigger('chat', [{source: update.source, name: update.playerName, text: update.message, account: update.account, + channel: update.channel}]); } }); }); diff --git a/web/js/chatbox.js b/web/js/chatbox.js index 67ecdfd2..7d032533 100644 --- a/web/js/chatbox.js +++ b/web/js/chatbox.js @@ -50,20 +50,29 @@ componentconstructors['chatbox'] = function(dynmap, configuration) { $(dynmap).bind('chat', function(event, message) { var playerName = message.name; + var playerAccount = message.account; var messageRow = $('
') .addClass('messagerow'); var playerIconContainer = $('') .addClass('messageicon'); - if (message.source === 'player' && configuration.showplayerfaces) { - getMinecraftHead(playerName, 16, function(head) { + if (message.source === 'player' && configuration.showplayerfaces && + playerAccount) { + getMinecraftHead(playerAccount, 16, function(head) { messageRow.icon = $(head) .addClass('playerIcon') .appendTo(playerIconContainer); }); } + var playerChannelContainer = ''; + if (message.channel) { + playerChannelContainer = $('').addClass('messagetext') + .text('[' + message.channel + '] ') + .appendTo(messageRow); + } + if (message.source === 'player' && configuration.showworld) { var playerWorldContainer = $('') .addClass('messagetext') @@ -79,7 +88,7 @@ componentconstructors['chatbox'] = function(dynmap, configuration) { .addClass('messagetext') .text(message.text); - messageRow.append(playerIconContainer,playerNameContainer,playerMessageContainer); + messageRow.append(playerIconContainer,playerChannelContainer,playerNameContainer,playerMessageContainer); addrow(messageRow); }); }; \ No newline at end of file