-
Notifications
You must be signed in to change notification settings - Fork 497
/
EntityPassiveItem.java
366 lines (313 loc) · 9.2 KB
/
EntityPassiveItem.java
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Copyright (c) SpaceToad, 2011
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.transport;
import buildcraft.BuildCraftCore;
import buildcraft.api.core.Position;
import buildcraft.api.transport.IPassiveItemContribution;
import buildcraft.core.proxy.CoreProxy;
import java.util.TreeMap;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
public class EntityPassiveItem implements IPipedItem {
private TreeMap<String, IPassiveItemContribution> contributions = new TreeMap<String, IPassiveItemContribution>();
protected static int maxId = 0;
protected World worldObj;
protected float speed = 0.01F;
protected ItemStack item;
protected TileEntity container;
protected Position position;
protected int entityId;
/* CONSTRUCTORS */
public EntityPassiveItem(World world) {
this(world, maxId < Short.MAX_VALUE ? ++maxId : (maxId = Short.MIN_VALUE));
}
public EntityPassiveItem(World world, int id) {
setEntityId(id);
// PipeManager.getAllEntities().put(getEntityId(), this);
worldObj = world;
}
public EntityPassiveItem(World world, double d, double d1, double d2) {
this(world);
position = new Position(d, d1, d2);
worldObj = world;
}
public EntityPassiveItem(World world, double d, double d1, double d2, ItemStack itemstack) {
this(world, d, d1, d2);
this.setItemStack(itemstack.copy());
}
/* CREATING & CACHING */
public static IPipedItem getOrCreate(World world, int id) {
// if (PipeManager.getAllEntities().containsKey(id)) {
// return PipeManager.getAllEntities().get(id);
// } else {
return new EntityPassiveItem(world, id);
// }
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#remove()
*/
@Override
public void remove() {
// if (PipeManager.getAllEntities().containsKey(getEntityId())) {
// PipeManager.getAllEntities().remove(getEntityId());
// }
}
/* GETTING & SETTING */
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#setWorld(net.minecraft.src.World)
*/
@Override
public void setWorld(World world) {
worldObj = world;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#getPosition()
*/
@Override
public Position getPosition() {
return position;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#setPosition(double, double, double)
*/
@Override
public void setPosition(double x, double y, double z) {
position = new Position(x, y, z);
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#getSpeed()
*/
@Override
public float getSpeed() {
return speed;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#setSpeed(float)
*/
@Override
public void setSpeed(float speed) {
this.speed = speed;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#getItemStack()
*/
@Override
public ItemStack getItemStack() {
return item;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#setItemStack(net.minecraft.src.ItemStack)
*/
@Override
public void setItemStack(ItemStack item) {
this.item = item;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#getContainer()
*/
@Override
public TileEntity getContainer() {
return container;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#setContainer(net.minecraft.src.TileEntity)
*/
@Override
public void setContainer(TileEntity container) {
this.container = container;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#getEntityId()
*/
@Override
public int getEntityId() {
return entityId;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#setEntityId(int)
*/
@Override
public void setEntityId(int entityId) {
this.entityId = entityId;
}
/* SAVING & LOADING */
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#readFromNBT(net.minecraft.src.NBTTagCompound)
*/
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
setPosition(nbttagcompound.getDouble("x"), nbttagcompound.getDouble("y"), nbttagcompound.getDouble("z"));
setSpeed(nbttagcompound.getFloat("speed"));
setItemStack(ItemStack.loadItemStackFromNBT(nbttagcompound.getCompoundTag("Item")));
NBTTagList contribList = nbttagcompound.getTagList("contribList");
for (int i = 0; i < contribList.tagCount(); ++i) {
NBTTagCompound cpt = (NBTTagCompound) contribList.tagAt(i);
String key = cpt.getString("key");
String className = cpt.getString("class");
if (getClass().getName().startsWith("net.minecraft.src")) {
className = "net.minecraft.src." + className;
}
try {
IPassiveItemContribution contrib = ((IPassiveItemContribution) Class.forName(className).newInstance());
contrib.readFromNBT(cpt);
contributions.put(key, contrib);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#writeToNBT(net.minecraft.src.NBTTagCompound)
*/
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
nbttagcompound.setDouble("x", position.x);
nbttagcompound.setDouble("y", position.y);
nbttagcompound.setDouble("z", position.z);
nbttagcompound.setFloat("speed", getSpeed());
NBTTagCompound nbttagcompound2 = new NBTTagCompound();
getItemStack().writeToNBT(nbttagcompound2);
nbttagcompound.setCompoundTag("Item", nbttagcompound2);
NBTTagList contribList = new NBTTagList();
for (String key : contributions.keySet()) {
IPassiveItemContribution contrib = contributions.get(key);
NBTTagCompound cpt = new NBTTagCompound();
contrib.writeToNBT(cpt);
cpt.setString("key", key);
String className = contrib.getClass().getName();
if (className.startsWith("net.minecraft.src.")) {
className = className.replace("net.minecraft.src.", "");
}
cpt.setString("class", className);
contribList.appendTag(cpt);
}
nbttagcompound.setTag("contribList", contribList);
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#toEntityItem(net.minecraft.src.buildcraft.api.Orientations)
*/
@Override
public EntityItem toEntityItem(ForgeDirection dir) {
if (!CoreProxy.proxy.isRenderWorld(worldObj)) {
if (getItemStack().stackSize <= 0)
return null;
Position motion = new Position(0, 0, 0, dir);
motion.moveForwards(0.1 + getSpeed() * 2F);
EntityItem entityitem = new EntityItem(worldObj, position.x, position.y, position.z, getItemStack());
entityitem.lifespan = BuildCraftCore.itemLifespan;
entityitem.delayBeforeCanPickup = 10;
float f3 = 0.00F + worldObj.rand.nextFloat() * 0.04F - 0.02F;
entityitem.motionX = (float) worldObj.rand.nextGaussian() * f3 + motion.x;
entityitem.motionY = (float) worldObj.rand.nextGaussian() * f3 + motion.y;
entityitem.motionZ = (float) worldObj.rand.nextGaussian() * f3 + +motion.z;
worldObj.spawnEntityInWorld(entityitem);
remove();
return entityitem;
} else
return null;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#getEntityBrightness(float)
*/
@Override
public float getEntityBrightness(float f) {
int i = MathHelper.floor_double(position.x);
int j = MathHelper.floor_double(position.z);
worldObj.getClass();
if (worldObj.blockExists(i, 128 / 2, j)) {
double d = 0.66000000000000003D;
int k = MathHelper.floor_double(position.y + d);
return worldObj.getLightBrightness(i, k, j);
} else
return 0.0F;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#isCorrupted()
*/
@Override
public boolean isCorrupted() {
return getItemStack() == null || getItemStack().stackSize <= 0 || Item.itemsList[getItemStack().itemID] == null;
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#addContribution(java.lang.String, net.minecraft.src.buildcraft.api.IPassiveItemContribution)
*/
@Override
public void addContribution(String key, IPassiveItemContribution contribution) {
contributions.put(key, contribution);
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#getContribution(java.lang.String)
*/
@Override
public IPassiveItemContribution getContribution(String key) {
return contributions.get(key);
}
/*
* (non-Javadoc)
*
* @see net.minecraft.src.buildcraft.api.IPipedItem#hasContributions()
*/
@Override
public boolean hasContributions() {
return contributions.size() > 0;
}
@Override
public boolean canSinkTo(TileEntity entity) {
return true; // a passive item can sink anywhere
}
}