From 699259500bf8a54fe65f5503e59d6cfb696ba721 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 ++++++++++++++++++++++ .../rei/plugin/client/DefaultClientPlugin.java | 40 +++++++------------ 3 files changed, 61 insertions(+), 26 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> { 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 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)) { diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java index ec6c8edaa..5e514af3d 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java @@ -43,7 +43,7 @@ import me.shedaniel.rei.api.client.registry.screen.ExclusionZones; import me.shedaniel.rei.api.client.registry.screen.ScreenRegistry; import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerRegistry; import me.shedaniel.rei.api.common.entry.EntryIngredient; -import me.shedaniel.rei.api.common.util.CollectionUtils; +import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.util.EntryIngredients; import me.shedaniel.rei.api.common.util.EntryStacks; import me.shedaniel.rei.impl.ClientInternals; @@ -75,12 +75,9 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.gui.screens.inventory.*; import net.minecraft.client.gui.screens.recipebook.RecipeUpdateListener; -import net.minecraft.core.Holder; -import net.minecraft.core.HolderSet; import net.minecraft.core.Registry; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.TranslatableComponent; -import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.BlockTags; import net.minecraft.tags.ItemTags; @@ -195,32 +192,23 @@ public class DefaultClientPlugin implements REIClientPlugin, BuiltinClientPlugin registry.addWorkstations(PATHING, EntryStacks.of(item)); } }); - for (Item item : getTag(new ResourceLocation("c", "axes"))) { - if (axes.add(item)) { - registry.addWorkstations(STRIPPING, EntryStacks.of(item)); - registry.addWorkstations(WAX_SCRAPING, EntryStacks.of(item)); - registry.addWorkstations(OXIDATION_SCRAPING, EntryStacks.of(item)); + for (EntryStack stack : getTag(new ResourceLocation("c", "axes"))) { + if (axes.add(stack.castValue().getItem())) { + registry.addWorkstations(STRIPPING, stack); + registry.addWorkstations(WAX_SCRAPING, stack); + registry.addWorkstations(OXIDATION_SCRAPING, stack); } } - for (Item item : getTag(new ResourceLocation("c", "hoes"))) { - if (hoes.add(item)) registry.addWorkstations(TILLING, EntryStacks.of(item)); + for (EntryStack stack : getTag(new ResourceLocation("c", "hoes"))) { + if (hoes.add(stack.castValue().getItem())) registry.addWorkstations(TILLING, stack); } - for (Item item : getTag(new ResourceLocation("c", "shovels"))) { - if (shovels.add(item)) registry.addWorkstations(PATHING, EntryStacks.of(item)); + for (EntryStack stack : getTag(new ResourceLocation("c", "shovels"))) { + if (shovels.add(stack.castValue().getItem())) registry.addWorkstations(PATHING, stack); } } - private static Iterable resolveTag(TagKey tagKey) { - Registry registry = ((Registry>) Registry.REGISTRY).get((ResourceKey>) tagKey.registry()); - HolderSet.Named holders = registry.getTag(tagKey).orElse(null); - if (holders == null) return Collections.emptyList(); - return () -> holders.stream() - .map(Holder::value) - .iterator(); - } - - private static Iterable getTag(ResourceLocation tagId) { - return resolveTag(TagKey.create(Registry.ITEM_REGISTRY, tagId)); + private static EntryIngredient getTag(ResourceLocation tagId) { + return EntryIngredients.ofItemTag(TagKey.create(Registry.ITEM_REGISTRY, tagId)); } @Override @@ -271,8 +259,8 @@ public class DefaultClientPlugin implements REIClientPlugin, BuiltinClientPlugin DummyShovelItem.getPathBlocksMap().entrySet().stream().sorted(Comparator.comparing(b -> Registry.BLOCK.getKey(b.getKey()))).forEach(set -> { registry.add(new DefaultPathingDisplay(EntryStacks.of(set.getKey()), EntryStacks.of(set.getValue().getBlock()))); }); - registry.add(new DefaultBeaconBaseDisplay(CollectionUtils.map(resolveTag(BlockTags.BEACON_BASE_BLOCKS), ItemStack::new))); - registry.add(new DefaultBeaconPaymentDisplay(CollectionUtils.map(resolveTag(ItemTags.BEACON_PAYMENT_ITEMS), ItemStack::new))); + registry.add(new DefaultBeaconBaseDisplay(Collections.singletonList(EntryIngredients.ofItemTag(BlockTags.BEACON_BASE_BLOCKS)), Collections.emptyList())); + registry.add(new DefaultBeaconPaymentDisplay(Collections.singletonList(EntryIngredients.ofItemTag(ItemTags.BEACON_PAYMENT_ITEMS)), Collections.emptyList())); HoneycombItem.WAXABLES.get().entrySet().stream().sorted(Comparator.comparing(b -> Registry.BLOCK.getKey(b.getKey()))).forEach(set -> { registry.add(new DefaultWaxingDisplay(EntryStacks.of(set.getKey()), EntryStacks.of(set.getValue()))); }); -- cgit