-
Notifications
You must be signed in to change notification settings - Fork 497
/
PipeTransportItems.java
523 lines (404 loc) · 16.1 KB
/
PipeTransportItems.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/**
* 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 java.util.HashSet;
import java.util.LinkedList;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import cpw.mods.fml.common.network.PacketDispatcher;
import buildcraft.BuildCraftTransport;
import net.minecraftforge.common.ForgeDirection;
import buildcraft.api.core.Position;
import buildcraft.api.gates.ITrigger;
import buildcraft.api.inventory.ISpecialInventory;
import buildcraft.api.transport.IPipeEntry;
import buildcraft.api.transport.IPipedItem;
import buildcraft.core.DefaultProps;
import buildcraft.core.EntityPassiveItem;
import buildcraft.core.IMachine;
import buildcraft.core.inventory.Transactor;
import buildcraft.core.network.PacketIds;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
import buildcraft.transport.network.PacketPipeTransportContent;
import net.minecraft.src.EntityItem;
import net.minecraft.src.IInventory;
import net.minecraft.src.ItemStack;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.NBTTagList;
import net.minecraft.src.Packet;
import net.minecraft.src.TileEntity;
public class PipeTransportItems extends PipeTransport {
public boolean allowBouncing = false;
public Map<Integer, EntityData> travelingEntities = new TreeMap<Integer, EntityData>();
private final List<EntityData> entitiesToLoad = new ArrayList<EntityData>();
// TODO: generalize the use of this hook in particular for obsidian pipe
public IItemTravelingHook travelHook;
public void readjustSpeed(IPipedItem item) {
if (container.pipe instanceof IPipeTransportItemsHook)
((IPipeTransportItemsHook) container.pipe).readjustSpeed(item);
else
defaultReajustSpeed(item);
}
public void defaultReajustSpeed(IPipedItem item) {
if (item.getSpeed() > Utils.pipeNormalSpeed)
item.setSpeed(item.getSpeed() - Utils.pipeNormalSpeed);
if (item.getSpeed() < Utils.pipeNormalSpeed)
item.setSpeed(Utils.pipeNormalSpeed);
}
@Override
public void entityEntering(IPipedItem item, ForgeDirection orientation) {
if (item.isCorrupted())
// Safe guard - if for any reason the item is corrupted at this
// stage, avoid adding it to the pipe to avoid further exceptions.
return;
readjustSpeed(item);
EntityData data = travelingEntities.get(item.getEntityId());
if (data == null) {
data = new EntityData(item, orientation);
travelingEntities.put(item.getEntityId(), data);
if (item.getContainer() != null && item.getContainer() != this.container)
((PipeTransportItems) ((TileGenericPipe) item.getContainer()).pipe.transport).scheduleRemoval(item);
item.setContainer(container);
}
// Reajusting Ypos to make sure the object looks like sitting on the
// pipe.
if (orientation != ForgeDirection.UP && orientation != ForgeDirection.DOWN)
item.setPosition(item.getPosition().x, yCoord + Utils.getPipeFloorOf(item.getItemStack()), item.getPosition().z);
if (!worldObj.isRemote)
data.output = resolveDestination(data);
if (container.pipe instanceof IPipeTransportItemsHook)
((IPipeTransportItemsHook) container.pipe).entityEntered(item, orientation);
if (!worldObj.isRemote) {
sendItemPacket(data);
}
if (travelingEntities.size() > BuildCraftTransport.groupItemsTrigger) {
groupEntities();
if (travelingEntities.size() > BuildCraftTransport.maxItemsInPipes)
worldObj.createExplosion(null, xCoord, yCoord, zCoord, 1, false);
}
}
/**
* Bounces the item back into the pipe without changing the travelingEntities map.
* @param data
*/
private void reverseItem(EntityData data) {
if (data.item.isCorrupted())
// Safe guard - if for any reason the item is corrupted at this
// stage, avoid adding it to the pipe to avoid further exceptions.
return;
unscheduleRemoval(data.item);
data.toCenter = true;
data.input = data.output.getOpposite();
readjustSpeed(data.item);
// Reajusting Ypos to make sure the object looks like sitting on the
// pipe.
if (data.input != ForgeDirection.UP && data.input != ForgeDirection.DOWN)
data.item.setPosition(data.item.getPosition().x, yCoord + Utils.getPipeFloorOf(data.item.getItemStack()), data.item.getPosition().z);
if (!worldObj.isRemote)
data.output = resolveDestination(data);
if (container.pipe instanceof IPipeTransportItemsHook)
((IPipeTransportItemsHook) container.pipe).entityEntered(data.item, data.input);
if (!worldObj.isRemote) {
sendItemPacket(data);
}
}
public ForgeDirection resolveDestination(EntityData data) {
LinkedList<ForgeDirection> listOfPossibleMovements = getPossibleMovements(data);
if (listOfPossibleMovements.size() == 0)
return ForgeDirection.UNKNOWN;
else {
int i = worldObj.rand.nextInt(listOfPossibleMovements.size());
return listOfPossibleMovements.get(i);
}
}
/**
* Returns a list of all possible movements, that is to say adjacent
* implementers of IPipeEntry or TileEntityChest.
*/
public LinkedList<ForgeDirection> getPossibleMovements(EntityData data) {
LinkedList<ForgeDirection> result = new LinkedList<ForgeDirection>();
data.blacklist.add(data.input.getOpposite());
for (ForgeDirection o : ForgeDirection.VALID_DIRECTIONS)
if (!data.blacklist.contains(o) && container.pipe.outputOpen(o))
if (canReceivePipeObjects(o, data.item))
result.add(o);
if (result.size() == 0 && allowBouncing) {
if (canReceivePipeObjects(data.input.getOpposite(), data.item))
result.add(data.input.getOpposite());
}
if (this.container.pipe instanceof IPipeTransportItemsHook) {
Position pos = new Position(xCoord, yCoord, zCoord, data.input);
result = ((IPipeTransportItemsHook) this.container.pipe).filterPossibleMovements(result, pos, data.item);
}
return result;
}
public boolean canReceivePipeObjects(ForgeDirection o, IPipedItem item) {
TileEntity entity = container.getTile(o);
if (!Utils.checkPipesConnections(entity, container))
return false;
if (entity instanceof IPipeEntry)
return true;
else if (entity instanceof TileGenericPipe) {
TileGenericPipe pipe = (TileGenericPipe) entity;
return pipe.pipe.transport instanceof PipeTransportItems;
} else if (entity instanceof IInventory)
if(Transactor.getTransactorFor(entity).add(item.getItemStack(), o.getOpposite(), false).stackSize > 0)
return true;
return false;
}
@Override
public void updateEntity() {
moveSolids();
}
HashSet<Integer> toRemove = new HashSet<Integer>();
public void scheduleRemoval(IPipedItem item) {
toRemove.add(item.getEntityId());
}
public void unscheduleRemoval(IPipedItem item) {
toRemove.remove(item.getEntityId());
}
public void performRemoval() {
travelingEntities.keySet().removeAll(toRemove);
toRemove.clear();
}
private void moveSolids() {
for (EntityData data : entitiesToLoad) {
data.item.setWorld(worldObj);
travelingEntities.put(data.item.getEntityId(), data);
}
entitiesToLoad.clear();
performRemoval();
for (EntityData data : travelingEntities.values()) {
if (data.item.isCorrupted()) {
scheduleRemoval(data.item);
data.item.remove();
continue;
}
Position motion = new Position(0, 0, 0, data.toCenter ? data.input : data.output);
motion.moveForwards(data.item.getSpeed());
data.item.setPosition(data.item.getPosition().x + motion.x, data.item.getPosition().y + motion.y, data.item.getPosition().z + motion.z);
if ((data.toCenter && middleReached(data)) || outOfBounds(data)) {
data.toCenter = false;
// Reajusting to the middle
data.item.setPosition(xCoord + 0.5, yCoord + Utils.getPipeFloorOf(data.item.getItemStack()), zCoord + 0.5);
if (data.output == ForgeDirection.UNKNOWN) {
if (travelHook != null)
travelHook.drop(this, data);
EntityItem dropped = null;
if (!toRemove.contains(data.item.getEntityId()))
dropped = data.item.toEntityItem(data.input);
scheduleRemoval(data.item);
if (dropped != null)
onDropped(dropped);
} else {
if (travelHook != null)
travelHook.centerReached(this, data);
}
} else if (!data.toCenter && endReached(data)) {
Position destPos = new Position(xCoord, yCoord, zCoord, data.output);
destPos.moveForwards(1.0);
TileEntity tile = worldObj.getBlockTileEntity((int) destPos.x, (int) destPos.y, (int) destPos.z);
if (travelHook != null)
travelHook.endReached(this, data, tile);
// If the item has not been scheduled to removal by the hook
if (!toRemove.contains(data.item.getEntityId())) {
scheduleRemoval(data.item);
handleTileReached(data, tile);
}
}
}
performRemoval();
}
private void handleTileReached(EntityData data, TileEntity tile) {
if (tile instanceof IPipeEntry)
((IPipeEntry) tile).entityEntering(data.item, data.output);
else if (tile instanceof TileGenericPipe && ((TileGenericPipe) tile).pipe.transport instanceof PipeTransportItems) {
TileGenericPipe pipe = (TileGenericPipe) tile;
((PipeTransportItems) pipe.pipe.transport).entityEntering(data.item, data.output);
} else if (tile instanceof IInventory) {
if (!CoreProxy.proxy.isRenderWorld(worldObj)) {
ItemStack added = Transactor.getTransactorFor(tile).add(data.item.getItemStack(), data.output.getOpposite(), true);
data.item.getItemStack().stackSize -= added.stackSize;
if(data.item.getItemStack().stackSize > 0) {
reverseItem(data);
}
}
} else {
if (travelHook != null)
travelHook.drop(this, data);
EntityItem dropped = data.item.toEntityItem(data.output);
if (dropped != null)
// On SMP, the client side doesn't actually drops
// items
onDropped(dropped);
}
}
public boolean middleReached(EntityData entity) {
float middleLimit = entity.item.getSpeed() * 1.01F;
return (Math.abs(xCoord + 0.5 - entity.item.getPosition().x) < middleLimit
&& Math.abs(yCoord + Utils.getPipeFloorOf(entity.item.getItemStack()) - entity.item.getPosition().y) < middleLimit && Math.abs(zCoord
+ 0.5 - entity.item.getPosition().z) < middleLimit);
}
public boolean endReached(EntityData entity) {
return entity.item.getPosition().x > xCoord + 1.0 || entity.item.getPosition().x < xCoord || entity.item.getPosition().y > yCoord + 1.0
|| entity.item.getPosition().y < yCoord || entity.item.getPosition().z > zCoord + 1.0 || entity.item.getPosition().z < zCoord;
}
public boolean outOfBounds(EntityData entity) {
return entity.item.getPosition().x > xCoord + 2.0 || entity.item.getPosition().x < xCoord - 1.0 || entity.item.getPosition().y > yCoord + 2.0
|| entity.item.getPosition().y < yCoord - 1.0 || entity.item.getPosition().z > zCoord + 2.0 || entity.item.getPosition().z < zCoord - 1.0;
}
public Position getPosition() {
return new Position(xCoord, yCoord, zCoord);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
NBTTagList nbttaglist = nbt.getTagList("travelingEntities");
for (int j = 0; j < nbttaglist.tagCount(); ++j)
try {
NBTTagCompound dataTag = (NBTTagCompound) nbttaglist.tagAt(j);
IPipedItem entity = new EntityPassiveItem(null);
entity.readFromNBT(dataTag);
if (entity.isCorrupted()) {
entity.remove();
continue;
}
entity.setContainer(container);
EntityData data = new EntityData(entity, ForgeDirection.getOrientation(dataTag.getInteger("input")));
data.output = ForgeDirection.getOrientation(dataTag.getInteger("output"));
data.toCenter = dataTag.getBoolean("toCenter");
entitiesToLoad.add(data);
} catch (Throwable t) {
t.printStackTrace();
// It may be the case that entities cannot be reloaded between
// two versions - ignore these errors.
}
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
NBTTagList nbttaglist = new NBTTagList();
for (EntityData data : travelingEntities.values()) {
NBTTagCompound dataTag = new NBTTagCompound();
nbttaglist.appendTag(dataTag);
data.item.writeToNBT(dataTag);
dataTag.setBoolean("toCenter", data.toCenter);
dataTag.setInteger("input", data.input.ordinal());
dataTag.setInteger("output", data.output.ordinal());
}
nbt.setTag("travelingEntities", nbttaglist);
}
protected void doWork() {}
/**
* Handles a packet describing a stack of items inside a pipe.
*
* @param packet
*/
public void handleItemPacket(PacketPipeTransportContent packet) {
if (packet.getID() != PacketIds.PIPE_CONTENTS)
return;
EntityData data = travelingEntities.remove(packet.getEntityId());
IPipedItem item = null;
if(data == null) {
item = EntityPassiveItem.getOrCreate(worldObj, packet.getEntityId());
} else {
item = data.item;
}
item.setItemStack(new ItemStack(packet.getItemId(), packet.getStackSize(), packet.getItemDamage()));
if(item.getPosition() == null)
item.setPosition(packet.getPosX(), packet.getPosY(), packet.getPosZ());
item.setSpeed(packet.getSpeed());
if (item.getContainer() != null && item.getContainer() != container) {
((PipeTransportItems) ((TileGenericPipe) item.getContainer()).pipe.transport).scheduleRemoval(item);
item.setContainer(container);
}
data = new EntityData(item, packet.getInputOrientation());
data.output = packet.getOutputOrientation();
travelingEntities.put(item.getEntityId(), data);
}
/**
* Creates a packet describing a stack of items inside a pipe.
*
* @param data
* @return
*/
public Packet createItemPacket(EntityData data) {
PacketPipeTransportContent packet = new PacketPipeTransportContent(container.xCoord, container.yCoord, container.zCoord, data);
return packet.getPacket();
}
private void sendItemPacket(EntityData data) {
int dimension = worldObj.provider.dimensionId;
PacketDispatcher.sendPacketToAllAround(xCoord, yCoord, zCoord, DefaultProps.PIPE_CONTENTS_RENDER_DIST, dimension, createItemPacket(data));
}
public int getNumberOfItems() {
return travelingEntities.size();
}
public void onDropped(EntityItem item) {
this.container.pipe.onDropped(item);
}
protected void neighborChange() {
}
@Override
public boolean isPipeConnected(TileEntity tile) {
return tile instanceof TileGenericPipe
|| tile instanceof IPipeEntry
|| tile instanceof ISpecialInventory
|| (tile instanceof IInventory && ((IInventory)tile).getSizeInventory() > 0)
|| (tile instanceof IMachine && ((IMachine) tile).manageSolids());
}
@Override
public boolean acceptItems() {
return true;
}
public boolean isTriggerActive(ITrigger trigger) {
return false;
}
/**
* Group all items that are similar, that is to say same dmg, same id and no
* contribution controlling them
*/
public void groupEntities() {
EntityData[] entities = travelingEntities.values().toArray(new EntityData[travelingEntities.size()]);
TreeSet<Integer> remove = new TreeSet<Integer>();
for (int i = 0; i < entities.length; ++i) {
EntityData data1 = entities[i];
for (int j = i + 1; j < entities.length; ++j) {
EntityData data2 = entities[j];
if (data1.item.getItemStack().itemID == data2.item.getItemStack().itemID
&& data1.item.getItemStack().getItemDamage() == data2.item.getItemStack().getItemDamage()
&& !remove.contains(data1.item.getEntityId()) && !remove.contains(data2.item.getEntityId())
&& !data1.item.hasContributions() && !data2.item.hasContributions()
&& data1.item.getItemStack().stackSize + data2.item.getItemStack().stackSize < data1.item.getItemStack().getMaxStackSize()) {
data1.item.getItemStack().stackSize += data2.item.getItemStack().stackSize;
remove.add(data2.item.getEntityId());
}
}
}
for (Integer i : remove) {
travelingEntities.get(i).item.remove();
travelingEntities.remove(i);
}
}
@Override
public void dropContents() {
groupEntities();
for (EntityData data : travelingEntities.values()) {
Utils.dropItems(worldObj, data.item.getItemStack(), xCoord, yCoord, zCoord);
}
travelingEntities.clear();
}
@Override
public boolean allowsConnect(PipeTransport with) {
return with instanceof PipeTransportItems;
}
}