Some OC API changes.

- Removed getMissingItems() and getMissingFluids() functions from the OpenComputers integration, that info is now accessible through schedule(Fluid)Task(). If you just want to check if there are missing items/fluids but don't want to start an actual task, use the "canSchedule" parameter.
- Added fluid functions for the fluid autocrafting to the OpenComputers integration.
This commit is contained in:
raoulvdberge
2018-11-01 23:32:06 +01:00
parent f40cd21f27
commit 7e4423881e
6 changed files with 126 additions and 47 deletions

View File

@@ -7,6 +7,8 @@
- Fixed network node scanning allowing multiple controllers in some cases (raoulvdberge) - Fixed network node scanning allowing multiple controllers in some cases (raoulvdberge)
- Fixed OpenComputers integration not giving back a crafting task instance in the schedule task API (raoulvdberge) - Fixed OpenComputers integration not giving back a crafting task instance in the schedule task API (raoulvdberge)
- Fixed OpenComputers integration causing log spam when getting processing patterns (raoulvdberge) - Fixed OpenComputers integration causing log spam when getting processing patterns (raoulvdberge)
- Removed getMissingItems() and getMissingFluids() functions from the OpenComputers integration, that info is now accessible through schedule(Fluid)Task(). If you just want to check if there are missing items/fluids but don't want to start an actual task, use the "canSchedule" parameter (raoulvdberge)
- Added fluid functions for the fluid autocrafting to the OpenComputers integration (raoulvdberge)
### 1.6.9 ### 1.6.9
- Fixed OpenComputers "unknown error" when using extract item API (raoulvdberge) - Fixed OpenComputers "unknown error" when using extract item API (raoulvdberge)

View File

@@ -14,7 +14,10 @@ public class ConverterCraftingPattern implements Converter {
output.put("outputs", pattern.getOutputs()); output.put("outputs", pattern.getOutputs());
output.put("inputs", pattern.getInputs()); output.put("inputs", pattern.getInputs());
if (!pattern.isProcessing()) { if (pattern.isProcessing()) {
output.put("fluidInputs", pattern.getFluidInputs());
output.put("fluidOutputs", pattern.getFluidOutputs());
} else {
output.put("byproducts", pattern.getByproducts()); output.put("byproducts", pattern.getByproducts());
} }

View File

@@ -0,0 +1,16 @@
package com.raoulvdberge.refinedstorage.integration.oc;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingRequestInfo;
import li.cil.oc.api.driver.Converter;
import java.util.Map;
public class ConverterCraftingRequestInfo implements Converter {
@Override
public void convert(Object value, Map<Object, Object> map) {
if (value instanceof ICraftingRequestInfo) {
map.put("item", ((ICraftingRequestInfo) value).getItem());
map.put("fluid", ((ICraftingRequestInfo) value).getFluid());
}
}
}

View File

@@ -13,6 +13,7 @@ public class ConverterCraftingTask implements Converter {
output.put("stack", task.getRequested()); output.put("stack", task.getRequested());
output.put("missing", task.getMissing().getStacks()); output.put("missing", task.getMissing().getStacks());
output.put("missingFluids", task.getMissingFluids().getStacks());
output.put("pattern", task.getPattern()); output.put("pattern", task.getPattern());
output.put("quantity", task.getQuantity()); output.put("quantity", task.getQuantity());
} }

View File

@@ -33,5 +33,6 @@ public class DriverNetwork implements DriverBlock {
Driver.add(new ConverterCraftingPattern()); Driver.add(new ConverterCraftingPattern());
Driver.add(new ConverterCraftingTask()); Driver.add(new ConverterCraftingTask());
Driver.add(new ConverterCraftingRequestInfo());
} }
} }

View File

@@ -18,6 +18,7 @@ import li.cil.oc.api.prefab.AbstractManagedEnvironment;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
@@ -42,7 +43,7 @@ public class EnvironmentNetwork extends AbstractManagedEnvironment {
@Callback(doc = "function():boolean -- Whether the node is connected.") @Callback(doc = "function():boolean -- Whether the node is connected.")
public Object[] isConnected(final Context context, final Arguments args) { public Object[] isConnected(final Context context, final Arguments args) {
return new Object[]{node.getNetwork() != null}; return new Object[]{node.canUpdate()};
} }
@Callback(doc = "function():number -- Gets the energy usage of this network.") @Callback(doc = "function():number -- Gets the energy usage of this network.")
@@ -73,6 +74,16 @@ public class EnvironmentNetwork extends AbstractManagedEnvironment {
return new Object[]{node.getNetwork().getCraftingManager().getPattern(stack)}; return new Object[]{node.getNetwork().getCraftingManager().getPattern(stack)};
} }
@Callback(doc = "function(stack:table):table -- Get one fluid pattern of this network.")
public Object[] getFluidPattern(final Context context, final Arguments args) {
if (node.getNetwork() == null) {
return new Object[]{null, "not connected"};
}
FluidStack stack = checkFluid(args.checkTable(0), 1000);
return new Object[]{node.getNetwork().getCraftingManager().getPattern(stack)};
}
@Callback(doc = "function():table -- Gets the patterns of this network.") @Callback(doc = "function():table -- Gets the patterns of this network.")
public Object[] getPatterns(final Context context, final Arguments args) { public Object[] getPatterns(final Context context, final Arguments args) {
if (node.getNetwork() == null) { if (node.getNetwork() == null) {
@@ -81,8 +92,26 @@ public class EnvironmentNetwork extends AbstractManagedEnvironment {
List<ItemStack> patterns = new LinkedList<>(); List<ItemStack> patterns = new LinkedList<>();
for (ICraftingPattern pattern : node.getNetwork().getCraftingManager().getPatterns()) { for (ICraftingPattern pattern : node.getNetwork().getCraftingManager().getPatterns()) {
if (!pattern.getOutputs().isEmpty()) {
patterns.addAll(pattern.getOutputs()); patterns.addAll(pattern.getOutputs());
} }
}
return new Object[]{patterns};
}
@Callback(doc = "function():table -- Gets the fluid patterns of this network.")
public Object[] getFluidPatterns(final Context context, final Arguments args) {
if (node.getNetwork() == null) {
return new Object[]{null, "not connected"};
}
List<FluidStack> patterns = new LinkedList<>();
for (ICraftingPattern pattern : node.getNetwork().getCraftingManager().getPatterns()) {
if (!pattern.getFluidOutputs().isEmpty()) {
patterns.addAll(pattern.getFluidOutputs());
}
}
return new Object[]{patterns}; return new Object[]{patterns};
} }
@@ -98,26 +127,18 @@ public class EnvironmentNetwork extends AbstractManagedEnvironment {
return new Object[]{node.getNetwork().getCraftingManager().getPattern(stack) != null}; return new Object[]{node.getNetwork().getCraftingManager().getPattern(stack) != null};
} }
@Callback(doc = "function(stack:table[, count: number]):table -- Gets a list of missing items for a crafting task.") @Callback(doc = "function(stack:table):boolean -- Whether a crafting pattern exists for this fluid.")
public Object[] getMissingItems(final Context context, final Arguments args) { public Object[] hasFluidPattern(final Context context, final Arguments args) {
if (node.getNetwork() == null) { if (node.getNetwork() == null) {
return new Object[]{null, "not connected"}; return new Object[]{null, "not connected"};
} }
ItemStack stack = args.checkItemStack(0); FluidStack stack = checkFluid(args.checkTable(0), 1000);
int count = args.optInteger(1, 1);
ICraftingTask task = node.getNetwork().getCraftingManager().create(stack, count); return new Object[]{node.getNetwork().getCraftingManager().getPattern(stack) != null};
if (task == null) {
throw new IllegalArgumentException("Could not create crafting task");
} }
task.calculate(); @Callback(doc = "function(stack:table[, count: number, [canSchedule: boolean]]):table -- Schedules a crafting task.")
return new Object[]{task.getMissing().getStacks()};
}
@Callback(doc = "function(stack:table[, count: number]):table -- Schedules a crafting task.")
public Object[] scheduleTask(final Context context, final Arguments args) { public Object[] scheduleTask(final Context context, final Arguments args) {
if (node.getNetwork() == null) { if (node.getNetwork() == null) {
return new Object[]{"not connected"}; return new Object[]{"not connected"};
@@ -133,7 +154,29 @@ public class EnvironmentNetwork extends AbstractManagedEnvironment {
ICraftingTaskError error = task.calculate(); ICraftingTaskError error = task.calculate();
if (error == null) { if (error == null && !task.hasMissing() && args.optBoolean(2, true)) {
node.getNetwork().getCraftingManager().add(task);
}
return new Object[]{task};
}
@Callback(doc = "function(stack:table[, count: number]):table -- Schedules a fluid crafting task.")
public Object[] scheduleFluidTask(final Context context, final Arguments args) {
if (node.getNetwork() == null) {
return new Object[]{"not connected"};
}
FluidStack stack = checkFluid(args.checkTable(0), args.optInteger(1, Fluid.BUCKET_VOLUME));
ICraftingTask task = node.getNetwork().getCraftingManager().create(stack, stack.amount);
if (task == null) {
throw new IllegalArgumentException("Could not create crafting task");
}
ICraftingTaskError error = task.calculate();
if (error == null && !task.hasMissing() && args.optBoolean(2, true)) {
node.getNetwork().getCraftingManager().add(task); node.getNetwork().getCraftingManager().add(task);
} }
@@ -162,28 +205,36 @@ public class EnvironmentNetwork extends AbstractManagedEnvironment {
return new Object[]{count}; return new Object[]{count};
} }
@Callback(doc = "function(stack:table):number -- Cancels a fluid task and returns the amount of tasks cancelled.")
public Object[] cancelFluidTask(final Context context, final Arguments args) {
if (node.getNetwork() == null) {
return new Object[]{null, "not connected"};
}
FluidStack stack = checkFluid(args.checkTable(0), 1000);
int count = 0;
for (ICraftingTask task : node.getNetwork().getCraftingManager().getTasks()) {
if (task.getRequested().getFluid() != null) {
if (API.instance().getComparer().isEqual(task.getRequested().getFluid(), stack, COMPARE_NBT)) {
node.getNetwork().getCraftingManager().cancel(task.getId());
count++;
}
}
}
return new Object[]{count};
}
@Callback(doc = "function(stack:table[, amount:number[, direction:number]]):table -- Extracts a fluid from the network.") @Callback(doc = "function(stack:table[, amount:number[, direction:number]]):table -- Extracts a fluid from the network.")
public Object[] extractFluid(final Context context, final Arguments args) { public Object[] extractFluid(final Context context, final Arguments args) {
if (node.getNetwork() == null) { if (node.getNetwork() == null) {
return new Object[]{null, "not connected"}; return new Object[]{null, "not connected"};
} }
// First argument: the fluid stack to extract // First and second argument: fluid and amount.
// There is no args.checkFluidStack(), we have to deal with this ourselves FluidStack stack = checkFluid(args.checkTable(0), args.checkInteger(1));
Map<String, Object> fluidMap = args.checkTable(0);
if (!fluidMap.containsKey("name") || !(fluidMap.get("name") instanceof String) || ((String) fluidMap.get("name")).length() == 0) {
throw new IllegalArgumentException("no fluid name");
}
String fluid = (String) fluidMap.get("name");
// Second argument: the amount of liquid to extract, at least 1mb
int amount = Math.max(1, args.checkInteger(1));
// With the amount ready, we can actually try to create a fluid stack for the given fluid
FluidStack stack = FluidRegistry.getFluidStack(fluid, amount);
if (stack == null) {
throw new IllegalArgumentException("invalid fluid stack, does not exist");
}
// Third argument: which direction to extract to // Third argument: which direction to extract to
EnumFacing facing = EnumFacing.byIndex(args.optInteger(2, 0)); EnumFacing facing = EnumFacing.byIndex(args.optInteger(2, 0));
@@ -194,7 +245,7 @@ public class EnvironmentNetwork extends AbstractManagedEnvironment {
throw new IllegalArgumentException("No fluid tank on the given side"); throw new IllegalArgumentException("No fluid tank on the given side");
} }
FluidStack extractedSim = node.getNetwork().extractFluid(stack, amount, Action.SIMULATE); FluidStack extractedSim = node.getNetwork().extractFluid(stack, stack.amount, Action.SIMULATE);
if (extractedSim == null || extractedSim.amount <= 0) { if (extractedSim == null || extractedSim.amount <= 0) {
return new Object[]{null, "could not extract the specified fluid"}; return new Object[]{null, "could not extract the specified fluid"};
} }
@@ -207,7 +258,7 @@ public class EnvironmentNetwork extends AbstractManagedEnvironment {
} }
// Actually do it and return how much fluid we've inserted // Actually do it and return how much fluid we've inserted
FluidStack extracted = node.getNetwork().extractFluid(stack, amount, Action.PERFORM); FluidStack extracted = node.getNetwork().extractFluid(stack, stack.amount, Action.PERFORM);
handler.fill(extracted, true); handler.fill(extracted, true);
return new Object[]{filledAmountSim}; return new Object[]{filledAmountSim};
@@ -219,18 +270,7 @@ public class EnvironmentNetwork extends AbstractManagedEnvironment {
return new Object[]{null, "not connected"}; return new Object[]{null, "not connected"};
} }
// There is no args.checkFluidStack(), we have to deal with this ourselves FluidStack needle = checkFluid(args.checkTable(0), 1000);
Map<String, Object> fluidMap = args.checkTable(0);
if (!fluidMap.containsKey("name") || !(fluidMap.get("name") instanceof String) || ((String) fluidMap.get("name")).length() == 0) {
throw new IllegalArgumentException("no fluid name");
}
String fluid = (String) fluidMap.get("name");
FluidStack needle = FluidRegistry.getFluidStack(fluid, 1000);
if (needle == null) {
throw new IllegalArgumentException("invalid fluid stack, does not exist");
}
return new Object[]{node.getNetwork().getFluidStorageCache().getList().get(needle)}; return new Object[]{node.getNetwork().getFluidStorageCache().getList().get(needle)};
} }
@@ -393,4 +433,20 @@ public class EnvironmentNetwork extends AbstractManagedEnvironment {
return new Object[]{response}; return new Object[]{response};
} }
private FluidStack checkFluid(Map<String, Object> fluidMap, int amount) {
if (!fluidMap.containsKey("name") || !(fluidMap.get("name") instanceof String) || ((String) fluidMap.get("name")).length() == 0) {
throw new IllegalArgumentException("no fluid name");
}
String fluid = (String) fluidMap.get("name");
amount = Math.max(1, amount);
FluidStack stack = FluidRegistry.getFluidStack(fluid, amount);
if (stack == null) {
throw new IllegalArgumentException("invalid fluid stack, does not exist");
}
return stack;
}
} }