-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #3770 Add an ItemStack cache for creating ItemStack from typed in…
…gredients
- Loading branch information
Showing
5 changed files
with
103 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
Library/src/main/java/mezz/jei/library/ingredients/itemStacks/FullTypedItemStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Item> itemHolder; | ||
private final @Nullable CompoundTag tag; | ||
private final int count; | ||
|
||
public FullTypedItemStack( | ||
Holder<Item> 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 + | ||
'}'; | ||
} | ||
} |
20 changes: 8 additions & 12 deletions
20
Library/src/main/java/mezz/jei/library/ingredients/itemStacks/NormalizedTypedItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 16 additions & 37 deletions
53
Library/src/main/java/mezz/jei/library/ingredients/itemStacks/NormalizedTypedItemStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 34 additions & 27 deletions
61
Library/src/main/java/mezz/jei/library/ingredients/itemStacks/TypedItemStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Item> itemHolder, | ||
@Nullable CompoundTag tag, | ||
int count | ||
) implements ITypedIngredient<ItemStack> { | ||
public abstract class TypedItemStack implements ITypedIngredient<ItemStack> { | ||
private static final LoadingCache<TypedItemStack, ItemStack> CACHE = CacheBuilder.newBuilder() | ||
.expireAfterAccess(Duration.ofSeconds(1)) | ||
.build(new CacheLoader<>() { | ||
@Override | ||
public ItemStack load(TypedItemStack key) { | ||
return key.createItemStackUncached(); | ||
} | ||
}); | ||
|
||
public static ITypedIngredient<ItemStack> 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<ItemStack> normalize(ITypedIngredient<ItemStack> 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<ItemStack> getItemStack() { | ||
return Optional.of(getIngredient()); | ||
public final ItemStack getIngredient() { | ||
return CACHE.getUnchecked(this); | ||
} | ||
|
||
@Override | ||
public IIngredientType<ItemStack> getType() { | ||
return VanillaTypes.ITEM_STACK; | ||
public final Optional<ItemStack> getItemStack() { | ||
return Optional.of(getIngredient()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TypedItemStack{" + | ||
"itemHolder=" + itemHolder + | ||
", tag=" + tag + | ||
", count=" + count + | ||
'}'; | ||
public final IIngredientType<ItemStack> getType() { | ||
return VanillaTypes.ITEM_STACK; | ||
} | ||
|
||
protected abstract TypedItemStack getNormalized(); | ||
|
||
protected abstract ItemStack createItemStackUncached(); | ||
} |