Skip to content

Commit

Permalink
[#555] Further implement and test the unified ME and RS Bridge functi…
Browse files Browse the repository at this point in the history
…ons. Implemented storage disks and drives for the RS Bridge and fixed minor bugs for the ME Bridge
  • Loading branch information
SirEndii committed Aug 28, 2024
1 parent 614b149 commit d0dafd8
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ public static Pair<Long, AEFluidKey> findAEFluidFromFilter(MEStorage monitor, @N
}

if (crafting == null)
return null;
return Pair.of(0L, AEFluidKey.of(FluidStack.EMPTY));

for (var temp : crafting.getCraftables(param -> true)) {
if (temp instanceof AEFluidKey key && item.test(key.toStack(1)))
return Pair.of(0L, key);
}

return null;
return Pair.of(0L, AEFluidKey.of(FluidStack.EMPTY));
}

/**
Expand Down Expand Up @@ -157,7 +157,7 @@ public static List<Object> listStacks(MEStorage monitor, ICraftingService servic
KeyCounter keyCounter = monitor.getAvailableStacks();
for (Object2LongMap.Entry<AEKey> aeKey : keyCounter) {
if (aeKey.getKey() instanceof AEItemKey itemKey) {
items.add(getObjectFromStack(Pair.of(aeKey.getLongValue(), itemKey), service));
items.add(parseAeStack(Pair.of(aeKey.getLongValue(), itemKey), service));
}
}
return items;
Expand All @@ -169,7 +169,7 @@ public static List<Object> listCraftableStacks(MEStorage monitor, ICraftingServi
Set<AEKey> craftables = service.getCraftables(AEKeyFilter.none());
for (AEKey aeKey : craftables) {
if (aeKey instanceof AEItemKey) {
items.add(getObjectFromStack(Pair.of(keyCounter.get(aeKey), aeKey), service));
items.add(parseAeStack(Pair.of(keyCounter.get(aeKey), aeKey), service));
}
}
return items;
Expand All @@ -179,7 +179,7 @@ public static List<Object> listFluids(MEStorage monitor, ICraftingService servic
List<Object> items = new ArrayList<>();
for (Object2LongMap.Entry<AEKey> aeKey : monitor.getAvailableStacks()) {
if (aeKey.getKey() instanceof AEFluidKey itemKey) {
items.add(getObjectFromStack(Pair.of(aeKey.getLongValue(), itemKey), service));
items.add(parseAeStack(Pair.of(aeKey.getLongValue(), itemKey), service));
}
}
return items;
Expand All @@ -189,7 +189,7 @@ public static List<Object> listGases(MEStorage monitor, ICraftingService service
List<Object> items = new ArrayList<>();
for (Object2LongMap.Entry<AEKey> aeKey : monitor.getAvailableStacks()) {
if (APAddons.appMekLoaded && aeKey.getKey() instanceof MekanismKey itemKey) {
items.add(getObjectFromStack(Pair.of(aeKey.getLongValue(), itemKey), service));
items.add(parseAeStack(Pair.of(aeKey.getLongValue(), itemKey), service));
}
}
return items;
Expand All @@ -201,7 +201,7 @@ public static List<Object> listCraftableFluids(MEStorage monitor, ICraftingServi
Set<AEKey> craftables = service.getCraftables(AEKeyFilter.none());
for (AEKey aeKey : craftables) {
if (aeKey instanceof AEFluidKey) {
items.add(getObjectFromStack(Pair.of(keyCounter.get(aeKey), aeKey), service));
items.add(parseAeStack(Pair.of(keyCounter.get(aeKey), aeKey), service));
}
}
return items;
Expand Down Expand Up @@ -230,7 +230,7 @@ public static List<IPatternDetails> getPatterns(IGrid grid, Level level) {
}

public static List<Object> listPatterns(IGrid grid, Level level) {
return getPatterns(grid, level).stream().map(AppEngApi::getObjectFromPattern).collect(Collectors.toList());
return getPatterns(grid, level).stream().map(AppEngApi::parsePattern).collect(Collectors.toList());
}

public static List<Object> listDrives(IGrid grid) {
Expand All @@ -243,7 +243,7 @@ public static List<Object> listDrives(IGrid grid) {
if (drive == null || drive.getCellCount() != 10)
continue;

drives.add(getObjectFromDrive(drive));
drives.add(parseDrive(drive));
}

return drives;
Expand All @@ -256,21 +256,34 @@ private static Class<? extends PatternContainer> tryCastMachineToContainer(Class
return null;
}

public static <T extends AEKey> Map<String, Object> getObjectFromStack(Pair<Long, T> stack, @Nullable ICraftingService service) {
if (stack.getRight() == null)
public static <T extends AEKey> Map<String, Object> parseAeStack(Pair<Long, T> stack, @Nullable ICraftingService service) {
if (stack == null || stack.getRight() == null)
return Collections.emptyMap();
if (stack.getRight() instanceof AEItemKey itemKey)
return getObjectFromItemStack(Pair.of(stack.getLeft(), itemKey), service);
return parseItemStack(Pair.of(stack.getLeft(), itemKey), service);
if (stack.getRight() instanceof AEFluidKey fluidKey)
return getObjectFromFluidStack(Pair.of(stack.getLeft(), fluidKey), service);
return parseFluidStack(Pair.of(stack.getLeft(), fluidKey), service);
if (APAddons.appMekLoaded && (stack.getRight() instanceof MekanismKey gasKey))
return getObjectFromGasStack(Pair.of(stack.getLeft(), gasKey), service);
return parseChemStack(Pair.of(stack.getLeft(), gasKey));

AdvancedPeripherals.debug("Could not create table from unknown stack " + stack.getRight().getClass() + " - Report this to the maintainer of ap", org.apache.logging.log4j.Level.WARN);
return Collections.emptyMap();
}

public static Map<String, Object> parseGenericStack(GenericStack stack) {
if (stack.what() == null)
return Collections.emptyMap();
if (stack.what() instanceof AEItemKey aeItemKey)
return parseItemStack(Pair.of(stack.amount(), aeItemKey), null);
if (stack.what() instanceof AEFluidKey aeFluidKey)
return parseFluidStack(Pair.of(stack.amount(), aeFluidKey), null);

AdvancedPeripherals.debug("Could not create table from unknown stack " + stack.getRight().getClass() + " - Report this to the maintainer of ap", org.apache.logging.log4j.Level.ERROR);
AdvancedPeripherals.debug("Could not create table from unknown stack " + stack.getClass() + " - Report this to the maintainer of ap", org.apache.logging.log4j.Level.WARN);
return Collections.emptyMap();
}

public static Map<Object, Object> getObjectFromDrive(DriveBlockEntity drive) {

public static Map<Object, Object> parseDrive(DriveBlockEntity drive) {
Map<Object, Object> map = new HashMap<>();

map.put("powered", drive.isPowered());
Expand All @@ -288,7 +301,7 @@ public static Map<Object, Object> getObjectFromDrive(DriveBlockEntity drive) {
totalBytes += cellInventory.getTotalBytes();
usedBytes += cellInventory.getUsedBytes();

driveCells.add(getObjectFromCell(cell, item));
driveCells.add(parseCell(cell, item));
}
}

Expand All @@ -303,7 +316,7 @@ public static Map<Object, Object> getObjectFromDrive(DriveBlockEntity drive) {
return map;
}

public static Map<Object, Object> getObjectFromCell(BasicStorageCell cell, ItemStack cellItem) {
public static Map<Object, Object> parseCell(IBasicCellItem cell, ItemStack cellItem) {
Map<Object, Object> map = new HashMap<>();
BasicCellInventory cellInventory = BasicCellHandler.INSTANCE.getCellInventory(cellItem, null);

Expand All @@ -318,7 +331,7 @@ public static Map<Object, Object> getObjectFromCell(BasicStorageCell cell, ItemS
return map;
}

private static Map<String, Object> getObjectFromItemStack(Pair<Long, AEItemKey> stack, @Nullable ICraftingService craftingService) {
private static Map<String, Object> parseItemStack(Pair<Long, AEItemKey> stack, @Nullable ICraftingService craftingService) {
Map<String, Object> map = new HashMap<>();
String displayName = stack.getRight().getDisplayName().getString();
CompoundTag nbt = stack.getRight().toTag();
Expand All @@ -334,7 +347,7 @@ private static Map<String, Object> getObjectFromItemStack(Pair<Long, AEItemKey>
return map;
}

private static Map<String, Object> getObjectFromFluidStack(Pair<Long, AEFluidKey> stack, @Nullable ICraftingService craftingService) {
private static Map<String, Object> parseFluidStack(Pair<Long, AEFluidKey> stack, @Nullable ICraftingService craftingService) {
Map<String, Object> map = new HashMap<>();
long amount = stack.getLeft();
map.put("name", ForgeRegistries.FLUIDS.getKey(stack.getRight().getFluid()).toString());
Expand All @@ -346,7 +359,7 @@ private static Map<String, Object> getObjectFromFluidStack(Pair<Long, AEFluidKey
return map;
}

private static Map<String, Object> getObjectFromGasStack(Pair<Long, MekanismKey> stack, @Nullable ICraftingService craftingService) {
private static Map<String, Object> parseChemStack(Pair<Long, MekanismKey> stack) {
Map<String, Object> map = new HashMap<>();
long amount = stack.getLeft();
map.put("name", stack.getRight().getStack().getTypeRegistryName().toString());
Expand All @@ -357,27 +370,27 @@ private static Map<String, Object> getObjectFromGasStack(Pair<Long, MekanismKey>
return map;
}

public static Map<String, Object> getObjectFromPattern(IPatternDetails pattern) {
public static Map<String, Object> parsePattern(IPatternDetails pattern) {
Map<String, Object> map = new HashMap<>();

map.put("inputs", Arrays.stream(pattern.getInputs()).map(AppEngApi::getObjectFromPatternInput).collect(Collectors.toList()));
map.put("outputs", Arrays.stream(pattern.getOutputs()).map(AppEngApi::getObjectFromGenericStack).collect(Collectors.toList()));
map.put("primaryOutput", getObjectFromGenericStack(pattern.getPrimaryOutput()));
map.put("inputs", Arrays.stream(pattern.getInputs()).map(AppEngApi::parsePatternInput).collect(Collectors.toList()));
map.put("outputs", Arrays.stream(pattern.getOutputs()).map(AppEngApi::parseGenericStack).collect(Collectors.toList()));
map.put("primaryOutput", parseGenericStack(pattern.getPrimaryOutput()));
return map;
}

public static Map<String, Object> getObjectFromPatternInput(IPatternDetails.IInput patternInput) {
public static Map<String, Object> parsePatternInput(IPatternDetails.IInput patternInput) {
Map<String, Object> map = new HashMap<>();
map.put("primaryInput", getObjectFromGenericStack(patternInput.getPossibleInputs()[0]));
map.put("primaryInput", parseGenericStack(patternInput.getPossibleInputs()[0]));
map.put("possibleInputs",
Arrays.stream(Arrays.copyOfRange(patternInput.getPossibleInputs(), 1, patternInput.getPossibleInputs().length))
.map(AppEngApi::getObjectFromGenericStack));
.map(AppEngApi::parseGenericStack));
map.put("multiplier", patternInput.getMultiplier());
map.put("remaining", patternInput.getRemainingKey(patternInput.getPossibleInputs()[0].what()));
return map;
}

public static Map<String, Object> getObjectFromCPU(ICraftingCPU cpu, boolean recursive) {
public static Map<String, Object> parseCraftingCPU(ICraftingCPU cpu, boolean recursive) {
Map<String, Object> map = new HashMap<>();
long storage = cpu.getAvailableStorage();
int coProcessors = cpu.getCoProcessors();
Expand All @@ -386,36 +399,26 @@ public static Map<String, Object> getObjectFromCPU(ICraftingCPU cpu, boolean rec
map.put("coProcessors", coProcessors);
map.put("isBusy", isBusy);
if (!recursive)
map.put("craftingJob", cpu.getJobStatus() != null ? getObjectFromJob(cpu.getJobStatus(), null) : null);
map.put("craftingJob", cpu.getJobStatus() != null ? parseCraftingJob(cpu.getJobStatus(), null) : null);
map.put("name", cpu.getName() != null ? cpu.getName().getString() : "Unnamed");
map.put("selectionMode", cpu.getSelectionMode().toString());

return map;
}

public static Map<String, Object> getObjectFromJob(CraftingJobStatus job, @Nullable ICraftingCPU cpu) {
public static Map<String, Object> parseCraftingJob(CraftingJobStatus job, @Nullable ICraftingCPU cpu) {
Map<String, Object> map = new HashMap<>();
map.put("storage", getObjectFromGenericStack(job.crafting()));
map.put("storage", parseGenericStack(job.crafting()));
map.put("elapsedTimeNanos", job.elapsedTimeNanos());
map.put("totalItem", job.totalItems());
map.put("progress", job.progress());

if (cpu != null)
map.put("cpu", getObjectFromCPU(cpu, true));
map.put("cpu", parseCraftingCPU(cpu, true));

return map;
}

public static Map<String, Object> getObjectFromGenericStack(GenericStack stack) {
if (stack.what() == null)
return Collections.emptyMap();
if (stack.what() instanceof AEItemKey aeItemKey)
return getObjectFromItemStack(Pair.of(stack.amount(), aeItemKey), null);
if (stack.what() instanceof AEFluidKey aeFluidKey)
return getObjectFromFluidStack(Pair.of(stack.amount(), aeFluidKey), null);
return Collections.emptyMap();
}

public static MEStorage getMonitor(IGridNode node) {
return node.getGrid().getService(IStorageService.class).getInventory();
}
Expand Down Expand Up @@ -941,7 +944,7 @@ public static List<Object> listCells(IGridNode node) {
continue;

if (stack.getItem() instanceof IBasicCellItem cell) {
items.add(getObjectFromCell(cell, stack));
items.add(parseCell(cell, stack));
} else if (APAddons.aeThingsLoaded && stack.getItem() instanceof DISKDrive disk) {
items.add(getObjectFromDisk(disk, stack));
} else if (APAddons.aeAdditionsLoaded && stack.getItem() instanceof SuperStorageCell superStorageCell) {
Expand All @@ -953,26 +956,6 @@ public static List<Object> listCells(IGridNode node) {
return items;
}

private static Map<String, Object> getObjectFromCell(IBasicCellItem cell, ItemStack stack) {
Map<String, Object> map = new HashMap<>();

map.put("item", ItemUtil.getRegistryKey(stack.getItem()).toString());

String cellType = "";

if (cell.getKeyType().getClass().isAssignableFrom(AEKeyType.items().getClass())) {
cellType = "item";
} else if (cell.getKeyType().getClass().isAssignableFrom(AEKeyType.fluids().getClass())) {
cellType = "fluid";
}

map.put("cellType", cellType);
map.put("bytesPerType", cell.getBytesPerType(null));
map.put("totalBytes", cell.getBytes(null));

return map;
}

private static Map<String, Object> getObjectFromDisk(DISKDrive drive, ItemStack stack) {
Map<String, Object> map = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public final MethodResult getItem(IArguments arguments) throws LuaException {
if (parsedFilter.isEmpty())
return MethodResult.of(null, "EMPTY_FILTER");

return MethodResult.of(AppEngApi.getObjectFromStack(AppEngApi.findAEStackFromFilter(monitor, getCraftingService(), parsedFilter), getCraftingService()));
return MethodResult.of(AppEngApi.parseAeStack(AppEngApi.findAEStackFromFilter(monitor, getCraftingService(), parsedFilter), getCraftingService()));
}

@Override
Expand All @@ -196,7 +196,7 @@ public MethodResult getFluid(IArguments arguments) throws LuaException {
if (parsedFilter.isEmpty())
return MethodResult.of(null, "EMPTY_FILTER");

return MethodResult.of(AppEngApi.findAEFluidFromFilter(AppEngApi.getMonitor(node), getCraftingService(), parsedFilter));
return MethodResult.of(AppEngApi.parseAeStack(AppEngApi.findAEFluidFromFilter(AppEngApi.getMonitor(node), getCraftingService(), parsedFilter), getCraftingService()));
}

@Override
Expand Down Expand Up @@ -232,7 +232,7 @@ public final MethodResult listCraftableFluids() {
if (!isAvailable())
return notConnected();

return MethodResult.of(AppEngApi.listCraftableStacks(AppEngApi.getMonitor(node), getCraftingService()));
return MethodResult.of(AppEngApi.listCraftableFluids(AppEngApi.getMonitor(node), getCraftingService()));
}

@Override
Expand Down Expand Up @@ -328,13 +328,12 @@ public MethodResult getFilteredPatterns(IArguments arguments) throws LuaExceptio
outputFilter = GenericFilter.parseGeneric(outputFilterTable).getLeft();
}


Pair<IPatternDetails, String> pattern = AppEngApi.findPatternFromFilters(node.getGrid(), getLevel(), inputFilter, outputFilter);

if (pattern.getRight() != null)
return MethodResult.of(null, pattern.getRight());

return MethodResult.of(AppEngApi.getObjectFromPattern(pattern.getLeft()));
return MethodResult.of(AppEngApi.parsePattern(pattern.getLeft()));
}

@Override
Expand Down Expand Up @@ -610,7 +609,7 @@ public MethodResult getCraftingTasks() {

for (ICraftingCPU cpu : craftingGrid.getCpus()) {
if (cpu.getJobStatus() != null)
jobs.add(AppEngApi.getObjectFromJob(cpu.getJobStatus(), cpu));
jobs.add(AppEngApi.parseCraftingJob(cpu.getJobStatus(), cpu));
}
return MethodResult.of(jobs);
}
Expand Down Expand Up @@ -776,7 +775,7 @@ public final MethodResult getCraftingCPUs() throws LuaException {
List<Object> map = new ArrayList<>();

for (ICraftingCPU iCraftingCPU : grid.getCpus()) {
Object cpu = AppEngApi.getObjectFromCPU(iCraftingCPU, false);
Object cpu = AppEngApi.parseCraftingCPU(iCraftingCPU, false);
map.add(cpu);
}
return MethodResult.of(map);
Expand Down
Loading

0 comments on commit d0dafd8

Please sign in to comment.