-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileGenericPipe.java
1223 lines (1036 loc) · 33.1 KB
/
TileGenericPipe.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2011-2014, 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 org.apache.logging.log4j.Level;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.Packet;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import cofh.api.energy.IEnergyHandler;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftTransport;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.EnumColor;
import buildcraft.api.core.IIconProvider;
import buildcraft.api.core.ISerializable;
import buildcraft.api.core.Position;
import buildcraft.api.gates.IGateExpansion;
import buildcraft.api.power.IRedstoneEngine;
import buildcraft.api.power.IRedstoneEngineReceiver;
import buildcraft.api.transport.IPipe;
import buildcraft.api.transport.IPipeConnection;
import buildcraft.api.transport.IPipeTile;
import buildcraft.api.transport.PipeManager;
import buildcraft.api.transport.PipeWire;
import buildcraft.api.transport.pluggable.PipePluggable;
import buildcraft.core.DefaultProps;
import buildcraft.core.IDropControlInventory;
import buildcraft.core.ITileBufferHolder;
import buildcraft.core.TileBuffer;
import buildcraft.core.inventory.InvUtils;
import buildcraft.core.network.BuildCraftPacket;
import buildcraft.core.network.IGuiReturnHandler;
import buildcraft.core.network.ISyncedTile;
import buildcraft.core.network.PacketTileState;
import buildcraft.core.robots.DockingStation;
import buildcraft.core.utils.Utils;
import buildcraft.transport.ItemFacade.FacadeState;
import buildcraft.transport.gates.GateFactory;
import buildcraft.transport.gates.GatePluggable;
import buildcraft.transport.pipes.PipePowerWood;
import buildcraft.transport.pluggable.PlugPluggable;
import buildcraft.transport.pluggable.RobotStationPluggable;
public class TileGenericPipe extends TileEntity implements IFluidHandler,
IPipeTile, ITileBufferHolder, IEnergyHandler, IDropControlInventory,
ISyncedTile, ISolidSideTile, IGuiReturnHandler, IRedstoneEngineReceiver {
public boolean initialized = false;
public final PipeRenderState renderState = new PipeRenderState();
public final PipePluggableState pluggableState = new PipePluggableState();
public final CoreState coreState = new CoreState();
public boolean[] pipeConnectionsBuffer = new boolean[6];
public Pipe pipe;
public int redstoneInput;
public int[] redstoneInputSide = new int[ForgeDirection.VALID_DIRECTIONS.length];
protected boolean deletePipe = false;
protected boolean sendClientUpdate = false;
protected boolean blockNeighborChange = false;
protected boolean refreshRenderState = false;
protected boolean pipeBound = false;
protected boolean resyncGateExpansions = false;
protected boolean attachPluggables = false;
private SideProperties sideProperties = new SideProperties();
private TileBuffer[] tileBuffer;
private int glassColor = -1;
public static class CoreState implements ISerializable {
public int pipeId = -1;
@Override
public void writeData(ByteBuf data) {
data.writeInt(pipeId);
}
@Override
public void readData(ByteBuf data) {
pipeId = data.readInt();
}
}
public static class SideProperties {
PipePluggable[] pluggables = new PipePluggable[ForgeDirection.VALID_DIRECTIONS.length];
public void writeToNBT(NBTTagCompound nbt) {
for (int i = 0; i < ForgeDirection.VALID_DIRECTIONS.length; i++) {
PipePluggable pluggable = pluggables[i];
final String key = "pluggable[" + i + "]";
if (pluggable == null) {
nbt.removeTag(key);
} else {
NBTTagCompound pluggableData = new NBTTagCompound();
pluggableData.setString("pluggableName", PipeManager.getPluggableName(pluggable.getClass()));
pluggable.writeToNBT(pluggableData);
nbt.setTag(key, pluggableData);
}
}
}
public void readFromNBT(NBTTagCompound nbt) {
for (int i = 0; i < ForgeDirection.VALID_DIRECTIONS.length; i++) {
final String key = "pluggable[" + i + "]";
if (!nbt.hasKey(key)) {
continue;
}
try {
NBTTagCompound pluggableData = nbt.getCompoundTag(key);
Class<?> pluggableClass = null;
// Migration support for 6.1.x/6.2.x
if (pluggableData.hasKey("pluggableClass")) {
String c = pluggableData.getString("pluggableClass");
if ("buildcraft.transport.gates.ItemGate$GatePluggable".equals(c)) {
pluggableClass = GatePluggable.class;
} else if ("buildcraft.transport.gates.ItemFacade$FacadePluggable".equals(c)) {
pluggableClass = FacadePluggable.class;
} else if ("buildcraft.transport.gates.ItemPlug$PlugPluggable".equals(c)) {
pluggableClass = PlugPluggable.class;
} else if ("buildcraft.transport.gates.ItemRobotStation$RobotStationPluggable".equals(c)) {
pluggableClass = RobotStationPluggable.class;
}
} else {
pluggableClass = PipeManager.getPluggableByName(pluggableData.getString("pluggableName"));
}
if (!PipePluggable.class.isAssignableFrom(pluggableClass)) {
BCLog.logger.warn("Wrong pluggable class: " + pluggableClass);
continue;
}
PipePluggable pluggable = (PipePluggable) pluggableClass.newInstance();
pluggable.readFromNBT(pluggableData);
pluggables[i] = pluggable;
} catch (Exception e) {
BCLog.logger.warn("Failed to load side state");
e.printStackTrace();
}
}
// Migration code
for (int i = 0; i < ForgeDirection.VALID_DIRECTIONS.length; i++) {
PipePluggable pluggable = null;
if (nbt.hasKey("facadeState[" + i + "]")) {
pluggable = new FacadePluggable(FacadeState.readArray(nbt.getTagList("facadeState[" + i + "]", Constants.NBT.TAG_COMPOUND)));
} else {
// Migration support for 5.0.x and 6.0.x
if (nbt.hasKey("facadeBlocks[" + i + "]")) {
// 5.0.x
Block block = (Block) Block.blockRegistry.getObjectById(nbt.getInteger("facadeBlocks[" + i + "]"));
int blockId = nbt.getInteger("facadeBlocks[" + i + "]");
if (blockId != 0) {
int metadata = nbt.getInteger("facadeMeta[" + i + "]");
pluggable = new FacadePluggable(new FacadeState[]{FacadeState.create(block, metadata)});
}
} else if (nbt.hasKey("facadeBlocksStr[" + i + "][0]")) {
// 6.0.x
FacadeState mainState = FacadeState.create(
(Block) Block.blockRegistry.getObject(nbt.getString("facadeBlocksStr[" + i + "][0]")),
nbt.getInteger("facadeMeta[" + i + "][0]")
);
if (nbt.hasKey("facadeBlocksStr[" + i + "][1]")) {
FacadeState phasedState = FacadeState.create(
(Block) Block.blockRegistry.getObject(nbt.getString("facadeBlocksStr[" + i + "][1]")),
nbt.getInteger("facadeMeta[" + i + "][1]"),
PipeWire.fromOrdinal(nbt.getInteger("facadeWires[" + i + "]"))
);
pluggable = new FacadePluggable(new FacadeState[]{mainState, phasedState});
} else {
pluggable = new FacadePluggable(new FacadeState[]{mainState});
}
}
}
if (nbt.getBoolean("plug[" + i + "]")) {
pluggable = new PlugPluggable();
}
if (nbt.getBoolean("robotStation[" + i + "]")) {
pluggable = new RobotStationPluggable();
}
if (pluggable != null) {
pluggables[i] = pluggable;
}
}
}
public void rotateLeft() {
PipePluggable[] newPluggables = new PipePluggable[ForgeDirection.VALID_DIRECTIONS.length];
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
newPluggables[dir.getRotation(ForgeDirection.UP).ordinal()] = pluggables[dir.ordinal()];
}
pluggables = newPluggables;
}
public boolean dropItem(TileGenericPipe pipe, ForgeDirection direction) {
boolean result = false;
PipePluggable pluggable = pluggables[direction.ordinal()];
if (pluggable != null) {
pluggable.onDetachedPipe(pipe, direction);
if (!pipe.getWorld().isRemote) {
ItemStack[] stacks = pluggable.getDropItems(pipe);
if (stacks != null) {
for (ItemStack stack : stacks) {
InvUtils.dropItems(pipe.worldObj, stack, pipe.xCoord, pipe.yCoord, pipe.zCoord);
}
}
}
result = true;
}
return result;
}
public void invalidate() {
for (PipePluggable p : pluggables) {
if (p != null) {
p.invalidate();
}
}
}
public void validate(TileGenericPipe pipe) {
for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) {
PipePluggable p = pluggables[d.ordinal()];
if (p != null) {
p.validate(pipe, d);
}
}
}
}
public TileGenericPipe() {
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
if (glassColor >= 0) {
nbt.setByte("stainedColor", (byte) glassColor);
}
for (int i = 0; i < ForgeDirection.VALID_DIRECTIONS.length; i++) {
final String key = "redstoneInputSide[" + i + "]";
nbt.setByte(key, (byte) redstoneInputSide[i]);
}
if (pipe != null) {
nbt.setInteger("pipeId", Item.getIdFromItem(pipe.item));
pipe.writeToNBT(nbt);
} else {
nbt.setInteger("pipeId", coreState.pipeId);
}
sideProperties.writeToNBT(nbt);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
glassColor = nbt.hasKey("stainedColor") ? nbt.getByte("stainedColor") : -1;
redstoneInput = 0;
for (int i = 0; i < ForgeDirection.VALID_DIRECTIONS.length; i++) {
final String key = "redstoneInputSide[" + i + "]";
if (nbt.hasKey(key)) {
redstoneInputSide[i] = nbt.getByte(key);
if (redstoneInputSide[i] > redstoneInput) {
redstoneInput = redstoneInputSide[i];
}
} else {
redstoneInputSide[i] = 0;
}
}
coreState.pipeId = nbt.getInteger("pipeId");
pipe = BlockGenericPipe.createPipe((Item) Item.getItemById(coreState.pipeId));
bindPipe();
if (pipe != null) {
pipe.readFromNBT(nbt);
} else {
BCLog.logger.log(Level.WARN, "Pipe failed to load from NBT at {0},{1},{2}", xCoord, yCoord, zCoord);
deletePipe = true;
}
sideProperties.readFromNBT(nbt);
attachPluggables = true;
}
@Override
public void invalidate() {
initialized = false;
tileBuffer = null;
if (pipe != null) {
pipe.invalidate();
}
sideProperties.invalidate();
super.invalidate();
}
@Override
public void validate() {
super.validate();
initialized = false;
tileBuffer = null;
bindPipe();
if (pipe != null) {
pipe.validate();
}
sideProperties.validate(this);
}
protected void notifyBlockChanged() {
worldObj.notifyBlockOfNeighborChange(xCoord, yCoord, zCoord, getBlock());
scheduleRenderUpdate();
sendUpdateToClient();
BlockGenericPipe.updateNeighbourSignalState(pipe);
}
@Override
public void updateEntity() {
if (attachPluggables) {
attachPluggables = false;
// Attach callback
for (int i = 0; i < ForgeDirection.VALID_DIRECTIONS.length; i++) {
if (sideProperties.pluggables[i] != null) {
pipe.eventBus.registerHandler(sideProperties.pluggables[i]);
sideProperties.pluggables[i].onAttachedPipe(this, ForgeDirection.getOrientation(i));
}
}
notifyBlockChanged();
}
if (!worldObj.isRemote) {
if (deletePipe) {
worldObj.setBlockToAir(xCoord, yCoord, zCoord);
}
if (pipe == null) {
return;
}
if (!initialized) {
initialize(pipe);
}
}
if (!BlockGenericPipe.isValid(pipe)) {
return;
}
pipe.updateEntity();
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) {
PipePluggable p = getPipePluggable(direction);
if (p != null) {
p.update(this, direction);
}
}
if (worldObj.isRemote) {
if (resyncGateExpansions) {
syncGateExpansions();
}
return;
}
if (blockNeighborChange) {
computeConnections();
pipe.onNeighborBlockChange(0);
blockNeighborChange = false;
refreshRenderState = true;
}
if (refreshRenderState) {
refreshRenderState();
refreshRenderState = false;
}
if (sendClientUpdate) {
sendClientUpdate = false;
if (worldObj instanceof WorldServer) {
WorldServer world = (WorldServer) worldObj;
BuildCraftPacket updatePacket = getBCDescriptionPacket();
for (Object o : world.playerEntities) {
EntityPlayerMP player = (EntityPlayerMP) o;
if (world.getPlayerManager().isPlayerWatchingChunk (player, xCoord >> 4, zCoord >> 4)) {
BuildCraftCore.instance.sendToPlayer(player, updatePacket);
}
}
}
}
}
public void initializeFromItemMetadata(int i) {
if (i >= 1 && i <= 16) {
setPipeColor((i - 1) & 15);
}
}
public int getItemMetadata() {
return getPipeColor() >= 0 ? (1 + getPipeColor()) : 0;
}
public int getPipeColor() {
return worldObj.isRemote ? renderState.glassColor : this.glassColor;
}
public boolean setPipeColor(int color) {
// -1 = no color
if (!worldObj.isRemote && color >= -1 && color < 16 && glassColor != color) {
glassColor = color;
notifyBlockChanged();
worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, blockType);
return true;
}
return false;
}
/**
* PRECONDITION: worldObj must not be null
*/
protected void refreshRenderState() {
renderState.glassColor = (byte) glassColor;
// Pipe connections;
for (ForgeDirection o : ForgeDirection.VALID_DIRECTIONS) {
renderState.pipeConnectionMatrix.setConnected(o, this.pipeConnectionsBuffer[o.ordinal()]);
}
// Pipe Textures
for (int i = 0; i < 7; i++) {
ForgeDirection o = ForgeDirection.getOrientation(i);
renderState.textureMatrix.setIconIndex(o, pipe.getIconIndex(o));
}
// WireState
for (PipeWire color : PipeWire.values()) {
renderState.wireMatrix.setWire(color, pipe.wireSet[color.ordinal()]);
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) {
renderState.wireMatrix.setWireConnected(color, direction, pipe.isWireConnectedTo(this.getTile(direction), color));
}
boolean lit = pipe.signalStrength[color.ordinal()] > 0;
switch (color) {
case RED:
renderState.wireMatrix.setWireIndex(color, lit ? WireIconProvider.Texture_Red_Lit : WireIconProvider.Texture_Red_Dark);
break;
case BLUE:
renderState.wireMatrix.setWireIndex(color, lit ? WireIconProvider.Texture_Blue_Lit : WireIconProvider.Texture_Blue_Dark);
break;
case GREEN:
renderState.wireMatrix.setWireIndex(color, lit ? WireIconProvider.Texture_Green_Lit : WireIconProvider.Texture_Green_Dark);
break;
case YELLOW:
renderState.wireMatrix.setWireIndex(color, lit ? WireIconProvider.Texture_Yellow_Lit : WireIconProvider.Texture_Yellow_Dark);
break;
default:
break;
}
}
// Facades
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) {
PipePluggable pluggable = sideProperties.pluggables[direction.ordinal()];
if (!(pluggable instanceof FacadePluggable)) {
continue;
}
FacadeState[] states = ((FacadePluggable) pluggable).states;
// Iterate over all states and activate first proper
int defaultState = -1;
int activeState = -1;
for (int i = 0; i < states.length; i++) {
FacadeState state = states[i];
if (state.wire == null) {
defaultState = i;
continue;
}
if (pipe != null && pipe.isWireActive(state.wire)) {
activeState = i;
break;
}
}
if (activeState < 0) {
activeState = defaultState;
}
((FacadePluggable) pluggable).setActiveState(activeState);
}
pluggableState.setPluggables(sideProperties.pluggables);
// TODO: Add way of signalizing render update via Pluggables
if (renderState.isDirty()) {
renderState.clean();
}
sendUpdateToClient();
}
public void initialize(Pipe<?> pipe) {
this.blockType = getBlockType();
if (pipe == null) {
BCLog.logger.log(Level.WARN, "Pipe failed to initialize at {0},{1},{2}, deleting", xCoord, yCoord, zCoord);
worldObj.setBlockToAir(xCoord, yCoord, zCoord);
return;
}
this.pipe = pipe;
for (ForgeDirection o : ForgeDirection.VALID_DIRECTIONS) {
TileEntity tile = getTile(o);
if (tile instanceof ITileBufferHolder) {
((ITileBufferHolder) tile).blockCreated(o, BuildCraftTransport.genericPipeBlock, this);
}
if (tile instanceof IPipeTile) {
((IPipeTile) tile).scheduleNeighborChange();
}
}
bindPipe();
computeConnections();
scheduleNeighborChange();
scheduleRenderUpdate();
if (pipe.needsInit()) {
pipe.initialize();
}
initialized = true;
}
private void bindPipe() {
if (!pipeBound && pipe != null) {
pipe.setTile(this);
coreState.pipeId = Item.getIdFromItem(pipe.item);
pipeBound = true;
}
}
public boolean isInitialized() {
return initialized;
}
public void scheduleNeighborChange() {
blockNeighborChange = true;
}
@Override
public boolean canInjectItems(ForgeDirection from) {
if (getPipeType() != IPipeTile.PipeType.ITEM) {
return false;
}
return isPipeConnected(from);
}
@Override
public int injectItem(ItemStack payload, boolean doAdd, ForgeDirection from, EnumColor color) {
if (!pipe.inputOpen(from)) {
return 0;
}
if (BlockGenericPipe.isValid(pipe) && pipe.transport instanceof PipeTransportItems && isPipeConnected(from)) {
if (doAdd) {
Position itemPos = new Position(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5, from.getOpposite());
itemPos.moveBackwards(0.4);
TravelingItem pipedItem = TravelingItem.make(itemPos.x, itemPos.y, itemPos.z, payload);
pipedItem.color = color;
((PipeTransportItems) pipe.transport).injectItem(pipedItem, itemPos.orientation);
}
return payload.stackSize;
}
return 0;
}
@Override
public int injectItem(ItemStack payload, boolean doAdd, ForgeDirection from) {
return injectItem(payload, doAdd, from, null);
}
@Override
public PipeType getPipeType() {
if (BlockGenericPipe.isValid(pipe)) {
return pipe.transport.getPipeType();
}
return null;
}
@Override
public int x() {
return xCoord;
}
@Override
public int y() {
return yCoord;
}
@Override
public int z() {
return zCoord;
}
/* SMP */
public BuildCraftPacket getBCDescriptionPacket() {
bindPipe();
updateCoreState();
PacketTileState packet = new PacketTileState(this.xCoord, this.yCoord, this.zCoord);
if (pipe != null && pipe.transport != null) {
pipe.transport.sendDescriptionPacket();
}
packet.addStateForSerialization((byte) 0, coreState);
packet.addStateForSerialization((byte) 1, renderState);
packet.addStateForSerialization((byte) 2, pluggableState);
if (pipe instanceof ISerializable) {
packet.addStateForSerialization((byte) 3, (ISerializable) pipe);
}
return packet;
}
@Override
public Packet getDescriptionPacket() {
return Utils.toPacket(getBCDescriptionPacket(), 1);
}
public void sendUpdateToClient() {
sendClientUpdate = true;
}
@Override
public void blockRemoved(ForgeDirection from) {
}
public TileBuffer[] getTileCache() {
if (tileBuffer == null && pipe != null) {
tileBuffer = TileBuffer.makeBuffer(worldObj, xCoord, yCoord, zCoord, pipe.transport.delveIntoUnloadedChunks());
}
return tileBuffer;
}
@Override
public void blockCreated(ForgeDirection from, Block block, TileEntity tile) {
TileBuffer[] cache = getTileCache();
if (cache != null) {
cache[from.getOpposite().ordinal()].set(block, tile);
}
}
@Override
public Block getBlock(ForgeDirection to) {
TileBuffer[] cache = getTileCache();
if (cache != null) {
return cache[to.ordinal()].getBlock();
} else {
return null;
}
}
@Override
public TileEntity getTile(ForgeDirection to) {
TileBuffer[] cache = getTileCache();
if (cache != null) {
return cache[to.ordinal()].getTile();
} else {
return null;
}
}
protected boolean canPipeConnect_internal(TileEntity with, ForgeDirection side) {
if (!(pipe instanceof IPipeConnectionForced) || !((IPipeConnectionForced) pipe).ignoreConnectionOverrides(side)) {
if (with instanceof IPipeConnection) {
IPipeConnection.ConnectOverride override = ((IPipeConnection) with).overridePipeConnection(pipe.transport.getPipeType(), side.getOpposite());
if (override != IPipeConnection.ConnectOverride.DEFAULT) {
return override == IPipeConnection.ConnectOverride.CONNECT;
}
}
}
if (with instanceof IPipeTile) {
IPipeTile other = (IPipeTile) with;
if (other.hasBlockingPluggable(side.getOpposite())) {
return false;
}
if (other.getPipeColor() >= 0 && glassColor >= 0 && other.getPipeColor() != glassColor) {
return false;
}
Pipe<?> otherPipe = (Pipe<?>) other.getPipe();
if (!BlockGenericPipe.isValid(otherPipe)) {
return false;
}
if (!otherPipe.canPipeConnect(this, side.getOpposite())) {
return false;
}
}
return pipe.canPipeConnect(with, side);
}
/**
* Checks if this tile can connect to another tile
*
* @param with - The other Tile
* @param side - The orientation to get to the other tile ('with')
* @return true if pipes are considered connected
*/
protected boolean canPipeConnect(TileEntity with, ForgeDirection side) {
if (with == null) {
return false;
}
if (hasBlockingPluggable(side)) {
return false;
}
if (!BlockGenericPipe.isValid(pipe)) {
return false;
}
return canPipeConnect_internal(with, side);
}
public boolean hasBlockingPluggable(ForgeDirection side) {
PipePluggable pluggable = getPipePluggable(side);
if (pluggable == null) {
return false;
}
if (pluggable instanceof IPipeConnection) {
IPipe neighborPipe = getNeighborPipe(side);
if (neighborPipe != null) {
IPipeConnection.ConnectOverride override = ((IPipeConnection) pluggable).overridePipeConnection(neighborPipe.getTile().getPipeType(), side);
if (override == IPipeConnection.ConnectOverride.CONNECT) {
return true;
} else if (override == IPipeConnection.ConnectOverride.DISCONNECT) {
return false;
}
}
}
return pluggable.isBlocking(this, side);
}
private void computeConnections() {
TileBuffer[] cache = getTileCache();
if (cache == null) {
return;
}
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) {
TileBuffer t = cache[side.ordinal()];
t.refresh();
pipeConnectionsBuffer[side.ordinal()] = canPipeConnect(t.getTile(), side);
}
}
@Override
public boolean isPipeConnected(ForgeDirection with) {
if (worldObj.isRemote) {
return renderState.pipeConnectionMatrix.isConnected(with);
}
return pipeConnectionsBuffer[with.ordinal()];
}
@Override
public boolean doDrop() {
if (BlockGenericPipe.isValid(pipe)) {
return pipe.doDrop();
} else {
return false;
}
}
@Override
public void onChunkUnload() {
if (pipe != null) {
pipe.onChunkUnload();
}
}
/**
* ITankContainer implementation *
*/
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
if (BlockGenericPipe.isValid(pipe) && pipe.transport instanceof IFluidHandler && !hasBlockingPluggable(from)) {
return ((IFluidHandler) pipe.transport).fill(from, resource, doFill);
} else {
return 0;
}
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
if (BlockGenericPipe.isValid(pipe) && pipe.transport instanceof IFluidHandler && !hasBlockingPluggable(from)) {
return ((IFluidHandler) pipe.transport).drain(from, maxDrain, doDrain);
} else {
return null;
}
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
if (BlockGenericPipe.isValid(pipe) && pipe.transport instanceof IFluidHandler && !hasBlockingPluggable(from)) {
return ((IFluidHandler) pipe.transport).drain(from, resource, doDrain);
} else {
return null;
}
}
@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
if (BlockGenericPipe.isValid(pipe) && pipe.transport instanceof IFluidHandler && !hasBlockingPluggable(from)) {
return ((IFluidHandler) pipe.transport).canFill(from, fluid);
} else {
return false;
}
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
if (BlockGenericPipe.isValid(pipe) && pipe.transport instanceof IFluidHandler && !hasBlockingPluggable(from)) {
return ((IFluidHandler) pipe.transport).canDrain(from, fluid);
} else {
return false;
}
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from) {
return null;
}
public void scheduleRenderUpdate() {
refreshRenderState = true;
}
public boolean hasFacade(ForgeDirection direction) {
if (direction == null || direction == ForgeDirection.UNKNOWN) {
return false;
} else {
return sideProperties.pluggables[direction.ordinal()] instanceof FacadePluggable;
}
}
public boolean hasGate(ForgeDirection direction) {
if (direction == null || direction == ForgeDirection.UNKNOWN) {
return false;
} else {
return sideProperties.pluggables[direction.ordinal()] instanceof GatePluggable;
}
}
public boolean setPluggable(ForgeDirection direction, PipePluggable pluggable) {
if (worldObj != null && worldObj.isRemote) {
return false;
}
if (direction == null || direction == ForgeDirection.UNKNOWN) {
return false;
}
// Remove old pluggable
if (sideProperties.pluggables[direction.ordinal()] != null) {
sideProperties.dropItem(this, direction);
pipe.eventBus.unregisterHandler(sideProperties.pluggables[direction.ordinal()]);
}
sideProperties.pluggables[direction.ordinal()] = pluggable;
if (pluggable != null) {
pipe.eventBus.registerHandler(pluggable);
pluggable.onAttachedPipe(this, direction);
}
notifyBlockChanged();
return true;
}
private void updateCoreState() {
}
public boolean hasEnabledFacade(ForgeDirection direction) {
return hasFacade(direction) && !((FacadePluggable) getPipePluggable(direction)).getRenderingTransparent();
}
public DockingStation getStation(ForgeDirection direction) {
PipePluggable pluggable = sideProperties.pluggables[direction.ordinal()];
return pluggable instanceof RobotStationPluggable ?
((RobotStationPluggable) pluggable).getStation() : null;
}
// Legacy
public void setGate(Gate gate, int direction) {
if (sideProperties.pluggables[direction] == null) {
gate.setDirection(ForgeDirection.getOrientation(direction));
pipe.gates[direction] = gate;
sideProperties.pluggables[direction] = new GatePluggable(gate);
}
}
@SideOnly(Side.CLIENT)
public IIconProvider getPipeIcons() {
if (pipe == null) {
return null;
}
return pipe.getIconProvider();
}
@Override
public ISerializable getStateInstance(byte stateId) {
switch (stateId) {
case 0:
return coreState;
case 1:
return renderState;
case 2:
return pluggableState;
case 3:
return (ISerializable) pipe;
}
throw new RuntimeException("Unknown state requested: " + stateId + " this is a bug!");
}
@Override
public void afterStateUpdated(byte stateId) {
if (!worldObj.isRemote) {
return;
}
switch (stateId) {
case 0:
if (pipe == null && coreState.pipeId != 0) {
initialize(BlockGenericPipe.createPipe((Item) Item.itemRegistry.getObjectById(coreState.pipeId)));
}
if (pipe == null) {
break;
}
worldObj.markBlockRangeForRenderUpdate(xCoord, yCoord, zCoord, xCoord, yCoord, zCoord);
break;
case 1: {