More work

This commit is contained in:
md_5 2013-01-17 12:37:02 +11:00
parent 592a504e77
commit 55867dbdc3
8 changed files with 148 additions and 166 deletions

View File

@ -98,6 +98,21 @@ public abstract class ProxyServer
*/ */
public abstract void setConfigurationAdapter(ConfigurationAdapter adapter); public abstract void setConfigurationAdapter(ConfigurationAdapter adapter);
/**
* Get the currently in use tab list handle.
*
* @return the tab list handler
*/
public abstract TabListHandler getTabListHandler();
/**
* Set the used tab list handler, should not be changed once players have
* connected
*
* @param handler the tab list handler to set
*/
public abstract void setTabListHandler(TabListHandler handler);
/** /**
* Gracefully mark this instance for shutdown. * Gracefully mark this instance for shutdown.
*/ */

View File

@ -22,11 +22,11 @@ public class PluginMessageEvent extends TargetedEvent implements Cancellable
/** /**
* Tag specified for this plugin message. * Tag specified for this plugin message.
*/ */
private String tag; private final String tag;
/** /**
* Data contained in this plugin message. * Data contained in this plugin message.
*/ */
private byte[] data; private final byte[] data;
public PluginMessageEvent(Connection sender, Connection receiver, String tag, byte[] data) public PluginMessageEvent(Connection sender, Connection receiver, String tag, byte[] data)
{ {

View File

@ -0,0 +1,26 @@
package net.md_5.bungee.api.event;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.plugin.Event;
@Data
@AllArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class ServerConnectEvent extends Event
{
/**
* Player connecting to a new server.
*/
private final ProxiedPlayer player;
/**
* Server the player will be connected to.
*/
private Server target;
}

View File

@ -2,7 +2,9 @@ package net.md_5.bungee.api.event;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import net.md_5.bungee.api.connection.Connection; import net.md_5.bungee.api.connection.Connection;
import net.md_5.bungee.api.plugin.Event;
/** /**
* An event which occurs in the communication between two nodes. It is not * An event which occurs in the communication between two nodes. It is not
@ -12,7 +14,8 @@ import net.md_5.bungee.api.connection.Connection;
*/ */
@Data @Data
@AllArgsConstructor @AllArgsConstructor
public abstract class TargetedEvent @EqualsAndHashCode(callSuper = false)
public abstract class TargetedEvent extends Event
{ {
/** /**

View File

@ -268,7 +268,7 @@ public class Configuration
*/ */
public void setServer(UserConnection user, String server) public void setServer(UserConnection user, String server)
{ {
reconnectLocations.put(user.username, server); reconnectLocations.put(user.name, server);
} }
/** /**

View File

@ -4,6 +4,7 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.Socket; import java.net.Socket;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import static net.md_5.bungee.Logger.$; import static net.md_5.bungee.Logger.$;
import net.md_5.bungee.packet.PacketFFKick; import net.md_5.bungee.packet.PacketFFKick;
@ -20,8 +21,10 @@ public class GenericConnection
protected final Socket socket; protected final Socket socket;
protected final PacketInputStream in; protected final PacketInputStream in;
protected final OutputStream out; protected final OutputStream out;
public String username; @Getter
public String tabListName; public String name;
@Getter
public String displayName;
/** /**
* Close the socket with the specified reason. * Close the socket with the specified reason.
@ -55,6 +58,6 @@ public class GenericConnection
public void log(String message) public void log(String message)
{ {
$().info(socket.getInetAddress() + ((username == null) ? " " : " [" + username + "] ") + message); $().info(socket.getInetAddress() + ((name == null) ? " " : " [" + name + "] ") + message);
} }
} }

View File

@ -4,17 +4,18 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import javax.print.attribute.standard.Destination; import lombok.Getter;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server; import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.event.ChatEvent; import net.md_5.bungee.api.event.ChatEvent;
import net.md_5.bungee.api.event.PluginMessageEvent; import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.packet.*; import net.md_5.bungee.packet.*;
public class UserConnection extends GenericConnection implements ProxiedPlayer public class UserConnection extends GenericConnection implements ProxiedPlayer
@ -23,6 +24,7 @@ public class UserConnection extends GenericConnection implements ProxiedPlayer
public final Packet2Handshake handshake; public final Packet2Handshake handshake;
public Queue<DefinedPacket> packetQueue = new ConcurrentLinkedQueue<>(); public Queue<DefinedPacket> packetQueue = new ConcurrentLinkedQueue<>();
public List<byte[]> loginPackets = new ArrayList<>(); public List<byte[]> loginPackets = new ArrayList<>();
@Getter
private ServerConnection server; private ServerConnection server;
private UpstreamBridge upBridge; private UpstreamBridge upBridge;
private DownstreamBridge downBridge; private DownstreamBridge downBridge;
@ -33,6 +35,7 @@ public class UserConnection extends GenericConnection implements ProxiedPlayer
// ping stuff // ping stuff
private int trackingPingId; private int trackingPingId;
private long pingTime; private long pingTime;
@Getter
private int ping; private int ping;
public UserConnection instance = this; public UserConnection instance = this;
@ -40,39 +43,25 @@ public class UserConnection extends GenericConnection implements ProxiedPlayer
{ {
super(socket, in, out); super(socket, in, out);
this.handshake = handshake; this.handshake = handshake;
username = handshake.username; name = handshake.username;
tabListName = handshake.username; displayName = handshake.username;
this.loginPackets = loginPackets; this.loginPackets = loginPackets;
BungeeCord.instance.connections.put(username, this); BungeeCord.instance.connections.put(name, this);
BungeeCord.instance.tabListHandler.onJoin(this); BungeeCord.instance.tabListHandler.onJoin(this);
} }
public void setTabListName(String newName) @Override
public void setDisplayName(String name)
{ {
BungeeCord.instance.tabListHandler.onDisconnect(this); ProxyServer.getInstance().getTabListHandler().onDisconnect(this);
tabListName = newName; displayName = name;
BungeeCord.instance.tabListHandler.onJoin(this); ProxyServer.getInstance().getTabListHandler().onConnect(this);
} }
public void connect(String server) public void connect(Server server)
{ {
ServerConnectEvent event = new ServerConnectEvent(this.server == null, this, server); ServerConnectEvent event = new ServerConnectEvent(this, server);
event.setNewServer(server); BungeeCord.getInstance().getPluginManager().callEvent(event);
BungeeCord.instance.pluginManager.onServerConnect(event);
if (event.getMessage() != null)
{
this.sendMessage(event.getMessage());
}
if (event.getNewServer() == null)
{
if (event.isFirstTime())
{
event.setNewServer(BungeeCord.instance.config.defaultServerName);
} else
{
return;
}
}
InetSocketAddress addr = BungeeCord.instance.config.getServer(event.getNewServer()); InetSocketAddress addr = BungeeCord.instance.config.getServer(event.getNewServer());
connect(server, addr); connect(server, addr);
} }
@ -146,21 +135,6 @@ public class UserConnection extends GenericConnection implements ProxiedPlayer
} }
} }
public String getServer()
{
return server.name;
}
public SocketAddress getAddress()
{
return socket.getRemoteSocketAddress();
}
public int getPing()
{
return ping;
}
private void setPing(int ping) private void setPing(int ping)
{ {
BungeeCord.instance.tabListHandler.onPingChange(this, ping); BungeeCord.instance.tabListHandler.onPingChange(this, ping);
@ -171,7 +145,7 @@ public class UserConnection extends GenericConnection implements ProxiedPlayer
{ {
if (BungeeCord.instance.isRunning) if (BungeeCord.instance.isRunning)
{ {
BungeeCord.instance.connections.remove(username); BungeeCord.instance.connections.remove(name);
if (server != null) if (server != null)
{ {
List<UserConnection> conns = BungeeCord.instance.connectionsByServer.get(server.name); List<UserConnection> conns = BungeeCord.instance.connectionsByServer.get(server.name);
@ -202,45 +176,10 @@ public class UserConnection extends GenericConnection implements ProxiedPlayer
packetQueue.add(new Packet3Chat(message)); packetQueue.add(new Packet3Chat(message));
} }
public void sendPluginMessage(String tag, byte[] data)
{
server.packetQueue.add(new PacketFAPluginMessage(tag, data));
}
@Override
public String getName()
{
return username;
}
@Override
public String getDisplayName()
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void setDisplayName(String name)
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void connect(Server server)
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Server getServer()
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override @Override
public void sendData(String channel, byte[] data) public void sendData(String channel, byte[] data)
{ {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. server.packetQueue.add(new PacketFAPluginMessage(channel, data));
} }
@Override @Override
@ -284,7 +223,7 @@ public class UserConnection extends GenericConnection implements ProxiedPlayer
public UpstreamBridge() public UpstreamBridge()
{ {
super("Upstream Bridge - " + username); super("Upstream Bridge - " + name);
} }
@Override @Override
@ -298,39 +237,37 @@ public class UserConnection extends GenericConnection implements ProxiedPlayer
boolean sendPacket = true; boolean sendPacket = true;
int id = Util.getId(packet); int id = Util.getId(packet);
if (id == 0xFA) switch (id)
{ {
// Call the onPluginMessage event case 0x00:
PacketFAPluginMessage message = new PacketFAPluginMessage(packet); if (trackingPingId == new Packet0KeepAlive(packet).id)
PluginMessageEvent event = new PluginMessageEvent(Destination.SERVER, instance); {
event.setTag(message.tag); setPing((int) (System.currentTimeMillis() - pingTime));
event.setData(new String(message.data)); }
BungeeCord.instance.pluginManager.onPluginMessage(event); break;
case 0x03:
Packet3Chat chat = new Packet3Chat(packet);
if (chat.message.startsWith("/"))
{
sendPacket = !ProxyServer.getInstance().getPluginManager().dispatchCommand(UserConnection.this, chat.message.substring(1));
} else
{
ChatEvent chatEvent = new ChatEvent(UserConnection.this, server, chat.message);
ProxyServer.getInstance().getPluginManager().callEvent(chatEvent);
sendPacket = !chatEvent.isCancelled();
}
break;
case 0xFA:
// Call the onPluginMessage event
PacketFAPluginMessage message = new PacketFAPluginMessage(packet);
PluginMessageEvent event = new PluginMessageEvent(UserConnection.this, server, message.tag, message.data);
ProxyServer.getInstance().getPluginManager().callEvent(event);
if (event.isCancelled()) if (event.isCancelled())
{ {
continue; continue;
} }
} else if (id == 0x03) break;
{
Packet3Chat chat = new Packet3Chat(packet);
String message = chat.message;
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)
{
if (trackingPingId == new Packet0KeepAlive(packet).id)
{
setPing((int) (System.currentTimeMillis() - pingTime));
}
} }
while (!server.packetQueue.isEmpty()) while (!server.packetQueue.isEmpty())
@ -363,7 +300,7 @@ public class UserConnection extends GenericConnection implements ProxiedPlayer
public DownstreamBridge() public DownstreamBridge()
{ {
super("Downstream Bridge - " + username); super("Downstream Bridge - " + name);
} }
@Override @Override
@ -371,61 +308,59 @@ public class UserConnection extends GenericConnection implements ProxiedPlayer
{ {
try try
{ {
outer:
while (!reconnecting) while (!reconnecting)
{ {
byte[] packet = server.in.readPacket(); byte[] packet = server.in.readPacket();
int id = Util.getId(packet); int id = Util.getId(packet);
if (id == 0xFA) switch (id)
{ {
// Call the onPluginMessage event case 0x00:
PacketFAPluginMessage message = new PacketFAPluginMessage(packet); trackingPingId = new Packet0KeepAlive(packet).id;
PluginMessageEvent event = new PluginMessageEvent(Destination.CLIENT, instance); pingTime = System.currentTimeMillis();
event.setTag(message.tag);
event.setData(new String(message.data));
BungeeCord.instance.pluginManager.onPluginMessage(event);
if (event.isCancelled())
{
continue;
}
message.tag = event.getTag();
message.data = event.getData().getBytes();
// Allow a message for killing the connection outright
if (message.tag.equals("KillCon"))
{
break; break;
} case 0x03:
Packet3Chat chat = new Packet3Chat(packet);
ChatEvent chatEvent = new ChatEvent(server, UserConnection.this, chat.message);
ProxyServer.getInstance().getPluginManager().callEvent(chatEvent);
if (message.tag.equals("RubberBand")) if (chatEvent.isCancelled())
{ {
String server = new String(message.data); continue;
connect(server); }
break; break;
} case 0xC9:
} else if (id == 0x00) PacketC9PlayerListItem playerList = new PacketC9PlayerListItem(packet);
{ if (!ProxyServer.getInstance().getTabListHandler().onListUpdate(instance, playerList.username, playerList.online, playerList.ping))
trackingPingId = new Packet0KeepAlive(packet).id; {
pingTime = System.currentTimeMillis(); continue;
} else if (id == 0x03) }
{ break;
Packet3Chat chat = new Packet3Chat(packet); case 0xFA:
String message = chat.message; // Call the onPluginMessage event
ChatEvent chatEvent = new ChatEvent(ChatEvent.Destination.CLIENT, instance); PacketFAPluginMessage message = new PacketFAPluginMessage(packet);
chatEvent.setText(message); PluginMessageEvent event = new PluginMessageEvent(server, UserConnection.this, message.tag, message.data);
BungeeCord.instance.pluginManager.onChat(chatEvent); ProxyServer.getInstance().getPluginManager().callEvent(event);
if (chatEvent.isCancelled())
{ if (event.isCancelled())
continue; {
} continue;
} else if (id == 0xC9) }
{
if (!BungeeCord.instance.tabListHandler.onPacketC9(UserConnection.this, new PacketC9PlayerListItem(packet))) switch (message.tag)
{ {
continue; case "BungeeCord::Disconnect":
} break outer;
case "BungeeCord::Connect":
Server server = ProxyServer.getInstance().getServer(new String(message.data));
if (server != null)
{
connect(server);
break outer;
}
break;
}
} }
while (!packetQueue.isEmpty()) while (!packetQueue.isEmpty())

View File

@ -19,7 +19,7 @@ public class GlobalTabList implements TabListHandler
{ {
for (UserConnection c : BungeeCord.getInstance().connections.values()) for (UserConnection c : BungeeCord.getInstance().connections.values())
{ {
c.packetQueue.add(new PacketC9PlayerListItem(c.tabListName, true, c.getPing())); c.packetQueue.add(new PacketC9PlayerListItem(c.displayName, true, c.getPing()));
} }
} }