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    public int field_92077_h;
038    public int field_92078_i;
039
040    /** The type of object. */
041    public int type;
042
043    /** 0 if not a fireball. Otherwise, this is the Entity ID of the thrower. */
044    public int throwerEntityId;
045
046    public Packet23VehicleSpawn() {}
047
048    public Packet23VehicleSpawn(Entity par1Entity, int par2)
049    {
050        this(par1Entity, par2, 0);
051    }
052
053    public Packet23VehicleSpawn(Entity par1Entity, int par2, int par3)
054    {
055        this.entityId = par1Entity.entityId;
056        this.xPosition = MathHelper.floor_double(par1Entity.posX * 32.0D);
057        this.yPosition = MathHelper.floor_double(par1Entity.posY * 32.0D);
058        this.zPosition = MathHelper.floor_double(par1Entity.posZ * 32.0D);
059        this.field_92077_h = MathHelper.floor_float(par1Entity.rotationPitch * 256.0F / 360.0F);
060        this.field_92078_i = MathHelper.floor_float(par1Entity.rotationYaw * 256.0F / 360.0F);
061        this.type = par2;
062        this.throwerEntityId = par3;
063
064        if (par3 > 0)
065        {
066            double d0 = par1Entity.motionX;
067            double d1 = par1Entity.motionY;
068            double d2 = par1Entity.motionZ;
069            double d3 = 3.9D;
070
071            if (d0 < -d3)
072            {
073                d0 = -d3;
074            }
075
076            if (d1 < -d3)
077            {
078                d1 = -d3;
079            }
080
081            if (d2 < -d3)
082            {
083                d2 = -d3;
084            }
085
086            if (d0 > d3)
087            {
088                d0 = d3;
089            }
090
091            if (d1 > d3)
092            {
093                d1 = d3;
094            }
095
096            if (d2 > d3)
097            {
098                d2 = d3;
099            }
100
101            this.speedX = (int)(d0 * 8000.0D);
102            this.speedY = (int)(d1 * 8000.0D);
103            this.speedZ = (int)(d2 * 8000.0D);
104        }
105    }
106
107    /**
108     * Abstract. Reads the raw packet data from the data stream.
109     */
110    public void readPacketData(DataInputStream par1DataInputStream) throws IOException
111    {
112        this.entityId = par1DataInputStream.readInt();
113        this.type = par1DataInputStream.readByte();
114        this.xPosition = par1DataInputStream.readInt();
115        this.yPosition = par1DataInputStream.readInt();
116        this.zPosition = par1DataInputStream.readInt();
117        this.field_92077_h = par1DataInputStream.readByte();
118        this.field_92078_i = par1DataInputStream.readByte();
119        this.throwerEntityId = par1DataInputStream.readInt();
120
121        if (this.throwerEntityId > 0)
122        {
123            this.speedX = par1DataInputStream.readShort();
124            this.speedY = par1DataInputStream.readShort();
125            this.speedZ = par1DataInputStream.readShort();
126        }
127    }
128
129    /**
130     * Abstract. Writes the raw packet data to the data stream.
131     */
132    public void writePacketData(DataOutputStream par1DataOutputStream) throws IOException
133    {
134        par1DataOutputStream.writeInt(this.entityId);
135        par1DataOutputStream.writeByte(this.type);
136        par1DataOutputStream.writeInt(this.xPosition);
137        par1DataOutputStream.writeInt(this.yPosition);
138        par1DataOutputStream.writeInt(this.zPosition);
139        par1DataOutputStream.writeByte(this.field_92077_h);
140        par1DataOutputStream.writeByte(this.field_92078_i);
141        par1DataOutputStream.writeInt(this.throwerEntityId);
142
143        if (this.throwerEntityId > 0)
144        {
145            par1DataOutputStream.writeShort(this.speedX);
146            par1DataOutputStream.writeShort(this.speedY);
147            par1DataOutputStream.writeShort(this.speedZ);
148        }
149    }
150
151    /**
152     * Passes this Packet on to the NetHandler for processing.
153     */
154    public void processPacket(NetHandler par1NetHandler)
155    {
156        par1NetHandler.handleVehicleSpawn(this);
157    }
158
159    /**
160     * Abstract. Return the size of the packet (not counting the header).
161     */
162    public int getPacketSize()
163    {
164        return 21 + this.throwerEntityId > 0 ? 6 : 0;
165    }
166}