Skip to content

Commit

Permalink
[#555] Implement storage information functions and some crafting func…
Browse files Browse the repository at this point in the history
…tions for the rs bridge
  • Loading branch information
SirEndii committed Aug 31, 2024
1 parent c9c37ae commit 68e9d07
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral;

import com.refinedmods.refinedstorage.api.autocrafting.ICraftingManager;
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPattern;
import com.refinedmods.refinedstorage.api.autocrafting.task.CalculationResultType;
import com.refinedmods.refinedstorage.api.autocrafting.task.ICalculationResult;
Expand Down Expand Up @@ -148,76 +149,120 @@ public final MethodResult getTotalFluidStorage() {
}

@Override
@LuaFunction(mainThread = true)
public MethodResult getTotalChemicalStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(-1);
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult getUsedExternItemStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(RefinedStorage.getUsedItemExternalStorage(getNetwork()));
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult getUsedExternFluidStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(RefinedStorage.getUsedFluidExternalStorage(getNetwork()));
}

@Override
@LuaFunction(mainThread = true)
public MethodResult getUsedExternChemicalStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(-1);
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult getUsedItemStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(RefinedStorage.getUsedItemDiskStorage(getNetwork()));
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult getUsedFluidStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(RefinedStorage.getUsedFluidDiskStorage(getNetwork()));
}

@Override
@LuaFunction(mainThread = true)
public MethodResult getUsedChemicalStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(-1);
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult getAvailableExternItemStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(RefinedStorage.getMaxItemExternalStorage(getNetwork()) - RefinedStorage.getUsedItemExternalStorage(getNetwork()));
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult getAvailableExternFluidStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(RefinedStorage.getMaxFluidExternalStorage(getNetwork()) - RefinedStorage.getUsedFluidExternalStorage(getNetwork()));
}

@Override
@LuaFunction(mainThread = true)
public MethodResult getAvailableExternChemicalStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(-1);
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult getAvailableItemStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(RefinedStorage.getMaxItemDiskStorage(getNetwork()) - RefinedStorage.getUsedItemDiskStorage(getNetwork()));
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult getAvailableFluidStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(RefinedStorage.getMaxFluidDiskStorage(getNetwork()) - RefinedStorage.getUsedFluidDiskStorage(getNetwork()));
}

@Override
@LuaFunction(mainThread = true)
public MethodResult getAvailableChemicalStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(-1);
}

@Override
Expand All @@ -239,8 +284,12 @@ public final MethodResult getTotalExternFluidStorage() {
}

@Override
@LuaFunction(mainThread = true)
public MethodResult getTotalExternChemicalStorage() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(-1);
}

@Override
Expand Down Expand Up @@ -539,20 +588,52 @@ public final MethodResult craftFluid(IComputerAccess computerAccess, IArguments
}

@Override
@LuaFunction(mainThread = true)
public MethodResult getCraftingTasks() {
return null;
if (!isAvailable())
return notConnected();

return MethodResult.of(RefinedStorage.getCraftingTasks(getNetwork()));
}

@Override
@LuaFunction(mainThread = true)
public MethodResult cancelCraftingTasks(IArguments arguments) throws LuaException {
return null;
if (!isAvailable())
return notConnected();

Pair<? extends GenericFilter<?>, String> filter = GenericFilter.parseGeneric(arguments.getTable(0));
if (filter.getRight() != null)
return MethodResult.of(null, filter.getRight());

ICraftingManager craftingManager = getNetwork().getCraftingManager();
int canceled = 0;

for (ICraftingTask task : craftingManager.getTasks()) {
if (filter.getLeft() instanceof ItemFilter itemFilter) {
if (itemFilter.test(task.getRequested().getItem())) {
craftingManager.cancel(task.getId());
canceled++;
}
}

if (filter.getLeft() instanceof FluidFilter fluidFilter) {
if (fluidFilter.test(task.getRequested().getFluid())) {
craftingManager.cancel(task.getId());
canceled++;
}
}
}

return MethodResult.of(canceled);
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult isItemCrafting(IArguments arguments) throws LuaException {
if (!isAvailable())
return notConnected();

Pair<ItemFilter, String> filter = ItemFilter.parse(arguments.getTable(0));
if (filter.rightPresent())
return MethodResult.of(null, filter.getRight());
Expand All @@ -572,13 +653,40 @@ public final MethodResult isItemCrafting(IArguments arguments) throws LuaExcepti
@Override
@LuaFunction(mainThread = true)
public final MethodResult isFluidCraftable(IArguments arguments) throws LuaException {
return null;
if (!isAvailable())
return notConnected();

Pair<FluidFilter, String> filter = FluidFilter.parse(arguments.getTable(0));
if (filter.rightPresent())
return MethodResult.of(false, filter.getRight());

FluidFilter parsedFilter = filter.getLeft();
if (parsedFilter.isEmpty())
return MethodResult.of(false, "EMPTY_FILTER");

return MethodResult.of(RefinedStorage.isFluidCraftable(getNetwork(), parsedFilter.toFluidStack()));
}

@Override
@LuaFunction(mainThread = true)
public final MethodResult isFluidCrafting(IArguments arguments) throws LuaException {
return null;
if (!isAvailable())
return notConnected();

Pair<FluidFilter, String> filter = FluidFilter.parse(arguments.getTable(0));
if (filter.rightPresent())
return MethodResult.of(null, filter.getRight());

FluidStack stack = RefinedStorage.findFluidFromFilter(getNetwork(), getNetwork().getCraftingManager(), filter.getLeft());
if (stack.isEmpty())
return MethodResult.of(null, "NOT_CRAFTABLE");

for (ICraftingTask task : getNetwork().getCraftingManager().getTasks()) {
FluidStack taskStack = task.getRequested().getFluid();
if (taskStack != null && taskStack.isFluidEqual(stack))
return MethodResult.of(true);
}
return MethodResult.of(false);
}

@Override
Expand All @@ -594,6 +702,7 @@ public final MethodResult isItemCraftable(IArguments arguments) throws LuaExcept
ItemFilter parsedFilter = filter.getLeft();
if (parsedFilter.isEmpty())
return MethodResult.of(false, "EMPTY_FILTER");

return MethodResult.of(RefinedStorage.isItemCraftable(getNetwork(), parsedFilter.toItemStack()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.refinedmods.refinedstorage.api.IRSAPI;
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingManager;
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPattern;
import com.refinedmods.refinedstorage.api.autocrafting.task.ICraftingTask;
import com.refinedmods.refinedstorage.api.network.INetwork;
import com.refinedmods.refinedstorage.api.network.INetworkNodeGraphEntry;
import com.refinedmods.refinedstorage.api.network.node.INetworkNode;
Expand Down Expand Up @@ -211,6 +212,26 @@ public static long getMaxFluidDiskStorage(INetwork network) {
return creative ? -1 : total;
}

public static long getUsedItemDiskStorage(INetwork network) {
long used = 0;
for (IStorage<ItemStack> store : network.getItemStorageCache().getStorages()) {
if (store instanceof IStorageDisk<ItemStack> storageDisk) {
used += storageDisk.getStored();
}
}
return used;
}

public static long getUsedFluidDiskStorage(INetwork network) {
long used = 0;
for (IStorage<FluidStack> store : network.getFluidStorageCache().getStorages()) {
if (store instanceof IStorageDisk<FluidStack> storageDisk) {
used += storageDisk.getStored();
}
}
return used;
}

public static long getMaxItemExternalStorage(INetwork network) {
long total = 0;
for (IStorage<ItemStack> store : network.getItemStorageCache().getStorages()) {
Expand All @@ -231,6 +252,26 @@ public static long getMaxFluidExternalStorage(INetwork network) {
return total;
}

public static long getUsedItemExternalStorage(INetwork network) {
long used = 0;
for (IStorage<ItemStack> store : network.getItemStorageCache().getStorages()) {
if (store instanceof IExternalStorage<ItemStack> externalStorage) {
used += externalStorage.getStored();
}
}
return used;
}

public static long getUsedFluidExternalStorage(INetwork network) {
long used = 0;
for (IStorage<FluidStack> store : network.getFluidStorageCache().getStorages()) {
if (store instanceof IExternalStorage<FluidStack> externalStorage) {
used += externalStorage.getStored();
}
}
return used;
}

public static Object getItem(INetwork network, ItemStack item) {
for (ItemStack itemStack : getItems(network)) {
if (itemStack.sameItem(item) && Objects.equals(itemStack.getTag(), item.getTag()))
Expand Down Expand Up @@ -314,9 +355,7 @@ public static List<Object> getStorageDisks(INetwork network) {
public static List<Object> getDiskDrives(INetwork network) {
List<Object> diskDrives = new ArrayList<>();

Collection<INetworkNodeGraphEntry> collection = network.getNodeGraph().all();

for (INetworkNodeGraphEntry graphEntry : collection) {
for (INetworkNodeGraphEntry graphEntry : network.getNodeGraph().all()) {
INetworkNode node = graphEntry.getNode();
if (node instanceof DiskDriveNetworkNode diskDrive) {
diskDrives.add(parseDiskDrive(diskDrive));
Expand All @@ -326,6 +365,27 @@ public static List<Object> getDiskDrives(INetwork network) {
return diskDrives;
}

public static List<Object> getCraftingTasks(INetwork network) {
List<Object> tasks = new ArrayList<>();

for (ICraftingTask task : network.getCraftingManager().getTasks()) {
tasks.add(parseCraftingTask(task, network));
}

return tasks;
}

public static Object parseCraftingTask(ICraftingTask task, INetwork network) {
Map<String, Object> properties = new HashMap<>();

properties.put("id", task.getId());
properties.put("pattern", parsePattern(task.getPattern(), network));
properties.put("quantity", task.getQuantity());
properties.put("completion", task.getCompletionPercentage());

return properties;
}

public static Object parseStorageDisk(IStorageDisk<?> disk) {
Map<String, Object> properties = new HashMap<>();

Expand Down

0 comments on commit 68e9d07

Please sign in to comment.