Skip to content

Commit 1154d8a

Browse files
committed
Improve and extend the functionality of the ME Bridge by implementing the handling of external fluid and item storage. This includes the capability to get the total, used, and available external storage for both fluid and item elements.
1 parent 09a1874 commit 1154d8a

File tree

2 files changed

+129
-68
lines changed

2 files changed

+129
-68
lines changed

src/main/java/de/srendi/advancedperipherals/common/addons/appliedenergistics/AppEngApi.java

Lines changed: 105 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,18 @@ public static Pair<Long, AEFluidKey> findAEFluidFromFilter(MEStorage monitor, @N
9090
return null;
9191
}
9292

93-
public static Pair<IPatternDetails, String> findPatternFromFilters(IGrid grid, Level level, GenericFilter inputFilter, GenericFilter outputFilter) {
93+
/**
94+
* Finds a pattern from filters.
95+
*
96+
* @param grid The grid to search patterns from.
97+
* @param level The level of the grid.
98+
* @param inputFilter The input filter to apply, can be null to ignore input filter.
99+
* @param outputFilter The output filter to apply, can be null to ignore output filter.
100+
* @return A Pair object containing the matched pattern and an error message if no pattern is found.
101+
* The pattern can be null if no pattern is found.
102+
* The error message is "NO_PATTERN_FOUND" if no pattern is found.
103+
*/
104+
public static Pair<IPatternDetails, String> findPatternFromFilters(IGrid grid, Level level, @Nullable GenericFilter inputFilter, @Nullable GenericFilter outputFilter) {
94105
for (IPatternDetails pattern : getPatterns(grid, level)) {
95106
if (pattern.getInputs().length == 0)
96107
continue;
@@ -101,10 +112,13 @@ public static Pair<IPatternDetails, String> findPatternFromFilters(IGrid grid, L
101112
boolean outputMatch = false;
102113

103114
if (inputFilter != null) {
115+
outerLoop:
104116
for (IPatternDetails.IInput input : pattern.getInputs()) {
105-
if (inputFilter.test(input.getPossibleInputs()[0])) {
106-
inputMatch = true;
107-
break;
117+
for (GenericStack possibleInput : input.getPossibleInputs()) {
118+
if (inputFilter.test(possibleInput)) {
119+
inputMatch = true;
120+
break outerLoop;
121+
}
108122
}
109123
}
110124
} else {
@@ -263,8 +277,8 @@ private static Map<String, Object> getObjectFromGasStack(Pair<Long, MekanismKey>
263277
public static Map<String, Object> getObjectFromPattern(IPatternDetails pattern) {
264278
Map<String, Object> map = new HashMap<>();
265279

266-
map.put("inputs", Arrays.stream(pattern.getInputs()).map(AppEngApi::getObjectFromPatternInput));
267-
map.put("outputs", Arrays.stream(pattern.getOutputs()).map(AppEngApi::getObjectFromGenericStack));
280+
map.put("inputs", Arrays.stream(pattern.getInputs()).map(AppEngApi::getObjectFromPatternInput).collect(Collectors.toList()));
281+
map.put("outputs", Arrays.stream(pattern.getOutputs()).map(AppEngApi::getObjectFromGenericStack).collect(Collectors.toList()));
268282
map.put("primaryOutput", getObjectFromGenericStack(pattern.getPrimaryOutput()));
269283
return map;
270284
}
@@ -393,13 +407,35 @@ public static boolean isFluidCrafting(MEStorage monitor, ICraftingService grid,
393407
return false;
394408
}
395409

396-
public static long getTotalItemStorage(IGridNode node) {
410+
public static long getTotalExternalItemStorage(IGridNode node) {
397411
long total = 0;
398412

399-
Iterator<IGridNode> iterator = node.getGrid().getMachineNodes(DriveBlockEntity.class).iterator();
413+
for (IGridNode iGridNode : node.getGrid().getMachineNodes(StorageBusPart.class)) {
414+
StorageBusPart bus = (StorageBusPart) iGridNode.getService(IStorageProvider.class);
415+
Level level = bus.getLevel();
416+
BlockPos connectedInventoryPos = bus.getHost().getBlockEntity().getBlockPos().relative(bus.getSide());
417+
BlockEntity connectedInventoryEntity = level.getBlockEntity(connectedInventoryPos);
400418

401-
while (iterator.hasNext()) {
402-
DriveBlockEntity entity = (DriveBlockEntity) iterator.next().getService(IStorageProvider.class);
419+
if (connectedInventoryEntity == null)
420+
continue;
421+
422+
LazyOptional<IItemHandler> itemHandler = connectedInventoryEntity.getCapability(ForgeCapabilities.ITEM_HANDLER);
423+
if (itemHandler.isPresent()) {
424+
IItemHandler handler = itemHandler.orElse(null);
425+
for (int i = 0; i < handler.getSlots(); i++) {
426+
total += handler.getSlotLimit(i);
427+
}
428+
}
429+
}
430+
431+
return total;
432+
}
433+
434+
public static long getTotalItemStorage(IGridNode node) {
435+
long total = 0;
436+
437+
for (IGridNode iGridNode : node.getGrid().getMachineNodes(DriveBlockEntity.class)) {
438+
DriveBlockEntity entity = (DriveBlockEntity) iGridNode.getService(IStorageProvider.class);
403439
if (entity == null)
404440
continue;
405441

@@ -420,24 +456,33 @@ public static long getTotalItemStorage(IGridNode node) {
420456
total += disk.getBytes(null);
421457
}
422458
} else if (APAddons.aeAdditionsLoaded && (stack.getItem() instanceof SuperStorageCell superStorageCell)) {
423-
total += superStorageCell.getKiloBytes() * 1024;
459+
total += superStorageCell.getKiloBytes() * 1024L;
424460
}
425461
}
426462
}
427463

428-
iterator = node.getGrid().getMachineNodes(StorageBusPart.class).iterator();
464+
total += getTotalExternalItemStorage(node);
429465

430-
while (iterator.hasNext()) {
431-
StorageBusPart bus = (StorageBusPart) iterator.next().getService(IStorageProvider.class);
432-
net.minecraft.world.level.Level level = bus.getLevel();
466+
return total;
467+
}
468+
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();
433475
BlockPos connectedInventoryPos = bus.getHost().getBlockEntity().getBlockPos().relative(bus.getSide());
434476
BlockEntity connectedInventoryEntity = level.getBlockEntity(connectedInventoryPos);
435477

436-
LazyOptional<IItemHandler> itemHandler = connectedInventoryEntity.getCapability(ForgeCapabilities.ITEM_HANDLER);
437-
if (itemHandler.isPresent()) {
438-
IItemHandler handler = itemHandler.orElse(null);
439-
for (int i = 0; i < handler.getSlots(); i++) {
440-
total += handler.getSlotLimit(i);
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);
441486
}
442487
}
443488
}
@@ -448,10 +493,8 @@ public static long getTotalItemStorage(IGridNode node) {
448493
public static long getTotalFluidStorage(IGridNode node) {
449494
long total = 0;
450495

451-
Iterator<IGridNode> iterator = node.getGrid().getMachineNodes(DriveBlockEntity.class).iterator();
452-
453-
while (iterator.hasNext()) {
454-
DriveBlockEntity entity = (DriveBlockEntity) iterator.next().getService(IStorageProvider.class);
496+
for (IGridNode iGridNode : node.getGrid().getMachineNodes(DriveBlockEntity.class)) {
497+
DriveBlockEntity entity = (DriveBlockEntity) iGridNode.getService(IStorageProvider.class);
455498
if (entity == null)
456499
continue;
457500

@@ -468,38 +511,38 @@ public static long getTotalFluidStorage(IGridNode node) {
468511
total += cell.getBytes(null);
469512
}
470513
} else if (APAddons.aeAdditionsLoaded && stack.getItem() instanceof SuperStorageCell superStorageCell) {
471-
total += superStorageCell.getKiloBytes() * 1024;
514+
total += superStorageCell.getKiloBytes() * 1024L;
472515
}
473516
}
474517
}
475518

476-
iterator = node.getGrid().getMachineNodes(StorageBusPart.class).iterator();
519+
total += getTotalExternalFluidStorage(node);
477520

478-
while (iterator.hasNext()) {
479-
StorageBusPart bus = (StorageBusPart) iterator.next().getService(IStorageProvider.class);
480-
net.minecraft.world.level.Level level = bus.getLevel();
481-
BlockPos connectedInventoryPos = bus.getHost().getBlockEntity().getBlockPos().relative(bus.getSide());
482-
BlockEntity connectedInventoryEntity = level.getBlockEntity(connectedInventoryPos);
521+
return total;
522+
}
483523

484-
LazyOptional<IFluidHandler> fluidHandler = connectedInventoryEntity.getCapability(ForgeCapabilities.FLUID_HANDLER);
485-
if (fluidHandler.isPresent()) {
486-
IFluidHandler handler = fluidHandler.orElse(null);
487-
for (int i = 0; i < handler.getTanks(); i++) {
488-
total += handler.getTankCapacity(i);
524+
public static long getUsedExternalItemStorage(IGridNode node) {
525+
long used = 0;
526+
527+
for (IGridNode iGridNode : node.getGrid().getMachineNodes(StorageBusPart.class)) {
528+
StorageBusPart bus = (StorageBusPart) iGridNode.getService(IStorageProvider.class);
529+
KeyCounter keyCounter = bus.getInternalHandler().getAvailableStacks();
530+
531+
for (Object2LongMap.Entry<AEKey> aeKey : keyCounter) {
532+
if (aeKey.getKey() instanceof AEItemKey) {
533+
used += aeKey.getLongValue();
489534
}
490535
}
491536
}
492537

493-
return total;
538+
return used;
494539
}
495540

496541
public static long getUsedItemStorage(IGridNode node) {
497542
long used = 0;
498543

499-
Iterator<IGridNode> iterator = node.getGrid().getMachineNodes(DriveBlockEntity.class).iterator();
500-
501-
while (iterator.hasNext()) {
502-
DriveBlockEntity entity = (DriveBlockEntity) iterator.next().getService(IStorageProvider.class);
544+
for (IGridNode iGridNode : node.getGrid().getMachineNodes(DriveBlockEntity.class)) {
545+
DriveBlockEntity entity = (DriveBlockEntity) iGridNode.getService(IStorageProvider.class);
503546
if (entity == null)
504547
continue;
505548

@@ -539,14 +582,20 @@ public static long getUsedItemStorage(IGridNode node) {
539582
}
540583
}
541584

542-
iterator = node.getGrid().getMachineNodes(StorageBusPart.class).iterator();
585+
used += getUsedExternalItemStorage(node);
543586

544-
while (iterator.hasNext()) {
545-
StorageBusPart bus = (StorageBusPart) iterator.next().getService(IStorageProvider.class);
587+
return used;
588+
}
589+
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);
546595
KeyCounter keyCounter = bus.getInternalHandler().getAvailableStacks();
547596

548597
for (Object2LongMap.Entry<AEKey> aeKey : keyCounter) {
549-
if (aeKey.getKey() instanceof AEItemKey) {
598+
if (aeKey.getKey() instanceof AEFluidKey) {
550599
used += aeKey.getLongValue();
551600
}
552601
}
@@ -558,10 +607,8 @@ public static long getUsedItemStorage(IGridNode node) {
558607
public static long getUsedFluidStorage(IGridNode node) {
559608
long used = 0;
560609

561-
Iterator<IGridNode> iterator = node.getGrid().getMachineNodes(DriveBlockEntity.class).iterator();
562-
563-
while (iterator.hasNext()) {
564-
DriveBlockEntity entity = (DriveBlockEntity) iterator.next().getService(IStorageProvider.class);
610+
for (IGridNode iGridNode : node.getGrid().getMachineNodes(DriveBlockEntity.class)) {
611+
DriveBlockEntity entity = (DriveBlockEntity) iGridNode.getService(IStorageProvider.class);
565612
if (entity == null)
566613
continue;
567614

@@ -591,18 +638,7 @@ public static long getUsedFluidStorage(IGridNode node) {
591638
}
592639
}
593640

594-
iterator = node.getGrid().getMachineNodes(StorageBusPart.class).iterator();
595-
596-
while (iterator.hasNext()) {
597-
StorageBusPart bus = (StorageBusPart) iterator.next().getService(IStorageProvider.class);
598-
KeyCounter keyCounter = bus.getInternalHandler().getAvailableStacks();
599-
600-
for (Object2LongMap.Entry<AEKey> aeKey : keyCounter) {
601-
if (aeKey.getKey() instanceof AEFluidKey) {
602-
used += aeKey.getLongValue();
603-
}
604-
}
605-
}
641+
used += getUsedExternalFluidStorage(node);
606642

607643
return used;
608644
}
@@ -615,6 +651,14 @@ public static long getAvailableFluidStorage(IGridNode node) {
615651
return getTotalFluidStorage(node) - getUsedFluidStorage(node);
616652
}
617653

654+
public static long getAvailableExternalItemStorage(IGridNode node) {
655+
return getTotalExternalItemStorage(node) - getUsedExternalItemStorage(node);
656+
}
657+
658+
public static long getAvailableExternalFluidStorage(IGridNode node) {
659+
return getTotalExternalFluidStorage(node) - getUsedExternalFluidStorage(node);
660+
}
661+
618662
public static List<Object> listCells(IGridNode node) {
619663
List<Object> items = new ArrayList<>();
620664

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/MeBridgePeripheral.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,19 @@ public final MethodResult getAvgPowerInjection() {
376376
@Override
377377
@LuaFunction(mainThread = true)
378378
public MethodResult getTotalExternItemStorage() {
379-
return null;
379+
if (!isAvailable())
380+
return notConnected();
381+
382+
return MethodResult.of(AppEngApi.getTotalExternalItemStorage(node));
380383
}
381384

382385
@Override
383386
@LuaFunction(mainThread = true)
384387
public MethodResult getTotalExternFluidStorage() {
385-
return null;
388+
if (!isAvailable())
389+
return notConnected();
390+
391+
return MethodResult.of(AppEngApi.getTotalExternalFluidStorage(node));
386392
}
387393

388394

@@ -407,13 +413,19 @@ public final MethodResult getTotalFluidStorage() {
407413
@Override
408414
@LuaFunction(mainThread = true)
409415
public MethodResult getUsedExternItemStorage() {
410-
return null;
416+
if (!isAvailable())
417+
return notConnected();
418+
419+
return MethodResult.of(AppEngApi.getUsedExternalFluidStorage(node));
411420
}
412421

413422
@Override
414423
@LuaFunction(mainThread = true)
415424
public MethodResult getUsedExternFluidStorage() {
416-
return null;
425+
if (!isAvailable())
426+
return notConnected();
427+
428+
return MethodResult.of(AppEngApi.getUsedExternalFluidStorage(node));
417429
}
418430

419431
@Override
@@ -437,13 +449,18 @@ public final MethodResult getUsedFluidStorage() {
437449
@Override
438450
@LuaFunction(mainThread = true)
439451
public MethodResult getAvailableExternItemStorage() {
440-
return null;
441-
}
452+
if (!isAvailable())
453+
return notConnected();
442454

455+
return MethodResult.of(AppEngApi.getAvailableExternalItemStorage(node));
456+
}
443457
@Override
444458
@LuaFunction(mainThread = true)
445459
public MethodResult getAvailableExternFluidStorage() {
446-
return null;
460+
if (!isAvailable())
461+
return notConnected();
462+
463+
return MethodResult.of(AppEngApi.getAvailableExternalFluidStorage(node));
447464
}
448465

449466
@Override

0 commit comments

Comments
 (0)