Skip to content

Commit

Permalink
Fixes #5513: Fix validation code for encoding processing patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Oct 11, 2021
1 parent 159a307 commit c6f86d9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package appeng.crafting.pattern;

import java.util.Arrays;
import java.util.Objects;

import javax.annotation.Nullable;

import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -41,7 +44,11 @@ public AEProcessingPattern decode(CompoundTag tag, Level level, boolean tryRecov
}

public ItemStack encode(IAEStack[] sparseInputs, IAEStack[] sparseOutputs) {
Preconditions.checkNotNull(sparseInputs[0]);
if (Arrays.stream(sparseInputs).noneMatch(Objects::nonNull)) {
throw new IllegalArgumentException("At least one input must be non-null.");
}
Preconditions.checkNotNull(sparseOutputs[0],
"The first (primary) output must be non-null.");
checkItemsOrFluids(sparseInputs);
checkItemsOrFluids(sparseOutputs);

Expand Down
72 changes: 28 additions & 44 deletions src/main/java/appeng/menu/me/items/PatternTermMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,40 +188,40 @@ public void encode() {
return;
}

ItemStack output = this.encodedPatternSlot.getItem();
var encodeOutput = this.encodedPatternSlot.getItem();

final ItemStack[] in = this.getInputs();
final ItemStack[] out = this.getOutputs();
var in = this.getValidatedInputs();
var out = this.getValidatedOutputs();

// if there is no input, this would be silly.
if (in == null || out == null || isCraftingMode() && currentRecipe == null) {
return;
}

// first check the output slots, should either be null, or a pattern
if (!output.isEmpty() && !craftingHelper.isEncodedPattern(output)) {
if (!encodeOutput.isEmpty() && !craftingHelper.isEncodedPattern(encodeOutput)) {
return;
} // if nothing is there we should snag a new pattern.
else if (output.isEmpty()) {
output = this.blankPatternSlot.getItem();
if (output.isEmpty() || !isPattern(output)) {
else if (encodeOutput.isEmpty()) {
var blankPattern = this.blankPatternSlot.getItem();
if (!isPattern(blankPattern)) {
return; // no blanks.
}

// remove one, and clear the input slot.
output.setCount(output.getCount() - 1);
if (output.getCount() == 0) {
blankPattern.shrink(1);
if (blankPattern.getCount() <= 0) {
this.blankPatternSlot.set(ItemStack.EMPTY);
}
}

ItemStack encodedPattern;
if (this.isCraftingMode()) {
output = craftingHelper.encodeCraftingPattern(this.currentRecipe, in, out[0], isSubstitute());
encodedPattern = craftingHelper.encodeCraftingPattern(this.currentRecipe, in, out[0], isSubstitute());
} else {
output = craftingHelper.encodeProcessingPattern(toAeStacks(in), toAeStacks(out));
encodedPattern = craftingHelper.encodeProcessingPattern(toAeStacks(in), toAeStacks(out));
}
this.encodedPatternSlot.set(output);

this.encodedPatternSlot.set(encodedPattern);
}

private static IAEStack[] toAeStacks(ItemStack... stacks) {
Expand All @@ -237,43 +237,39 @@ private static IAEStack[] toAeStacks(ItemStack... stacks) {
return out;
}

private ItemStack[] getInputs() {
final ItemStack[] input = new ItemStack[9];
boolean hasValue = false;
private ItemStack[] getValidatedInputs() {
var input = new ItemStack[9];
var valid = false;

for (int x = 0; x < this.craftingGridSlots.length; x++) {
input[x] = this.craftingGridSlots[x].getItem();
if (!input[x].isEmpty()) {
hasValue = true;
// At least one input must be set, but it doesn't matter which one
valid = true;
}
}

if (hasValue) {
return input;
}

return null;
return valid ? input : null;
}

private ItemStack[] getOutputs() {
private ItemStack[] getValidatedOutputs() {
if (this.isCraftingMode()) {
final ItemStack out = this.getAndUpdateOutput();
var out = this.getAndUpdateOutput();

if (!out.isEmpty() && out.getCount() > 0) {
return new ItemStack[] { out };
}
} else {
boolean hasValue = false;
final ItemStack[] list = new ItemStack[3];
var list = new ItemStack[3];

for (int i = 0; i < this.processingOutputSlots.length; i++) {
final ItemStack out = this.processingOutputSlots[i].getItem();
list[i] = out;
if (!out.isEmpty()) {
hasValue = true;
}
list[i] = this.processingOutputSlots[i].getItem();

}
if (hasValue) {
if (list[0].isEmpty()) {
// The first output slot is required
return null;
} else {
return list;
}
}
Expand Down Expand Up @@ -525,16 +521,4 @@ private static boolean canConvertItemToFluid(Slot slot) {
return FluidContainerHelper.getContainedFluid(slot.getItem()) != null;
}

public FakeCraftingMatrixSlot[] getCraftingGridSlots() {
return craftingGridSlots;
}

public OptionalFakeSlot[] getProcessingOutputSlots() {
return processingOutputSlots;
}

public PatternTermSlot getCraftOutputSlot() {
return craftOutputSlot;
}

}

0 comments on commit c6f86d9

Please sign in to comment.