Refactored crafting patterns (#4458)

* Deduplicate crafting patterns internals

This should save a bit memory for duplicate patterns as well as speed up
loading new patterns on reloading chunks.

* Encode recipe id in pattern
* Use the recipe id directly for looking it up
* Slightly refactored how patterns encode whether they are crafting patterns or not.
* Renamed PatternHelper.
* Added APIs to encode patterns. (#4463)
* Removed ICraftingPatternItem.
* Renamed isCrafting to isCraftable

Co-authored-by: shartte <shartte@users.noreply.github.com>
This commit is contained in:
yueh
2020-07-12 16:21:30 +02:00
committed by GitHub
parent 06735f5c39
commit 9bb23bcac8
19 changed files with 551 additions and 243 deletions
@@ -25,13 +25,15 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.base.Preconditions;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.ICraftingRecipe;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import appeng.api.networking.crafting.ICraftingPatternDetails;
@@ -39,12 +41,11 @@ import appeng.api.storage.channels.IItemStorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.container.ContainerNull;
import appeng.core.Api;
import appeng.items.misc.EncodedPatternItem;
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
public class PatternHelper implements ICraftingPatternDetails, Comparable<PatternHelper> {
public class CraftingPatternDetails implements ICraftingPatternDetails, Comparable<CraftingPatternDetails> {
private final ItemStack patternItem;
private final CraftingInventory crafting = new CraftingInventory(new ContainerNull(), 3, 3);
private final CraftingInventory testFrame = new CraftingInventory(new ContainerNull(), 3, 3);
private final ItemStack correctOutput;
@@ -53,79 +54,73 @@ public class PatternHelper implements ICraftingPatternDetails, Comparable<Patter
private final IAEItemStack[] condensedOutputs;
private final IAEItemStack[] inputs;
private final IAEItemStack[] outputs;
private final boolean isCrafting;
private final boolean isCraftable;
private final boolean canSubstitute;
private final Set<TestLookup> failCache = new HashSet<>();
private final Set<TestLookup> passCache = new HashSet<>();
private final IAEItemStack pattern;
private int priority = 0;
public PatternHelper(final ItemStack is, final World w) {
final CompoundNBT encodedValue = is.getTag();
public CraftingPatternDetails(final IAEItemStack is, final World w) {
Preconditions.checkArgument(is.getItem() instanceof EncodedPatternItem,
"itemStack is not a ICraftingPatternItem");
if (encodedValue == null) {
throw new IllegalArgumentException("No pattern here!");
}
final EncodedPatternItem templateItem = (EncodedPatternItem) is.getItem();
final ItemStack itemStack = is.createItemStack();
final ListNBT inTag = encodedValue.getList("in", 10);
final ListNBT outTag = encodedValue.getList("out", 10);
this.isCrafting = encodedValue.getBoolean("crafting");
final List<IAEItemStack> ingredients = templateItem.getIngredients(itemStack);
final List<IAEItemStack> products = templateItem.getProducts(itemStack);
final ResourceLocation recipeId = templateItem.getCraftingRecipeId(itemStack);
this.canSubstitute = this.isCrafting && encodedValue.getBoolean("substitute");
this.patternItem = is;
this.pattern = AEItemStack.fromItemStack(is);
this.pattern = is.copy();
this.isCraftable = recipeId != null;
this.canSubstitute = templateItem.allowsSubstitution(itemStack);
final List<IAEItemStack> in = new ArrayList<>();
final List<IAEItemStack> out = new ArrayList<>();
for (int x = 0; x < inTag.size(); x++) {
CompoundNBT ingredient = inTag.getCompound(x);
final ItemStack gs = ItemStack.read(ingredient);
if (!ingredient.isEmpty() && gs.isEmpty()) {
throw new IllegalArgumentException("No pattern here!");
}
for (int x = 0; x < 9; x++) {
final IAEItemStack ais = ingredients.get(x);
final ItemStack gs = ais != null ? ais.createItemStack() : ItemStack.EMPTY;
this.crafting.setInventorySlotContents(x, gs);
if (!gs.isEmpty() && (!this.isCrafting || !gs.hasTag())) {
if (!gs.isEmpty() && (!this.isCraftable) || !gs.hasTag()) {
this.markItemAs(x, gs, TestStatus.ACCEPT);
}
in.add(Api.instance().storage().getStorageChannel(IItemStorageChannel.class).createStack(gs));
in.add(ais != null ? ais.copy() : null);
this.testFrame.setInventorySlotContents(x, gs);
}
if (this.isCrafting) {
this.standardRecipe = w.getRecipeManager().getRecipe(IRecipeType.CRAFTING, this.crafting, w).orElse(null);
if (this.isCraftable) {
IRecipe<?> recipe = w.getRecipeManager().getRecipes(IRecipeType.CRAFTING).get(recipeId);
if (this.standardRecipe != null) {
this.correctOutput = this.standardRecipe.getCraftingResult(this.crafting);
out.add(Api.instance().storage().getStorageChannel(IItemStorageChannel.class)
.createStack(this.correctOutput));
} else {
throw new IllegalStateException("No pattern here!");
if (recipe == null || recipe.getType() != IRecipeType.CRAFTING) {
throw new IllegalStateException("recipe id is not a crafting recipe");
}
this.standardRecipe = (ICraftingRecipe) recipe;
this.correctOutput = this.standardRecipe.getCraftingResult(this.crafting);
out.add(Api.instance().storage().getStorageChannel(IItemStorageChannel.class)
.createStack(this.correctOutput));
} else {
this.standardRecipe = null;
this.correctOutput = ItemStack.EMPTY;
for (int x = 0; x < outTag.size(); x++) {
CompoundNBT resultItemTag = outTag.getCompound(x);
final ItemStack gs = ItemStack.read(resultItemTag);
if (!resultItemTag.isEmpty() && gs.isEmpty()) {
throw new IllegalArgumentException("No pattern here!");
}
for (int x = 0; x < 3; x++) {
final IAEItemStack ais = products.get(x);
final ItemStack gs = ais.createItemStack();
if (!gs.isEmpty()) {
out.add(Api.instance().storage().getStorageChannel(IItemStorageChannel.class).createStack(gs));
out.add(ais.copy());
}
}
}
this.outputs = out.toArray(new IAEItemStack[0]);
this.inputs = in.toArray(new IAEItemStack[0]);
this.outputs = out.toArray(new IAEItemStack[0]);
final Map<IAEItemStack, IAEItemStack> tmpOutputs = new HashMap<>();
@@ -190,12 +185,12 @@ public class PatternHelper implements ICraftingPatternDetails, Comparable<Patter
@Override
public ItemStack getPattern() {
return this.patternItem;
return this.pattern.createItemStack();
}
@Override
public synchronized boolean isValidItemForSlot(final int slotIndex, final ItemStack i, final World w) {
if (!this.isCrafting) {
if (!this.isCraftable) {
throw new IllegalStateException("Only crafting recipes supported.");
}
@@ -241,7 +236,7 @@ public class PatternHelper implements ICraftingPatternDetails, Comparable<Patter
@Override
public boolean isCraftable() {
return this.isCrafting;
return this.isCraftable;
}
@Override
@@ -271,7 +266,7 @@ public class PatternHelper implements ICraftingPatternDetails, Comparable<Patter
@Override
public ItemStack getOutput(final CraftingInventory craftingInv, final World w) {
if (!this.isCrafting) {
if (!this.isCraftable) {
throw new IllegalStateException("Only crafting recipes supported.");
}
@@ -323,7 +318,7 @@ public class PatternHelper implements ICraftingPatternDetails, Comparable<Patter
}
@Override
public int compareTo(final PatternHelper o) {
public int compareTo(final CraftingPatternDetails o) {
return Integer.compare(o.priority, this.priority);
}
@@ -341,7 +336,7 @@ public class PatternHelper implements ICraftingPatternDetails, Comparable<Patter
return false;
}
final PatternHelper other = (PatternHelper) obj;
final CraftingPatternDetails other = (CraftingPatternDetails) obj;
if (this.pattern != null && other.pattern != null) {
return this.pattern.equals(other.pattern);
@@ -57,7 +57,6 @@ import appeng.api.config.Actionable;
import appeng.api.config.Settings;
import appeng.api.config.Upgrades;
import appeng.api.config.YesNo;
import appeng.api.implementations.ICraftingPatternItem;
import appeng.api.implementations.IUpgradeableHost;
import appeng.api.implementations.tiles.ICraftingMachine;
import appeng.api.networking.GridFlags;
@@ -407,21 +406,15 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
}
private void addToCraftingList(final ItemStack is) {
if (is.isEmpty()) {
return;
}
final ICraftingPatternDetails details = Api.instance().crafting().decodePattern(is,
this.iHost.getTileEntity().getWorld());
if (is.getItem() instanceof ICraftingPatternItem) {
final ICraftingPatternItem cpi = (ICraftingPatternItem) is.getItem();
final ICraftingPatternDetails details = cpi.getPatternForItem(is, this.iHost.getTileEntity().getWorld());
if (details != null) {
if (this.craftingList == null) {
this.craftingList = new ArrayList<>();
}
this.craftingList.add(details);
if (details != null) {
if (this.craftingList == null) {
this.craftingList = new ArrayList<>();
}
this.craftingList.add(details);
}
}