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
|
package gregtech.api.multitileentity.multiblock.base;
import static gregtech.GT_Mod.GT_FML_LOGGER;
import static gregtech.api.enums.GT_Values.NBT;
import com.gtnewhorizon.structurelib.StructureLibAPI;
import com.gtnewhorizon.structurelib.alignment.IAlignment;
import com.gtnewhorizon.structurelib.alignment.IAlignmentLimits;
import com.gtnewhorizon.structurelib.alignment.constructable.IConstructable;
import com.gtnewhorizon.structurelib.alignment.enumerable.ExtendedFacing;
import com.gtnewhorizon.structurelib.alignment.enumerable.Flip;
import com.gtnewhorizon.structurelib.alignment.enumerable.Rotation;
import com.gtnewhorizon.structurelib.structure.IStructureDefinition;
import com.gtnewhorizon.structurelib.structure.IStructureElement;
import com.gtnewhorizon.structurelib.util.Vec3Impl;
import cpw.mods.fml.common.network.NetworkRegistry;
import gnu.trove.list.TIntList;
import gnu.trove.list.array.TIntArrayList;
import gregtech.api.enums.GT_Values;
import gregtech.api.enums.OrePrefixes;
import gregtech.api.enums.TextureSet;
import gregtech.api.interfaces.IDescribable;
import gregtech.api.interfaces.tileentity.IMachineProgress;
import gregtech.api.multitileentity.MultiTileEntityContainer;
import gregtech.api.multitileentity.MultiTileEntityRegistry;
import gregtech.api.multitileentity.interfaces.IMultiBlockController;
import gregtech.api.multitileentity.interfaces.IMultiBlockFluidHandler;
import gregtech.api.multitileentity.interfaces.IMultiBlockInventory;
import gregtech.api.multitileentity.interfaces.IMultiTileEntity;
import gregtech.api.multitileentity.interfaces.IMultiTileEntity.IMTE_AddToolTips;
import gregtech.api.multitileentity.machine.MultiTileBasicMachine;
import gregtech.api.objects.GT_ItemStack;
import gregtech.api.util.GT_Multiblock_Tooltip_Builder;
import gregtech.api.util.GT_Utility;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
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.IFluidTank;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.lwjgl.input.Keyboard;
public abstract class MultiBlockController<T extends MultiBlockController<T>> extends MultiTileBasicMachine
implements IAlignment,
IConstructable,
IMultiBlockController,
IDescribable,
IMachineProgress,
IMultiBlockFluidHandler,
IMultiBlockInventory,
IMTE_AddToolTips {
private static final Map<Integer, GT_Multiblock_Tooltip_Builder> tooltip = new ConcurrentHashMap<>();
protected BuildState buildState = new BuildState();
// The 0th slot is the default inventory of the MultiBlock; any other has been added by an Inventory Extender of
// sorts
protected List<ItemStack[]> multiBlockInventory = new ArrayList<>();
private int mMaxProgresstime = 0, mProgresstime = 0;
private boolean mStructureOkay = false, mStructureChanged = false;
private boolean mWorks = true, mWorkUpdate = false, mWasShutdown = false, mActive = false;
private ExtendedFacing mExtendedFacing = ExtendedFacing.DEFAULT;
private IAlignmentLimits mLimits = getInitialAlignmentLimits();
/** Registry ID of the required casing */
public abstract short getCasingRegistryID();
/** Meta ID of the required casing */
public abstract short getCasingMeta();
/**
* Create the tooltip for this multi block controller.
*/
protected abstract GT_Multiblock_Tooltip_Builder createTooltip();
/**
* @return The starting offset for the structure builder
*/
public abstract Vec3Impl getStartingStructureOffset();
/**
* Due to limitation of Java type system, you might need to do an unchecked cast.
* HOWEVER, the returned IStructureDefinition is expected to be evaluated against current instance only, and should
* not be used against other instances, even for those of the same class.
*/
public abstract IStructureDefinition<T> getStructureDefinition();
/**
* Checks the Machine.
* <p>
* NOTE: If using `buildState` be sure to `startBuilding()` and either `endBuilding()` or `failBuilding()`
*/
public abstract boolean checkMachine();
/**
* Checks the Recipe
*/
public abstract boolean checkRecipe(ItemStack aStack);
@Override
public void writeMultiTileNBT(NBTTagCompound aNBT) {
super.writeMultiTileNBT(aNBT);
aNBT.setBoolean(NBT.STRUCTURE_OK, mStructureOkay);
aNBT.setByte(NBT.ROTATION, (byte) mExtendedFacing.getRotation().getIndex());
aNBT.setByte(NBT.FLIP, (byte) mExtendedFacing.getFlip().getIndex());
}
@Override
public void readMultiTileNBT(NBTTagCompound aNBT) {
super.readMultiTileNBT(aNBT);
// Multiblock inventories are a collection of inventories. The first inventory is the default internal
// inventory,
// and the others are added by inventory extending blocks.
if (mInventory != null) multiBlockInventory.add(mInventory);
mStructureOkay = aNBT.getBoolean(NBT.STRUCTURE_OK);
mExtendedFacing = ExtendedFacing.of(
ForgeDirection.getOrientation(getFrontFacing()),
Rotation.byIndex(aNBT.getByte(NBT.ROTATION)),
Flip.byIndex(aNBT.getByte(NBT.FLIP)));
}
@Override
public void addToolTips(List<String> aList, ItemStack aStack, boolean aF3_H) {
aList.addAll(Arrays.asList(getDescription()));
}
@Override
public String[] getDescription() {
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
return getTooltip().getStructureInformation();
} else {
return getTooltip().getInformation();
}
}
@Override
protected void addDebugInfo(EntityPlayer aPlayer, int aLogLevel, ArrayList<String> tList) {
tList.add("Structure ok: " + checkStructure(false));
}
protected int getToolTipID() {
return getMultiTileEntityRegistryID() << 16 + getMultiTileEntityID();
}
protected GT_Multiblock_Tooltip_Builder getTooltip() {
return createTooltip();
// final int tooltipId = getToolTipID();
// final GT_Multiblock_Tooltip_Builder tt = tooltip.get(tooltipId);
// if (tt == null) {
// return tooltip.computeIfAbsent(tooltipId, k -> createTooltip());
// }
// return tt;
}
@Override
public boolean checkStructure(boolean aForceReset) {
if (!isServerSide()) return mStructureOkay;
// Only trigger an update if forced (from onPostTick, generally), or if the structure has changed
if ((mStructureChanged || aForceReset)) {
mStructureOkay = checkMachine();
}
mStructureChanged = false;
return mStructureOkay;
}
@Override
public void onStructureChange() {
mStructureChanged = true;
}
public final boolean checkPiece(String piece, Vec3Impl offset) {
return checkPiece(piece, offset.get0(), offset.get1(), offset.get2());
}
/**
* Explanation of the world coordinate these offset means:
* <p>
* Imagine you stand in front of the controller, with controller facing towards you not rotated or flipped.
* <p>
* The horizontalOffset would be the number of blocks on the left side of the controller, not counting controller itself.
* The verticalOffset would be the number of blocks on the top side of the controller, not counting controller itself.
* The depthOffset would be the number of blocks between you and controller, not counting controller itself.
* <p>
* All these offsets can be negative.
*/
protected final boolean checkPiece(String piece, int horizontalOffset, int verticalOffset, int depthOffset) {
return getCastedStructureDefinition()
.check(
this,
piece,
getWorld(),
getExtendedFacing(),
getXCoord(),
getYCoord(),
getZCoord(),
horizontalOffset,
verticalOffset,
depthOffset,
!mStructureOkay);
}
public final boolean buildPiece(String piece, ItemStack trigger, boolean hintsOnly, Vec3Impl offset) {
return buildPiece(piece, trigger, hintsOnly, offset.get0(), offset.get1(), offset.get2());
}
protected final boolean buildPiece(
String piece,
ItemStack trigger,
boolean hintOnly,
int horizontalOffset,
int verticalOffset,
int depthOffset) {
return getCastedStructureDefinition()
.buildOrHints(
this,
trigger,
piece,
getWorld(),
getExtendedFacing(),
getXCoord(),
getYCoord(),
getZCoord(),
horizontalOffset,
verticalOffset,
depthOffset,
hintOnly);
}
@SuppressWarnings("unchecked")
private IStructureDefinition<MultiBlockController<T>> getCastedStructureDefinition() {
return (IStructureDefinition<MultiBlockController<T>>) getStructureDefinition();
}
@Override
public ExtendedFacing getExtendedFacing() {
return mExtendedFacing;
}
@Override
public void setExtendedFacing(ExtendedFacing newExtendedFacing) {
if (mExtendedFacing != newExtendedFacing) {
onStructureChange();
if (mStructureOkay) stopMachine();
mExtendedFacing = newExtendedFacing;
mStructureOkay = false;
if (isServerSide()) {
StructureLibAPI.sendAlignment(
this,
new NetworkRegistry.TargetPoint(
getWorld().provider.dimensionId, getXCoord(), getYCoord(), getZCoord(), 512));
} else {
issueTextureUpdate();
}
}
}
@Override
public boolean onWrenchRightClick(
EntityPlayer aPlayer, ItemStack tCurrentItem, byte wrenchSide, float aX, float aY, float aZ) {
if (wrenchSide != getFrontFacing())
return super.onWrenchRightClick(aPlayer, tCurrentItem, wrenchSide, aX, aY, aZ);
if (aPlayer.isSneaking()) {
// we won't be allowing horizontal flips, as it can be perfectly emulated by rotating twice and flipping
// horizontally
// allowing an extra round of flip make it hard to draw meaningful flip markers in GT_Proxy#drawGrid
toolSetFlip(getFlip().isHorizontallyFlipped() ? Flip.NONE : Flip.HORIZONTAL);
} else {
toolSetRotation(null);
}
return true;
}
@Override
public void onFirstTick(boolean aIsServerSide) {
super.onFirstTick(aIsServerSide);
if (aIsServerSide) checkStructure(true);
else StructureLibAPI.queryAlignment(this);
}
@Override
public void onPostTick(long aTick, boolean aIsServerSide) {
if (aIsServerSide) {
if (aTick % 600 == 5) {
// Recheck the structure every 30 seconds or so
if (!checkStructure(false)) checkStructure(true);
}
}
}
@Override
public final boolean isFacingValid(byte aFacing) {
return canSetToDirectionAny(ForgeDirection.getOrientation(aFacing));
}
@Override
public void onFacingChange() {
toolSetDirection(ForgeDirection.getOrientation(getFrontFacing()));
onStructureChange();
}
@Override
public boolean allowCoverOnSide(byte aSide, GT_ItemStack aCoverID) {
return aSide != mFacing;
}
@Override
public String[] getStructureDescription(ItemStack stackSize) {
return getTooltip().getStructureHint();
}
@Override
public IAlignmentLimits getAlignmentLimits() {
return mLimits;
}
protected void setAlignmentLimits(IAlignmentLimits mLimits) {
this.mLimits = mLimits;
}
// IMachineProgress
@Override
public int getProgress() {
return mProgresstime;
}
@Override
public int getMaxProgress() {
return mMaxProgresstime;
}
@Override
public boolean increaseProgress(int aProgressAmountInTicks) {
return increaseProgressGetOverflow(aProgressAmountInTicks) != aProgressAmountInTicks;
}
@Override
public FluidStack getDrainableFluid(byte aSide) {
final IFluidTank tank = getFluidTankDrainable(aSide, null);
return tank == null ? null : tank.getFluid();
}
/**
* Increases the Progress, returns the overflown Progress.
*/
public int increaseProgressGetOverflow(int aProgress) {
return 0;
}
@Override
public boolean hasThingsToDo() {
return getMaxProgress() > 0;
}
@Override
public boolean hasWorkJustBeenEnabled() {
return mWorkUpdate;
}
@Override
public void enableWorking() {
if (!mWorks) mWorkUpdate = true;
mWorks = true;
mWasShutdown = false;
}
@Override
public void disableWorking() {
mWorks = false;
}
@Override
public boolean isAllowedToWork() {
return mWorks;
}
@Override
public boolean isActive() {
return mActive;
}
@Override
public void setActive(boolean aActive) {
mActive = aActive;
}
@Override
public boolean wasShutdown() {
return mWasShutdown;
}
// End IMachineProgress
public void stopMachine() {
disableWorking();
}
protected IAlignmentLimits getInitialAlignmentLimits() {
return (d, r, f) -> !f.isVerticallyFliped();
}
public static class BuildState {
/**
* Utility class to keep track of the build state of a multiblock
*/
boolean building = false;
Vec3Impl currentOffset;
public void startBuilding(Vec3Impl structureOffset) {
if (building) throw new IllegalStateException("Already building!");
building = true;
setCurrentOffset(structureOffset);
}
public Vec3Impl setCurrentOffset(Vec3Impl structureOffset) {
verifyBuilding();
return (currentOffset = structureOffset);
}
private void verifyBuilding() {
if (!building) throw new IllegalStateException("Not building!");
}
public boolean failBuilding() {
building = false;
currentOffset = null;
return false;
}
public Vec3Impl stopBuilding() {
final Vec3Impl toReturn = getCurrentOffset();
building = false;
currentOffset = null;
return toReturn;
}
public Vec3Impl getCurrentOffset() {
verifyBuilding();
return currentOffset;
}
public Vec3Impl addOffset(Vec3Impl offset) {
verifyBuilding();
return setCurrentOffset(currentOffset.add(offset));
}
}
public <S> IStructureElement<S> addMultiTileCasing(int aRegistryID, int aBlockMeta, int aModes) {
return new IStructureElement<S>() {
private final short[] DEFAULT = new short[] {255, 255, 255, 0};
private IIcon[] mIcons = null;
@Override
public boolean check(S t, World world, int x, int y, int z) {
final TileEntity tileEntity = world.getTileEntity(x, y, z);
if (!(tileEntity instanceof MultiBlockPart)) return false;
final MultiBlockPart part = (MultiBlockPart) tileEntity;
if (aRegistryID != part.getMultiTileEntityRegistryID() || aBlockMeta != part.getMultiTileEntityID())
return false;
final IMultiBlockController tTarget = part.getTarget(false);
if (tTarget != null && tTarget != MultiBlockController.this) return false;
part.setTarget(MultiBlockController.this, aModes);
return true;
}
@Override
public boolean spawnHint(S t, World world, int x, int y, int z, ItemStack trigger) {
if (mIcons == null) {
mIcons = new IIcon[6];
Arrays.fill(mIcons, TextureSet.SET_NONE.mTextures[OrePrefixes.block.mTextureIndex].getIcon());
// Arrays.fill(mIcons, getTexture(aCasing);
// for (int i = 0; i < 6; i++) {
// mIcons[i] = aCasing.getIcon(i, aMeta);
// }
}
final short[] RGBA = DEFAULT;
StructureLibAPI.hintParticleTinted(world, x, y, z, mIcons, RGBA);
// StructureLibAPI.hintParticle(world, x, y, z, aCasing, aMeta);
return true;
}
@Override
public boolean placeBlock(S t, World world, int x, int y, int z, ItemStack trigger) {
final MultiTileEntityRegistry tRegistry = MultiTileEntityRegistry.getRegistry(aRegistryID);
final MultiTileEntityContainer tContainer =
tRegistry.getNewTileEntityContainer(world, x, y, z, aBlockMeta, null);
if (tContainer == null) {
GT_FML_LOGGER.error("NULL CONTAINER");
return false;
}
final IMultiTileEntity te = ((IMultiTileEntity) tContainer.mTileEntity);
if (!(te instanceof MultiBlockPart)) {
GT_FML_LOGGER.error("Not a multiblock part");
return false;
}
if (world.setBlock(x, y, z, tContainer.mBlock, 15 - tContainer.mBlockMetaData, 2)) {
tContainer.setMultiTile(world, x, y, z);
((MultiBlockPart) te).setTarget(MultiBlockController.this, aModes);
}
return false;
}
public IIcon getTexture(OrePrefixes aBlock) {
return TextureSet.SET_NONE.mTextures[OrePrefixes.block.mTextureIndex].getIcon();
}
};
}
/**
* Fluid - MultiBlock related Fluid Tank behaviour.
*/
protected IFluidTank getFluidTankFillable(MultiBlockPart aPart, byte aSide, FluidStack aFluidToFill) {
return getFluidTankFillable(aSide, aFluidToFill);
}
protected IFluidTank getFluidTankDrainable(MultiBlockPart aPart, byte aSide, FluidStack aFluidToDrain) {
return getFluidTankDrainable(aSide, aFluidToDrain);
}
protected IFluidTank[] getFluidTanks(MultiBlockPart aPart, byte aSide) {
return getFluidTanks(aSide);
}
@Override
public int fill(MultiBlockPart aPart, ForgeDirection aDirection, FluidStack aFluid, boolean aDoFill) {
if (aFluid == null || aFluid.amount <= 0) return 0;
final IFluidTank tTank = getFluidTankFillable(aPart, (byte) aDirection.ordinal(), aFluid);
if (tTank == null) return 0;
final int rFilledAmount = tTank.fill(aFluid, aDoFill);
if (rFilledAmount > 0 && aDoFill) mInventoryChanged = true;
return rFilledAmount;
}
@Override
public FluidStack drain(MultiBlockPart aPart, ForgeDirection aDirection, FluidStack aFluid, boolean aDoDrain) {
if (aFluid == null || aFluid.amount <= 0) return null;
final IFluidTank tTank = getFluidTankDrainable(aPart, (byte) aDirection.ordinal(), aFluid);
if (tTank == null
|| tTank.getFluid() == null
|| tTank.getFluidAmount() == 0
|| !tTank.getFluid().isFluidEqual(aFluid)) return null;
final FluidStack rDrained = tTank.drain(aFluid.amount, aDoDrain);
if (rDrained != null && aDoDrain) markInventoryBeenModified();
return rDrained;
}
@Override
public FluidStack drain(MultiBlockPart aPart, ForgeDirection aDirection, int aAmountToDrain, boolean aDoDrain) {
if (aAmountToDrain <= 0) return null;
final IFluidTank tTank = getFluidTankDrainable(aPart, (byte) aDirection.ordinal(), null);
if (tTank == null || tTank.getFluid() == null || tTank.getFluidAmount() == 0) return null;
final FluidStack rDrained = tTank.drain(aAmountToDrain, aDoDrain);
if (rDrained != null && aDoDrain) markInventoryBeenModified();
return rDrained;
}
@Override
public boolean canFill(MultiBlockPart aPart, ForgeDirection aDirection, Fluid aFluid) {
if (aFluid == null) return false;
final IFluidTank tTank = getFluidTankFillable(aPart, (byte) aDirection.ordinal(), new FluidStack(aFluid, 0));
return tTank != null && (tTank.getFluid() == null || tTank.getFluid().getFluid() == aFluid);
}
@Override
public boolean canDrain(MultiBlockPart aPart, ForgeDirection aDirection, Fluid aFluid) {
if (aFluid == null) return false;
final IFluidTank tTank = getFluidTankDrainable(aPart, (byte) aDirection.ordinal(), new FluidStack(aFluid, 0));
return tTank != null && (tTank.getFluid() != null && tTank.getFluid().getFluid() == aFluid);
}
@Override
public FluidTankInfo[] getTankInfo(MultiBlockPart aPart, ForgeDirection aDirection) {
final IFluidTank[] tTanks = getFluidTanks(aPart, (byte) aDirection.ordinal());
if (tTanks == null || tTanks.length <= 0) return GT_Values.emptyFluidTankInfo;
final FluidTankInfo[] rInfo = new FluidTankInfo[tTanks.length];
for (int i = 0; i < tTanks.length; i++) rInfo[i] = new FluidTankInfo(tTanks[i]);
return rInfo;
}
/**
* Energy - MultiBlock related Energy behavior
*/
@Override
public boolean isUniversalEnergyStored(MultiBlockPart aPart, long aEnergyAmount) {
return getUniversalEnergyStored(aPart) >= aEnergyAmount;
}
@Override
public long getUniversalEnergyStored(MultiBlockPart aPart) {
return Math.min(getUniversalEnergyStored(), getUniversalEnergyCapacity());
}
@Override
public long getUniversalEnergyCapacity(MultiBlockPart aPart) {
return getUniversalEnergyCapacity();
}
@Override
public long getOutputAmperage(MultiBlockPart aPart) {
return getOutputAmperage();
}
@Override
public long getOutputVoltage(MultiBlockPart aPart) {
return getOutputVoltage();
}
@Override
public long getInputAmperage(MultiBlockPart aPart) {
return getInputAmperage();
}
@Override
public long getInputVoltage(MultiBlockPart aPart) {
return getInputVoltage();
}
@Override
public boolean decreaseStoredEnergyUnits(MultiBlockPart aPart, long aEnergy, boolean aIgnoreTooLittleEnergy) {
return decreaseStoredEnergyUnits(aEnergy, aIgnoreTooLittleEnergy);
}
@Override
public boolean increaseStoredEnergyUnits(MultiBlockPart aPart, long aEnergy, boolean aIgnoreTooMuchEnergy) {
return increaseStoredEnergyUnits(aEnergy, aIgnoreTooMuchEnergy);
}
@Override
public boolean drainEnergyUnits(MultiBlockPart aPart, byte aSide, long aVoltage, long aAmperage) {
return drainEnergyUnits(aSide, aVoltage, aAmperage);
}
@Override
public long injectEnergyUnits(MultiBlockPart aPart, byte aSide, long aVoltage, long aAmperage) {
return injectEnergyUnits(aSide, aVoltage, aAmperage);
}
@Override
public long getAverageElectricInput(MultiBlockPart aPart) {
return getAverageElectricInput();
}
@Override
public long getAverageElectricOutput(MultiBlockPart aPart) {
return getAverageElectricOutput();
}
@Override
public long getStoredEU(MultiBlockPart aPart) {
return getStoredEU();
}
@Override
public long getEUCapacity(MultiBlockPart aPart) {
return getEUCapacity();
}
@Override
public boolean inputEnergyFrom(MultiBlockPart aPart, byte aSide) {
if (aSide == GT_Values.SIDE_UNKNOWN) return true;
if (aSide >= 0 && aSide < 6) {
if (isInvalid()) return false;
if (isEnetInput()) return isEnergyInputSide(aSide);
}
return false;
}
@Override
public boolean outputsEnergyTo(MultiBlockPart aPart, byte aSide) {
if (aSide == GT_Values.SIDE_UNKNOWN) return true;
if (aSide >= 0 && aSide < 6) {
if (isInvalid()) return false;
if (isEnetOutput()) return isEnergyOutputSide(aSide);
}
return false;
}
/**
* Item - MultiBlock related Item behaviour.
*/
@Override
public boolean hasInventoryBeenModified(MultiBlockPart aPart) {
// TODO: MultiInventory - Figure this out based on locked & the part
return hasInventoryBeenModified();
}
@Override
public boolean isValidSlot(MultiBlockPart aPart, int aIndex) {
return false;
}
@Override
public boolean addStackToSlot(MultiBlockPart aPart, int aIndex, ItemStack aStack) {
return false;
}
@Override
public boolean addStackToSlot(MultiBlockPart aPart, int aIndex, ItemStack aStack, int aAmount) {
return false;
}
protected Pair<ItemStack[], Integer> getInventory(int lockedInventory, int aSlot) {
if (lockedInventory != -1) return new ImmutablePair<>(multiBlockInventory.get(lockedInventory), aSlot);
int start = 0;
for (ItemStack[] inv : multiBlockInventory) {
if (aSlot > start && aSlot < start + inv.length) {
return new ImmutablePair<>(inv, aSlot - start);
}
start += inv.length;
}
return null;
}
@Override
public int[] getAccessibleSlotsFromSide(MultiBlockPart aPart, byte aSide) {
final TIntList tList = new TIntArrayList();
final int lockedInventory = aPart.getLockedInventory();
int start = 0;
if (lockedInventory == -1) {
for (ItemStack[] inv : multiBlockInventory) {
for (int i = start; i < inv.length + start; i++) tList.add(i);
start += inv.length;
}
} else {
final int len = multiBlockInventory.get(lockedInventory).length;
for (int i = 0; i < len; i++) tList.add(i);
}
return tList.toArray();
}
@Override
public boolean canInsertItem(MultiBlockPart aPart, int aSlot, ItemStack aStack, byte aSide) {
final int lockedInventory = aPart.getLockedInventory(), tSlot;
final ItemStack[] inv;
if (lockedInventory == -1) {
final Pair<ItemStack[], Integer> tInv = getInventory(lockedInventory, aSlot);
if (tInv == null) return false;
inv = tInv.getLeft();
tSlot = tInv.getRight();
} else {
inv = multiBlockInventory.get(lockedInventory);
tSlot = aSlot;
}
return inv[tSlot] == null
|| GT_Utility.areStacksEqual(
aStack, inv[tSlot]); // && allowPutStack(getBaseMetaTileEntity(), aIndex, (byte) aSide, aStack)
}
@Override
public boolean canExtractItem(MultiBlockPart aPart, int aSlot, ItemStack aStack, byte aSide) {
final int lockedInventory = aPart.getLockedInventory(), tSlot;
final ItemStack[] inv;
if (lockedInventory == -1) {
final Pair<ItemStack[], Integer> tInv = getInventory(lockedInventory, aSlot);
if (tInv == null) return false;
inv = tInv.getLeft();
tSlot = tInv.getRight();
} else {
inv = multiBlockInventory.get(lockedInventory);
tSlot = aSlot;
}
return inv[tSlot] != null; // && allowPullStack(getBaseMetaTileEntity(), aIndex, (byte) aSide, aStack);
}
@Override
public int getSizeInventory(MultiBlockPart aPart) {
final int lockedInventory = aPart.getLockedInventory();
if (lockedInventory == -1) {
int len = 0;
for (ItemStack[] inv : multiBlockInventory) len += inv.length;
return len;
} else {
return multiBlockInventory.get(lockedInventory).length;
}
}
@Override
public ItemStack getStackInSlot(MultiBlockPart aPart, int aSlot) {
final int lockedInventory = aPart.getLockedInventory(), tSlot;
final ItemStack[] inv;
if (lockedInventory == -1) {
final Pair<ItemStack[], Integer> tInv = getInventory(lockedInventory, aSlot);
if (tInv == null) return null;
inv = tInv.getLeft();
tSlot = tInv.getRight();
} else {
inv = multiBlockInventory.get(lockedInventory);
tSlot = aSlot;
}
return inv[tSlot];
}
@Override
public ItemStack decrStackSize(MultiBlockPart aPart, int aSlot, int aDecrement) {
final ItemStack tStack = getStackInSlot(aPart, aSlot);
ItemStack rStack = GT_Utility.copyOrNull(tStack);
if (tStack != null) {
if (tStack.stackSize <= aDecrement) {
setInventorySlotContents(aSlot, null);
} else {
rStack = tStack.splitStack(aDecrement);
if (tStack.stackSize == 0) setInventorySlotContents(aSlot, null);
}
}
return rStack;
}
@Override
public ItemStack getStackInSlotOnClosing(MultiBlockPart aPart, int aSlot) {
final Pair<ItemStack[], Integer> tInv = getInventory(aPart.getLockedInventory(), aSlot);
if (tInv == null) return null;
final ItemStack[] inv = tInv.getLeft();
final int tSlot = tInv.getRight();
final ItemStack rStack = inv[tSlot];
inv[tSlot] = null;
return rStack;
}
@Override
public void setInventorySlotContents(MultiBlockPart aPart, int aSlot, ItemStack aStack) {
final Pair<ItemStack[], Integer> tInv = getInventory(aPart.getLockedInventory(), aSlot);
if (tInv == null) return;
final ItemStack[] inv = tInv.getLeft();
final int tSlot = tInv.getRight();
inv[tSlot] = aStack;
}
@Override
public String getInventoryName(MultiBlockPart aPart) {
return getInventoryName(); // TODO: MultiInventory: Include part Name?
}
@Override
public boolean hasCustomInventoryName(MultiBlockPart aPart) {
return hasCustomInventoryName();
}
@Override
public int getInventoryStackLimit(MultiBlockPart aPart) {
return getInventoryStackLimit();
}
@Override
public void markDirty(MultiBlockPart aPart) {
// TODO: MultiInventory - Consider the part?
markDirty();
markInventoryBeenModified();
}
@Override
public boolean isUseableByPlayer(MultiBlockPart aPart, EntityPlayer aPlayer) {
return isUseableByPlayer(aPlayer);
}
@Override
public void openInventory(MultiBlockPart aPart) {
// TODO: MultiInventory - consider the part's inventory
openInventory();
}
@Override
public void closeInventory(MultiBlockPart aPart) {
// TODO: MultiInventory - consider the part's inventory
closeInventory();
}
@Override
public boolean isItemValidForSlot(MultiBlockPart aPart, int aSlot, ItemStack aStack) {
return isItemValidForSlot(aSlot, aStack);
}
}
|