    001package net.minecraft.network.packet;
    002
    003import java.io.DataInputStream;
    004import java.io.DataOutputStream;
    005import java.io.IOException;
    006
    007import cpw.mods.fml.common.network.FMLNetworkHandler;
    008import net.minecraft.world.EnumGameType;
    009import net.minecraft.world.WorldType;
    010
    011public class Packet1Login extends Packet
    012{
    013    /** The player's entity ID */
    014    public int clientEntityId = 0;
    015    public WorldType terrainType;
    016    public boolean hardcoreMode;
    017    public EnumGameType gameType;
    018
    019    /** -1: The Nether, 0: The Overworld, 1: The End */
    020    public int dimension;
    021
    022    /** The difficulty setting byte. */
    023    public byte difficultySetting;
    024
    025    /** Defaults to 128 */
    026    public byte worldHeight;
    027
    028    /** The maximum players. */
    029    public byte maxPlayers;
    030
    031    private boolean vanillaCompatible;
    032
    033    public Packet1Login()
    034    {
    035        this.vanillaCompatible = FMLNetworkHandler.vanillaLoginPacketCompatibility();
    036    }
    037
    038    public Packet1Login(int par1, WorldType par2WorldType, EnumGameType par3EnumGameType, boolean par4, int par5, int par6, int par7, int par8)
    039    {
    040        this.clientEntityId = par1;
    041        this.terrainType = par2WorldType;
    042        this.dimension = par5;
    043        this.difficultySetting = (byte)par6;
    044        this.gameType = par3EnumGameType;
    045        this.worldHeight = (byte)par7;
    046        this.maxPlayers = (byte)par8;
    047        this.hardcoreMode = par4;
    048        this.vanillaCompatible = false;
    049    }
    050
    051    /**
    052     * Abstract. Reads the raw packet data from the data stream.
    053     */
    054    public void readPacketData(DataInputStream par1DataInputStream) throws IOException
    055    {
    056        this.clientEntityId = par1DataInputStream.readInt();
    057        String s = readString(par1DataInputStream, 16);
    058        this.terrainType = WorldType.parseWorldType(s);
    059
    060        if (this.terrainType == null)
    061        {
    062            this.terrainType = WorldType.DEFAULT;
    063        }
    064
    065        byte b0 = par1DataInputStream.readByte();
    066        this.hardcoreMode = (b0 & 8) == 8;
    067        int i = b0 & -9;
    068        this.gameType = EnumGameType.getByID(i);
    069
    070        if (vanillaCompatible)
    071        {
    072            this.dimension = par1DataInputStream.readByte();
    073        }
    074        else
    075        {
    076            this.dimension = par1DataInputStream.readInt();
    077        }
    078
    079        this.difficultySetting = par1DataInputStream.readByte();
    080        this.worldHeight = par1DataInputStream.readByte();
    081        this.maxPlayers = par1DataInputStream.readByte();
    082    }
    083
    084    /**
    085     * Abstract. Writes the raw packet data to the data stream.
    086     */
    087    public void writePacketData(DataOutputStream par1DataOutputStream) throws IOException
    088    {
    089        par1DataOutputStream.writeInt(this.clientEntityId);
    090        writeString(this.terrainType == null ? "" : this.terrainType.getWorldTypeName(), par1DataOutputStream);
    091        int i = this.gameType.getID();
    092
    093        if (this.hardcoreMode)
    094        {
    095            i |= 8;
    096        }
    097
    098        par1DataOutputStream.writeByte(i);
    099
    100        if (vanillaCompatible)
    101        {
    102            par1DataOutputStream.writeByte(this.dimension);
    103        }
    104        else
    105        {
    106            par1DataOutputStream.writeInt(this.dimension);
    107        }
    108
    109        par1DataOutputStream.writeByte(this.difficultySetting);
    110        par1DataOutputStream.writeByte(this.worldHeight);
    111        par1DataOutputStream.writeByte(this.maxPlayers);
    112    }
    113
    114    /**
    115     * Passes this Packet on to the NetHandler for processing.
    116     */
    117    public void processPacket(NetHandler par1NetHandler)
    118    {
    119        par1NetHandler.handleLogin(this);
    120    }
    121
    122    /**
    123     * Abstract. Return the size of the packet (not counting the header).
    124     */
    125    public int getPacketSize()
    126    {
    127        int i = 0;
    128
    129        if (this.terrainType != null)
    130        {
    131            i = this.terrainType.getWorldTypeName().length();
    132        }
    133
    134        return 6 + 2 * i + 4 + 4 + 1 + 1 + 1 + (vanillaCompatible ? 0 : 3);
    135    }
    136}