    001package net.minecraft.network.packet;
    002
    003import java.io.DataInputStream;
    004import java.io.DataOutputStream;
    005import java.io.IOException;
    006import net.minecraft.entity.Entity;
    007import net.minecraft.util.MathHelper;
    008
    009public class Packet23VehicleSpawn extends Packet
    010{
    011    /** Entity ID of the object. */
    012    public int entityId;
    013
    014    /** The X position of the object. */
    015    public int xPosition;
    016
    017    /** The Y position of the object. */
    018    public int yPosition;
    019
    020    /** The Z position of the object. */
    021    public int zPosition;
    022
    023    /**
    024     * Not sent if the thrower entity ID is 0. The speed of this fireball along the X axis.
    025     */
    026    public int speedX;
    027
    028    /**
    029     * Not sent if the thrower entity ID is 0. The speed of this fireball along the Y axis.
    030     */
    031    public int speedY;
    032
    033    /**
    034     * Not sent if the thrower entity ID is 0. The speed of this fireball along the Z axis.
    035     */
    036    public int speedZ;
    037
    038    /** The pitch in steps of 2p/256 */
    039    public int pitch;
    040
    041    /** The yaw in steps of 2p/256 */
    042    public int yaw;
    043
    044    /** The type of object. */
    045    public int type;
    046
    047    /** 0 if not a fireball. Otherwise, this is the Entity ID of the thrower. */
    048    public int throwerEntityId;
    049
    050    public Packet23VehicleSpawn() {}
    051
    052    public Packet23VehicleSpawn(Entity par1Entity, int par2)
    053    {
    054        this(par1Entity, par2, 0);
    055    }
    056
    057    public Packet23VehicleSpawn(Entity par1Entity, int par2, int par3)
    058    {
    059        this.entityId = par1Entity.entityId;
    060        this.xPosition = MathHelper.floor_double(par1Entity.posX * 32.0D);
    061        this.yPosition = MathHelper.floor_double(par1Entity.posY * 32.0D);
    062        this.zPosition = MathHelper.floor_double(par1Entity.posZ * 32.0D);
    063        this.pitch = MathHelper.floor_float(par1Entity.rotationPitch * 256.0F / 360.0F);
    064        this.yaw = MathHelper.floor_float(par1Entity.rotationYaw * 256.0F / 360.0F);
    065        this.type = par2;
    066        this.throwerEntityId = par3;
    067
    068        if (par3 > 0)
    069        {
    070            double d0 = par1Entity.motionX;
    071            double d1 = par1Entity.motionY;
    072            double d2 = par1Entity.motionZ;
    073            double d3 = 3.9D;
    074
    075            if (d0 < -d3)
    076            {
    077                d0 = -d3;
    078            }
    079
    080            if (d1 < -d3)
    081            {
    082                d1 = -d3;
    083            }
    084
    085            if (d2 < -d3)
    086            {
    087                d2 = -d3;
    088            }
    089
    090            if (d0 > d3)
    091            {
    092                d0 = d3;
    093            }
    094
    095            if (d1 > d3)
    096            {
    097                d1 = d3;
    098            }
    099
    100            if (d2 > d3)
    101            {
    102                d2 = d3;
    103            }
    104
    105            this.speedX = (int)(d0 * 8000.0D);
    106            this.speedY = (int)(d1 * 8000.0D);
    107            this.speedZ = (int)(d2 * 8000.0D);
    108        }
    109    }
    110
    111    /**
    112     * Abstract. Reads the raw packet data from the data stream.
    113     */
    114    public void readPacketData(DataInputStream par1DataInputStream) throws IOException
    115    {
    116        this.entityId = par1DataInputStream.readInt();
    117        this.type = par1DataInputStream.readByte();
    118        this.xPosition = par1DataInputStream.readInt();
    119        this.yPosition = par1DataInputStream.readInt();
    120        this.zPosition = par1DataInputStream.readInt();
    121        this.pitch = par1DataInputStream.readByte();
    122        this.yaw = par1DataInputStream.readByte();
    123        this.throwerEntityId = par1DataInputStream.readInt();
    124
    125        if (this.throwerEntityId > 0)
    126        {
    127            this.speedX = par1DataInputStream.readShort();
    128            this.speedY = par1DataInputStream.readShort();
    129            this.speedZ = par1DataInputStream.readShort();
    130        }
    131    }
    132
    133    /**
    134     * Abstract. Writes the raw packet data to the data stream.
    135     */
    136    public void writePacketData(DataOutputStream par1DataOutputStream) throws IOException
    137    {
    138        par1DataOutputStream.writeInt(this.entityId);
    139        par1DataOutputStream.writeByte(this.type);
    140        par1DataOutputStream.writeInt(this.xPosition);
    141        par1DataOutputStream.writeInt(this.yPosition);
    142        par1DataOutputStream.writeInt(this.zPosition);
    143        par1DataOutputStream.writeByte(this.pitch);
    144        par1DataOutputStream.writeByte(this.yaw);
    145        par1DataOutputStream.writeInt(this.throwerEntityId);
    146
    147        if (this.throwerEntityId > 0)
    148        {
    149            par1DataOutputStream.writeShort(this.speedX);
    150            par1DataOutputStream.writeShort(this.speedY);
    151            par1DataOutputStream.writeShort(this.speedZ);
    152        }
    153    }
    154
    155    /**
    156     * Passes this Packet on to the NetHandler for processing.
    157     */
    158    public void processPacket(NetHandler par1NetHandler)
    159    {
    160        par1NetHandler.handleVehicleSpawn(this);
    161    }
    162
    163    /**
    164     * Abstract. Return the size of the packet (not counting the header).
    165     */
    166    public int getPacketSize()
    167    {
    168        return 21 + this.throwerEntityId > 0 ? 6 : 0;
    169    }
    170}