Adding support for plugin packet broadcasting.

This commit is contained in:
KaneFreeman 2013-01-09 08:48:20 -05:00 committed by md_5
parent 08801f5d03
commit 91ac250d35
2 changed files with 72 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -70,6 +71,7 @@ public class BungeeCord
* Fully qualified connections. * Fully qualified connections.
*/ */
public Map<String, UserConnection> connections = new ConcurrentHashMap<>(); public Map<String, UserConnection> connections = new ConcurrentHashMap<>();
public Map<String, List<UserConnection>> connectionsByServer = new ConcurrentHashMap<>();
/** /**
* Registered commands. * 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("end", new CommandEnd());
commandMap.put("glist", new CommandList()); commandMap.put("glist", new CommandList());
commandMap.put("server", new CommandServer()); 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<UserConnection> 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<UserConnection> 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 * Register a plugin channel for all users
* *

View File

@ -116,6 +116,13 @@ public class UserConnection extends GenericConnection implements CommandSender
reconnecting = false; reconnecting = false;
downBridge = new DownstreamBridge(); downBridge = new DownstreamBridge();
server = newServer; server = newServer;
List<UserConnection> 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(); downBridge.start();
} catch (KickException ex) } catch (KickException ex)
{ {
@ -153,6 +160,14 @@ public class UserConnection extends GenericConnection implements CommandSender
if (BungeeCord.instance.isRunning) if (BungeeCord.instance.isRunning)
{ {
BungeeCord.instance.connections.remove(username); BungeeCord.instance.connections.remove(username);
if (server != null)
{
List<UserConnection> conns = BungeeCord.instance.connectionsByServer.get(server.name);
if (conns != null)
{
conns.remove(this);
}
}
} }
disconnect(reason); disconnect(reason);
if (server != null) if (server != null)