    001package net.minecraft.tileentity;
    002
    003import java.util.Random;
    004import net.minecraft.entity.player.EntityPlayer;
    005import net.minecraft.nbt.NBTTagCompound;
    006
    007public class TileEntityEnchantmentTable extends TileEntity
    008{
    009    /** Used by the render to make the book 'bounce' */
    010    public int tickCount;
    011
    012    /** Value used for determining how the page flip should look. */
    013    public float pageFlip;
    014
    015    /** The last tick's pageFlip value. */
    016    public float pageFlipPrev;
    017    public float field_70373_d;
    018    public float field_70374_e;
    019
    020    /** The amount that the book is open. */
    021    public float bookSpread;
    022
    023    /** The amount that the book is open. */
    024    public float bookSpreadPrev;
    025    public float bookRotation2;
    026    public float bookRotationPrev;
    027    public float bookRotation;
    028    private static Random rand = new Random();
    029    private String field_94136_s;
    030
    031    /**
    032     * Writes a tile entity to NBT.
    033     */
    034    public void writeToNBT(NBTTagCompound par1NBTTagCompound)
    035    {
    036        super.writeToNBT(par1NBTTagCompound);
    037
    038        if (this.func_94135_b())
    039        {
    040            par1NBTTagCompound.setString("CustomName", this.field_94136_s);
    041        }
    042    }
    043
    044    /**
    045     * Reads a tile entity from NBT.
    046     */
    047    public void readFromNBT(NBTTagCompound par1NBTTagCompound)
    048    {
    049        super.readFromNBT(par1NBTTagCompound);
    050
    051        if (par1NBTTagCompound.hasKey("CustomName"))
    052        {
    053            this.field_94136_s = par1NBTTagCompound.getString("CustomName");
    054        }
    055    }
    056
    057    /**
    058     * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count
    059     * ticks and creates a new spawn inside its implementation.
    060     */
    061    public void updateEntity()
    062    {
    063        super.updateEntity();
    064        this.bookSpreadPrev = this.bookSpread;
    065        this.bookRotationPrev = this.bookRotation2;
    066        EntityPlayer entityplayer = this.worldObj.getClosestPlayer((double)((float)this.xCoord + 0.5F), (double)((float)this.yCoord + 0.5F), (double)((float)this.zCoord + 0.5F), 3.0D);
    067
    068        if (entityplayer != null)
    069        {
    070            double d0 = entityplayer.posX - (double)((float)this.xCoord + 0.5F);
    071            double d1 = entityplayer.posZ - (double)((float)this.zCoord + 0.5F);
    072            this.bookRotation = (float)Math.atan2(d1, d0);
    073            this.bookSpread += 0.1F;
    074
    075            if (this.bookSpread < 0.5F || rand.nextInt(40) == 0)
    076            {
    077                float f = this.field_70373_d;
    078
    079                do
    080                {
    081                    this.field_70373_d += (float)(rand.nextInt(4) - rand.nextInt(4));
    082                }
    083                while (f == this.field_70373_d);
    084            }
    085        }
    086        else
    087        {
    088            this.bookRotation += 0.02F;
    089            this.bookSpread -= 0.1F;
    090        }
    091
    092        while (this.bookRotation2 >= (float)Math.PI)
    093        {
    094            this.bookRotation2 -= ((float)Math.PI * 2F);
    095        }
    096
    097        while (this.bookRotation2 < -(float)Math.PI)
    098        {
    099            this.bookRotation2 += ((float)Math.PI * 2F);
    100        }
    101
    102        while (this.bookRotation >= (float)Math.PI)
    103        {
    104            this.bookRotation -= ((float)Math.PI * 2F);
    105        }
    106
    107        while (this.bookRotation < -(float)Math.PI)
    108        {
    109            this.bookRotation += ((float)Math.PI * 2F);
    110        }
    111
    112        float f1;
    113
    114        for (f1 = this.bookRotation - this.bookRotation2; f1 >= (float)Math.PI; f1 -= ((float)Math.PI * 2F))
    115        {
    116            ;
    117        }
    118
    119        while (f1 < -(float)Math.PI)
    120        {
    121            f1 += ((float)Math.PI * 2F);
    122        }
    123
    124        this.bookRotation2 += f1 * 0.4F;
    125
    126        if (this.bookSpread < 0.0F)
    127        {
    128            this.bookSpread = 0.0F;
    129        }
    130
    131        if (this.bookSpread > 1.0F)
    132        {
    133            this.bookSpread = 1.0F;
    134        }
    135
    136        ++this.tickCount;
    137        this.pageFlipPrev = this.pageFlip;
    138        float f2 = (this.field_70373_d - this.pageFlip) * 0.4F;
    139        float f3 = 0.2F;
    140
    141        if (f2 < -f3)
    142        {
    143            f2 = -f3;
    144        }
    145
    146        if (f2 > f3)
    147        {
    148            f2 = f3;
    149        }
    150
    151        this.field_70374_e += (f2 - this.field_70374_e) * 0.9F;
    152        this.pageFlip += this.field_70374_e;
    153    }
    154
    155    public String func_94133_a()
    156    {
    157        return this.func_94135_b() ? this.field_94136_s : "container.enchant";
    158    }
    159
    160    public boolean func_94135_b()
    161    {
    162        return this.field_94136_s != null && this.field_94136_s.length() > 0;
    163    }
    164
    165    public void func_94134_a(String par1Str)
    166    {
    167        this.field_94136_s = par1Str;
    168    }
    169}