| 1 | package net.minecraft.tileentity;
|
| 2 |
|
| 3 | import java.util.Random;
|
| 4 | import net.minecraft.entity.player.EntityPlayer;
|
| 5 | import net.minecraft.entity.player.InventoryPlayer;
|
| 6 | import net.minecraft.inventory.Container;
|
| 7 | import net.minecraft.inventory.ContainerEnchantment;
|
| 8 | import net.minecraft.nbt.NBTTagCompound;
|
| 9 | import net.minecraft.util.ChatComponentText;
|
| 10 | import net.minecraft.util.ChatComponentTranslation;
|
| 11 | import net.minecraft.util.IChatComponent;
|
| 12 | import net.minecraft.util.ITickable;
|
| 13 | import net.minecraft.util.MathHelper;
|
| 14 | import net.minecraft.world.IInteractionObject;
|
| 15 |
|
| 16 | public class TileEntityEnchantmentTable extends TileEntity implements ITickable, IInteractionObject
|
| 17 | {
|
| 18 | public int tickCount;
|
| 19 | public float pageFlip;
|
| 20 | public float pageFlipPrev;
|
| 21 | public float field_145932_k;
|
| 22 | public float field_145929_l;
|
| 23 | public float bookSpread;
|
| 24 | public float bookSpreadPrev;
|
| 25 | public float bookRotation;
|
| 26 | public float bookRotationPrev;
|
| 27 | public float field_145924_q;
|
| 28 | private static Random rand = new Random();
|
| 29 | private String customName;
|
| 30 |
|
| 31 | public void writeToNBT(NBTTagCompound compound)
|
| 32 | {
|
| 33 | super.writeToNBT(compound);
|
| 34 |
|
| 35 | if (this.hasCustomName())
|
| 36 | {
|
| 37 | compound.setString("CustomName", this.customName);
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | public void readFromNBT(NBTTagCompound compound)
|
| 42 | {
|
| 43 | super.readFromNBT(compound);
|
| 44 |
|
| 45 | if (compound.hasKey("CustomName", 8))
|
| 46 | {
|
| 47 | this.customName = compound.getString("CustomName");
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | /**
|
| 52 | * Like the old updateEntity(), except more generic.
|
| 53 | */
|
| 54 | public void update()
|
| 55 | {
|
| 56 | this.bookSpreadPrev = this.bookSpread;
|
| 57 | this.bookRotationPrev = this.bookRotation;
|
| 58 | EntityPlayer entityplayer = this.worldObj.getClosestPlayer((double)((float)this.pos.getX() + 0.5F), (double)((float)this.pos.getY() + 0.5F), (double)((float)this.pos.getZ() + 0.5F), 3.0D);
|
| 59 |
|
| 60 | if (entityplayer != null)
|
| 61 | {
|
| 62 | double d0 = entityplayer.posX - (double)((float)this.pos.getX() + 0.5F);
|
| 63 | double d1 = entityplayer.posZ - (double)((float)this.pos.getZ() + 0.5F);
|
| 64 | this.field_145924_q = (float)MathHelper.atan2(d1, d0);
|
| 65 | this.bookSpread += 0.1F;
|
| 66 |
|
| 67 | if (this.bookSpread < 0.5F || rand.nextInt(40) == 0)
|
| 68 | {
|
| 69 | float f1 = this.field_145932_k;
|
| 70 |
|
| 71 | while (true)
|
| 72 | {
|
| 73 | this.field_145932_k += (float)(rand.nextInt(4) - rand.nextInt(4));
|
| 74 |
|
| 75 | if (f1 != this.field_145932_k)
|
| 76 | {
|
| 77 | break;
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 | else
|
| 83 | {
|
| 84 | this.field_145924_q += 0.02F;
|
| 85 | this.bookSpread -= 0.1F;
|
| 86 | }
|
| 87 |
|
| 88 | while (this.bookRotation >= (float)Math.PI)
|
| 89 | {
|
| 90 | this.bookRotation -= ((float)Math.PI * 2F);
|
| 91 | }
|
| 92 |
|
| 93 | while (this.bookRotation < -(float)Math.PI)
|
| 94 | {
|
| 95 | this.bookRotation += ((float)Math.PI * 2F);
|
| 96 | }
|
| 97 |
|
| 98 | while (this.field_145924_q >= (float)Math.PI)
|
| 99 | {
|
| 100 | this.field_145924_q -= ((float)Math.PI * 2F);
|
| 101 | }
|
| 102 |
|
| 103 | while (this.field_145924_q < -(float)Math.PI)
|
| 104 | {
|
| 105 | this.field_145924_q += ((float)Math.PI * 2F);
|
| 106 | }
|
| 107 |
|
| 108 | float f2;
|
| 109 |
|
| 110 | for (f2 = this.field_145924_q - this.bookRotation; f2 >= (float)Math.PI; f2 -= ((float)Math.PI * 2F))
|
| 111 | {
|
| 112 | ;
|
| 113 | }
|
| 114 |
|
| 115 | while (f2 < -(float)Math.PI)
|
| 116 | {
|
| 117 | f2 += ((float)Math.PI * 2F);
|
| 118 | }
|
| 119 |
|
| 120 | this.bookRotation += f2 * 0.4F;
|
| 121 | this.bookSpread = MathHelper.clamp_float(this.bookSpread, 0.0F, 1.0F);
|
| 122 | ++this.tickCount;
|
| 123 | this.pageFlipPrev = this.pageFlip;
|
| 124 | float f = (this.field_145932_k - this.pageFlip) * 0.4F;
|
| 125 | float f3 = 0.2F;
|
| 126 | f = MathHelper.clamp_float(f, -f3, f3);
|
| 127 | this.field_145929_l += (f - this.field_145929_l) * 0.9F;
|
| 128 | this.pageFlip += this.field_145929_l;
|
| 129 | }
|
| 130 |
|
| 131 | /**
|
| 132 | * Get the name of this object. For players this returns their username
|
| 133 | */
|
| 134 | public String getName()
|
| 135 | {
|
| 136 | return this.hasCustomName() ? this.customName : "container.enchant";
|
| 137 | }
|
| 138 |
|
| 139 | /**
|
| 140 | * Returns true if this thing is named
|
| 141 | */
|
| 142 | public boolean hasCustomName()
|
| 143 | {
|
| 144 | return this.customName != null && this.customName.length() > 0;
|
| 145 | }
|
| 146 |
|
| 147 | public void setCustomName(String customNameIn)
|
| 148 | {
|
| 149 | this.customName = customNameIn;
|
| 150 | }
|
| 151 |
|
| 152 | /**
|
| 153 | * Get the formatted ChatComponent that will be used for the sender's username in chat
|
| 154 | */
|
| 155 | public IChatComponent getDisplayName()
|
| 156 | {
|
| 157 | return (IChatComponent)(this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName(), new Object[0]));
|
| 158 | }
|
| 159 |
|
| 160 | public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
|
| 161 | {
|
| 162 | return new ContainerEnchantment(playerInventory, this.worldObj, this.pos);
|
| 163 | }
|
| 164 |
|
| 165 | public String getGuiID()
|
| 166 | {
|
| 167 | return "minecraft:enchanting_table";
|
| 168 | }
|
| 169 | }
|
| 170 |
|