Fixed oredict autocrafting sometimes reporting that a craftable item is missing. #1954

This commit is contained in:
raoulvdberge
2018-09-20 22:58:42 +02:00
parent 0641eca086
commit acfcbba7bc
2 changed files with 69 additions and 39 deletions

View File

@@ -5,6 +5,7 @@
- Added a config option to configure the autocrafting calculation timeout in milliseconds (raoulvdberge) - Added a config option to configure the autocrafting calculation timeout in milliseconds (raoulvdberge)
- Fixed an autocrafting bug where it crashed when external inventories couldn't be filled (raoulvdberge) - Fixed an autocrafting bug where it crashed when external inventories couldn't be filled (raoulvdberge)
- Fixed a duplication bug with a disconnected Crafting Grid (raoulvdberge) - Fixed a duplication bug with a disconnected Crafting Grid (raoulvdberge)
- Fixed oredict autocrafting sometimes reporting that a craftable item is missing (raoulvdberge)
- Removed handling of reusable items in autocrafting, to avoid problems (raoulvdberge) - Removed handling of reusable items in autocrafting, to avoid problems (raoulvdberge)
### 1.6.5 ### 1.6.5

View File

@@ -274,6 +274,48 @@ public class CraftingTask implements ICraftingTask {
return null; return null;
} }
class PossibleInputs {
private List<ItemStack> possibilities;
private int pos;
PossibleInputs(List<ItemStack> possibilities) {
this.possibilities = possibilities;
}
ItemStack get() {
return possibilities.get(pos);
}
// Return false if we're exhausted.
boolean cycle() {
if (pos + 1 >= possibilities.size()) {
pos = 0;
return false;
}
pos++;
return true;
}
void sort(IStackList<ItemStack> mutatedStorage, IStackList<ItemStack> results) {
possibilities.sort((a, b) -> {
ItemStack ar = mutatedStorage.get(a);
ItemStack br = mutatedStorage.get(b);
return (br == null ? 0 : br.getCount()) - (ar == null ? 0 : ar.getCount());
});
possibilities.sort((a, b) -> {
ItemStack ar = results.get(a);
ItemStack br = results.get(b);
return (br == null ? 0 : br.getCount()) - (ar == null ? 0 : ar.getCount());
});
}
}
@Nullable @Nullable
private ICraftingTaskError calculateInternal( private ICraftingTaskError calculateInternal(
IStackList<ItemStack> mutatedStorage, IStackList<ItemStack> mutatedStorage,
@@ -297,43 +339,23 @@ public class CraftingTask implements ICraftingTask {
NonNullList<ItemStack> took = NonNullList.create(); NonNullList<ItemStack> took = NonNullList.create();
for (NonNullList<ItemStack> possibleInputs : pattern.getInputs()) { for (NonNullList<ItemStack> inputs : pattern.getInputs()) {
if (possibleInputs.isEmpty()) { if (inputs.isEmpty()) {
took.add(ItemStack.EMPTY); took.add(ItemStack.EMPTY);
continue; continue;
} }
ItemStack possibleInput; PossibleInputs possibleInputs = new PossibleInputs(inputs);
possibleInputs.sort(mutatedStorage, results);
if (possibleInputs.size() == 1) { ItemStack possibleInput = possibleInputs.get();
possibleInput = possibleInputs.get(0);
} else {
NonNullList<ItemStack> sortedPossibleInputs = NonNullList.create();
sortedPossibleInputs.addAll(possibleInputs);
sortedPossibleInputs.sort((a, b) -> { ItemStack fromSelf = results.get(possibleInput);
ItemStack ar = mutatedStorage.get(a); ItemStack fromNetwork = mutatedStorage.get(possibleInput);
ItemStack br = mutatedStorage.get(b);
return (br == null ? 0 : br.getCount()) - (ar == null ? 0 : ar.getCount());
});
sortedPossibleInputs.sort((a, b) -> {
ItemStack ar = results.get(a);
ItemStack br = results.get(b);
return (br == null ? 0 : br.getCount()) - (ar == null ? 0 : ar.getCount());
});
possibleInput = sortedPossibleInputs.get(0);
}
took.add(possibleInput); took.add(possibleInput);
ItemStack fromSelf = results.get(possibleInput, DEFAULT_EXTRACT_FLAGS);
ItemStack fromNetwork = mutatedStorage.get(possibleInput, DEFAULT_EXTRACT_FLAGS);
int remaining = possibleInput.getCount(); int remaining = possibleInput.getCount();
while (remaining > 0) { while (remaining > 0) {
@@ -346,9 +368,7 @@ public class CraftingTask implements ICraftingTask {
remaining -= toTake; remaining -= toTake;
took.set(took.size() - 1, ItemHandlerHelper.copyStackWithSize(fromSelf, possibleInput.getCount())); fromSelf = results.get(possibleInput);
fromSelf = results.get(possibleInput, DEFAULT_EXTRACT_FLAGS);
} else if (fromNetwork != null) { } else if (fromNetwork != null) {
int toTake = Math.min(remaining, fromNetwork.getCount()); int toTake = Math.min(remaining, fromNetwork.getCount());
@@ -360,9 +380,7 @@ public class CraftingTask implements ICraftingTask {
remaining -= toTake; remaining -= toTake;
took.set(took.size() - 1, ItemHandlerHelper.copyStackWithSize(fromNetwork, possibleInput.getCount())); fromNetwork = mutatedStorage.get(possibleInput);
fromNetwork = mutatedStorage.get(possibleInput, DEFAULT_EXTRACT_FLAGS);
toExtractInitial.add(took.get(took.size() - 1)); toExtractInitial.add(took.get(took.size() - 1));
} else { } else {
@@ -378,12 +396,12 @@ public class CraftingTask implements ICraftingTask {
return result; return result;
} }
fromSelf = results.get(possibleInput, DEFAULT_EXTRACT_FLAGS); fromSelf = results.get(possibleInput);
if (fromSelf == null) { if (fromSelf == null) {
throw new IllegalStateException("Recursive calculation didn't yield anything"); throw new IllegalStateException("Recursive calculation didn't yield anything");
} }
fromNetwork = mutatedStorage.get(possibleInput, DEFAULT_EXTRACT_FLAGS); fromNetwork = mutatedStorage.get(possibleInput);
subPatternChain.cycle(); subPatternChain.cycle();
} }
@@ -391,11 +409,22 @@ public class CraftingTask implements ICraftingTask {
// fromSelf contains the amount crafted after the loop. // fromSelf contains the amount crafted after the loop.
this.toCraft.add(possibleInput, fromSelf.getCount()); this.toCraft.add(possibleInput, fromSelf.getCount());
} else { } else {
if (!possibleInputs.cycle()) {
// Give up.
possibleInput = possibleInputs.get(); // Revert back to 0.
this.missing.add(possibleInput, remaining); this.missing.add(possibleInput, remaining);
itemsToExtract.add(possibleInput, remaining); itemsToExtract.add(possibleInput, remaining);
remaining = 0; remaining = 0;
} else {
// Retry with new input...
possibleInput = possibleInputs.get();
fromSelf = results.get(possibleInput);
fromNetwork = mutatedStorage.get(possibleInput);
}
} }
} }
} }
@@ -526,7 +555,7 @@ public class CraftingTask implements ICraftingTask {
List<ItemStack> toRemove = new ArrayList<>(); List<ItemStack> toRemove = new ArrayList<>();
for (ItemStack toExtract : toExtractInitial.getStacks()) { for (ItemStack toExtract : toExtractInitial.getStacks()) {
ItemStack result = network.extractItem(toExtract, toExtract.getCount(), DEFAULT_EXTRACT_FLAGS, Action.PERFORM); ItemStack result = network.extractItem(toExtract, toExtract.getCount(), Action.PERFORM);
if (result != null) { if (result != null) {
internalStorage.insert(toExtract, toExtract.getCount(), Action.PERFORM); internalStorage.insert(toExtract, toExtract.getCount(), Action.PERFORM);