Fixes #4602: Prevent encoding invalid patterns and handle corrupted patterns a little more gracefully. (#4608) (#4629)

(cherry picked from commit 1f9707e256)
This commit is contained in:
shartte
2020-08-21 19:24:47 +02:00
committed by GitHub
parent ef97381e5f
commit 98733844a6
4 changed files with 30 additions and 2 deletions
@@ -309,14 +309,27 @@ public class EncodedPatternItem extends AEBaseItem {
final ListNBT tagIn = new ListNBT();
final ListNBT tagOut = new ListNBT();
boolean hasInput = false;
for (final ItemStack i : in) {
tagIn.add(createItemTag(i));
if (!i.isEmpty()) {
hasInput = true;
}
}
Preconditions.checkArgument(hasInput, "cannot encode a pattern that has no inputs.");
boolean hasNonEmptyOutput = false;
for (final ItemStack i : out) {
tagOut.add(createItemTag(i));
if (!i.isEmpty()) {
hasNonEmptyOutput = true;
}
}
// Patterns without any outputs are corrupt! Never encode such a pattern.
Preconditions.checkArgument(hasNonEmptyOutput, "cannot encode a pattern that has no output.");
encodedValue.put(EncodedPatternItem.NBT_INGREDIENTS, tagIn);
encodedValue.put(EncodedPatternItem.NBT_PRODUCTS, tagOut);
return encodedValue;