001    package net.minecraft.network.packet;
002    
003    import cpw.mods.fml.relauncher.Side;
004    import cpw.mods.fml.relauncher.SideOnly;
005    import java.io.DataInputStream;
006    import java.io.DataOutputStream;
007    import java.io.IOException;
008    import java.util.List;
009    import net.minecraft.entity.DataWatcher;
010    import net.minecraft.entity.EntityList;
011    import net.minecraft.entity.EntityLiving;
012    import net.minecraft.util.MathHelper;
013    
014    public 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 var2 = 3.9D;
060            double var4 = par1EntityLiving.motionX;
061            double var6 = par1EntityLiving.motionY;
062            double var8 = par1EntityLiving.motionZ;
063    
064            if (var4 < -var2)
065            {
066                var4 = -var2;
067            }
068    
069            if (var6 < -var2)
070            {
071                var6 = -var2;
072            }
073    
074            if (var8 < -var2)
075            {
076                var8 = -var2;
077            }
078    
079            if (var4 > var2)
080            {
081                var4 = var2;
082            }
083    
084            if (var6 > var2)
085            {
086                var6 = var2;
087            }
088    
089            if (var8 > var2)
090            {
091                var8 = var2;
092            }
093    
094            this.velocityX = (int)(var4 * 8000.0D);
095            this.velocityY = (int)(var6 * 8000.0D);
096            this.velocityZ = (int)(var8 * 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.func_75685_c();
160            }
161    
162            return this.metadata;
163        }
164    }