Finish encapsulating all userconnection fields

This commit is contained in:
md_5 2013-04-28 11:36:36 +10:00
parent ff32764f9a
commit 50f8bd2eb0
4 changed files with 63 additions and 31 deletions

View File

@ -12,6 +12,7 @@ import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.event.ServerConnectedEvent; import net.md_5.bungee.api.event.ServerConnectedEvent;
import net.md_5.bungee.api.event.ServerKickEvent; import net.md_5.bungee.api.event.ServerKickEvent;
import net.md_5.bungee.api.scoreboard.Objective; import net.md_5.bungee.api.scoreboard.Objective;
import net.md_5.bungee.api.scoreboard.Scoreboard;
import net.md_5.bungee.api.scoreboard.Team; import net.md_5.bungee.api.scoreboard.Team;
import net.md_5.bungee.connection.CancelSendSignal; import net.md_5.bungee.connection.CancelSendSignal;
import net.md_5.bungee.connection.DownstreamBridge; import net.md_5.bungee.connection.DownstreamBridge;
@ -59,6 +60,12 @@ public class ServerConnector extends PacketHandler
channel.write( PacketCDClientStatus.CLIENT_LOGIN ); channel.write( PacketCDClientStatus.CLIENT_LOGIN );
} }
@Override
public void disconnected(ChannelWrapper channel) throws Exception
{
user.getPendingConnects().remove( target );
}
@Override @Override
public void handle(Packet1Login login) throws Exception public void handle(Packet1Login login) throws Exception
{ {
@ -76,9 +83,9 @@ public class ServerConnector extends PacketHandler
{ {
ch.write( packetQueue.poll() ); ch.write( packetQueue.poll() );
} }
if ( user.settings != null ) if ( user.getSettings() != null )
{ {
ch.write( user.settings ); ch.write( user.getSettings() );
} }
synchronized ( user.getSwitchMutex() ) synchronized ( user.getSwitchMutex() )
@ -86,8 +93,8 @@ public class ServerConnector extends PacketHandler
if ( user.getServer() == null ) if ( user.getServer() == null )
{ {
// Once again, first connection // Once again, first connection
user.clientEntityId = login.entityId; user.setClientEntityId( login.entityId );
user.serverEntityId = login.entityId; user.setServerEntityId( login.entityId );
// Set tab list size // Set tab list size
Packet1Login modLogin = new Packet1Login( Packet1Login modLogin = new Packet1Login(
login.entityId, login.entityId,
@ -102,20 +109,21 @@ public class ServerConnector extends PacketHandler
{ {
bungee.getTabListHandler().onServerChange( user ); bungee.getTabListHandler().onServerChange( user );
for ( Objective objective : user.serverSentScoreboard.getObjectives() ) Scoreboard serverScoreboard = user.getServerSentScoreboard();
for ( Objective objective : serverScoreboard.getObjectives() )
{ {
user.sendPacket( new PacketCEScoreboardObjective( objective.getName(), objective.getValue(), (byte) 1 ) ); user.sendPacket( new PacketCEScoreboardObjective( objective.getName(), objective.getValue(), (byte) 1 ) );
} }
for ( Team team : user.serverSentScoreboard.getTeams() ) for ( Team team : serverScoreboard.getTeams() )
{ {
user.sendPacket( PacketD1Team.destroy( team.getName() ) ); user.sendPacket( PacketD1Team.destroy( team.getName() ) );
} }
user.serverSentScoreboard.clear(); serverScoreboard.clear();
user.sendPacket( Packet9Respawn.DIM1_SWITCH ); user.sendPacket( Packet9Respawn.DIM1_SWITCH );
user.sendPacket( Packet9Respawn.DIM2_SWITCH ); user.sendPacket( Packet9Respawn.DIM2_SWITCH );
user.serverEntityId = login.entityId; user.setServerEntityId( login.entityId );
user.sendPacket( new Packet9Respawn( login.dimension, login.difficulty, login.gameMode, (short) 256, login.levelType ) ); user.sendPacket( new Packet9Respawn( login.dimension, login.difficulty, login.gameMode, (short) 256, login.levelType ) );
// Remove from old servers // Remove from old servers
@ -135,7 +143,7 @@ public class ServerConnector extends PacketHandler
// Add to new server // Add to new server
// TODO: Move this to the connected() method of DownstreamBridge // TODO: Move this to the connected() method of DownstreamBridge
target.addPlayer( user ); target.addPlayer( user );
user.pendingConnects.remove( target ); user.getPendingConnects().remove( target );
user.setServer( server ); user.setServer( server );
ch.getHandle().pipeline().get( HandlerBoss.class ).setHandler( new DownstreamBridge( bungee, user, server ) ); ch.getHandle().pipeline().get( HandlerBoss.class ).setHandler( new DownstreamBridge( bungee, user, server ) );

View File

@ -16,6 +16,7 @@ import java.util.Objects;
import java.util.logging.Level; import java.util.logging.Level;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
@ -41,21 +42,24 @@ public final class UserConnection implements ProxiedPlayer
{ {
/*========================================================================*/ /*========================================================================*/
@NonNull
private final ProxyServer bungee; private final ProxyServer bungee;
@NonNull
private final ChannelWrapper ch; private final ChannelWrapper ch;
@Getter @Getter
@NonNull
private final String name; private final String name;
@Getter @Getter
@NonNull
private final InitialHandler pendingConnection; private final InitialHandler pendingConnection;
/*========================================================================*/ /*========================================================================*/
@Getter @Getter
@Setter(AccessLevel.PACKAGE) @Setter
private ServerConnection server; private ServerConnection server;
// reconnect stuff
public int clientEntityId;
public int serverEntityId;
@Getter @Getter
private String displayName; private final Object switchMutex = new Object();
@Getter
private final Collection<ServerInfo> pendingConnects = new HashSet<>();
/*========================================================================*/ /*========================================================================*/
@Getter @Getter
@Setter @Setter
@ -71,10 +75,19 @@ public final class UserConnection implements ProxiedPlayer
private final Collection<String> permissions = new HashSet<>(); private final Collection<String> permissions = new HashSet<>();
/*========================================================================*/ /*========================================================================*/
@Getter @Getter
private final Object switchMutex = new Object(); private int clientEntityId;
public PacketCCSettings settings; @Getter
public final Scoreboard serverSentScoreboard = new Scoreboard(); @Setter
public final Collection<ServerInfo> pendingConnects = new HashSet<>(); private int serverEntityId;
@Getter
@Setter
private PacketCCSettings settings;
@Getter
private final Scoreboard serverSentScoreboard = new Scoreboard();
/*========================================================================*/
@Getter
private String displayName;
/*========================================================================*/
public void init() public void init()
{ {
@ -295,4 +308,10 @@ public final class UserConnection implements ProxiedPlayer
{ {
return name; return name;
} }
public void setClientEntityId(int clientEntityId)
{
Preconditions.checkState( this.clientEntityId == 0, "Client entityId already set!" );
this.clientEntityId = clientEntityId;
}
} }

View File

@ -20,6 +20,7 @@ import net.md_5.bungee.api.event.ServerKickEvent;
import net.md_5.bungee.api.scoreboard.Objective; import net.md_5.bungee.api.scoreboard.Objective;
import net.md_5.bungee.api.scoreboard.Position; import net.md_5.bungee.api.scoreboard.Position;
import net.md_5.bungee.api.scoreboard.Score; import net.md_5.bungee.api.scoreboard.Score;
import net.md_5.bungee.api.scoreboard.Scoreboard;
import net.md_5.bungee.api.scoreboard.Team; import net.md_5.bungee.api.scoreboard.Team;
import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.packet.Packet0KeepAlive; import net.md_5.bungee.packet.Packet0KeepAlive;
@ -71,7 +72,7 @@ public class DownstreamBridge extends PacketHandler
@Override @Override
public void handle(byte[] buf) throws Exception public void handle(byte[] buf) throws Exception
{ {
EntityMap.rewrite( buf, con.serverEntityId, con.clientEntityId ); EntityMap.rewrite( buf, con.getServerEntityId(), con.getClientEntityId() );
con.sendPacket( buf ); con.sendPacket( buf );
} }
@ -107,13 +108,14 @@ public class DownstreamBridge extends PacketHandler
@Override @Override
public void handle(PacketCEScoreboardObjective objective) throws Exception public void handle(PacketCEScoreboardObjective objective) throws Exception
{ {
Scoreboard serverScoreboard = con.getServerSentScoreboard();
switch ( objective.action ) switch ( objective.action )
{ {
case 0: case 0:
con.serverSentScoreboard.addObjective( new Objective( objective.name, objective.text ) ); serverScoreboard.addObjective( new Objective( objective.name, objective.text ) );
break; break;
case 1: case 1:
con.serverSentScoreboard.removeObjective( objective.name ); serverScoreboard.removeObjective( objective.name );
break; break;
} }
} }
@ -121,15 +123,16 @@ public class DownstreamBridge extends PacketHandler
@Override @Override
public void handle(PacketCFScoreboardScore score) throws Exception public void handle(PacketCFScoreboardScore score) throws Exception
{ {
Scoreboard serverScoreboard = con.getServerSentScoreboard();
switch ( score.action ) switch ( score.action )
{ {
case 0: case 0:
Score s = new Score( score.itemName, score.scoreName, score.value ); Score s = new Score( score.itemName, score.scoreName, score.value );
con.serverSentScoreboard.removeScore( score.itemName ); serverScoreboard.removeScore( score.itemName );
con.serverSentScoreboard.addScore( s ); serverScoreboard.addScore( s );
break; break;
case 1: case 1:
con.serverSentScoreboard.removeScore( score.itemName ); serverScoreboard.removeScore( score.itemName );
break; break;
} }
} }
@ -137,17 +140,19 @@ public class DownstreamBridge extends PacketHandler
@Override @Override
public void handle(PacketD0DisplayScoreboard displayScoreboard) throws Exception public void handle(PacketD0DisplayScoreboard displayScoreboard) throws Exception
{ {
con.serverSentScoreboard.setName( displayScoreboard.name ); Scoreboard serverScoreboard = con.getServerSentScoreboard();
con.serverSentScoreboard.setPosition( Position.values()[displayScoreboard.position] ); serverScoreboard.setName( displayScoreboard.name );
serverScoreboard.setPosition( Position.values()[displayScoreboard.position] );
} }
@Override @Override
public void handle(PacketD1Team team) throws Exception public void handle(PacketD1Team team) throws Exception
{ {
Scoreboard serverScoreboard = con.getServerSentScoreboard();
// Remove team and move on // Remove team and move on
if ( team.mode == 1 ) if ( team.mode == 1 )
{ {
con.serverSentScoreboard.removeTeam( team.name ); serverScoreboard.removeTeam( team.name );
return; return;
} }
@ -156,10 +161,10 @@ public class DownstreamBridge extends PacketHandler
if ( team.mode == 0 ) if ( team.mode == 0 )
{ {
t = new Team( team.name ); t = new Team( team.name );
con.serverSentScoreboard.addTeam( t ); serverScoreboard.addTeam( t );
} else } else
{ {
t = con.serverSentScoreboard.getTeam( team.name ); t = serverScoreboard.getTeam( team.name );
} }
if ( t != null ) if ( t != null )

View File

@ -64,7 +64,7 @@ public class UpstreamBridge extends PacketHandler
@Override @Override
public void handle(byte[] buf) throws Exception public void handle(byte[] buf) throws Exception
{ {
EntityMap.rewrite( buf, con.clientEntityId, con.serverEntityId ); EntityMap.rewrite( buf, con.getClientEntityId(), con.getServerEntityId() );
if ( con.getServer() != null ) if ( con.getServer() != null )
{ {
con.getServer().getCh().write( buf ); con.getServer().getCh().write( buf );
@ -102,7 +102,7 @@ public class UpstreamBridge extends PacketHandler
@Override @Override
public void handle(PacketCCSettings settings) throws Exception public void handle(PacketCCSettings settings) throws Exception
{ {
con.settings = settings; con.setSettings( settings );
} }
@Override @Override