    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.player.EntityPlayer;
    011import net.minecraft.item.ItemStack;
    012import net.minecraft.util.MathHelper;
    013
    014public class Packet20NamedEntitySpawn extends Packet
    015{
    016    /** The entity ID, in this case it's the player ID. */
    017    public int entityId;
    018
    019    /** The player's name. */
    020    public String name;
    021
    022    /** The player's X position. */
    023    public int xPosition;
    024
    025    /** The player's Y position. */
    026    public int yPosition;
    027
    028    /** The player's Z position. */
    029    public int zPosition;
    030
    031    /** The player's rotation. */
    032    public byte rotation;
    033
    034    /** The player's pitch. */
    035    public byte pitch;
    036
    037    /** The current item the player is holding. */
    038    public int currentItem;
    039    private DataWatcher metadata;
    040    private List metadataWatchableObjects;
    041
    042    public Packet20NamedEntitySpawn() {}
    043
    044    public Packet20NamedEntitySpawn(EntityPlayer par1EntityPlayer)
    045    {
    046        this.entityId = par1EntityPlayer.entityId;
    047        this.name = par1EntityPlayer.username;
    048        this.xPosition = MathHelper.floor_double(par1EntityPlayer.posX * 32.0D);
    049        this.yPosition = MathHelper.floor_double(par1EntityPlayer.posY * 32.0D);
    050        this.zPosition = MathHelper.floor_double(par1EntityPlayer.posZ * 32.0D);
    051        this.rotation = (byte)((int)(par1EntityPlayer.rotationYaw * 256.0F / 360.0F));
    052        this.pitch = (byte)((int)(par1EntityPlayer.rotationPitch * 256.0F / 360.0F));
    053        ItemStack itemstack = par1EntityPlayer.inventory.getCurrentItem();
    054        this.currentItem = itemstack == null ? 0 : itemstack.itemID;
    055        this.metadata = par1EntityPlayer.getDataWatcher();
    056    }
    057
    058    /**
    059     * Abstract. Reads the raw packet data from the data stream.
    060     */
    061    public void readPacketData(DataInputStream par1DataInputStream) throws IOException
    062    {
    063        this.entityId = par1DataInputStream.readInt();
    064        this.name = readString(par1DataInputStream, 16);
    065        this.xPosition = par1DataInputStream.readInt();
    066        this.yPosition = par1DataInputStream.readInt();
    067        this.zPosition = par1DataInputStream.readInt();
    068        this.rotation = par1DataInputStream.readByte();
    069        this.pitch = par1DataInputStream.readByte();
    070        this.currentItem = par1DataInputStream.readShort();
    071        this.metadataWatchableObjects = DataWatcher.readWatchableObjects(par1DataInputStream);
    072    }
    073
    074    /**
    075     * Abstract. Writes the raw packet data to the data stream.
    076     */
    077    public void writePacketData(DataOutputStream par1DataOutputStream) throws IOException
    078    {
    079        par1DataOutputStream.writeInt(this.entityId);
    080        writeString(this.name, par1DataOutputStream);
    081        par1DataOutputStream.writeInt(this.xPosition);
    082        par1DataOutputStream.writeInt(this.yPosition);
    083        par1DataOutputStream.writeInt(this.zPosition);
    084        par1DataOutputStream.writeByte(this.rotation);
    085        par1DataOutputStream.writeByte(this.pitch);
    086        par1DataOutputStream.writeShort(this.currentItem);
    087        this.metadata.writeWatchableObjects(par1DataOutputStream);
    088    }
    089
    090    /**
    091     * Passes this Packet on to the NetHandler for processing.
    092     */
    093    public void processPacket(NetHandler par1NetHandler)
    094    {
    095        par1NetHandler.handleNamedEntitySpawn(this);
    096    }
    097
    098    /**
    099     * Abstract. Return the size of the packet (not counting the header).
    100     */
    101    public int getPacketSize()
    102    {
    103        return 28;
    104    }
    105
    106    @SideOnly(Side.CLIENT)
    107    public List getWatchedMetadata()
    108    {
    109        if (this.metadataWatchableObjects == null)
    110        {
    111            this.metadataWatchableObjects = this.metadata.getAllWatched();
    112        }
    113
    114        return this.metadataWatchableObjects;
    115    }
    116}