summaryrefslogtreecommitdiff
path: root/src/main/java/darkknight/jewelrycraft/tileentity/TileEntityHandPedestal.java
blob: f6fb32326b092b2f1d7243ce38a50b9bb317c72f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package darkknight.jewelrycraft.tileentity;

import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

/**
 * @author Paul Fulham (pau101)
 */
public class TileEntityHandPedestal extends TileEntity {
	protected boolean	isDirty;
	protected ItemStack	heldItemStack;
	/**
	 * When the hand is open the grip is 0 and is 20 when closed.
	 */
	private float		grip;
	private float		prevGrip;
	private float		gripMax;
	private float		gripScale;
	private boolean		isHandOpen;

	/**
	 * 
	 */
	public TileEntityHandPedestal() {
		isDirty = false;
		heldItemStack = null;
		grip = 0;
		gripMax = 20;
		gripScale = 1;
		isHandOpen = true;
	}

	/**
	 * @param tagCompound
	 */
	@Override
	public void writeToNBT(NBTTagCompound tagCompound) {
		super.writeToNBT(tagCompound);
		if (heldItemStack != null) {
			NBTTagCompound objectCompound = new NBTTagCompound();
			heldItemStack.writeToNBT(objectCompound);
			tagCompound.setTag("object", objectCompound);
		}
		tagCompound.setBoolean("isHandOpen", isHandOpen);
	}

	/**
	 * @param tagCompound
	 */
	@Override
	public void readFromNBT(NBTTagCompound tagCompound) {
		super.readFromNBT(tagCompound);
		if (tagCompound.hasKey("object", 10))
			setHeldItemStack(ItemStack.loadItemStackFromNBT(
					tagCompound.getCompoundTag(
							"object")));
		else
			removeHeldItemStack();
		isHandOpen = tagCompound.getBoolean("isHandOpen");
	}

	/**
	 * 
	 */
	@Override
	public void updateEntity() {
		super.updateEntity();
		updateGrip();
		if (isDirty) {
			worldObj.markBlockForUpdate(xCoord, yCoord,
					zCoord);
			isDirty = false;
		}
	}

	/**
	 * 
	 */
	private void updateGrip() {
		prevGrip = grip;
		if (grip > 0 && isHandOpen)
			grip -= 1 / gripScale;
		else if (grip < gripMax && !isHandOpen)
			grip += 1 / gripScale;
	}

	/**
	 * @return
	 */
	@Override
	public Packet getDescriptionPacket() {
		NBTTagCompound nbttagcompound = new NBTTagCompound();
		writeToNBT(nbttagcompound);
		return new S35PacketUpdateTileEntity(xCoord, yCoord,
				zCoord, 0, nbttagcompound);
	}

	/**
	 * @param networkManager
	 * @param packet
	 */
	@Override
	public void onDataPacket(NetworkManager networkManager,
			S35PacketUpdateTileEntity packet) {
		readFromNBT(packet.func_148857_g());
		worldObj.func_147479_m(xCoord, yCoord, zCoord);
	}

	/**
	 * 
	 */
	@Override
	public void markDirty() {
		super.markDirty();
		isDirty = true;
	}

	/**
	 * @return
	 */
	public ItemStack getHeldItemStack() {
		return heldItemStack;
	}

	/**
	 * @param heldItemStack
	 */
	public void setHeldItemStack(ItemStack heldItemStack) {
		if (heldItemStack != null
				&& heldItemStack.getItem() != null) {
			heldItemStack.stackSize = 1;
			this.heldItemStack = heldItemStack;
			if (heldItemStack.getItem() instanceof ItemBlock)
				gripScale = 0.5f;
			else
				gripScale = 1;
		}
	}

	/**
	 * 
	 */
	public void removeHeldItemStack() {
		heldItemStack = null;
	}

	/**
	 * 
	 */
	public void openHand() {
		isHandOpen = true;
	}

	/**
	 * 
	 */
	public void closeHand() {
		isHandOpen = false;
	}

	/**
	 * @param t
	 * @return
	 */
	public float getGrip(float t) {
		return (prevGrip * (1 - t) + grip * t) / gripMax;
	}

	/**
	 * @return
	 */
	public float getGripScale() {
		return gripScale;
	}
}