2011-02-05 19:15:04 +01:00
package net.minecraft.server ;
import java.net.Socket ;
2011-09-15 02:23:52 +02:00
import java.util.Iterator ;
2011-02-05 19:15:04 +01:00
import java.util.Random ;
import java.util.logging.Logger ;
2011-09-27 00:32:05 +02:00
// CraftBukkit start
2012-01-20 17:03:15 +01:00
import org.bukkit.ChatColor ;
2011-09-27 00:32:05 +02:00
import org.bukkit.craftbukkit.event.CraftEventFactory ;
import org.bukkit.event.server.ServerListPingEvent ;
// CraftBukkit end
2011-02-05 19:15:04 +01:00
public class NetLoginHandler extends NetHandler {
2012-01-14 21:03:48 +01:00
public static Logger logger = Logger . getLogger ( " Minecraft " ) ;
private static Random random = new Random ( ) ;
2011-04-20 19:05:14 +02:00
public NetworkManager networkManager ;
2011-02-05 19:15:04 +01:00
public boolean c = false ;
2011-04-20 19:05:14 +02:00
private MinecraftServer server ;
2011-02-05 19:15:04 +01:00
private int f = 0 ;
private String g = null ;
private Packet1Login h = null ;
2012-01-14 21:03:48 +01:00
private String loginKey = Long . toString ( random . nextLong ( ) , 16 ) ; // CraftBukkit - Security fix
2012-03-03 19:38:36 +01:00
public String hostname = " " ; // CraftBukkit - add field
2011-02-05 19:15:04 +01:00
public NetLoginHandler ( MinecraftServer minecraftserver , Socket socket , String s ) {
2011-04-20 19:05:14 +02:00
this . server = minecraftserver ;
this . networkManager = new NetworkManager ( socket , s , this ) ;
2011-05-26 14:48:22 +02:00
this . networkManager . f = 0 ;
2011-02-05 19:15:04 +01:00
}
2011-04-20 19:05:14 +02:00
2011-04-16 12:08:13 +02:00
// CraftBukkit start
public Socket getSocket ( ) {
2011-06-27 00:25:01 +02:00
return this . networkManager . socket ;
2011-04-16 12:08:13 +02:00
}
// CraftBukkit end
2011-02-05 19:15:04 +01:00
public void a ( ) {
if ( this . h ! = null ) {
this . b ( this . h ) ;
this . h = null ;
}
if ( this . f + + = = 600 ) {
2011-04-20 19:05:14 +02:00
this . disconnect ( " Took too long to log in " ) ;
2011-02-05 19:15:04 +01:00
} else {
2011-05-26 14:48:22 +02:00
this . networkManager . b ( ) ;
2011-02-05 19:15:04 +01:00
}
}
2011-04-20 19:05:14 +02:00
public void disconnect ( String s ) {
2011-02-05 19:15:04 +01:00
try {
2012-01-14 21:03:48 +01:00
logger . info ( " Disconnecting " + this . getName ( ) + " : " + s ) ;
2011-06-27 00:25:01 +02:00
this . networkManager . queue ( new Packet255KickDisconnect ( s ) ) ;
2011-05-26 14:48:22 +02:00
this . networkManager . d ( ) ;
2011-02-05 19:15:04 +01:00
this . c = true ;
} catch ( Exception exception ) {
exception . printStackTrace ( ) ;
}
}
public void a ( Packet2Handshake packet2handshake ) {
2012-03-03 19:38:36 +01:00
// CraftBukkit start
int i = packet2handshake . a . indexOf ( ';' ) ;
if ( i = = - 1 ) {
this . hostname = " " ;
} else this . hostname = packet2handshake . a . substring ( i + 1 ) ;
// CraftBukkit end
2011-04-20 19:05:14 +02:00
if ( this . server . onlineMode ) {
2012-01-14 21:03:48 +01:00
this . loginKey = Long . toString ( random . nextLong ( ) , 16 ) ;
this . networkManager . queue ( new Packet2Handshake ( this . loginKey ) ) ;
2011-02-05 19:15:04 +01:00
} else {
2011-06-27 00:25:01 +02:00
this . networkManager . queue ( new Packet2Handshake ( " - " ) ) ;
2011-02-05 19:15:04 +01:00
}
}
public void a ( Packet1Login packet1login ) {
2011-04-20 19:05:14 +02:00
this . g = packet1login . name ;
2012-03-22 21:39:39 +01:00
if ( packet1login . a ! = 29 ) {
if ( packet1login . a > 29 ) {
2011-04-20 19:05:14 +02:00
this . disconnect ( " Outdated server! " ) ;
2011-02-05 19:15:04 +01:00
} else {
2011-04-20 19:05:14 +02:00
this . disconnect ( " Outdated client! " ) ;
2011-02-05 19:15:04 +01:00
}
} else {
2011-04-20 19:05:14 +02:00
if ( ! this . server . onlineMode ) {
2012-01-20 17:03:15 +01:00
// CraftBukkit start - disallow colour in names
if ( ! packet1login . name . equals ( ChatColor . stripColor ( packet1login . name ) ) ) {
this . disconnect ( " Colourful names are not permitted! " ) ;
return ;
}
// CraftBukkit end
2011-02-05 19:15:04 +01:00
this . b ( packet1login ) ;
} else {
2011-04-20 22:47:26 +02:00
( new ThreadLoginVerifier ( this , packet1login , this . server . server ) ) . start ( ) ; // CraftBukkit
2011-02-05 19:15:04 +01:00
}
}
}
public void b ( Packet1Login packet1login ) {
2012-03-03 19:38:36 +01:00
EntityPlayer entityplayer = this . server . serverConfigurationManager . attemptLogin ( this , packet1login . name , this . hostname ) ; // CraftBukkit - add hostname parameter
2011-02-05 19:15:04 +01:00
if ( entityplayer ! = null ) {
2012-04-18 19:28:50 +02:00
//this.server.serverConfigurationManager.b(entityplayer); // CraftBukkit - Moved to attemptLogin
2011-06-08 19:40:40 +02:00
// entityplayer.a((World) this.server.a(entityplayer.dimension)); // CraftBukkit - set by Entity
2011-09-15 02:23:52 +02:00
entityplayer . itemInWorldManager . a ( ( WorldServer ) entityplayer . world ) ;
2011-11-20 09:01:14 +01:00
// CraftBukkit - add world and location to 'logged in' message.
2012-01-14 21:03:48 +01:00
logger . info ( this . getName ( ) + " logged in with entity id " + entityplayer . id + " at ([ " + entityplayer . world . worldData . name + " ] " + entityplayer . locX + " , " + entityplayer . locY + " , " + entityplayer . locZ + " ) " ) ;
2011-06-08 19:40:40 +02:00
WorldServer worldserver = ( WorldServer ) entityplayer . world ; // CraftBukkit
2011-05-26 14:48:22 +02:00
ChunkCoordinates chunkcoordinates = worldserver . getSpawn ( ) ;
2011-09-15 02:23:52 +02:00
2011-11-30 00:17:43 +01:00
entityplayer . itemInWorldManager . b ( worldserver . getWorldData ( ) . getGameType ( ) ) ;
2011-04-20 19:05:14 +02:00
NetServerHandler netserverhandler = new NetServerHandler ( this . server , this . networkManager , entityplayer ) ;
2011-02-05 19:15:04 +01:00
2011-09-25 05:14:27 +02:00
// CraftBukkit start -- Don't send a higher than 60 MaxPlayer size, otherwise the PlayerInfo window won't render correctly.
2011-11-30 00:17:43 +01:00
int maxPlayers = this . server . serverConfigurationManager . getMaxPlayers ( ) ;
2011-09-25 05:14:27 +02:00
if ( maxPlayers > 60 ) {
maxPlayers = 60 ;
2011-09-16 09:52:36 +02:00
}
2012-03-01 19:41:20 +01:00
netserverhandler . sendPacket ( new Packet1Login ( " " , entityplayer . id , worldserver . getWorldData ( ) . getType ( ) , entityplayer . itemInWorldManager . getGameMode ( ) , worldserver . worldProvider . dimension , ( byte ) worldserver . difficulty , ( byte ) worldserver . getHeight ( ) , ( byte ) maxPlayers ) ) ;
2012-01-29 16:06:11 +01:00
entityplayer . getBukkitEntity ( ) . sendSupportedChannels ( ) ;
2011-09-16 09:52:36 +02:00
// CraftBukkit end
2011-09-15 02:23:52 +02:00
2011-04-20 19:05:14 +02:00
netserverhandler . sendPacket ( new Packet6SpawnPosition ( chunkcoordinates . x , chunkcoordinates . y , chunkcoordinates . z ) ) ;
2012-03-22 21:39:39 +01:00
netserverhandler . sendPacket ( new Packet202Abilities ( entityplayer . abilities ) ) ;
2011-05-26 14:48:22 +02:00
this . server . serverConfigurationManager . a ( entityplayer , worldserver ) ;
2012-01-14 23:02:10 +01:00
// this.server.serverConfigurationManager.sendAll(new Packet3Chat("\u00A7e" + entityplayer.name + " joined the game.")); // CraftBukkit - message moved to join event
2011-05-26 14:48:22 +02:00
this . server . serverConfigurationManager . c ( entityplayer ) ;
2012-03-22 22:40:07 +01:00
netserverhandler . a ( entityplayer . locX , entityplayer . locY , entityplayer . locZ , entityplayer . yaw , entityplayer . pitch ) ;
2011-04-20 19:05:14 +02:00
this . server . networkListenThread . a ( netserverhandler ) ;
2011-06-17 08:29:16 +02:00
netserverhandler . sendPacket ( new Packet4UpdateTime ( entityplayer . getPlayerTime ( ) ) ) ; // CraftBukkit - add support for player specific time
2011-09-24 23:03:31 +02:00
Iterator iterator = entityplayer . getEffects ( ) . iterator ( ) ;
2011-09-15 02:23:52 +02:00
while ( iterator . hasNext ( ) ) {
MobEffect mobeffect = ( MobEffect ) iterator . next ( ) ;
netserverhandler . sendPacket ( new Packet41MobEffect ( entityplayer . id , mobeffect ) ) ;
}
2011-04-20 19:05:14 +02:00
entityplayer . syncInventory ( ) ;
2011-02-05 19:15:04 +01:00
}
this . c = true ;
}
public void a ( String s , Object [ ] aobject ) {
2012-01-14 21:03:48 +01:00
logger . info ( this . getName ( ) + " lost connection " ) ;
2011-02-05 19:15:04 +01:00
this . c = true ;
}
2011-09-15 02:23:52 +02:00
public void a ( Packet254GetInfo packet254getinfo ) {
2012-01-14 21:03:48 +01:00
if ( this . networkManager . getSocket ( ) = = null ) return ; // CraftBukkit - fix NPE when a client queries a server that is unable to handle it.
2011-09-15 02:23:52 +02:00
try {
2011-09-27 00:32:05 +02:00
// CraftBukkit start
2012-01-14 21:03:48 +01:00
ServerListPingEvent pingEvent = CraftEventFactory . callServerListPingEvent ( this . server . server , getSocket ( ) . getInetAddress ( ) , this . server . motd , this . server . serverConfigurationManager . getPlayerCount ( ) , this . server . serverConfigurationManager . getMaxPlayers ( ) ) ;
2011-11-30 00:17:43 +01:00
String s = pingEvent . getMotd ( ) + " \ u00A7 " + this . server . serverConfigurationManager . getPlayerCount ( ) + " \ u00A7 " + pingEvent . getMaxPlayers ( ) ;
2011-09-27 00:32:05 +02:00
// CraftBukkit end
2011-09-15 02:23:52 +02:00
this . networkManager . queue ( new Packet255KickDisconnect ( s ) ) ;
this . networkManager . d ( ) ;
2012-01-14 21:03:48 +01:00
this . server . networkListenThread . a ( this . networkManager . getSocket ( ) ) ;
2011-09-15 02:23:52 +02:00
this . c = true ;
} catch ( Exception exception ) {
exception . printStackTrace ( ) ;
}
}
2012-02-29 22:31:04 +01:00
public void onUnhandledPacket ( Packet packet ) {
2011-04-20 19:05:14 +02:00
this . disconnect ( " Protocol error " ) ;
2011-02-05 19:15:04 +01:00
}
2012-01-14 21:03:48 +01:00
public String getName ( ) {
2011-04-20 19:05:14 +02:00
return this . g ! = null ? this . g + " [ " + this . networkManager . getSocketAddress ( ) . toString ( ) + " ] " : this . networkManager . getSocketAddress ( ) . toString ( ) ;
2011-02-05 19:15:04 +01:00
}
2011-04-20 22:47:26 +02:00
public boolean c ( ) {
return true ;
}
2011-02-05 19:15:04 +01:00
static String a ( NetLoginHandler netloginhandler ) {
2012-01-14 21:03:48 +01:00
return netloginhandler . loginKey ;
2011-02-05 19:15:04 +01:00
}
static Packet1Login a ( NetLoginHandler netloginhandler , Packet1Login packet1login ) {
return netloginhandler . h = packet1login ;
}
}