Skip to content

Commit f5408ce

Browse files
committed
Fix custom ingredients
1 parent ce2de8d commit f5408ce

File tree

8 files changed

+158
-5
lines changed

8 files changed

+158
-5
lines changed

api/src/main/java/me/shedaniel/rei/api/common/util/EntryIngredients.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,24 @@ public static EntryIngredient ofItemStacks(Collection<ItemStack> stacks) {
153153
}
154154

155155
public static EntryIngredient ofIngredient(Ingredient ingredient) {
156-
return ofItemsHolderSet(ingredient.values);
156+
return Internals.toEntryIngredient(ingredient);
157157
}
158158

159159
public static List<EntryIngredient> ofIngredients(List<Ingredient> ingredients) {
160160
if (ingredients.size() == 0) return Collections.emptyList();
161161
if (ingredients.size() == 1) {
162162
Ingredient ingredient = ingredients.get(0);
163-
if (ingredient.values.size() == 0) return List.of();
164-
return List.of(ofIngredient(ingredient));
163+
EntryIngredient entryIngredient = ofIngredient(ingredient);
164+
if (entryIngredient.isEmpty()) return List.of();
165+
return List.of(entryIngredient);
165166
}
166167
boolean emptyFlag = true;
167168
List<EntryIngredient> result = new ArrayList<>(ingredients.size());
168169
for (int i = ingredients.size() - 1; i >= 0; i--) {
169170
Ingredient ingredient = ingredients.get(i);
170-
if (emptyFlag && ingredient.values.size() == 0) continue;
171-
result.add(0, ofIngredient(ingredient));
171+
EntryIngredient entryIngredient = ofIngredient(ingredient);
172+
if (emptyFlag && entryIngredient.isEmpty()) continue;
173+
result.add(0, entryIngredient);
172174
emptyFlag = false;
173175
}
174176
return ImmutableList.copyOf(result);

api/src/main/java/me/shedaniel/rei/impl/Internals.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import net.minecraft.core.component.DataComponentType;
3939
import net.minecraft.resources.ResourceLocation;
4040
import net.minecraft.util.Unit;
41+
import net.minecraft.world.item.crafting.Ingredient;
4142
import org.jetbrains.annotations.ApiStatus;
4243

4344
import java.lang.reflect.Field;
@@ -46,6 +47,7 @@
4647

4748
@ApiStatus.Internal
4849
public final class Internals {
50+
private static Function<Ingredient, EntryIngredient> ingredientToEntryIngredient = (object) -> throwNotSetup();
4951
private static Supplier<EntryStackProvider> entryStackProvider = Internals::throwNotSetup;
5052
private static Supplier<EntryIngredientProvider> entryIngredientProvider = Internals::throwNotSetup;
5153
private static Function<ResourceLocation, EntryType<?>> entryTypeDeferred = (object) -> throwNotSetup();
@@ -84,6 +86,10 @@ public static <T> void attachInstance(T instance, String name) {
8486
}
8587
}
8688

89+
public static EntryIngredient toEntryIngredient(Ingredient ingredient) {
90+
return ingredientToEntryIngredient.apply(ingredient);
91+
}
92+
8793
public static EntryStackProvider getEntryStackProvider() {
8894
return entryStackProvider.get();
8995
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is licensed under the MIT License, part of Roughly Enough Items.
3+
* Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
24+
package me.shedaniel.rei.fabric;
25+
26+
import me.shedaniel.rei.api.common.entry.EntryIngredient;
27+
import me.shedaniel.rei.api.common.entry.EntryStack;
28+
import me.shedaniel.rei.api.common.util.EntryIngredients;
29+
import me.shedaniel.rei.api.common.util.EntryStacks;
30+
import me.shedaniel.rei.impl.init.PlatformAdapter;
31+
import net.minecraft.world.item.crafting.Ingredient;
32+
33+
public class PlatformAdapterImpl implements PlatformAdapter {
34+
@Override
35+
public EntryIngredient fromIngredient(Ingredient ingredient) {
36+
if (ingredient.isEmpty()) return EntryIngredient.empty();
37+
if (ingredient.getCustomIngredient() != null) {
38+
EntryIngredient.Builder result = EntryIngredient.builder();
39+
ingredient.items().forEach(item -> {
40+
EntryStack<?> stack = EntryStacks.ofItemHolder(item);
41+
if (!stack.isEmpty()) {
42+
result.add(stack);
43+
}
44+
});
45+
return result.build();
46+
} else {
47+
return EntryIngredients.ofItemsHolderSet(ingredient.values);
48+
}
49+
}
50+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
me.shedaniel.rei.fabric.PlatformAdapterImpl
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is licensed under the MIT License, part of Roughly Enough Items.
3+
* Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
24+
package me.shedaniel.rei.forge;
25+
26+
import me.shedaniel.rei.api.common.entry.EntryIngredient;
27+
import me.shedaniel.rei.api.common.entry.EntryStack;
28+
import me.shedaniel.rei.api.common.util.EntryIngredients;
29+
import me.shedaniel.rei.api.common.util.EntryStacks;
30+
import me.shedaniel.rei.impl.init.PlatformAdapter;
31+
import net.minecraft.world.item.crafting.Ingredient;
32+
33+
public class PlatformAdapterImpl implements PlatformAdapter {
34+
@Override
35+
public EntryIngredient fromIngredient(Ingredient ingredient) {
36+
if (ingredient.isEmpty()) return EntryIngredient.empty();
37+
if (ingredient.isCustom()) {
38+
EntryIngredient.Builder result = EntryIngredient.builder();
39+
ingredient.items().forEach(item -> {
40+
EntryStack<?> stack = EntryStacks.ofItemHolder(item);
41+
if (!stack.isEmpty()) {
42+
result.add(stack);
43+
}
44+
});
45+
return result.build();
46+
} else {
47+
return EntryIngredients.ofItemsHolderSet(ingredient.values);
48+
}
49+
}
50+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
me.shedaniel.rei.forge.PlatformAdapterImpl

runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import dev.architectury.utils.Env;
3232
import dev.architectury.utils.EnvExecutor;
3333
import dev.architectury.utils.GameInstance;
34+
import me.shedaniel.rei.api.common.entry.EntryIngredient;
3435
import me.shedaniel.rei.api.common.entry.type.EntryType;
3536
import me.shedaniel.rei.api.common.plugins.REICommonPlugin;
3637
import me.shedaniel.rei.impl.Internals;
@@ -55,12 +56,14 @@
5556
import me.shedaniel.rei.impl.common.registry.displays.ServerDisplayRegistryImpl;
5657
import me.shedaniel.rei.impl.common.transfer.SlotAccessorRegistryImpl;
5758
import me.shedaniel.rei.impl.common.util.InstanceHelper;
59+
import me.shedaniel.rei.impl.init.PlatformAdapter;
5860
import me.shedaniel.rei.impl.init.PluginDetector;
5961
import me.shedaniel.rei.impl.init.PrimitivePlatformAdapter;
6062
import net.minecraft.core.RegistryAccess;
6163
import net.minecraft.resources.ResourceLocation;
6264
import net.minecraft.server.packs.PackType;
6365
import net.minecraft.util.Unit;
66+
import net.minecraft.world.item.crafting.Ingredient;
6467
import org.apache.commons.lang3.mutable.MutableLong;
6568
import org.apache.logging.log4j.Level;
6669
import org.apache.logging.log4j.LogManager;
@@ -126,6 +129,7 @@ private static <T> T make(T object, Consumer<T> consumer) {
126129
public static void attachCommonInternals() {
127130
Internals.attachInstanceSupplier(LOGGER, "logger");
128131
CategoryIdentifierImpl.attach();
132+
Internals.attachInstance((Function<Ingredient, EntryIngredient>) ingredient -> PlatformAdapter.get().fromIngredient(ingredient), "ingredientToEntryIngredient");
129133
Internals.attachInstance((Function<ResourceLocation, EntryType<?>>) DeferringEntryTypeProviderImpl.INSTANCE, "entryTypeDeferred");
130134
Internals.attachInstance((Supplier<RegistryAccess>) () -> InstanceHelper.getInstance().registryAccess(), "registryAccess");
131135
Internals.attachInstance(EntryStackProviderImpl.INSTANCE, Internals.EntryStackProvider.class);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is licensed under the MIT License, part of Roughly Enough Items.
3+
* Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
24+
package me.shedaniel.rei.impl.init;
25+
26+
import me.shedaniel.rei.api.common.entry.EntryIngredient;
27+
import net.minecraft.world.item.crafting.Ingredient;
28+
29+
import java.util.ServiceLoader;
30+
31+
public interface PlatformAdapter {
32+
ServiceLoader<PlatformAdapter> LOADER = ServiceLoader.load(PlatformAdapter.class);
33+
34+
static PlatformAdapter get() {
35+
return LOADER.findFirst().orElseThrow();
36+
}
37+
38+
EntryIngredient fromIngredient(Ingredient ingredient);
39+
}

0 commit comments

Comments
 (0)