Add processing pattern support to V2 crafting tasks
This commit is contained in:
@@ -18,7 +18,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class CraftingPattern implements ICraftingPattern {
|
public class CraftingPattern implements ICraftingPattern {
|
||||||
private World world;
|
|
||||||
private ICraftingPatternContainer container;
|
private ICraftingPatternContainer container;
|
||||||
private ItemStack stack;
|
private ItemStack stack;
|
||||||
private List<ItemStack> inputs = new ArrayList<>();
|
private List<ItemStack> inputs = new ArrayList<>();
|
||||||
@@ -26,7 +25,6 @@ public class CraftingPattern implements ICraftingPattern {
|
|||||||
private List<ItemStack> byproducts = new ArrayList<>();
|
private List<ItemStack> byproducts = new ArrayList<>();
|
||||||
|
|
||||||
public CraftingPattern(World world, ICraftingPatternContainer container, ItemStack stack) {
|
public CraftingPattern(World world, ICraftingPatternContainer container, ItemStack stack) {
|
||||||
this.world = world;
|
|
||||||
this.container = container;
|
this.container = container;
|
||||||
this.stack = stack;
|
this.stack = stack;
|
||||||
|
|
||||||
@@ -119,29 +117,12 @@ public class CraftingPattern implements ICraftingPattern {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object other) {
|
public String toString() {
|
||||||
if (!(other instanceof CraftingPattern)) {
|
return "CraftingPattern{" +
|
||||||
return false;
|
"container=" + container +
|
||||||
}
|
", inputs=" + inputs +
|
||||||
|
", outputs=" + outputs +
|
||||||
CraftingPattern otherPattern = (CraftingPattern) other;
|
", byproducts=" + byproducts +
|
||||||
|
'}';
|
||||||
if (inputs.size() != otherPattern.inputs.size() || outputs.size() != otherPattern.outputs.size()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < inputs.size(); ++i) {
|
|
||||||
if (!CompareUtils.compareStack(inputs.get(i), otherPattern.inputs.get(i))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < outputs.size(); ++i) {
|
|
||||||
if (!CompareUtils.compareStack(outputs.get(i), otherPattern.outputs.get(i))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -10,15 +10,32 @@ import refinedstorage.api.network.INetworkMaster;
|
|||||||
import refinedstorage.api.network.NetworkUtils;
|
import refinedstorage.api.network.NetworkUtils;
|
||||||
import refinedstorage.api.storage.CompareUtils;
|
import refinedstorage.api.storage.CompareUtils;
|
||||||
import refinedstorage.api.storage.item.IGroupedItemStorage;
|
import refinedstorage.api.storage.item.IGroupedItemStorage;
|
||||||
|
import refinedstorage.apiimpl.autocrafting.registry.CraftingTaskFactoryProcessing;
|
||||||
|
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Deque;
|
import java.util.Deque;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class CraftingTask {
|
public class CraftingTask {
|
||||||
|
class ProcessablePattern {
|
||||||
|
private ICraftingPattern pattern;
|
||||||
|
private Deque<ItemStack> toInsert = new ArrayDeque<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ProcessablePattern{" +
|
||||||
|
"pattern=" + pattern +
|
||||||
|
", toInsert=" + toInsert +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private INetworkMaster network;
|
private INetworkMaster network;
|
||||||
private ICraftingPattern pattern;
|
private ICraftingPattern pattern;
|
||||||
private int quantity;
|
private int quantity;
|
||||||
private Deque<ItemStack> toTake = new ArrayDeque<>();
|
private Deque<ItemStack> toTake = new ArrayDeque<>();
|
||||||
|
private List<ProcessablePattern> toProcess = new ArrayList<>();
|
||||||
private Multimap<Item, ItemStack> toCraft = ArrayListMultimap.create();
|
private Multimap<Item, ItemStack> toCraft = ArrayListMultimap.create();
|
||||||
private Multimap<Item, ItemStack> missing = ArrayListMultimap.create();
|
private Multimap<Item, ItemStack> missing = ArrayListMultimap.create();
|
||||||
private Multimap<Item, ItemStack> extras = ArrayListMultimap.create();
|
private Multimap<Item, ItemStack> extras = ArrayListMultimap.create();
|
||||||
@@ -30,15 +47,27 @@ public class CraftingTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void calculate() {
|
public void calculate() {
|
||||||
calculate(pattern, true);
|
calculate(network.getItemStorage().copy(), pattern, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void calculate(ICraftingPattern pattern, boolean basePattern) {
|
private void calculate(IGroupedItemStorage storage, ICraftingPattern pattern, boolean basePattern) {
|
||||||
IGroupedItemStorage itemStorage = network.getItemStorage().copy();
|
|
||||||
|
|
||||||
for (int i = 0; i < quantity; ++i) {
|
for (int i = 0; i < quantity; ++i) {
|
||||||
|
boolean isProcessing = pattern.getId().equals(CraftingTaskFactoryProcessing.ID);
|
||||||
|
|
||||||
|
if (isProcessing) {
|
||||||
|
ProcessablePattern processable = new ProcessablePattern();
|
||||||
|
|
||||||
|
processable.pattern = pattern;
|
||||||
|
|
||||||
|
for (int j = pattern.getInputs().size() - 1; j >= 0; --j) {
|
||||||
|
processable.toInsert.push(pattern.getInputs().get(j).copy());
|
||||||
|
}
|
||||||
|
|
||||||
|
toProcess.add(processable);
|
||||||
|
}
|
||||||
|
|
||||||
for (ItemStack input : pattern.getInputs()) {
|
for (ItemStack input : pattern.getInputs()) {
|
||||||
ItemStack inputInNetwork = itemStorage.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
|
ItemStack inputInNetwork = storage.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
|
||||||
|
|
||||||
if (inputInNetwork == null || inputInNetwork.stackSize == 0) {
|
if (inputInNetwork == null || inputInNetwork.stackSize == 0) {
|
||||||
if (getExtrasFor(input) != null) {
|
if (getExtrasFor(input) != null) {
|
||||||
@@ -51,30 +80,49 @@ public class CraftingTask {
|
|||||||
addToCraft(output);
|
addToCraft(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
calculate(inputPattern, false);
|
calculate(storage, inputPattern, false);
|
||||||
} else {
|
} else {
|
||||||
addMissing(input);
|
addMissing(input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
toTake.push(input);
|
if (!isProcessing) {
|
||||||
|
toTake.push(input);
|
||||||
|
}
|
||||||
|
|
||||||
itemStorage.remove(input);
|
storage.remove(input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!basePattern) {
|
if (!basePattern) {
|
||||||
pattern.getOutputs().stream().filter(o -> o.stackSize > 1).forEach(o -> addExtras(ItemHandlerHelper.copyStackWithSize(o, o.stackSize - 1)));
|
addExtras(pattern);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "{quantity=" + quantity + ",toTake=" + toTake.toString() + ",toCraft=" + toCraft.toString() + ",missing=" + missing.toString() + "}";
|
return "\nCraftingTask{quantity=" + quantity +
|
||||||
|
"\n, toTake=" + toTake +
|
||||||
|
"\n, toCraft=" + toCraft +
|
||||||
|
"\n, toProcess=" + toProcess +
|
||||||
|
"\n, missing=" + missing +
|
||||||
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean update() {
|
public boolean update() {
|
||||||
|
for (ProcessablePattern processable : toProcess) {
|
||||||
|
if (processable.pattern.getContainer().getFacingInventory() != null && !processable.toInsert.isEmpty()) {
|
||||||
|
ItemStack toInsert = processable.toInsert.peek();
|
||||||
|
|
||||||
|
if (ItemHandlerHelper.insertItem(processable.pattern.getContainer().getFacingInventory(), toInsert, true) == null) {
|
||||||
|
ItemHandlerHelper.insertItem(processable.pattern.getContainer().getFacingInventory(), toInsert, false);
|
||||||
|
|
||||||
|
processable.toInsert.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!toTake.isEmpty()) {
|
if (!toTake.isEmpty()) {
|
||||||
ItemStack took = NetworkUtils.extractItem(network, toTake.peek(), 1);
|
ItemStack took = NetworkUtils.extractItem(network, toTake.peek(), 1);
|
||||||
|
|
||||||
@@ -112,6 +160,10 @@ public class CraftingTask {
|
|||||||
missing.put(stack.getItem(), stack.copy());
|
missing.put(stack.getItem(), stack.copy());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addExtras(ICraftingPattern pattern) {
|
||||||
|
pattern.getOutputs().stream().filter(o -> o.stackSize > 1).forEach(o -> addExtras(ItemHandlerHelper.copyStackWithSize(o, o.stackSize - 1)));
|
||||||
|
}
|
||||||
|
|
||||||
private void addExtras(ItemStack stack) {
|
private void addExtras(ItemStack stack) {
|
||||||
ItemStack extras = getExtrasFor(stack);
|
ItemStack extras = getExtrasFor(stack);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user