diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-01-27 11:49:37 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-01-27 11:49:37 +0800 |
| commit | 9984685493efbae33b81b29695db71872515e688 (patch) | |
| tree | 6102fc29d57a01e5dee78c0ba1fe784713a26010 /api/src | |
| parent | 92a82cf12af09b3764d5c75350592fbc02f4350f (diff) | |
| download | RoughlyEnoughItems-9984685493efbae33b81b29695db71872515e688.tar.gz RoughlyEnoughItems-9984685493efbae33b81b29695db71872515e688.tar.bz2 RoughlyEnoughItems-9984685493efbae33b81b29695db71872515e688.zip | |
Fix 2x2 crafting
Diffstat (limited to 'api/src')
4 files changed, 115 insertions, 3 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/api/common/display/Display.java b/api/src/main/java/me/shedaniel/rei/api/common/display/Display.java index 82505f971..bf6dc0cef 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/display/Display.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/display/Display.java @@ -25,8 +25,11 @@ package me.shedaniel.rei.api.common.display; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.entry.EntryIngredient; +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.InputIngredient; import me.shedaniel.rei.api.common.transfer.info.MenuInfo; import me.shedaniel.rei.api.common.transfer.info.MenuSerializationContext; +import me.shedaniel.rei.api.common.util.CollectionUtils; import me.shedaniel.rei.impl.display.DisplaySpec; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.ApiStatus; @@ -49,10 +52,15 @@ public interface Display extends DisplaySpec { */ List<EntryIngredient> getInputEntries(); + @Deprecated default List<EntryIngredient> getInputEntries(MenuSerializationContext<?, ?, ?> context, MenuInfo<?, ?> info, boolean fill) { return getInputEntries(); } + default List<InputIngredient<EntryStack<?>>> getInputIngredients(MenuSerializationContext<?, ?, ?> context, MenuInfo<?, ?> info, boolean fill) { + return CollectionUtils.mapIndexed(getInputEntries(context, info, fill), InputIngredient::of); + } + /** * @return a list of outputs */ diff --git a/api/src/main/java/me/shedaniel/rei/api/common/entry/InputIngredient.java b/api/src/main/java/me/shedaniel/rei/api/common/entry/InputIngredient.java new file mode 100644 index 000000000..64ca047fb --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/InputIngredient.java @@ -0,0 +1,49 @@ +package me.shedaniel.rei.api.common.entry; + +import me.shedaniel.rei.api.common.entry.type.EntryType; +import me.shedaniel.rei.api.common.util.CollectionUtils; + +import java.util.Collections; +import java.util.List; + +public interface InputIngredient<T> { + static <T> InputIngredient<T> empty(int index) { + return of(index, Collections.emptyList()); + } + + static <T> InputIngredient<T> of(int index, List<T> ingredient) { + return new InputIngredient<>() { + @Override + public List<T> get() { + return ingredient; + } + + @Override + public int getIndex() { + return index; + } + }; + } + + static <T> InputIngredient<T> withType(InputIngredient<EntryStack<?>> ingredient, EntryType<T> type) { + return new InputIngredient<>() { + @SuppressWarnings("RedundantTypeArguments") + List<T> list = CollectionUtils.<EntryStack<?>, T>filterAndMap(ingredient.get(), + stack -> stack.getType() == type, EntryStack::castValue); + + @Override + public List<T> get() { + return list; + } + + @Override + public int getIndex() { + return ingredient.getIndex(); + } + }; + } + + List<T> get(); + + int getIndex(); +} diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfo.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfo.java index 9cedd8d58..9769d073e 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfo.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuInfo.java @@ -25,12 +25,14 @@ package me.shedaniel.rei.api.common.transfer.info; import com.mojang.blaze3d.vertex.PoseStack; import it.unimi.dsi.fastutil.ints.IntList; +import it.unimi.dsi.fastutil.ints.IntSet; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.client.gui.widgets.Slot; import me.shedaniel.rei.api.client.gui.widgets.Widget; import me.shedaniel.rei.api.common.display.Display; import me.shedaniel.rei.api.common.display.DisplaySerializerRegistry; import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.InputIngredient; import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; import me.shedaniel.rei.api.common.transfer.RecipeFinder; import me.shedaniel.rei.api.common.transfer.RecipeFinderPopulator; @@ -45,6 +47,7 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.ApiStatus; import java.util.Collections; import java.util.List; @@ -122,14 +125,30 @@ public interface MenuInfo<T extends AbstractContainerMenu, D extends Display> { * otherwise it should be aligned for the display category * @return the list of lists of items */ + @Deprecated + @ApiStatus.ScheduledForRemoval default List<List<ItemStack>> getInputs(MenuInfoContext<T, ?, D> context, boolean fill) { if (context.getDisplay() == null) return Collections.emptyList(); - return CollectionUtils.map(context.getDisplay().getInputEntries(context, this, fill), inputEntry -> - CollectionUtils.<EntryStack<?>, ItemStack>filterAndMap(inputEntry, + return CollectionUtils.map(context.getDisplay().getInputIngredients(context, this, fill), inputEntry -> + CollectionUtils.<EntryStack<?>, ItemStack>filterAndMap(inputEntry.get(), stack -> stack.getType() == VanillaEntryTypes.ITEM, EntryStack::castValue)); } /** + * Returns the inputs of the {@link Display}. The nested lists are possible stacks for that specific slot. + * + * @param context the context of the transfer + * @param fill whether this call is for a fill or not, if it is for a fill, the returned list should be aligned for the menu, + * otherwise it should be aligned for the display category + * @return the list of lists of items + */ + default List<InputIngredient<ItemStack>> getInputsIndexed(MenuInfoContext<T, ?, D> context, boolean fill) { + if (context.getDisplay() == null) return Collections.emptyList(); + return CollectionUtils.map(context.getDisplay().getInputIngredients(context, this, fill), entry -> + InputIngredient.withType(entry, VanillaEntryTypes.ITEM)); + } + + /** * Serializes the {@link Display} as {@link CompoundTag}, sent to the server for further info for the transfer. * * @param context the context of the transfer @@ -153,6 +172,7 @@ public interface MenuInfo<T extends AbstractContainerMenu, D extends Display> { * * @param context the context of the transfer * @param inputs the list of inputs + * @param missing the list of missing stacks * @param missingIndices the indices of the missing stacks * @param matrices the rendering transforming matrices * @param mouseX the mouse x position @@ -162,7 +182,7 @@ public interface MenuInfo<T extends AbstractContainerMenu, D extends Display> { * @param bounds the bounds of the display */ @Environment(EnvType.CLIENT) - default void renderMissingInput(MenuInfoContext<T, ?, D> context, List<List<ItemStack>> inputs, IntList missingIndices, PoseStack matrices, int mouseX, int mouseY, + default void renderMissingInput(MenuInfoContext<T, ?, D> context, List<InputIngredient<ItemStack>> inputs, List<InputIngredient<ItemStack>> missing, IntSet missingIndices, PoseStack matrices, int mouseX, int mouseY, float delta, List<Widget> widgets, Rectangle bounds) { int i = 0; for (Widget widget : widgets) { @@ -177,4 +197,25 @@ public interface MenuInfo<T extends AbstractContainerMenu, D extends Display> { } } } + + /** + * Renders the missing ingredients of the transfer. + * The indices of the missing stacks are provided, this aligns with the list returned by {@link #getInputs(MenuInfoContext, boolean)}. + * + * @param context the context of the transfer + * @param inputs the list of inputs + * @param missingIndices the indices of the missing stacks + * @param matrices the rendering transforming matrices + * @param mouseX the mouse x position + * @param mouseY the mouse y position + * @param delta the delta frame time + * @param widgets the widgets set-up by the category + * @param bounds the bounds of the display + */ + @Environment(EnvType.CLIENT) + @Deprecated + @ApiStatus.ScheduledForRemoval + default void renderMissingInput(MenuInfoContext<T, ?, D> context, List<List<ItemStack>> inputs, IntList missingIndices, PoseStack matrices, int mouseX, int mouseY, + float delta, List<Widget> widgets, Rectangle bounds) { + } } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java b/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java index 6efbc1901..7d00c76ec 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java @@ -130,6 +130,20 @@ public class CollectionUtils { return l; } + public static <T, R> List<R> mapIndexed(Iterable<T> list, IndexedFunction<T, R> function) { + List<R> l = list instanceof Collection ? new ArrayList<>(((Collection<T>) list).size() + 1) : new ArrayList<>(); + int i = 0; + for (T t : list) { + l.add(function.apply(i++, t)); + } + return l; + } + + @FunctionalInterface + public interface IndexedFunction<T, R> { + R apply(int index, T object); + } + public static <T, R> List<R> flatMap(Iterable<T> list, Function<T, Collection<R>> function) { List<R> l = new ArrayList<>(); for (T t : list) { |
