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 | |
| parent | ed7b4ba6d052c6324259a37f0dc834650b6ff1e2 (diff) | |
| download | RoughlyEnoughItems-699259500bf8a54fe65f5503e59d6cfb696ba721.tar.gz RoughlyEnoughItems-699259500bf8a54fe65f5503e59d6cfb696ba721.tar.bz2 RoughlyEnoughItems-699259500bf8a54fe65f5503e59d6cfb696ba721.zip | |
Add tag helper methods in EntryIngredients
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<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)) { 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.<ItemStack>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.<ItemStack>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.<ItemStack>castValue().getItem())) registry.addWorkstations(PATHING, stack); } } - private static <T> Iterable<T> resolveTag(TagKey<T> tagKey) { - Registry<T> registry = ((Registry<Registry<T>>) Registry.REGISTRY).get((ResourceKey<Registry<T>>) tagKey.registry()); - HolderSet.Named<T> holders = registry.getTag(tagKey).orElse(null); - if (holders == null) return Collections.emptyList(); - return () -> holders.stream() - .map(Holder::value) - .iterator(); - } - - private static Iterable<Item> 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()))); }); |
