    001package net.minecraft.network.packet;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.io.DataInputStream;
    006import java.io.DataOutputStream;
    007import java.io.IOException;
    008import java.util.List;
    009import net.minecraft.entity.DataWatcher;
    010import net.minecraft.entity.EntityList;
    011import net.minecraft.entity.EntityLiving;
    012import net.minecraft.util.MathHelper;
    013
    014public class Packet24MobSpawn extends Packet
    015{
    016    /** The entity ID. */
    017    public int entityId;
    018
    019    /** The type of mob. */
    020    public int type;
    021
    022    /** The X position of the entity. */
    023    public int xPosition;
    024
    025    /** The Y position of the entity. */
    026    public int yPosition;
    027
    028    /** The Z position of the entity. */
    029    public int zPosition;
    030    public int velocityX;
    031    public int velocityY;
    032    public int velocityZ;
    033
    034    /** The yaw of the entity. */
    035    public byte yaw;
    036
    037    /** The pitch of the entity. */
    038    public byte pitch;
    039
    040    /** The yaw of the entity's head. */
    041    public byte headYaw;
    042
    043    /** Indexed metadata for Mob, terminated by 0x7F */
    044    private DataWatcher metaData;
    045    private List metadata;
    046
    047    public Packet24MobSpawn() {}
    048
    049    public Packet24MobSpawn(EntityLiving par1EntityLiving)
    050    {
    051        this.entityId = par1EntityLiving.entityId;
    052        this.type = (byte)EntityList.getEntityID(par1EntityLiving);
    053        this.xPosition = par1EntityLiving.myEntitySize.multiplyBy32AndRound(par1EntityLiving.posX);
    054        this.yPosition = MathHelper.floor_double(par1EntityLiving.posY * 32.0D);
    055        this.zPosition = par1EntityLiving.myEntitySize.multiplyBy32AndRound(par1EntityLiving.posZ);
    056        this.yaw = (byte)((int)(par1EntityLiving.rotationYaw * 256.0F / 360.0F));
    057        this.pitch = (byte)((int)(par1EntityLiving.rotationPitch * 256.0F / 360.0F));
    058        this.headYaw = (byte)((int)(par1EntityLiving.rotationYawHead * 256.0F / 360.0F));
    059        double d0 = 3.9D;
    060        double d1 = par1EntityLiving.motionX;
    061        double d2 = par1EntityLiving.motionY;
    062        double d3 = par1EntityLiving.motionZ;
    063
    064        if (d1 < -d0)
    065        {
    066            d1 = -d0;
    067        }
    068
    069        if (d2 < -d0)
    070        {
    071            d2 = -d0;
    072        }
    073
    074        if (d3 < -d0)
    075        {
    076            d3 = -d0;
    077        }
    078
    079        if (d1 > d0)
    080        {
    081            d1 = d0;
    082        }
    083
    084        if (d2 > d0)
    085        {
    086            d2 = d0;
    087        }
    088
    089        if (d3 > d0)
    090        {
    091            d3 = d0;
    092        }
    093
    094        this.velocityX = (int)(d1 * 8000.0D);
    095        this.velocityY = (int)(d2 * 8000.0D);
    096        this.velocityZ = (int)(d3 * 8000.0D);
    097        this.metaData = par1EntityLiving.getDataWatcher();
    098    }
    099
    100    /**
    101     * Abstract. Reads the raw packet data from the data stream.
    102     */
    103    public void readPacketData(DataInputStream par1DataInputStream) throws IOException
    104    {
    105        this.entityId = par1DataInputStream.readInt();
    106        this.type = par1DataInputStream.readByte() & 255;
    107        this.xPosition = par1DataInputStream.readInt();
    108        this.yPosition = par1DataInputStream.readInt();
    109        this.zPosition = par1DataInputStream.readInt();
    110        this.yaw = par1DataInputStream.readByte();
    111        this.pitch = par1DataInputStream.readByte();
    112        this.headYaw = par1DataInputStream.readByte();
    113        this.velocityX = par1DataInputStream.readShort();
    114        this.velocityY = par1DataInputStream.readShort();
    115        this.velocityZ = par1DataInputStream.readShort();
    116        this.metadata = DataWatcher.readWatchableObjects(par1DataInputStream);
    117    }
    118
    119    /**
    120     * Abstract. Writes the raw packet data to the data stream.
    121     */
    122    public void writePacketData(DataOutputStream par1DataOutputStream) throws IOException
    123    {
    124        par1DataOutputStream.writeInt(this.entityId);
    125        par1DataOutputStream.writeByte(this.type & 255);
    126        par1DataOutputStream.writeInt(this.xPosition);
    127        par1DataOutputStream.writeInt(this.yPosition);
    128        par1DataOutputStream.writeInt(this.zPosition);
    129        par1DataOutputStream.writeByte(this.yaw);
    130        par1DataOutputStream.writeByte(this.pitch);
    131        par1DataOutputStream.writeByte(this.headYaw);
    132        par1DataOutputStream.writeShort(this.velocityX);
    133        par1DataOutputStream.writeShort(this.velocityY);
    134        par1DataOutputStream.writeShort(this.velocityZ);
    135        this.metaData.writeWatchableObjects(par1DataOutputStream);
    136    }
    137
    138    /**
    139     * Passes this Packet on to the NetHandler for processing.
    140     */
    141    public void processPacket(NetHandler par1NetHandler)
    142    {
    143        par1NetHandler.handleMobSpawn(this);
    144    }
    145
    146    /**
    147     * Abstract. Return the size of the packet (not counting the header).
    148     */
    149    public int getPacketSize()
    150    {
    151        return 26;
    152    }
    153
    154    @SideOnly(Side.CLIENT)
    155    public List getMetadata()
    156    {
    157        if (this.metadata == null)
    158        {
    159            this.metadata = this.metaData.getAllWatched();
    160        }
    161
    162        return this.metadata;
    163    }
    164}