1616import appeng .crafting .pattern .EncodedPatternItem ;
1717import appeng .helpers .iface .PatternContainer ;
1818import appeng .items .storage .BasicStorageCell ;
19+ import appeng .me .cells .BasicCellHandler ;
20+ import appeng .me .cells .BasicCellInventory ;
1921import appeng .parts .storagebus .StorageBusPart ;
2022import com .the9grounds .aeadditions .item .storage .SuperStorageCell ;
2123import dan200 .computercraft .shared .util .NBTUtil ;
2224import de .srendi .advancedperipherals .AdvancedPeripherals ;
2325import de .srendi .advancedperipherals .common .addons .APAddons ;
26+ import de .srendi .advancedperipherals .common .addons .mekanism .MekanismCapabilities ;
2427import de .srendi .advancedperipherals .common .util .LuaConverter ;
2528import de .srendi .advancedperipherals .common .util .Pair ;
2629import de .srendi .advancedperipherals .common .util .inventory .FluidFilter ;
3033import io .github .projectet .ae2things .item .DISKDrive ;
3134import it .unimi .dsi .fastutil .objects .Object2LongMap ;
3235import me .ramidzkh .mekae2 .ae2 .MekanismKey ;
36+ import me .ramidzkh .mekae2 .ae2 .MekanismKeyType ;
37+ import me .ramidzkh .mekae2 .item .ChemicalStorageCell ;
38+ import mekanism .api .chemical .IChemicalHandler ;
39+ import mekanism .api .chemical .gas .IGasHandler ;
40+ import mekanism .api .chemical .infuse .IInfusionHandler ;
41+ import mekanism .api .chemical .pigment .IPigmentHandler ;
42+ import mekanism .api .chemical .slurry .ISlurryHandler ;
43+ import mekanism .common .tile .TileEntityChemicalTank ;
3344import net .minecraft .core .BlockPos ;
3445import net .minecraft .nbt .CompoundTag ;
3546import net .minecraft .world .item .ItemStack ;
@@ -407,6 +418,9 @@ public static boolean isFluidCrafting(MEStorage monitor, ICraftingService grid,
407418 return false ;
408419 }
409420
421+ /// External Storage
422+ /// Total
423+
410424 public static long getTotalExternalItemStorage (IGridNode node ) {
411425 long total = 0 ;
412426
@@ -431,6 +445,109 @@ public static long getTotalExternalItemStorage(IGridNode node) {
431445 return total ;
432446 }
433447
448+ public static long getTotalExternalFluidStorage (IGridNode node ) {
449+ long total = 0 ;
450+
451+ for (IGridNode iGridNode : node .getGrid ().getMachineNodes (StorageBusPart .class )) {
452+ StorageBusPart bus = (StorageBusPart ) iGridNode .getService (IStorageProvider .class );
453+ Level level = bus .getLevel ();
454+ BlockPos connectedInventoryPos = bus .getHost ().getBlockEntity ().getBlockPos ().relative (bus .getSide ());
455+ BlockEntity connectedInventoryEntity = level .getBlockEntity (connectedInventoryPos );
456+
457+ if (connectedInventoryEntity == null )
458+ continue ;
459+
460+ LazyOptional <IFluidHandler > fluidHandler = connectedInventoryEntity .getCapability (ForgeCapabilities .FLUID_HANDLER );
461+ if (fluidHandler .isPresent ()) {
462+ IFluidHandler handler = fluidHandler .orElse (null );
463+ for (int i = 0 ; i < handler .getTanks (); i ++) {
464+ total += handler .getTankCapacity (i );
465+ }
466+ }
467+ }
468+
469+ return total ;
470+ }
471+
472+ public static long getTotalExternalChemicalStorage (IGridNode node ) {
473+ long total = 0 ;
474+
475+ if (!APAddons .appMekLoaded )
476+ return 0 ;
477+
478+ for (IGridNode iGridNode : node .getGrid ().getMachineNodes (StorageBusPart .class )) {
479+ StorageBusPart bus = (StorageBusPart ) iGridNode .getService (IStorageProvider .class );
480+ Level level = bus .getLevel ();
481+ BlockPos connectedInventoryPos = bus .getHost ().getBlockEntity ().getBlockPos ().relative (bus .getSide ());
482+ BlockEntity connectedInventoryEntity = level .getBlockEntity (connectedInventoryPos );
483+
484+ if (connectedInventoryEntity == null )
485+ continue ;
486+
487+ if (connectedInventoryEntity instanceof TileEntityChemicalTank tank ) {
488+ total += tank .getChemicalTank ().getTankFromCurrent (tank .getChemicalTank ().getCurrent ()).getCapacity ();
489+ }
490+ }
491+
492+ return total ;
493+ }
494+
495+ /// Used
496+
497+ public static long getUsedExternalItemStorage (IGridNode node ) {
498+ long used = 0 ;
499+
500+ for (IGridNode iGridNode : node .getGrid ().getMachineNodes (StorageBusPart .class )) {
501+ StorageBusPart bus = (StorageBusPart ) iGridNode .getService (IStorageProvider .class );
502+ KeyCounter keyCounter = bus .getInternalHandler ().getAvailableStacks ();
503+
504+ for (Object2LongMap .Entry <AEKey > aeKey : keyCounter ) {
505+ if (aeKey .getKey () instanceof AEItemKey ) {
506+ used += aeKey .getLongValue ();
507+ }
508+ }
509+ }
510+
511+ return used ;
512+ }
513+
514+ public static long getUsedExternalFluidStorage (IGridNode node ) {
515+ long used = 0 ;
516+
517+ for (IGridNode iGridNode : node .getGrid ().getMachineNodes (StorageBusPart .class )) {
518+ StorageBusPart bus = (StorageBusPart ) iGridNode .getService (IStorageProvider .class );
519+ KeyCounter keyCounter = bus .getInternalHandler ().getAvailableStacks ();
520+
521+ for (Object2LongMap .Entry <AEKey > aeKey : keyCounter ) {
522+ if (aeKey .getKey () instanceof AEFluidKey ) {
523+ used += aeKey .getLongValue ();
524+ }
525+ }
526+ }
527+
528+ return used ;
529+ }
530+
531+ public static long getUsedExternalChemicalStorage (IGridNode node ) {
532+ long used = 0 ;
533+
534+ for (IGridNode iGridNode : node .getGrid ().getMachineNodes (StorageBusPart .class )) {
535+ StorageBusPart bus = (StorageBusPart ) iGridNode .getService (IStorageProvider .class );
536+ KeyCounter keyCounter = bus .getInternalHandler ().getAvailableStacks ();
537+
538+ for (Object2LongMap .Entry <AEKey > aeKey : keyCounter ) {
539+ if (aeKey .getKey () instanceof MekanismKey ) {
540+ used += aeKey .getLongValue ();
541+ }
542+ }
543+ }
544+
545+ return used ;
546+ }
547+
548+ /// Internal Storage
549+ /// Total
550+
434551 public static long getTotalItemStorage (IGridNode node ) {
435552 long total = 0 ;
436553
@@ -466,30 +583,6 @@ public static long getTotalItemStorage(IGridNode node) {
466583 return total ;
467584 }
468585
469- public static long getTotalExternalFluidStorage (IGridNode node ) {
470- long total = 0 ;
471-
472- for (IGridNode iGridNode : node .getGrid ().getMachineNodes (StorageBusPart .class )) {
473- StorageBusPart bus = (StorageBusPart ) iGridNode .getService (IStorageProvider .class );
474- Level level = bus .getLevel ();
475- BlockPos connectedInventoryPos = bus .getHost ().getBlockEntity ().getBlockPos ().relative (bus .getSide ());
476- BlockEntity connectedInventoryEntity = level .getBlockEntity (connectedInventoryPos );
477-
478- if (connectedInventoryEntity == null )
479- continue ;
480-
481- LazyOptional <IFluidHandler > fluidHandler = connectedInventoryEntity .getCapability (ForgeCapabilities .FLUID_HANDLER );
482- if (fluidHandler .isPresent ()) {
483- IFluidHandler handler = fluidHandler .orElse (null );
484- for (int i = 0 ; i < handler .getTanks (); i ++) {
485- total += handler .getTankCapacity (i );
486- }
487- }
488- }
489-
490- return total ;
491- }
492-
493586 public static long getTotalFluidStorage (IGridNode node ) {
494587 long total = 0 ;
495588
@@ -521,23 +614,37 @@ public static long getTotalFluidStorage(IGridNode node) {
521614 return total ;
522615 }
523616
524- public static long getUsedExternalItemStorage (IGridNode node ) {
525- long used = 0 ;
617+ public static long getTotalChemicalStorage (IGridNode node ) {
618+ long total = 0 ;
526619
527- for (IGridNode iGridNode : node .getGrid ().getMachineNodes (StorageBusPart .class )) {
528- StorageBusPart bus = (StorageBusPart ) iGridNode .getService (IStorageProvider .class );
529- KeyCounter keyCounter = bus .getInternalHandler ().getAvailableStacks ();
620+ for (IGridNode iGridNode : node .getGrid ().getMachineNodes (DriveBlockEntity .class )) {
621+ DriveBlockEntity entity = (DriveBlockEntity ) iGridNode .getService (IStorageProvider .class );
622+ if (entity == null )
623+ continue ;
530624
531- for (Object2LongMap .Entry <AEKey > aeKey : keyCounter ) {
532- if (aeKey .getKey () instanceof AEItemKey ) {
533- used += aeKey .getLongValue ();
625+ InternalInventory inventory = entity .getInternalInventory ();
626+
627+ for (int i = 0 ; i < inventory .size (); i ++) {
628+ ItemStack stack = inventory .getStackInSlot (i );
629+
630+ if (stack .isEmpty ())
631+ continue ;
632+
633+ if (stack .getItem () instanceof ChemicalStorageCell cell ) {
634+ if (cell .getKeyType () instanceof MekanismKeyType ) {
635+ total += (long ) cell .getBytes (null ) * MekanismKeyType .TYPE .getAmountPerByte ();
636+ }
534637 }
535638 }
536639 }
537640
538- return used ;
641+ total += getTotalExternalChemicalStorage (node );
642+
643+ return total ;
539644 }
540645
646+ /// Used
647+
541648 public static long getUsedItemStorage (IGridNode node ) {
542649 long used = 0 ;
543650
@@ -587,23 +694,6 @@ public static long getUsedItemStorage(IGridNode node) {
587694 return used ;
588695 }
589696
590- public static long getUsedExternalFluidStorage (IGridNode node ) {
591- long used = 0 ;
592-
593- for (IGridNode iGridNode : node .getGrid ().getMachineNodes (StorageBusPart .class )) {
594- StorageBusPart bus = (StorageBusPart ) iGridNode .getService (IStorageProvider .class );
595- KeyCounter keyCounter = bus .getInternalHandler ().getAvailableStacks ();
596-
597- for (Object2LongMap .Entry <AEKey > aeKey : keyCounter ) {
598- if (aeKey .getKey () instanceof AEFluidKey ) {
599- used += aeKey .getLongValue ();
600- }
601- }
602- }
603-
604- return used ;
605- }
606-
607697 public static long getUsedFluidStorage (IGridNode node ) {
608698 long used = 0 ;
609699
@@ -643,22 +733,84 @@ public static long getUsedFluidStorage(IGridNode node) {
643733 return used ;
644734 }
645735
736+ public static long getUsedChemicalStorage (IGridNode node ) {
737+ long used = 0 ;
738+
739+ for (IGridNode iGridNode : node .getGrid ().getMachineNodes (DriveBlockEntity .class )) {
740+ DriveBlockEntity entity = (DriveBlockEntity ) iGridNode .getService (IStorageProvider .class );
741+ if (entity == null )
742+ continue ;
743+
744+ InternalInventory inventory = entity .getInternalInventory ();
745+
746+ for (int i = 0 ; i < inventory .size (); i ++) {
747+ ItemStack stack = inventory .getStackInSlot (i );
748+
749+ if (stack .getItem () instanceof ChemicalStorageCell ) {
750+ BasicCellInventory cellInventory = BasicCellHandler .INSTANCE .getCellInventory (stack , null );
751+
752+ used = cellInventory .getUsedBytes () / MekanismKeyType .TYPE .getAmountPerByte ();
753+ }
754+ }
755+ }
756+
757+ used += getUsedExternalChemicalStorage (node );
758+
759+ return used ;
760+ }
761+
762+ /// Available Storage
763+
764+ /**
765+ * Calculates the available item storage on a given grid node.
766+ * It subtracts the used item storage from the total item storage.
767+ *
768+ * @param node The grid node to calculate the available item storage for.
769+ * @return The available item storage in bytes.
770+ */
646771 public static long getAvailableItemStorage (IGridNode node ) {
647772 return getTotalItemStorage (node ) - getUsedItemStorage (node );
648773 }
649774
775+ /**
776+ * Calculates the available fluid storage in a given grid node.
777+ *
778+ * @param node The grid node to calculate the available fluid storage for.
779+ * @return The available fluid storage in bytes.
780+ */
650781 public static long getAvailableFluidStorage (IGridNode node ) {
651782 return getTotalFluidStorage (node ) - getUsedFluidStorage (node );
652783 }
653784
785+ public static long getAvailableChemicalStorage (IGridNode node ) {
786+ return getTotalChemicalStorage (node ) - getUsedChemicalStorage (node );
787+ }
788+
789+ /**
790+ * Calculates the available external item storage of a given grid node.
791+ *
792+ * @param node The grid node for which to calculate the available external item storage.
793+ * @return The available external item storage.
794+ */
654795 public static long getAvailableExternalItemStorage (IGridNode node ) {
655796 return getTotalExternalItemStorage (node ) - getUsedExternalItemStorage (node );
656797 }
657798
799+ /**
800+ * Calculates the available external fluid storage on a given grid node by subtracting the used external fluid storage
801+ * from the total external fluid storage.
802+ *
803+ * @param node The grid node on which to calculate the available external fluid storage.
804+ * @return The available external fluid storage on the grid node.
805+ */
658806 public static long getAvailableExternalFluidStorage (IGridNode node ) {
659807 return getTotalExternalFluidStorage (node ) - getUsedExternalFluidStorage (node );
660808 }
661809
810+ public static long getAvailableExternalChemicalStorage (IGridNode node ) {
811+ return getTotalExternalChemicalStorage (node ) - getUsedExternalChemicalStorage (node );
812+ }
813+
662814 public static List <Object > listCells (IGridNode node ) {
663815 List <Object > items = new ArrayList <>();
664816
0 commit comments