Oredict acrafting

This commit is contained in:
Raoul Van den Berge
2016-10-10 00:16:07 +02:00
parent d79c1eadc8
commit c231f87668
7 changed files with 62 additions and 20 deletions

View File

@@ -28,6 +28,11 @@ public interface ICraftingPattern {
*/ */
boolean isProcessing(); boolean isProcessing();
/**
* @return true if the crafting pattern is oredicted, false otherwise
*/
boolean isOredict();
/** /**
* @return the inputs * @return the inputs
*/ */

View File

@@ -10,6 +10,7 @@ public interface IComparer {
int COMPARE_DAMAGE = 1; int COMPARE_DAMAGE = 1;
int COMPARE_NBT = 2; int COMPARE_NBT = 2;
int COMPARE_QUANTITY = 4; int COMPARE_QUANTITY = 4;
int COMPARE_OREDICT = 8;
/** /**
* Compares two stacks by the given flags. * Compares two stacks by the given flags.

View File

@@ -83,6 +83,11 @@ public class CraftingPattern implements ICraftingPattern {
return ItemPattern.isProcessing(stack); return ItemPattern.isProcessing(stack);
} }
@Override
public boolean isOredict() {
return true;
}
@Override @Override
public List<ItemStack> getInputs() { public List<ItemStack> getInputs() {
return inputs; return inputs;

View File

@@ -10,6 +10,7 @@ import refinedstorage.api.autocrafting.craftingmonitor.ICraftingMonitorElement;
import refinedstorage.api.autocrafting.task.ICraftingTask; import refinedstorage.api.autocrafting.task.ICraftingTask;
import refinedstorage.api.autocrafting.task.IProcessable; import refinedstorage.api.autocrafting.task.IProcessable;
import refinedstorage.api.network.INetworkMaster; import refinedstorage.api.network.INetworkMaster;
import refinedstorage.api.util.IComparer;
import refinedstorage.api.util.IFluidStackList; import refinedstorage.api.util.IFluidStackList;
import refinedstorage.api.util.IItemStackList; import refinedstorage.api.util.IItemStackList;
import refinedstorage.apiimpl.API; import refinedstorage.apiimpl.API;
@@ -17,7 +18,9 @@ import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElemen
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementItemRender; import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementItemRender;
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementText; import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementText;
import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Deque;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -32,12 +35,18 @@ public class CraftingTask implements ICraftingTask {
private IItemStackList toCraft = API.instance().createItemStackList(); private IItemStackList toCraft = API.instance().createItemStackList();
private IItemStackList missing = API.instance().createItemStackList(); private IItemStackList missing = API.instance().createItemStackList();
private IItemStackList extras = API.instance().createItemStackList(); private IItemStackList extras = API.instance().createItemStackList();
private Deque<ItemStack> toInsert = new ArrayDeque<>();
private int compare = IComparer.COMPARE_DAMAGE | IComparer.COMPARE_NBT;
public CraftingTask(INetworkMaster network, ItemStack requested, ICraftingPattern pattern, int quantity) { public CraftingTask(INetworkMaster network, ItemStack requested, ICraftingPattern pattern, int quantity) {
this.network = network; this.network = network;
this.requested = requested; this.requested = requested;
this.pattern = pattern; this.pattern = pattern;
this.quantity = quantity; this.quantity = quantity;
if (pattern.isOredict()) {
this.compare = IComparer.COMPARE_OREDICT;
}
} }
public void calculate() { public void calculate() {
@@ -48,8 +57,16 @@ public class CraftingTask implements ICraftingTask {
while (newQuantity > 0) { while (newQuantity > 0) {
calculate(list, pattern, true); calculate(list, pattern, true);
for (ItemStack output : pattern.getOutputs()) {
toInsert.add(output.copy());
}
newQuantity -= requested == null ? newQuantity : pattern.getQuantityPerRequest(requested); newQuantity -= requested == null ? newQuantity : pattern.getQuantityPerRequest(requested);
} }
for (ItemStack extra : extras.getStacks()) {
toInsert.add(extra.copy());
}
} }
private void calculate(IItemStackList list, ICraftingPattern pattern, boolean basePattern) { private void calculate(IItemStackList list, ICraftingPattern pattern, boolean basePattern) {
@@ -61,18 +78,16 @@ public class CraftingTask implements ICraftingTask {
addExtras(pattern); addExtras(pattern);
} }
for (ItemStack byproduct : pattern.getByproducts()) {
extras.add(byproduct);
}
for (ItemStack input : pattern.getInputs()) { for (ItemStack input : pattern.getInputs()) {
ItemStack inputInNetwork = list.get(input); ItemStack inputInNetwork = list.get(input, compare);
if (inputInNetwork == null || inputInNetwork.stackSize == 0) { if (inputInNetwork == null || inputInNetwork.stackSize == 0) {
if (extras.get(input) != null) { ItemStack extra = extras.get(input, compare);
decrOrRemoveExtras(input);
if (extra != null) {
decrOrRemoveExtras(extra);
} else { } else {
ICraftingPattern inputPattern = network.getPattern(input); ICraftingPattern inputPattern = network.getPattern(input, compare);
if (inputPattern != null) { if (inputPattern != null) {
for (ItemStack output : inputPattern.getOutputs()) { for (ItemStack output : inputPattern.getOutputs()) {
@@ -114,9 +129,13 @@ public class CraftingTask implements ICraftingTask {
toTake.add(input); toTake.add(input);
} }
list.remove(input, true); list.remove(inputInNetwork, 1, true);
} }
} }
for (ItemStack byproduct : pattern.getByproducts()) {
extras.add(byproduct.copy());
}
} }
@Override @Override
@@ -137,7 +156,7 @@ public class CraftingTask implements ICraftingTask {
public boolean update() { public boolean update() {
for (IProcessable processable : toProcess) { for (IProcessable processable : toProcess) {
if (processable.getPattern().getContainer().getFacingInventory() != null && processable.getStackToInsert() != null) { if (processable.getPattern().getContainer().getFacingInventory() != null && processable.getStackToInsert() != null) {
ItemStack toInsert = network.extractItem(processable.getStackToInsert(), 1); ItemStack toInsert = network.extractItem(processable.getStackToInsert(), 1, compare);
if (ItemHandlerHelper.insertItem(processable.getPattern().getContainer().getFacingInventory(), toInsert, true) == null) { if (ItemHandlerHelper.insertItem(processable.getPattern().getContainer().getFacingInventory(), toInsert, true) == null) {
ItemHandlerHelper.insertItem(processable.getPattern().getContainer().getFacingInventory(), toInsert, false); ItemHandlerHelper.insertItem(processable.getPattern().getContainer().getFacingInventory(), toInsert, false);
@@ -148,7 +167,7 @@ public class CraftingTask implements ICraftingTask {
} }
for (ItemStack toTakeStack : toTake.getStacks()) { for (ItemStack toTakeStack : toTake.getStacks()) {
ItemStack took = network.extractItem(toTakeStack, 1); ItemStack took = network.extractItem(toTakeStack, 1, compare);
if (took != null) { if (took != null) {
toTake.remove(toTakeStack, 1, true); toTake.remove(toTakeStack, 1, true);
@@ -171,17 +190,15 @@ public class CraftingTask implements ICraftingTask {
} }
if (toTake.isEmpty() && toTakeFluids.isEmpty() && missing.isEmpty() && hasProcessedItems()) { if (toTake.isEmpty() && toTakeFluids.isEmpty() && missing.isEmpty() && hasProcessedItems()) {
for (ItemStack output : pattern.getOutputs()) { ItemStack insert = toInsert.peek();
// @TODO: Handle remainder
network.insertItem(output, output.stackSize, false); if (network.insertItem(insert, insert.stackSize, true) == null) {
network.insertItem(insert, insert.stackSize, false);
toInsert.pop();
} }
for (ItemStack byproduct : pattern.getByproducts()) { return toInsert.isEmpty();
// @TODO: Handle remainder
network.insertItem(byproduct, byproduct.stackSize, false);
}
return true;
} }
return false; return false;

View File

@@ -17,6 +17,10 @@ public class Comparer implements IComparer {
return false; return false;
} }
if ((flags & COMPARE_OREDICT) == COMPARE_OREDICT) {
return isEqualOredict(left, right);
}
if (left.getItem() != right.getItem()) { if (left.getItem() != right.getItem()) {
return false; return false;
} }

View File

@@ -94,4 +94,9 @@ public class FluidStackList implements IFluidStackList {
return list; return list;
} }
@Override
public String toString() {
return stacks.toString();
}
} }

View File

@@ -94,4 +94,9 @@ public class ItemStackList implements IItemStackList {
return list; return list;
} }
@Override
public String toString() {
return stacks.toString();
}
} }