From 52455d3249c59fcad7e720b5754ef9cad4e7efd8 Mon Sep 17 00:00:00 2001 From: Doridian Draconia Date: Mon, 10 Dec 2012 20:54:24 +0100 Subject: [PATCH] Added ability to intercept all chat and to set TabList names --- src/main/java/net/md_5/bungee/BungeeCord.java | 15 ++----- .../net/md_5/bungee/GenericConnection.java | 1 + .../net/md_5/bungee/ServerConnection.java | 5 --- .../java/net/md_5/bungee/UserConnection.java | 40 ++++++++++++++----- .../net/md_5/bungee/plugin/ChatEvent.java | 36 +++++++++++++++++ .../net/md_5/bungee/plugin/JavaPlugin.java | 9 ++++- .../md_5/bungee/plugin/JavaPluginManager.java | 9 +++++ .../bungee/plugin/PluginMessageEvent.java | 2 + .../bungee/tablist/GlobalPingTabList.java | 3 +- .../md_5/bungee/tablist/GlobalTabList.java | 7 ++-- .../bungee/tablist/ServerUniqueTabList.java | 1 - 11 files changed, 93 insertions(+), 35 deletions(-) create mode 100644 src/main/java/net/md_5/bungee/plugin/ChatEvent.java diff --git a/src/main/java/net/md_5/bungee/BungeeCord.java b/src/main/java/net/md_5/bungee/BungeeCord.java index c9d32d62..8a8b79c9 100644 --- a/src/main/java/net/md_5/bungee/BungeeCord.java +++ b/src/main/java/net/md_5/bungee/BungeeCord.java @@ -15,15 +15,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.logging.Level; import static net.md_5.bungee.Logger.$; -import net.md_5.bungee.command.Command; -import net.md_5.bungee.command.CommandAlert; -import net.md_5.bungee.command.CommandEnd; -import net.md_5.bungee.command.CommandIP; -import net.md_5.bungee.command.CommandList; -import net.md_5.bungee.command.CommandMotd; -import net.md_5.bungee.command.CommandSender; -import net.md_5.bungee.command.CommandServer; -import net.md_5.bungee.command.ConsoleCommandSender; +import net.md_5.bungee.command.*; import net.md_5.bungee.packet.DefinedPacket; import net.md_5.bungee.packet.PacketFAPluginMessage; import net.md_5.bungee.plugin.JavaPluginManager; @@ -37,10 +29,11 @@ import net.md_5.bungee.tablist.TabListHandler; */ public class BungeeCord { + /** * Server protocol version. */ - public static final int PROTOCOL_VERSION= 49; + public static final int PROTOCOL_VERSION = 49; /** * Server game version. */ @@ -275,7 +268,7 @@ public class BungeeCord /** * Register a plugin channel for all users - * + * * @param channel name */ public void registerPluginChannel(String channel) diff --git a/src/main/java/net/md_5/bungee/GenericConnection.java b/src/main/java/net/md_5/bungee/GenericConnection.java index d5f70332..b06fa2d2 100644 --- a/src/main/java/net/md_5/bungee/GenericConnection.java +++ b/src/main/java/net/md_5/bungee/GenericConnection.java @@ -21,6 +21,7 @@ public class GenericConnection protected final PacketInputStream in; protected final OutputStream out; public String username; + public String tabListName; /** * Close the socket with the specified reason. diff --git a/src/main/java/net/md_5/bungee/ServerConnection.java b/src/main/java/net/md_5/bungee/ServerConnection.java index ae1b05b8..2af817ed 100644 --- a/src/main/java/net/md_5/bungee/ServerConnection.java +++ b/src/main/java/net/md_5/bungee/ServerConnection.java @@ -3,21 +3,16 @@ package net.md_5.bungee; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; -import java.security.PublicKey; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; -import javax.crypto.SecretKey; import net.md_5.bungee.packet.DefinedPacket; import net.md_5.bungee.packet.Packet1Login; import net.md_5.bungee.packet.Packet2Handshake; import net.md_5.bungee.packet.PacketCDClientStatus; import net.md_5.bungee.packet.PacketFAPluginMessage; -import net.md_5.bungee.packet.PacketFCEncryptionResponse; import net.md_5.bungee.packet.PacketFDEncryptionRequest; import net.md_5.bungee.packet.PacketFFKick; import net.md_5.bungee.packet.PacketInputStream; -import org.bouncycastle.crypto.io.CipherInputStream; -import org.bouncycastle.crypto.io.CipherOutputStream; /** * Class representing a connection from the proxy to the server; ie upstream. diff --git a/src/main/java/net/md_5/bungee/UserConnection.java b/src/main/java/net/md_5/bungee/UserConnection.java index 2d653874..a134fbaa 100644 --- a/src/main/java/net/md_5/bungee/UserConnection.java +++ b/src/main/java/net/md_5/bungee/UserConnection.java @@ -10,18 +10,11 @@ import java.util.List; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import net.md_5.bungee.command.CommandSender; -import net.md_5.bungee.packet.DefinedPacket; -import net.md_5.bungee.packet.Packet0KeepAlive; -import net.md_5.bungee.packet.Packet1Login; -import net.md_5.bungee.packet.Packet2Handshake; -import net.md_5.bungee.packet.Packet3Chat; -import net.md_5.bungee.packet.Packet9Respawn; -import net.md_5.bungee.packet.PacketC9PlayerListItem; -import net.md_5.bungee.packet.PacketFAPluginMessage; -import net.md_5.bungee.packet.PacketInputStream; -import net.md_5.bungee.plugin.ServerConnectEvent; +import net.md_5.bungee.packet.*; +import net.md_5.bungee.plugin.ChatEvent; import net.md_5.bungee.plugin.PluginMessageEvent; import net.md_5.bungee.plugin.PluginMessageEvent.Destination; +import net.md_5.bungee.plugin.ServerConnectEvent; public class UserConnection extends GenericConnection implements CommandSender { @@ -47,11 +40,19 @@ public class UserConnection extends GenericConnection implements CommandSender super(socket, in, out); this.handshake = handshake; username = handshake.username; + tabListName = handshake.username; this.loginPackets = loginPackets; BungeeCord.instance.connections.put(username, this); BungeeCord.instance.tabListHandler.onJoin(this); } + public void setTabListName(String newName) + { + BungeeCord.instance.tabListHandler.onDisconnect(this); + tabListName = newName; + BungeeCord.instance.tabListHandler.onJoin(this); + } + public void connect(String server) { ServerConnectEvent event = new ServerConnectEvent(this.server == null, this, server); @@ -171,7 +172,7 @@ public class UserConnection extends GenericConnection implements CommandSender public void sendPluginMessage(String tag, byte[] data) { - server.packetQueue.add(new PacketFAPluginMessage(tag, data)); + server.packetQueue.add(new PacketFAPluginMessage(tag, data)); } @Override @@ -219,6 +220,12 @@ public class UserConnection extends GenericConnection implements CommandSender if (message.startsWith("/")) { sendPacket = !BungeeCord.instance.dispatchCommand(message.substring(1), UserConnection.this); + } else + { + ChatEvent chatEvent = new ChatEvent(ChatEvent.Destination.SERVER, instance); + chatEvent.setText(message); + BungeeCord.instance.pluginManager.onChat(chatEvent); + sendPacket = !chatEvent.isCancelled(); } } else if (id == 0x00) { @@ -304,6 +311,17 @@ public class UserConnection extends GenericConnection implements CommandSender { trackingPingId = new Packet0KeepAlive(packet).id; pingTime = System.currentTimeMillis(); + } else if (id == 0x03) + { + Packet3Chat chat = new Packet3Chat(packet); + String message = chat.message; + ChatEvent chatEvent = new ChatEvent(ChatEvent.Destination.CLIENT, instance); + chatEvent.setText(message); + BungeeCord.instance.pluginManager.onChat(chatEvent); + if (chatEvent.isCancelled()) + { + continue; + } } else if (id == 0xC9) { if (!BungeeCord.instance.tabListHandler.onPacketC9(UserConnection.this, new PacketC9PlayerListItem(packet))) diff --git a/src/main/java/net/md_5/bungee/plugin/ChatEvent.java b/src/main/java/net/md_5/bungee/plugin/ChatEvent.java new file mode 100644 index 00000000..76fab296 --- /dev/null +++ b/src/main/java/net/md_5/bungee/plugin/ChatEvent.java @@ -0,0 +1,36 @@ +package net.md_5.bungee.plugin; + +import lombok.Data; +import net.md_5.bungee.UserConnection; + +@Data +public class ChatEvent implements Cancellable +{ + + /** + * Canceled state. + */ + private boolean cancelled; + /** + * Whether this packet is destined for the server or the client. + */ + private final Destination destination; + /** + * User in question. + */ + private final UserConnection connection; + /** + * Text contained in this chat. + */ + private String text; + + /** + * An enum that signifies the destination for this packet. + */ + public enum Destination + { + + SERVER, + CLIENT + } +} diff --git a/src/main/java/net/md_5/bungee/plugin/JavaPlugin.java b/src/main/java/net/md_5/bungee/plugin/JavaPlugin.java index f24b6f1c..74bbe182 100644 --- a/src/main/java/net/md_5/bungee/plugin/JavaPlugin.java +++ b/src/main/java/net/md_5/bungee/plugin/JavaPlugin.java @@ -57,7 +57,14 @@ public abstract class JavaPlugin public void onPluginMessage(PluginMessageEvent event) { } - + + /** + * Called when a chat message is sent to the client or server + */ + public void onChat(ChatEvent event) + { + } + /** * Register a command for use with the proxy. */ diff --git a/src/main/java/net/md_5/bungee/plugin/JavaPluginManager.java b/src/main/java/net/md_5/bungee/plugin/JavaPluginManager.java index b21dd041..1a695cc9 100644 --- a/src/main/java/net/md_5/bungee/plugin/JavaPluginManager.java +++ b/src/main/java/net/md_5/bungee/plugin/JavaPluginManager.java @@ -115,4 +115,13 @@ public class JavaPluginManager extends JavaPlugin p.onPluginMessage(event); } } + + @Override + public void onChat(ChatEvent event) + { + for (JavaPlugin p : plugins) + { + p.onChat(event); + } + } } diff --git a/src/main/java/net/md_5/bungee/plugin/PluginMessageEvent.java b/src/main/java/net/md_5/bungee/plugin/PluginMessageEvent.java index 92d40d90..ad74ebdd 100644 --- a/src/main/java/net/md_5/bungee/plugin/PluginMessageEvent.java +++ b/src/main/java/net/md_5/bungee/plugin/PluginMessageEvent.java @@ -9,6 +9,7 @@ import net.md_5.bungee.UserConnection; @Data public class PluginMessageEvent implements Cancellable { + /** * Canceled state. */ @@ -39,6 +40,7 @@ public class PluginMessageEvent implements Cancellable */ public enum Destination { + SERVER, CLIENT } diff --git a/src/main/java/net/md_5/bungee/tablist/GlobalPingTabList.java b/src/main/java/net/md_5/bungee/tablist/GlobalPingTabList.java index 8696da46..c8b39b5b 100644 --- a/src/main/java/net/md_5/bungee/tablist/GlobalPingTabList.java +++ b/src/main/java/net/md_5/bungee/tablist/GlobalPingTabList.java @@ -3,7 +3,6 @@ package net.md_5.bungee.tablist; import java.util.Collections; import java.util.Map; import java.util.WeakHashMap; - import net.md_5.bungee.BungeeCord; import net.md_5.bungee.UserConnection; import net.md_5.bungee.packet.PacketC9PlayerListItem; @@ -20,7 +19,7 @@ public class GlobalPingTabList extends GlobalTabList Integer lastPing = lastPings.get(con); if (lastPing == null || (ping - PING_THRESHOLD > lastPing && ping + PING_THRESHOLD < lastPing)) { - BungeeCord.instance.broadcast(new PacketC9PlayerListItem(con.username, true, ping)); + BungeeCord.instance.broadcast(new PacketC9PlayerListItem(con.tabListName, true, ping)); lastPings.put(con, ping); } } diff --git a/src/main/java/net/md_5/bungee/tablist/GlobalTabList.java b/src/main/java/net/md_5/bungee/tablist/GlobalTabList.java index 45062f41..f97fd0e8 100644 --- a/src/main/java/net/md_5/bungee/tablist/GlobalTabList.java +++ b/src/main/java/net/md_5/bungee/tablist/GlobalTabList.java @@ -3,7 +3,6 @@ package net.md_5.bungee.tablist; import java.util.Collections; import java.util.HashSet; import java.util.Set; - import net.md_5.bungee.BungeeCord; import net.md_5.bungee.UserConnection; import net.md_5.bungee.packet.PacketC9PlayerListItem; @@ -18,7 +17,7 @@ public class GlobalTabList implements TabListHandler { for (UserConnection c : BungeeCord.instance.connections.values()) { - con.packetQueue.add(new PacketC9PlayerListItem(c.username, true, c.getPing())); + con.packetQueue.add(new PacketC9PlayerListItem(c.tabListName, true, c.getPing())); } } @@ -32,7 +31,7 @@ public class GlobalTabList implements TabListHandler { if (!sentPings.contains(con)) { - BungeeCord.instance.broadcast(new PacketC9PlayerListItem(con.username, true, con.getPing())); + BungeeCord.instance.broadcast(new PacketC9PlayerListItem(con.tabListName, true, con.getPing())); sentPings.add(con); } } @@ -40,7 +39,7 @@ public class GlobalTabList implements TabListHandler @Override public void onDisconnect(final UserConnection con) { - BungeeCord.instance.broadcast(new PacketC9PlayerListItem(con.username, false, 9999)); + BungeeCord.instance.broadcast(new PacketC9PlayerListItem(con.tabListName, false, 9999)); sentPings.remove(con); } diff --git a/src/main/java/net/md_5/bungee/tablist/ServerUniqueTabList.java b/src/main/java/net/md_5/bungee/tablist/ServerUniqueTabList.java index 48c747fe..e17f7ce4 100644 --- a/src/main/java/net/md_5/bungee/tablist/ServerUniqueTabList.java +++ b/src/main/java/net/md_5/bungee/tablist/ServerUniqueTabList.java @@ -5,7 +5,6 @@ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import java.util.WeakHashMap; - import net.md_5.bungee.UserConnection; import net.md_5.bungee.packet.PacketC9PlayerListItem;