From 95cdaff2ded9161472d6d56e19d22b4b76e6f8b3 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Thu, 27 Jan 2022 11:49:37 +0800 Subject: Fix 2x2 crafting --- .../shedaniel/rei/api/common/display/Display.java | 8 +++ .../rei/api/common/entry/InputIngredient.java | 72 ++++++++++++++++++++++ .../rei/api/common/transfer/info/MenuInfo.java | 46 +++++++++++++- .../rei/api/common/util/CollectionUtils.java | 14 +++++ 4 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/common/entry/InputIngredient.java (limited to 'api/src/main/java/me') 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 getInputEntries(); + @Deprecated default List getInputEntries(MenuSerializationContext context, MenuInfo info, boolean fill) { return getInputEntries(); } + default List>> 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..99dd3dcaa --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/InputIngredient.java @@ -0,0 +1,72 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.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 { + static InputIngredient empty(int index) { + return of(index, Collections.emptyList()); + } + + static InputIngredient of(int index, List ingredient) { + return new InputIngredient() { + @Override + public List get() { + return ingredient; + } + + @Override + public int getIndex() { + return index; + } + }; + } + + static InputIngredient withType(InputIngredient> ingredient, EntryType type) { + return new InputIngredient() { + @SuppressWarnings("RedundantTypeArguments") + List list = CollectionUtils., T>filterAndMap(ingredient.get(), + stack -> stack.getType() == type, EntryStack::castValue); + + @Override + public List get() { + return list; + } + + @Override + public int getIndex() { + return ingredient.getIndex(); + } + }; + } + + List 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 ad9a0e3cc..ca3b8ba74 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,6 +25,7 @@ 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; @@ -32,6 +33,7 @@ import me.shedaniel.rei.api.common.category.CategoryIdentifier; 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; @@ -133,10 +135,12 @@ public interface MenuInfo ex * otherwise it should be aligned for the display category * @return the list of lists of items */ + @Deprecated + @ApiStatus.ScheduledForRemoval default List> getInputs(MenuInfoContext context, boolean fill) { if (context.getDisplay() == null) return Collections.emptyList(); - return CollectionUtils.map(context.getDisplay().getInputEntries(context, this, fill), inputEntry -> - CollectionUtils., ItemStack>filterAndMap(inputEntry, + return CollectionUtils.map(context.getDisplay().getInputIngredients(context, this, fill), inputEntry -> + CollectionUtils., ItemStack>filterAndMap(inputEntry.get(), stack -> stack.getType() == VanillaEntryTypes.ITEM, EntryStack::castValue)); } @@ -152,6 +156,20 @@ public interface MenuInfo ex return getInputs(context, false); } + /** + * 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> getInputsIndexed(MenuInfoContext 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. * @@ -181,6 +199,7 @@ public interface MenuInfo ex * * @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 @@ -190,7 +209,7 @@ public interface MenuInfo ex * @param bounds the bounds of the display */ @Environment(EnvType.CLIENT) - default void renderMissingInput(MenuInfoContext context, List> inputs, IntList missingIndices, PoseStack matrices, int mouseX, int mouseY, + default void renderMissingInput(MenuInfoContext context, List> inputs, List> missing, IntSet missingIndices, PoseStack matrices, int mouseX, int mouseY, float delta, List widgets, Rectangle bounds) { int i = 0; for (Widget widget : widgets) { @@ -205,4 +224,25 @@ public interface MenuInfo ex } } } + + /** + * 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 context, List> inputs, IntList missingIndices, PoseStack matrices, int mouseX, int mouseY, + float delta, List 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 List mapIndexed(Iterable list, IndexedFunction function) { + List l = list instanceof Collection ? new ArrayList<>(((Collection) list).size() + 1) : new ArrayList<>(); + int i = 0; + for (T t : list) { + l.add(function.apply(i++, t)); + } + return l; + } + + @FunctionalInterface + public interface IndexedFunction { + R apply(int index, T object); + } + public static List flatMap(Iterable list, Function> function) { List l = new ArrayList<>(); for (T t : list) { -- cgit