diff --git a/src/main/java/net/md_5/bungee/BungeeCord.java b/src/main/java/net/md_5/bungee/BungeeCord.java index bcb93bfc..b1f29aca 100644 --- a/src/main/java/net/md_5/bungee/BungeeCord.java +++ b/src/main/java/net/md_5/bungee/BungeeCord.java @@ -7,6 +7,7 @@ import java.net.InetSocketAddress; import java.net.Socket; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Queue; import java.util.concurrent.ConcurrentHashMap; @@ -70,6 +71,7 @@ public class BungeeCord * Fully qualified connections. */ public Map connections = new ConcurrentHashMap<>(); + public Map> connectionsByServer = new ConcurrentHashMap<>(); /** * Registered commands. */ @@ -89,7 +91,7 @@ public class BungeeCord { - commandMap.put("greload",new CommandReload()); + commandMap.put("greload", new CommandReload()); commandMap.put("end", new CommandEnd()); commandMap.put("glist", new CommandList()); commandMap.put("server", new CommandServer()); @@ -270,6 +272,60 @@ public class BungeeCord } } + /** + * Broadcasts a plugin message to all servers with currently connected + * players. + * + * @param channel name + * @param message to send + */ + public void broadcastPluginMessage(String channel, String message) + { + broadcastPluginMessage(channel, message, null); + } + + /** + * Broadcasts a plugin message to all servers with currently connected + * players. + * + * @param channel name + * @param message to send + * @param server the message was sent from originally + */ + public void broadcastPluginMessage(String channel, String message, String sourceServer) + { + for (String server : connectionsByServer.keySet()) + { + if (sourceServer == null || !sourceServer.equals(server)) + { + List conns = BungeeCord.instance.connectionsByServer.get(server); + if (conns != null && conns.size() > 0) + { + UserConnection user = conns.get(0); + user.sendPluginMessage(channel, message.getBytes()); + } + } + } + } + + /** + * Send a plugin message to a specific server if it has currently connected + * players. + * + * @param channel name + * @param message to send + * @param server the message is to be sent to + */ + public void sendPluginMessage(String channel, String message, String targetServer) + { + List conns = BungeeCord.instance.connectionsByServer.get(targetServer); + if (conns != null && conns.size() > 0) + { + UserConnection user = conns.get(0); + user.sendPluginMessage(channel, message.getBytes()); + } + } + /** * Register a plugin channel for all users * diff --git a/src/main/java/net/md_5/bungee/UserConnection.java b/src/main/java/net/md_5/bungee/UserConnection.java index e6177578..dfe23c3e 100644 --- a/src/main/java/net/md_5/bungee/UserConnection.java +++ b/src/main/java/net/md_5/bungee/UserConnection.java @@ -116,6 +116,13 @@ public class UserConnection extends GenericConnection implements CommandSender reconnecting = false; downBridge = new DownstreamBridge(); server = newServer; + List conns = BungeeCord.instance.connectionsByServer.get(server.name); + if (conns == null) + { + conns = new ArrayList<>(); + BungeeCord.instance.connectionsByServer.put(server.name, conns); + } + conns.add(this); downBridge.start(); } catch (KickException ex) { @@ -153,6 +160,14 @@ public class UserConnection extends GenericConnection implements CommandSender if (BungeeCord.instance.isRunning) { BungeeCord.instance.connections.remove(username); + if (server != null) + { + List conns = BungeeCord.instance.connectionsByServer.get(server.name); + if (conns != null) + { + conns.remove(this); + } + } } disconnect(reason); if (server != null)