diff --git a/CHANGELOG.md b/CHANGELOG.md index c09994929..f491a4b7f 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### 1.6.6 - Added new Crafter modes: ignore redstone signal, redstone signal unlocks autocrafting, redstone signal locks autocrafting and redstone pulse inserts next set (replacement for blocking mode) (raoulvdberge) +- Fixed an autocrafting bug where it crashed when external inventories couldn't be filled (raoulvdberge) ### 1.6.5 - Fixed Refined Storage silicon's oredict entry being registered too late (raoulvdberge) diff --git a/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/autocrafting/task/CraftingTask.java b/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/autocrafting/task/CraftingTask.java index a375a2c0d..434ac91fb 100755 --- a/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/autocrafting/task/CraftingTask.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/apiimpl/autocrafting/task/CraftingTask.java @@ -32,6 +32,8 @@ import net.minecraft.world.World; import net.minecraftforge.common.util.Constants; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.items.ItemHandlerHelper; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import javax.annotation.Nullable; import java.util.*; @@ -57,6 +59,8 @@ public class CraftingTask implements ICraftingTask { private static final long CALCULATION_TIMEOUT_MS = 5000; + private static final Logger LOGGER = LogManager.getLogger(); + private INetwork network; private ICraftingRequestInfo requested; private int quantity; @@ -687,28 +691,60 @@ public class CraftingTask implements ICraftingTask { } if (p.getState() == ProcessingState.READY && hasAll) { - for (ItemStack need : p.getItemsToPut()) { + boolean abort = false; + + for (int i = 0; i < p.getItemsToPut().size(); ++i) { + ItemStack need = p.getItemsToPut().get(i); + ItemStack result = this.internalStorage.extract(need, need.getCount(), getFlags(need), Action.PERFORM); if (result == null || result.getCount() != need.getCount()) { - throw new IllegalStateException("Could not extract from the internal inventory even though we could"); + throw new IllegalStateException("The internal crafting inventory reported that " + need + " was available but we got " + result); } - if (!ItemHandlerHelper.insertItem(p.getPattern().getContainer().getConnectedInventory(), result, false).isEmpty()) { - throw new IllegalStateException("Can't fill up inventory even though we could"); + ItemStack remainder = ItemHandlerHelper.insertItem(p.getPattern().getContainer().getConnectedInventory(), result, false); + if (!remainder.isEmpty()) { + LOGGER.warn("In a simulation, " + p.getPattern().getContainer().getConnectedInventory() + " reported that we could insert " + result + " but we got " + remainder + " as a remainder"); + + this.internalStorage.insert(remainder, remainder.getCount(), Action.PERFORM); + + p.getItemsToPut().set(i, remainder); + + abort = true; + + break; } } - for (FluidStack need : p.getFluidsToPut()) { + if (abort) { + continue; + } + + for (int i = 0; i < p.getFluidsToPut().size(); ++i) { + FluidStack need = p.getFluidsToPut().get(i); + FluidStack result = this.internalFluidStorage.extract(need, need.amount, IComparer.COMPARE_NBT, Action.PERFORM); if (result == null || result.amount != need.amount) { - throw new IllegalStateException("Could not extract from the internal inventory even though we could"); + throw new IllegalStateException("The internal crafting inventory reported that " + need + " was available but we got " + result); } - if (p.getPattern().getContainer().getConnectedFluidInventory().fill(result, true) != result.amount) { - throw new IllegalStateException("Can't fill up inventory even though we could"); + int filled = p.getPattern().getContainer().getConnectedFluidInventory().fill(result, true); + if (filled != result.amount) { + LOGGER.warn("In a simulation, " + p.getPattern().getContainer().getConnectedFluidInventory() + " reported that we could fill " + result + " but we only filled " + filled); + + this.internalFluidStorage.insert(result, result.amount - filled, Action.PERFORM); + + p.getFluidsToPut().set(i, StackUtils.copy(result, result.amount - filled)); + + abort = true; + + break; } } + if (abort) { + continue; + } + p.setState(ProcessingState.EXTRACTED_ALL); p.getPattern().getContainer().onUsedForProcessing();