diff --git a/src/main/java/refinedstorage/api/autocrafting/registry/ICraftingTaskFactory.java b/src/main/java/refinedstorage/api/autocrafting/registry/ICraftingTaskFactory.java index 3ba57ea6a..c0cf62f81 100755 --- a/src/main/java/refinedstorage/api/autocrafting/registry/ICraftingTaskFactory.java +++ b/src/main/java/refinedstorage/api/autocrafting/registry/ICraftingTaskFactory.java @@ -1,5 +1,6 @@ package refinedstorage.api.autocrafting.registry; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import refinedstorage.api.autocrafting.ICraftingPattern; @@ -19,11 +20,12 @@ public interface ICraftingTaskFactory { * * @param world the world * @param network the network + * @param stack the stack to create task for * @param pattern the pattern * @param quantity the quantity * @param tag the NBT tag, if this is null it isn't reading from disk but is used for making a task on demand * @return the crafting task */ @Nonnull - ICraftingTask create(World world, INetworkMaster network, ICraftingPattern pattern, int quantity, @Nullable NBTTagCompound tag); + ICraftingTask create(World world, INetworkMaster network, @Nullable ItemStack stack, ICraftingPattern pattern, int quantity, @Nullable NBTTagCompound tag); } diff --git a/src/main/java/refinedstorage/api/network/NetworkUtils.java b/src/main/java/refinedstorage/api/network/NetworkUtils.java index 5e389476a..7f40ac0e2 100755 --- a/src/main/java/refinedstorage/api/network/NetworkUtils.java +++ b/src/main/java/refinedstorage/api/network/NetworkUtils.java @@ -18,6 +18,8 @@ import refinedstorage.api.autocrafting.registry.ICraftingTaskFactory; import refinedstorage.api.autocrafting.task.ICraftingTask; import refinedstorage.api.storage.CompareUtils; +import javax.annotation.Nullable; + /** * Utilities for network manipulation. */ @@ -34,8 +36,8 @@ public final class NetworkUtils { return network.getPattern(stack, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT); } - public static ICraftingTask createCraftingTask(INetworkMaster network, ICraftingPattern pattern, int quantity) { - return RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(pattern.getId()).create(network.getNetworkWorld(), network, pattern, quantity, null); + public static ICraftingTask createCraftingTask(INetworkMaster network, @Nullable ItemStack stack, ICraftingPattern pattern, int quantity) { + return RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(pattern.getId()).create(network.getNetworkWorld(), network, stack, pattern, quantity, null); } public static boolean hasPattern(INetworkMaster network, ItemStack stack) { @@ -69,7 +71,7 @@ public final class NetworkUtils { ICraftingPattern pattern = network.getPattern(stack, compare); if (pattern != null) { - network.addCraftingTask(createCraftingTask(network, pattern, 1)); + network.addCraftingTask(createCraftingTask(network, stack, pattern, 1)); } } } @@ -86,7 +88,7 @@ public final class NetworkUtils { ICraftingTaskFactory factory = RefinedStorageAPI.instance().getCraftingTaskRegistry().getFactory(tag.getString(ICraftingTask.NBT_PATTERN_ID)); if (factory != null) { - return factory.create(world, network, pattern, tag.getInteger(ICraftingTask.NBT_QUANTITY), tag); + return factory.create(world, network, null, pattern, tag.getInteger(ICraftingTask.NBT_QUANTITY), tag); } } } diff --git a/src/main/java/refinedstorage/apiimpl/autocrafting/registry/CraftingTaskFactoryNormal.java b/src/main/java/refinedstorage/apiimpl/autocrafting/registry/CraftingTaskFactoryNormal.java index d4f5d22d3..62d064073 100755 --- a/src/main/java/refinedstorage/apiimpl/autocrafting/registry/CraftingTaskFactoryNormal.java +++ b/src/main/java/refinedstorage/apiimpl/autocrafting/registry/CraftingTaskFactoryNormal.java @@ -1,5 +1,6 @@ package refinedstorage.apiimpl.autocrafting.registry; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import refinedstorage.api.autocrafting.ICraftingPattern; @@ -16,7 +17,7 @@ public class CraftingTaskFactoryNormal implements ICraftingTaskFactory { @Override @Nonnull - public ICraftingTask create(World world, INetworkMaster network, ICraftingPattern pattern, int quantity, @Nullable NBTTagCompound tag) { - return new CraftingTaskNormal(network, pattern, quantity); + public ICraftingTask create(World world, INetworkMaster network, @Nullable ItemStack stack, ICraftingPattern pattern, int quantity, @Nullable NBTTagCompound tag) { + return new CraftingTaskNormal(network, stack, pattern, quantity); } } \ No newline at end of file diff --git a/src/main/java/refinedstorage/apiimpl/autocrafting/task/CraftingTaskNormal.java b/src/main/java/refinedstorage/apiimpl/autocrafting/task/CraftingTaskNormal.java index 669f460e8..f5e6b6799 100755 --- a/src/main/java/refinedstorage/apiimpl/autocrafting/task/CraftingTaskNormal.java +++ b/src/main/java/refinedstorage/apiimpl/autocrafting/task/CraftingTaskNormal.java @@ -24,6 +24,7 @@ import java.util.List; public class CraftingTaskNormal implements ICraftingTask { private INetworkMaster network; + private ItemStack requested; private ICraftingPattern pattern; private int quantity; private Deque toTake = new ArrayDeque<>(); @@ -32,60 +33,65 @@ public class CraftingTaskNormal implements ICraftingTask { private Multimap missing = ArrayListMultimap.create(); private Multimap extras = ArrayListMultimap.create(); - public CraftingTaskNormal(INetworkMaster network, ICraftingPattern pattern, int quantity) { + public CraftingTaskNormal(INetworkMaster network, ItemStack requested, ICraftingPattern pattern, int quantity) { this.network = network; + this.requested = requested; this.pattern = pattern; this.quantity = quantity; } public void calculate() { - calculate(network.getItemStorage().copy(), pattern, true); + int newQuantity = quantity; + + while (newQuantity > 0) { + calculate(network.getItemStorage().copy(), pattern, true); + + newQuantity -= requested == null ? newQuantity : pattern.getQuantityPerRequest(requested); + } + } + + private void calculate(IGroupedItemStorage storage, ICraftingPattern pattern, boolean basePattern) { + if (pattern.isProcessing()) { + toProcess.add(new Processable(pattern)); + } + + for (ItemStack input : pattern.getInputs()) { + ItemStack inputInNetwork = storage.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT); + + if (inputInNetwork == null || inputInNetwork.stackSize == 0) { + if (getExtrasFor(input) != null) { + decrOrRemoveExtras(input); + } else { + ICraftingPattern inputPattern = NetworkUtils.getPattern(network, input); + + if (inputPattern != null) { + for (ItemStack output : inputPattern.getOutputs()) { + addToCraft(output); + } + + calculate(storage, inputPattern, false); + } else { + addMissing(input); + } + } + } else { + if (!pattern.isProcessing()) { + toTake.push(input); + } + + storage.remove(input); + } + } + + if (!basePattern) { + addExtras(pattern); + } } @Override public void onCancelled() { } - private void calculate(IGroupedItemStorage storage, ICraftingPattern pattern, boolean basePattern) { - for (int i = 0; i < quantity; ++i) { - if (pattern.isProcessing()) { - toProcess.add(new Processable(pattern)); - } - - for (ItemStack input : pattern.getInputs()) { - ItemStack inputInNetwork = storage.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT); - - if (inputInNetwork == null || inputInNetwork.stackSize == 0) { - if (getExtrasFor(input) != null) { - decrOrRemoveExtras(input); - } else { - ICraftingPattern inputPattern = NetworkUtils.getPattern(network, input); - - if (inputPattern != null) { - for (ItemStack output : inputPattern.getOutputs()) { - addToCraft(output); - } - - calculate(storage, inputPattern, false); - } else { - addMissing(input); - } - } - } else { - if (!pattern.isProcessing()) { - toTake.push(input); - } - - storage.remove(input); - } - } - - if (!basePattern) { - addExtras(pattern); - } - } - } - @Override public String toString() { return "\nCraftingTask{quantity=" + quantity + @@ -174,6 +180,7 @@ public class CraftingTaskNormal implements ICraftingTask { return pattern; } + @Override public List getToProcess() { return toProcess; diff --git a/src/main/java/refinedstorage/apiimpl/network/grid/ItemGridHandler.java b/src/main/java/refinedstorage/apiimpl/network/grid/ItemGridHandler.java index de14175a0..15bba8c82 100755 --- a/src/main/java/refinedstorage/apiimpl/network/grid/ItemGridHandler.java +++ b/src/main/java/refinedstorage/apiimpl/network/grid/ItemGridHandler.java @@ -123,7 +123,7 @@ public class ItemGridHandler implements IItemGridHandler { ItemStack stack = network.getItemStorage().get(hash); if (stack != null) { - CraftingTaskNormal task = new CraftingTaskNormal(network, NetworkUtils.getPattern(network, stack), quantity); + CraftingTaskNormal task = new CraftingTaskNormal(network, stack, NetworkUtils.getPattern(network, stack), quantity); task.calculate();