diff options
| author | shedaniel <daniel@shedaniel.me> | 2025-02-25 19:20:09 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2025-02-25 19:20:09 +0800 |
| commit | f5408ce8c85dda2bccbe78d8d2a505acc69aa9a0 (patch) | |
| tree | d69fd8db7da7e00f451c29b91b581c133d4f56a8 | |
| parent | ce2de8d07e696b5ef75f2decbd6961286d88fb4f (diff) | |
| download | RoughlyEnoughItems-f5408ce8c85dda2bccbe78d8d2a505acc69aa9a0.tar.gz RoughlyEnoughItems-f5408ce8c85dda2bccbe78d8d2a505acc69aa9a0.tar.bz2 RoughlyEnoughItems-f5408ce8c85dda2bccbe78d8d2a505acc69aa9a0.zip | |
Fix custom ingredients
8 files changed, 158 insertions, 5 deletions
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 c36811a1d..68a47b5e5 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 @@ -153,22 +153,24 @@ public final class EntryIngredients { } public static EntryIngredient ofIngredient(Ingredient ingredient) { - return ofItemsHolderSet(ingredient.values); + return Internals.toEntryIngredient(ingredient); } public static List<EntryIngredient> ofIngredients(List<Ingredient> ingredients) { if (ingredients.size() == 0) return Collections.emptyList(); if (ingredients.size() == 1) { Ingredient ingredient = ingredients.get(0); - if (ingredient.values.size() == 0) return List.of(); - return List.of(ofIngredient(ingredient)); + EntryIngredient entryIngredient = ofIngredient(ingredient); + if (entryIngredient.isEmpty()) return List.of(); + return List.of(entryIngredient); } boolean emptyFlag = true; List<EntryIngredient> result = new ArrayList<>(ingredients.size()); for (int i = ingredients.size() - 1; i >= 0; i--) { Ingredient ingredient = ingredients.get(i); - if (emptyFlag && ingredient.values.size() == 0) continue; - result.add(0, ofIngredient(ingredient)); + EntryIngredient entryIngredient = ofIngredient(ingredient); + if (emptyFlag && entryIngredient.isEmpty()) continue; + result.add(0, entryIngredient); emptyFlag = false; } return ImmutableList.copyOf(result); diff --git a/api/src/main/java/me/shedaniel/rei/impl/Internals.java b/api/src/main/java/me/shedaniel/rei/impl/Internals.java index bdf7e6d2f..5c8593c6e 100644 --- a/api/src/main/java/me/shedaniel/rei/impl/Internals.java +++ b/api/src/main/java/me/shedaniel/rei/impl/Internals.java @@ -38,6 +38,7 @@ import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponentType; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.Unit; +import net.minecraft.world.item.crafting.Ingredient; import org.jetbrains.annotations.ApiStatus; import java.lang.reflect.Field; @@ -46,6 +47,7 @@ import java.util.function.Supplier; @ApiStatus.Internal public final class Internals { + private static Function<Ingredient, EntryIngredient> ingredientToEntryIngredient = (object) -> throwNotSetup(); private static Supplier<EntryStackProvider> entryStackProvider = Internals::throwNotSetup; private static Supplier<EntryIngredientProvider> entryIngredientProvider = Internals::throwNotSetup; private static Function<ResourceLocation, EntryType<?>> entryTypeDeferred = (object) -> throwNotSetup(); @@ -84,6 +86,10 @@ public final class Internals { } } + public static EntryIngredient toEntryIngredient(Ingredient ingredient) { + return ingredientToEntryIngredient.apply(ingredient); + } + public static EntryStackProvider getEntryStackProvider() { return entryStackProvider.get(); } diff --git a/fabric/src/main/java/me/shedaniel/rei/fabric/PlatformAdapterImpl.java b/fabric/src/main/java/me/shedaniel/rei/fabric/PlatformAdapterImpl.java new file mode 100644 index 000000000..7a5ceef59 --- /dev/null +++ b/fabric/src/main/java/me/shedaniel/rei/fabric/PlatformAdapterImpl.java @@ -0,0 +1,50 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.fabric; + +import me.shedaniel.rei.api.common.entry.EntryIngredient; +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.init.PlatformAdapter; +import net.minecraft.world.item.crafting.Ingredient; + +public class PlatformAdapterImpl implements PlatformAdapter { + @Override + public EntryIngredient fromIngredient(Ingredient ingredient) { + if (ingredient.isEmpty()) return EntryIngredient.empty(); + if (ingredient.getCustomIngredient() != null) { + EntryIngredient.Builder result = EntryIngredient.builder(); + ingredient.items().forEach(item -> { + EntryStack<?> stack = EntryStacks.ofItemHolder(item); + if (!stack.isEmpty()) { + result.add(stack); + } + }); + return result.build(); + } else { + return EntryIngredients.ofItemsHolderSet(ingredient.values); + } + } +} diff --git a/fabric/src/main/resources/META-INF/services/me.shedaniel.rei.impl.init.PlatformAdapter b/fabric/src/main/resources/META-INF/services/me.shedaniel.rei.impl.init.PlatformAdapter new file mode 100644 index 000000000..e34e1dded --- /dev/null +++ b/fabric/src/main/resources/META-INF/services/me.shedaniel.rei.impl.init.PlatformAdapter @@ -0,0 +1 @@ +me.shedaniel.rei.fabric.PlatformAdapterImpl
\ No newline at end of file diff --git a/neoforge/src/main/java/me/shedaniel/rei/forge/PlatformAdapterImpl.java b/neoforge/src/main/java/me/shedaniel/rei/forge/PlatformAdapterImpl.java new file mode 100644 index 000000000..4bc160624 --- /dev/null +++ b/neoforge/src/main/java/me/shedaniel/rei/forge/PlatformAdapterImpl.java @@ -0,0 +1,50 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.forge; + +import me.shedaniel.rei.api.common.entry.EntryIngredient; +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.init.PlatformAdapter; +import net.minecraft.world.item.crafting.Ingredient; + +public class PlatformAdapterImpl implements PlatformAdapter { + @Override + public EntryIngredient fromIngredient(Ingredient ingredient) { + if (ingredient.isEmpty()) return EntryIngredient.empty(); + if (ingredient.isCustom()) { + EntryIngredient.Builder result = EntryIngredient.builder(); + ingredient.items().forEach(item -> { + EntryStack<?> stack = EntryStacks.ofItemHolder(item); + if (!stack.isEmpty()) { + result.add(stack); + } + }); + return result.build(); + } else { + return EntryIngredients.ofItemsHolderSet(ingredient.values); + } + } +} diff --git a/neoforge/src/main/resources/META-INF/services/me.shedaniel.rei.impl.init.PlatformAdapter b/neoforge/src/main/resources/META-INF/services/me.shedaniel.rei.impl.init.PlatformAdapter new file mode 100644 index 000000000..62ad2e5b3 --- /dev/null +++ b/neoforge/src/main/resources/META-INF/services/me.shedaniel.rei.impl.init.PlatformAdapter @@ -0,0 +1 @@ +me.shedaniel.rei.forge.PlatformAdapterImpl
\ No newline at end of file diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java index 6c9c62e79..05758d3b2 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java @@ -31,6 +31,7 @@ import dev.architectury.registry.ReloadListenerRegistry; import dev.architectury.utils.Env; import dev.architectury.utils.EnvExecutor; import dev.architectury.utils.GameInstance; +import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.entry.type.EntryType; import me.shedaniel.rei.api.common.plugins.REICommonPlugin; import me.shedaniel.rei.impl.Internals; @@ -55,12 +56,14 @@ import me.shedaniel.rei.impl.common.plugins.ReloadManagerImpl; import me.shedaniel.rei.impl.common.registry.displays.ServerDisplayRegistryImpl; import me.shedaniel.rei.impl.common.transfer.SlotAccessorRegistryImpl; import me.shedaniel.rei.impl.common.util.InstanceHelper; +import me.shedaniel.rei.impl.init.PlatformAdapter; import me.shedaniel.rei.impl.init.PluginDetector; import me.shedaniel.rei.impl.init.PrimitivePlatformAdapter; import net.minecraft.core.RegistryAccess; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.PackType; import net.minecraft.util.Unit; +import net.minecraft.world.item.crafting.Ingredient; import org.apache.commons.lang3.mutable.MutableLong; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; @@ -126,6 +129,7 @@ public class RoughlyEnoughItemsCore { public static void attachCommonInternals() { Internals.attachInstanceSupplier(LOGGER, "logger"); CategoryIdentifierImpl.attach(); + Internals.attachInstance((Function<Ingredient, EntryIngredient>) ingredient -> PlatformAdapter.get().fromIngredient(ingredient), "ingredientToEntryIngredient"); Internals.attachInstance((Function<ResourceLocation, EntryType<?>>) DeferringEntryTypeProviderImpl.INSTANCE, "entryTypeDeferred"); Internals.attachInstance((Supplier<RegistryAccess>) () -> InstanceHelper.getInstance().registryAccess(), "registryAccess"); Internals.attachInstance(EntryStackProviderImpl.INSTANCE, Internals.EntryStackProvider.class); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/init/PlatformAdapter.java b/runtime/src/main/java/me/shedaniel/rei/impl/init/PlatformAdapter.java new file mode 100644 index 000000000..2bfa7391e --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/init/PlatformAdapter.java @@ -0,0 +1,39 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.init; + +import me.shedaniel.rei.api.common.entry.EntryIngredient; +import net.minecraft.world.item.crafting.Ingredient; + +import java.util.ServiceLoader; + +public interface PlatformAdapter { + ServiceLoader<PlatformAdapter> LOADER = ServiceLoader.load(PlatformAdapter.class); + + static PlatformAdapter get() { + return LOADER.findFirst().orElseThrow(); + } + + EntryIngredient fromIngredient(Ingredient ingredient); +} |
