Add PlayerHandshakeEvent which allows changing of versions and online mode status amongst other things.

This commit is contained in:
marvin 2013-09-30 09:22:49 +10:00 committed by md_5
parent 33e11f4c44
commit a0d94282f6
3 changed files with 51 additions and 5 deletions

View File

@ -0,0 +1,34 @@
package net.md_5.bungee.api.event;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import net.md_5.bungee.api.connection.PendingConnection;
import net.md_5.bungee.protocol.packet.Packet2Handshake;
import net.md_5.bungee.api.plugin.Event;
/**
* Event called to represent a player first making their presence and username
* known.
*/
@Data
@ToString(callSuper = false)
@EqualsAndHashCode(callSuper = false)
public class PlayerHandshakeEvent extends Event
{
/**
* Connection attempting to login.
*/
private final PendingConnection connection;
/**
* The handshake.
*/
private final Packet2Handshake handshake;
public PlayerHandshakeEvent(PendingConnection connection, Packet2Handshake handshake)
{
this.connection = connection;
this.handshake = handshake;
}
}

View File

@ -40,9 +40,9 @@ public class EncryptionUtil
} }
} }
public static PacketFDEncryptionRequest encryptRequest() public static PacketFDEncryptionRequest encryptRequest(boolean onlinemode)
{ {
String hash = ( BungeeCord.getInstance().config.isOnlineMode() ) ? Long.toString( random.nextLong(), 16 ) : "-"; String hash = ( onlinemode ) ? Long.toString( random.nextLong(), 16 ) : "-";
byte[] pubKey = keys.getPublic().getEncoded(); byte[] pubKey = keys.getPublic().getEncoded();
byte[] verify = new byte[ 4 ]; byte[] verify = new byte[ 4 ];
random.nextBytes( verify ); random.nextBytes( verify );

View File

@ -52,6 +52,7 @@ import net.md_5.bungee.protocol.packet.PacketFDEncryptionRequest;
import net.md_5.bungee.protocol.packet.PacketFEPing; import net.md_5.bungee.protocol.packet.PacketFEPing;
import net.md_5.bungee.protocol.packet.PacketFFKick; import net.md_5.bungee.protocol.packet.PacketFFKick;
import net.md_5.bungee.api.AbstractReconnectHandler; import net.md_5.bungee.api.AbstractReconnectHandler;
import net.md_5.bungee.api.event.PlayerHandshakeEvent;
@RequiredArgsConstructor @RequiredArgsConstructor
public class InitialHandler extends PacketHandler implements PendingConnection public class InitialHandler extends PacketHandler implements PendingConnection
@ -80,6 +81,8 @@ public class InitialHandler extends PacketHandler implements PendingConnection
ch.write( packet ); ch.write( packet );
} }
}; };
@Getter
private boolean onlineMode = BungeeCord.getInstance().config.isOnlineMode();
private ScheduledFuture<?> pingFuture; private ScheduledFuture<?> pingFuture;
private InetSocketAddress vHost; private InetSocketAddress vHost;
private byte version = -1; private byte version = -1;
@ -198,6 +201,8 @@ public class InitialHandler extends PacketHandler implements PendingConnection
this.vHost = new InetSocketAddress( handshake.getHost(), handshake.getPort() ); this.vHost = new InetSocketAddress( handshake.getHost(), handshake.getPort() );
bungee.getLogger().log( Level.INFO, "{0} has connected", this ); bungee.getLogger().log( Level.INFO, "{0} has connected", this );
bungee.getPluginManager().callEvent( new PlayerHandshakeEvent( InitialHandler.this, handshake ) );
if ( handshake.getProtocolVersion() > Vanilla.PROTOCOL_VERSION ) if ( handshake.getProtocolVersion() > Vanilla.PROTOCOL_VERSION )
{ {
disconnect( bungee.getTranslation( "outdated_server" ) ); disconnect( bungee.getTranslation( "outdated_server" ) );
@ -220,7 +225,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
} }
// If offline mode and they are already on, don't allow connect // If offline mode and they are already on, don't allow connect
if ( !BungeeCord.getInstance().config.isOnlineMode() && bungee.getPlayer( handshake.getUsername() ) != null ) if ( !isOnlineMode() && bungee.getPlayer( handshake.getUsername() ) != null )
{ {
disconnect( bungee.getTranslation( "already_connected" ) ); disconnect( bungee.getTranslation( "already_connected" ) );
return; return;
@ -228,7 +233,8 @@ public class InitialHandler extends PacketHandler implements PendingConnection
unsafe().sendPacket( PacketConstants.I_AM_BUNGEE ); unsafe().sendPacket( PacketConstants.I_AM_BUNGEE );
unsafe().sendPacket( PacketConstants.FORGE_MOD_REQUEST ); unsafe().sendPacket( PacketConstants.FORGE_MOD_REQUEST );
unsafe().sendPacket( request = EncryptionUtil.encryptRequest() );
unsafe().sendPacket( request = EncryptionUtil.encryptRequest( this.onlineMode ) );
thisState = State.ENCRYPT; thisState = State.ENCRYPT;
} }
@ -241,7 +247,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
Cipher decrypt = EncryptionUtil.getCipher( Cipher.DECRYPT_MODE, sharedKey ); Cipher decrypt = EncryptionUtil.getCipher( Cipher.DECRYPT_MODE, sharedKey );
ch.addBefore( PipelineUtils.PACKET_DECODE_HANDLER, PipelineUtils.DECRYPT_HANDLER, new CipherDecoder( decrypt ) ); ch.addBefore( PipelineUtils.PACKET_DECODE_HANDLER, PipelineUtils.DECRYPT_HANDLER, new CipherDecoder( decrypt ) );
if ( BungeeCord.getInstance().config.isOnlineMode() ) if ( this.onlineMode )
{ {
String encName = URLEncoder.encode( InitialHandler.this.getName(), "UTF-8" ); String encName = URLEncoder.encode( InitialHandler.this.getName(), "UTF-8" );
@ -402,6 +408,12 @@ public class InitialHandler extends PacketHandler implements PendingConnection
return unsafe; return unsafe;
} }
public void setOnlineMode(boolean onlineMode)
{
Preconditions.checkState( thisState == State.HANDSHAKE, "Can only set online mode status whilst handshaking" );
this.onlineMode = onlineMode;
}
@Override @Override
public String toString() public String toString()
{ {