-
Notifications
You must be signed in to change notification settings - Fork 497
/
TravelingItem.java
345 lines (286 loc) · 8.53 KB
/
TravelingItem.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
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* 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 java.util.EnumSet;
import java.util.Map;
import com.google.common.collect.MapMaker;
import net.minecraft.client.renderer.GLAllocation;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftCore;
import buildcraft.api.core.EnumColor;
import buildcraft.api.core.Position;
import buildcraft.core.lib.inventory.StackHelper;
public class TravelingItem {
public static final TravelingItemCache serverCache = new TravelingItemCache();
public static final TravelingItemCache clientCache = new TravelingItemCache();
public static final InsertionHandler DEFAULT_INSERTION_HANDLER = new InsertionHandler();
private static int maxId = 0;
public final EnumSet<ForgeDirection> blacklist = EnumSet.noneOf(ForgeDirection.class);
public double xCoord, yCoord, zCoord;
public final int id;
public boolean toCenter = true;
public EnumColor color;
public ForgeDirection input = ForgeDirection.UNKNOWN;
public ForgeDirection output = ForgeDirection.UNKNOWN;
public int displayList;
public boolean hasDisplayList;
protected float speed = 0.01F;
protected ItemStack itemStack;
protected TileEntity container;
protected NBTTagCompound extraData;
protected InsertionHandler insertionHandler = DEFAULT_INSERTION_HANDLER;
/* CONSTRUCTORS */
protected TravelingItem(int id) {
this.id = id;
}
public static TravelingItem make(int id) {
TravelingItem item = new TravelingItem(id);
getCache().cache(item);
return item;
}
public static TravelingItem make() {
return make(maxId < Short.MAX_VALUE ? ++maxId : (maxId = Short.MIN_VALUE));
}
public static TravelingItem make(double x, double y, double z, ItemStack stack) {
TravelingItem item = make();
item.xCoord = x;
item.yCoord = y;
item.zCoord = z;
item.itemStack = stack.copy();
return item;
}
public static TravelingItem make(NBTTagCompound nbt) {
TravelingItem item = make();
item.readFromNBT(nbt);
return item;
}
public static TravelingItemCache getCache() {
if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) {
return clientCache;
}
return serverCache;
}
/* GETTING & SETTING */
public void setPosition(double x, double y, double z) {
this.xCoord = x;
this.yCoord = y;
this.zCoord = z;
}
public void movePosition(double x, double y, double z) {
this.xCoord += x;
this.yCoord += y;
this.zCoord += z;
}
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
this.speed = speed;
}
public ItemStack getItemStack() {
return itemStack;
}
public void setItemStack(ItemStack item) {
this.itemStack = item;
}
public TileEntity getContainer() {
return container;
}
public void setContainer(TileEntity container) {
this.container = container;
}
public NBTTagCompound getExtraData() {
if (extraData == null) {
extraData = new NBTTagCompound();
}
return extraData;
}
public boolean hasExtraData() {
return extraData != null;
}
@Deprecated
public void setInsetionHandler(InsertionHandler handler) {
if (handler == null) {
return;
}
this.insertionHandler = handler;
}
public void setInsertionHandler(InsertionHandler handler) {
if (handler == null) {
return;
}
this.insertionHandler = handler;
}
public InsertionHandler getInsertionHandler() {
return insertionHandler;
}
public void reset() {
toCenter = true;
blacklist.clear();
input = ForgeDirection.UNKNOWN;
output = ForgeDirection.UNKNOWN;
}
/* SAVING & LOADING */
public void readFromNBT(NBTTagCompound data) {
setPosition(data.getDouble("x"), data.getDouble("y"), data.getDouble("z"));
setSpeed(data.getFloat("speed"));
setItemStack(ItemStack.loadItemStackFromNBT(data.getCompoundTag("Item")));
toCenter = data.getBoolean("toCenter");
input = ForgeDirection.getOrientation(data.getByte("input"));
output = ForgeDirection.getOrientation(data.getByte("output"));
byte c = data.getByte("color");
if (c != -1) {
color = EnumColor.fromId(c);
}
if (data.hasKey("extraData")) {
extraData = data.getCompoundTag("extraData");
}
}
public void writeToNBT(NBTTagCompound data) {
data.setDouble("x", xCoord);
data.setDouble("y", yCoord);
data.setDouble("z", zCoord);
data.setFloat("speed", getSpeed());
NBTTagCompound itemStackTag = new NBTTagCompound();
getItemStack().writeToNBT(itemStackTag);
data.setTag("Item", itemStackTag);
data.setBoolean("toCenter", toCenter);
data.setByte("input", (byte) input.ordinal());
data.setByte("output", (byte) output.ordinal());
data.setByte("color", color != null ? (byte) color.ordinal() : -1);
if (extraData != null) {
data.setTag("extraData", extraData);
}
}
public EntityItem toEntityItem() {
if (container != null && !container.getWorldObj().isRemote) {
if (getItemStack().stackSize <= 0) {
return null;
}
Position motion = new Position(0, 0, 0, output);
motion.moveForwards(0.1 + getSpeed() * 2F);
EntityItem entity = new EntityItem(container.getWorldObj(), xCoord, yCoord, zCoord, getItemStack());
entity.lifespan = BuildCraftCore.itemLifespan * 20;
entity.delayBeforeCanPickup = 10;
float f3 = 0.00F + container.getWorldObj().rand.nextFloat() * 0.04F - 0.02F;
entity.motionX = (float) container.getWorldObj().rand.nextGaussian() * f3 + motion.x;
entity.motionY = (float) container.getWorldObj().rand.nextGaussian() * f3 + motion.y;
entity.motionZ = (float) container.getWorldObj().rand.nextGaussian() * f3 + +motion.z;
return entity;
}
return null;
}
public float getEntityBrightness(float f) {
int i = MathHelper.floor_double(xCoord);
int j = MathHelper.floor_double(zCoord);
if (container != null && container.getWorldObj().blockExists(i, 128 / 2, j)) {
double d = 0.66000000000000003D;
int k = MathHelper.floor_double(yCoord + d);
return container.getWorldObj().getLightBrightness(i, k, j);
} else {
return 0.0F;
}
}
public boolean isCorrupted() {
return itemStack == null || itemStack.stackSize <= 0 || itemStack.getItem() == null;
}
public boolean canBeGroupedWith(TravelingItem otherItem) {
if (otherItem == this) {
return false;
}
if (toCenter != otherItem.toCenter) {
return false;
}
if (output != otherItem.output) {
return false;
}
if (color != otherItem.color) {
return false;
}
if (hasExtraData() || otherItem.hasExtraData()) {
return false;
}
if (insertionHandler != DEFAULT_INSERTION_HANDLER) {
return false;
}
if (!blacklist.equals(otherItem.blacklist)) {
return false;
}
if (otherItem.isCorrupted()) {
return false;
}
return StackHelper.canStacksMerge(itemStack, otherItem.itemStack);
}
public boolean tryMergeInto(TravelingItem otherItem) {
if (!canBeGroupedWith(otherItem)) {
return false;
}
if (StackHelper.mergeStacks(itemStack, otherItem.itemStack, false) == itemStack.stackSize) {
StackHelper.mergeStacks(itemStack, otherItem.itemStack, true);
itemStack.stackSize = 0;
return true;
}
return false;
}
public boolean ignoreWeight() {
return false;
}
@Override
public int hashCode() {
return this.id;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final TravelingItem other = (TravelingItem) obj;
if (this.id != other.id) {
return false;
}
return true;
}
@Override
public void finalize() {
// Nasty hack.
if (hasDisplayList) {
GLAllocation.deleteDisplayLists(displayList);
hasDisplayList = false;
}
}
@Override
public String toString() {
return "TravelingItem: " + id;
}
public static class InsertionHandler {
public boolean canInsertItem(TravelingItem item, IInventory inv) {
return true;
}
}
public static class TravelingItemCache {
private final Map<Integer, TravelingItem> itemCache = new MapMaker().weakValues().makeMap();
public void cache(TravelingItem item) {
itemCache.put(item.id, item);
}
public TravelingItem get(int id) {
return itemCache.get(id);
}
}
}