From 3cf87de5b6fea02eb26b747761f8f85ad0b65f51 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 14 Aug 2024 23:11:28 +0900 Subject: New Optimised Craftable Filter (should remove almost all stutters with the filter) Developers can also use the new TransferHandlerMeta to provide custom info for available ingredients used for quickly determining whether the player can craft it. --- .../registry/transfer/TransferHandlerMeta.java | 48 ++++++++++++++++++++++ .../transfer/simple/SimpleTransferHandler.java | 11 ++++- .../me/shedaniel/rei/api/client/view/Views.java | 7 +++- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/TransferHandlerMeta.java (limited to 'api') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/TransferHandlerMeta.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/TransferHandlerMeta.java new file mode 100644 index 000000000..2bdb5616d --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/TransferHandlerMeta.java @@ -0,0 +1,48 @@ +/* + * 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.api.client.registry.transfer; + +import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.ApiStatus; + +import java.util.List; + +/** + * A meta interface for {@link TransferHandler}. + */ +@ApiStatus.Experimental +public interface TransferHandlerMeta { + /** + * Returns the available ingredients for the transfer handler. + * This is used in the craftable filter, and the quick transfer tooltip to quickly determine if the handler can handle the recipe. + *

+ * If this interface is not implemented, REI will assume stacks only from the player's inventory are available. + * + * @param context the context + * @return the available ingredients + */ + default Iterable getAvailableIngredients(TransferHandler.Context context) { + return List.of(); + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/simple/SimpleTransferHandler.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/simple/SimpleTransferHandler.java index e072cea9d..af21306ba 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/simple/SimpleTransferHandler.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/simple/SimpleTransferHandler.java @@ -23,14 +23,17 @@ package me.shedaniel.rei.api.client.registry.transfer.simple; +import com.google.common.collect.Iterables; import com.mojang.blaze3d.vertex.PoseStack; 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.client.registry.transfer.TransferHandler; +import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerMeta; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; +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.info.stack.SlotAccessor; @@ -44,13 +47,14 @@ import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; import org.jetbrains.annotations.ApiStatus; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; @ApiStatus.Experimental -public interface SimpleTransferHandler extends TransferHandler { +public interface SimpleTransferHandler extends TransferHandler, TransferHandlerMeta { static SimpleTransferHandler create(Class containerClass, CategoryIdentifier categoryIdentifier, IntRange inputSlots) { @@ -147,6 +151,11 @@ public interface SimpleTransferHandler extends TransferHandler { InputIngredient.withType(entry, VanillaEntryTypes.ITEM)); } + @Override + default Iterable getAvailableIngredients(Context context) { + return Iterables.transform(Iterables.concat(getInputSlots(context), getInventorySlots(context)), SlotAccessor::getItemStack); + } + /** * Renders the missing ingredients of the transfer. * diff --git a/api/src/main/java/me/shedaniel/rei/api/client/view/Views.java b/api/src/main/java/me/shedaniel/rei/api/client/view/Views.java index 061d5470d..fb4c6d019 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/view/Views.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/view/Views.java @@ -31,6 +31,7 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import java.util.Collection; +import java.util.List; public interface Views extends Reloadable { /** @@ -49,6 +50,10 @@ public interface Views extends Reloadable { * Returns all craftable items from materials. * * @return the list of craftable entries + * @deprecated This is no longer exposed in the API due to the lack of use cases and how this API is very specific to a type of implementation. */ - Collection> findCraftableEntriesByMaterials(); + @Deprecated(forRemoval = true) + default Collection> findCraftableEntriesByMaterials() { + return List.of(); + } } -- cgit