diff --git a/Library/src/main/java/mezz/jei/library/ingredients/TypedIngredient.java b/Library/src/main/java/mezz/jei/library/ingredients/TypedIngredient.java index cffcd4d7a..c3f3ed844 100644 --- a/Library/src/main/java/mezz/jei/library/ingredients/TypedIngredient.java +++ b/Library/src/main/java/mezz/jei/library/ingredients/TypedIngredient.java @@ -7,7 +7,6 @@ import mezz.jei.api.ingredients.IIngredientType; import mezz.jei.api.ingredients.ITypedIngredient; import mezz.jei.api.runtime.IIngredientManager; -import mezz.jei.library.ingredients.itemStacks.NormalizedTypedItemStack; import mezz.jei.library.ingredients.itemStacks.TypedItemStack; import net.minecraft.world.item.ItemStack; import org.apache.logging.log4j.LogManager; @@ -38,7 +37,7 @@ public static ITypedIngredient normalize(ITypedIngredient typedIngredi if (type == VanillaTypes.ITEM_STACK) { @SuppressWarnings("unchecked") ITypedIngredient cast = (ITypedIngredient) typedIngredient; - ITypedIngredient normalized = NormalizedTypedItemStack.normalize(cast); + ITypedIngredient normalized = TypedItemStack.normalize(cast); @SuppressWarnings("unchecked") ITypedIngredient castNormalized = (ITypedIngredient) normalized; return castNormalized; diff --git a/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/FullTypedItemStack.java b/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/FullTypedItemStack.java new file mode 100644 index 000000000..88faf03a5 --- /dev/null +++ b/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/FullTypedItemStack.java @@ -0,0 +1,44 @@ +package mezz.jei.library.ingredients.itemStacks; + +import net.minecraft.core.Holder; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.Nullable; + +final class FullTypedItemStack extends TypedItemStack { + private final Holder itemHolder; + private final @Nullable CompoundTag tag; + private final int count; + + public FullTypedItemStack( + Holder itemHolder, + @Nullable CompoundTag tag, + int count + ) { + this.itemHolder = itemHolder; + this.tag = tag; + this.count = count; + } + + @Override + protected ItemStack createItemStackUncached() { + ItemStack itemStack = new ItemStack(itemHolder, count); + itemStack.setTag(tag); + return itemStack; + } + + @Override + protected TypedItemStack getNormalized() { + return NormalizedTypedItemStack.create(itemHolder, tag); + } + + @Override + public String toString() { + return "TypedItemStack{" + + "itemHolder=" + itemHolder + + ", tag=" + tag + + ", count=" + count + + '}'; + } +} diff --git a/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/NormalizedTypedItem.java b/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/NormalizedTypedItem.java index 62bb185e7..c228d7a1b 100644 --- a/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/NormalizedTypedItem.java +++ b/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/NormalizedTypedItem.java @@ -1,28 +1,24 @@ package mezz.jei.library.ingredients.itemStacks; -import mezz.jei.api.constants.VanillaTypes; -import mezz.jei.api.ingredients.IIngredientType; -import mezz.jei.api.ingredients.ITypedIngredient; import net.minecraft.core.Holder; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; -import java.util.Optional; +final class NormalizedTypedItem extends TypedItemStack { + private final Holder itemHolder; -record NormalizedTypedItem(Holder itemHolder) implements ITypedIngredient { - @Override - public ItemStack getIngredient() { - return new ItemStack(itemHolder); + NormalizedTypedItem(Holder itemHolder) { + this.itemHolder = itemHolder; } @Override - public Optional getItemStack() { - return Optional.of(getIngredient()); + protected ItemStack createItemStackUncached() { + return new ItemStack(itemHolder); } @Override - public IIngredientType getType() { - return VanillaTypes.ITEM_STACK; + public TypedItemStack getNormalized() { + return this; } @Override diff --git a/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/NormalizedTypedItemStack.java b/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/NormalizedTypedItemStack.java index f710eed43..505a2f7d8 100644 --- a/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/NormalizedTypedItemStack.java +++ b/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/NormalizedTypedItemStack.java @@ -1,61 +1,40 @@ package mezz.jei.library.ingredients.itemStacks; -import mezz.jei.api.constants.VanillaTypes; -import mezz.jei.api.ingredients.IIngredientType; -import mezz.jei.api.ingredients.ITypedIngredient; import net.minecraft.core.Holder; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; - -import javax.annotation.Nullable; -import java.util.Optional; - -public record NormalizedTypedItemStack( - Holder itemHolder, - CompoundTag tag -) implements ITypedIngredient { - public static ITypedIngredient normalize(ITypedIngredient typedIngredient) { - if (typedIngredient instanceof NormalizedTypedItemStack normalized) { - return normalized; - } else if (typedIngredient instanceof NormalizedTypedItem normalized) { - return normalized; - } else if (typedIngredient instanceof TypedItemStack typedItemStack) { - return create(typedItemStack.itemHolder(), typedItemStack.tag()); - } - ItemStack itemStack = typedIngredient.getIngredient(); - return create(itemStack.getItemHolder(), itemStack.getTag()); +import org.jetbrains.annotations.Nullable; + +final class NormalizedTypedItemStack extends TypedItemStack { + private final Holder itemHolder; + private final CompoundTag tag; + + public NormalizedTypedItemStack( + Holder itemHolder, + CompoundTag tag + ) { + this.itemHolder = itemHolder; + this.tag = tag; } - public static ITypedIngredient create(Holder itemHolder, @Nullable CompoundTag tag) { + static TypedItemStack create(Holder itemHolder, @Nullable CompoundTag tag) { if (tag == null) { return new NormalizedTypedItem(itemHolder); } return new NormalizedTypedItemStack(itemHolder, tag); } - public static ITypedIngredient create(ItemStack itemStack) { - return create( - itemStack.getItemHolder(), - itemStack.getTag() - ); - } - @Override - public ItemStack getIngredient() { + public ItemStack createItemStackUncached() { ItemStack itemStack = new ItemStack(itemHolder, 1); itemStack.setTag(tag); return itemStack; } @Override - public Optional getItemStack() { - return Optional.of(getIngredient()); - } - - @Override - public IIngredientType getType() { - return VanillaTypes.ITEM_STACK; + public TypedItemStack getNormalized() { + return this; } @Override diff --git a/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/TypedItemStack.java b/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/TypedItemStack.java index 2250eb3c6..a844e87d0 100644 --- a/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/TypedItemStack.java +++ b/Library/src/main/java/mezz/jei/library/ingredients/itemStacks/TypedItemStack.java @@ -1,57 +1,64 @@ package mezz.jei.library.ingredients.itemStacks; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import mezz.jei.api.constants.VanillaTypes; import mezz.jei.api.ingredients.IIngredientType; import mezz.jei.api.ingredients.ITypedIngredient; -import net.minecraft.core.Holder; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; -import javax.annotation.Nullable; +import java.time.Duration; import java.util.Optional; -public record TypedItemStack( - Holder itemHolder, - @Nullable CompoundTag tag, - int count -) implements ITypedIngredient { +public abstract class TypedItemStack implements ITypedIngredient { + private static final LoadingCache CACHE = CacheBuilder.newBuilder() + .expireAfterAccess(Duration.ofSeconds(1)) + .build(new CacheLoader<>() { + @Override + public ItemStack load(TypedItemStack key) { + return key.createItemStackUncached(); + } + }); + public static ITypedIngredient create(ItemStack ingredient) { if (ingredient.getCount() == 1) { - return NormalizedTypedItemStack.create(ingredient.getItemHolder(), ingredient.getTag()); + return NormalizedTypedItemStack.create( + ingredient.getItemHolder(), + ingredient.getTag() + ); } - return new TypedItemStack( + return new FullTypedItemStack( ingredient.getItemHolder(), ingredient.getTag(), ingredient.getCount() ); } - @Override - public ItemStack getIngredient() { - ItemStack itemStack = new ItemStack(itemHolder, count); - if (tag != null) { - itemStack.setTag(tag); + public static ITypedIngredient normalize(ITypedIngredient typedIngredient) { + if (typedIngredient instanceof TypedItemStack typedItemStack) { + return typedItemStack.getNormalized(); } - return itemStack; + ItemStack itemStack = typedIngredient.getIngredient(); + return NormalizedTypedItemStack.create(itemStack.getItemHolder(), itemStack.getTag()); } @Override - public Optional getItemStack() { - return Optional.of(getIngredient()); + public final ItemStack getIngredient() { + return CACHE.getUnchecked(this); } @Override - public IIngredientType getType() { - return VanillaTypes.ITEM_STACK; + public final Optional getItemStack() { + return Optional.of(getIngredient()); } @Override - public String toString() { - return "TypedItemStack{" + - "itemHolder=" + itemHolder + - ", tag=" + tag + - ", count=" + count + - '}'; + public final IIngredientType getType() { + return VanillaTypes.ITEM_STACK; } + + protected abstract TypedItemStack getNormalized(); + + protected abstract ItemStack createItemStackUncached(); }