From e86c691c13b95834e89fe072b0a30e00e0da3ea8 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 21 Jun 2022 00:01:14 +0800 Subject: Add tag helper methods in EntryIngredients --- .../rei/api/common/entry/EntryIngredient.java | 2 + .../rei/api/common/util/EntryIngredients.java | 45 ++++++++++++++++++++++ 2 files changed, 47 insertions(+) (limited to 'api/src/main/java') diff --git a/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryIngredient.java b/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryIngredient.java index b3c5616c9..7cf3a3573 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryIngredient.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryIngredient.java @@ -36,6 +36,8 @@ import java.util.function.UnaryOperator; /** * An immutable representation of a list of {@link EntryStack}. + * + * @see me.shedaniel.rei.api.common.util.EntryIngredients */ @ApiStatus.NonExtendable public interface EntryIngredient extends List> { diff --git a/api/src/main/java/me/shedaniel/rei/api/common/util/EntryIngredients.java b/api/src/main/java/me/shedaniel/rei/api/common/util/EntryIngredients.java index 41c103a03..9ad64534e 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/util/EntryIngredients.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/util/EntryIngredients.java @@ -31,8 +31,13 @@ import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.entry.type.EntryDefinition; import me.shedaniel.rei.api.common.entry.type.EntryType; import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; +import net.minecraft.core.Holder; +import net.minecraft.core.HolderSet; +import net.minecraft.core.Registry; import net.minecraft.nbt.ListTag; import net.minecraft.nbt.Tag; +import net.minecraft.resources.ResourceKey; +import net.minecraft.tags.TagKey; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.level.ItemLike; @@ -42,6 +47,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.function.Function; public final class EntryIngredients { private EntryIngredients() {} @@ -134,6 +140,45 @@ public final class EntryIngredients { return ImmutableList.copyOf(result); } + public static EntryIngredient ofTag(TagKey tagKey, Function, EntryStack> mapper) { + Registry registry = ((Registry>) Registry.REGISTRY).get((ResourceKey>) tagKey.registry()); + HolderSet.Named holders = registry.getTag(tagKey).orElse(null); + if (holders == null) return EntryIngredient.empty(); + EntryIngredient.Builder result = EntryIngredient.builder(holders.size()); + for (Holder holder : holders) { + EntryStack stack = mapper.apply(holder); + if (!stack.isEmpty()) { + result.add(stack); + } + } + return result.build(); + } + + public static List ofTags(Iterable> tagKeys, Function, EntryStack> mapper) { + if (tagKeys instanceof Collection collection && collection.isEmpty()) return Collections.emptyList(); + ImmutableList.Builder ingredients = ImmutableList.builder(); + for (TagKey tagKey : tagKeys) { + ingredients.add(ofTag(tagKey, mapper)); + } + return ingredients.build(); + } + + public static EntryIngredient ofItemTag(TagKey tagKey) { + return ofTag(tagKey, holder -> EntryStacks.of(holder.value())); + } + + public static EntryIngredient ofFluidTag(TagKey tagKey) { + return ofTag(tagKey, holder -> EntryStacks.of(holder.value())); + } + + public static List ofItemTags(Iterable> tagKey) { + return ofTags(tagKey, holder -> EntryStacks.of(holder.value())); + } + + public static List ofFluidTags(Iterable> tagKey) { + return ofTags(tagKey, holder -> EntryStacks.of(holder.value())); + } + public static boolean testFuzzy(EntryIngredient ingredient, EntryStack stack) { for (EntryStack ingredientStack : ingredient) { if (EntryStacks.equalsFuzzy(ingredientStack, stack)) { -- cgit