diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-06-21 00:01:14 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-06-21 00:01:14 +0800 |
| commit | 699259500bf8a54fe65f5503e59d6cfb696ba721 (patch) | |
| tree | edeffa242f9c0f791fa92141e9c9bef656573b04 /api/src/main/java/me | |
| parent | ed7b4ba6d052c6324259a37f0dc834650b6ff1e2 (diff) | |
| download | RoughlyEnoughItems-699259500bf8a54fe65f5503e59d6cfb696ba721.tar.gz RoughlyEnoughItems-699259500bf8a54fe65f5503e59d6cfb696ba721.tar.bz2 RoughlyEnoughItems-699259500bf8a54fe65f5503e59d6cfb696ba721.zip | |
Add tag helper methods in EntryIngredients
Diffstat (limited to 'api/src/main/java/me')
| -rw-r--r-- | api/src/main/java/me/shedaniel/rei/api/common/entry/EntryIngredient.java | 2 | ||||
| -rw-r--r-- | api/src/main/java/me/shedaniel/rei/api/common/util/EntryIngredients.java | 45 |
2 files changed, 47 insertions, 0 deletions
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 cb3e68f44..ae9925788 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 @@ -38,6 +38,8 @@ import java.util.stream.Collectors; /** * An immutable representation of a list of {@link EntryStack}. + * + * @see me.shedaniel.rei.api.common.util.EntryIngredients */ @ApiStatus.NonExtendable public interface EntryIngredient extends List<EntryStack<?>> { 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 a0231a40c..a18379173 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 @@ -30,8 +30,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; @@ -41,6 +46,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() {} @@ -133,6 +139,45 @@ public final class EntryIngredients { return ImmutableList.copyOf(result); } + public static <S, T> EntryIngredient ofTag(TagKey<S> tagKey, Function<Holder<S>, EntryStack<T>> mapper) { + Registry<S> registry = ((Registry<Registry<S>>) Registry.REGISTRY).get((ResourceKey<Registry<S>>) tagKey.registry()); + HolderSet.Named<S> holders = registry.getTag(tagKey).orElse(null); + if (holders == null) return EntryIngredient.empty(); + EntryIngredient.Builder result = EntryIngredient.builder(holders.size()); + for (Holder<S> holder : holders) { + EntryStack<T> stack = mapper.apply(holder); + if (!stack.isEmpty()) { + result.add(stack); + } + } + return result.build(); + } + + public static <S, T> List<EntryIngredient> ofTags(Iterable<TagKey<S>> tagKeys, Function<Holder<S>, EntryStack<T>> mapper) { + if (tagKeys instanceof Collection collection && collection.isEmpty()) return Collections.emptyList(); + ImmutableList.Builder<EntryIngredient> ingredients = ImmutableList.builder(); + for (TagKey<S> tagKey : tagKeys) { + ingredients.add(ofTag(tagKey, mapper)); + } + return ingredients.build(); + } + + public static <T extends ItemLike> EntryIngredient ofItemTag(TagKey<T> tagKey) { + return ofTag(tagKey, holder -> EntryStacks.of(holder.value())); + } + + public static EntryIngredient ofFluidTag(TagKey<Fluid> tagKey) { + return ofTag(tagKey, holder -> EntryStacks.of(holder.value())); + } + + public static <T extends ItemLike> List<EntryIngredient> ofItemTags(Iterable<TagKey<T>> tagKey) { + return ofTags(tagKey, holder -> EntryStacks.of(holder.value())); + } + + public static List<EntryIngredient> ofFluidTags(Iterable<TagKey<Fluid>> tagKey) { + return ofTags(tagKey, holder -> EntryStacks.of(holder.value())); + } + public static <T> boolean testFuzzy(EntryIngredient ingredient, EntryStack<T> stack) { for (EntryStack<?> ingredientStack : ingredient) { if (EntryStacks.equalsFuzzy(ingredientStack, stack)) { |
