From 3cf87de5b6fea02eb26b747761f8f85ad0b65f51 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 14 Aug 2024 23:11:28 +0900 Subject: [PATCH] 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. --- .../transfer/TransferHandlerMeta.java | 48 +++++ .../simple/SimpleTransferHandler.java | 11 +- .../shedaniel/rei/api/client/view/Views.java | 7 +- .../InventoryCraftingTransferHandler.java | 9 +- .../craftable/CraftableFilterCalculator.java | 175 ++++++++++++++++++ .../entrylist/EntryListSearchManager.java | 13 +- .../client/search/AsyncSearchManager.java | 4 + .../rei/impl/client/view/ViewsImpl.java | 141 +------------- 8 files changed, 261 insertions(+), 147 deletions(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/TransferHandlerMeta.java create mode 100644 runtime/src/main/java/me/shedaniel/rei/impl/client/gui/craftable/CraftableFilterCalculator.java 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.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 @@ default List> getInputsIndexed(Context context) { 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.Nullable; import java.util.Collection; +import java.util.List; public interface Views extends Reloadable { /** @@ -49,6 +50,10 @@ static Views getInstance() { * 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(); + } } diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/autocrafting/InventoryCraftingTransferHandler.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/autocrafting/InventoryCraftingTransferHandler.java index d377c2add..d57fc1bb6 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/autocrafting/InventoryCraftingTransferHandler.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/autocrafting/InventoryCraftingTransferHandler.java @@ -24,6 +24,7 @@ package me.shedaniel.rei.plugin.autocrafting; import me.shedaniel.rei.api.client.registry.transfer.TransferHandler; +import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerMeta; import me.shedaniel.rei.api.client.registry.transfer.simple.SimpleTransferHandler; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.entry.InputIngredient; @@ -31,10 +32,11 @@ import me.shedaniel.rei.api.common.util.CollectionUtils; import me.shedaniel.rei.plugin.common.displays.crafting.DefaultCraftingDisplay; import net.minecraft.network.chat.Component; +import net.minecraft.world.item.ItemStack; import java.util.List; -public class InventoryCraftingTransferHandler implements TransferHandler { +public class InventoryCraftingTransferHandler implements TransferHandler, TransferHandlerMeta { private final SimpleTransferHandler parent; public InventoryCraftingTransferHandler(SimpleTransferHandler parent) { @@ -61,4 +63,9 @@ public Result handle(Context context) { CollectionUtils.map(inputs, entry -> InputIngredient.withType(entry, VanillaEntryTypes.ITEM)), parent.getInputSlots(context), parent.getInventorySlots(context)); } + + @Override + public Iterable getAvailableIngredients(Context context) { + return parent.getAvailableIngredients(context); + } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/craftable/CraftableFilterCalculator.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/craftable/CraftableFilterCalculator.java new file mode 100644 index 000000000..b23cc18bf --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/craftable/CraftableFilterCalculator.java @@ -0,0 +1,175 @@ +/* + * 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.client.gui.craftable; + +import com.google.common.base.Suppliers; +import it.unimi.dsi.fastutil.longs.*; +import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; +import me.shedaniel.rei.api.client.REIRuntime; +import me.shedaniel.rei.api.client.registry.display.DisplayRegistry; +import me.shedaniel.rei.api.client.registry.transfer.TransferHandler; +import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerMeta; +import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerRegistry; +import me.shedaniel.rei.api.common.display.Display; +import me.shedaniel.rei.api.common.entry.EntryIngredient; +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.comparison.ComparisonContext; +import me.shedaniel.rei.api.common.entry.type.EntryDefinition; +import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; +import me.shedaniel.rei.api.common.util.EntryStacks; +import me.shedaniel.rei.impl.client.registry.display.DisplayCache; +import me.shedaniel.rei.impl.client.registry.display.DisplayRegistryImpl; +import me.shedaniel.rei.impl.common.util.HashedEntryStackWrapper; +import me.shedaniel.rei.plugin.autocrafting.DefaultCategoryHandler; +import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.Nullable; + +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.function.Predicate; +import java.util.function.Supplier; + +public class CraftableFilterCalculator implements Predicate { + private final Supplier displayCache = Suppliers.memoize(() -> ((DisplayRegistryImpl) DisplayRegistry.getInstance()).displaysHolder().cache()); + private Set checkedCraftableDisplays = Collections.synchronizedSet(new ReferenceOpenHashSet<>()); + private Set checkedUncraftableDisplays = Collections.synchronizedSet(new ReferenceOpenHashSet<>()); + private LongSet checkedCraftableEntries = LongSets.synchronize(new LongOpenHashSet()); + + @Override + public boolean test(HashedEntryStackWrapper wrapper) { + EntryStack stack = wrapper.unwrap(); + if (stack.getType() != VanillaEntryTypes.ITEM || stack.isEmpty()) return false; + if (checkedCraftableEntries.contains(wrapper.hashExact())) return true; + DisplayCache cache = this.displayCache.get(); + for (Display display : cache.getAllDisplaysByOutputs(List.of(stack))) { + if (checkCraftableCachedByResult(display)) return true; + } + return false; + } + + private boolean checkCraftableCachedByDisplay(Display display) { + if (checkedCraftableDisplays.contains(display)) return true; + if (checkedUncraftableDisplays.contains(display)) return false; + boolean checkCraftable = checkCraftable(display); + if (checkCraftable) { + checkedCraftableDisplays.add(display); + } else { + checkedUncraftableDisplays.add(display); + } + return checkCraftable; + } + + private boolean checkCraftableCachedByResult(Display display) { + boolean checkCraftable = checkCraftableCachedByDisplay(display); + if (checkCraftable) { + for (EntryIngredient ingredient : display.getOutputEntries()) { + for (EntryStack stack : ingredient) { + if (stack.getType() != VanillaEntryTypes.ITEM || stack.isEmpty()) continue; + checkedCraftableEntries.add(EntryStacks.hashExact(stack)); + } + } + } + return checkCraftable; + } + + private boolean checkCraftable(Display display) { + @Nullable Long2LongMap ingredients = chooseHandler(display); + if (ingredients == null) { + return false; + } + + List requiredEntries = display.getRequiredEntries(); + if (requiredEntries.isEmpty()) { + return false; + } + + int slotsCraftable = 0; + boolean containsNonEmpty = false; + + for (EntryIngredient slot : requiredEntries) { + if (slot.isEmpty()) { + slotsCraftable++; + continue; + } + for (EntryStack slotPossible : slot) { + if (slotPossible.getType() != VanillaEntryTypes.ITEM) continue; + ItemStack stack = slotPossible.castValue(); + long hashFuzzy = EntryStacks.hashFuzzy(slotPossible); + long availableAmount = ingredients.get(hashFuzzy); + if (availableAmount >= stack.getCount()) { + ingredients.put(hashFuzzy, availableAmount - stack.getCount()); + containsNonEmpty = true; + slotsCraftable++; + break; + } + } + } + + return slotsCraftable == requiredEntries.size() && containsNonEmpty; + } + + @Nullable + public Long2LongMap chooseHandler(Display display) { + TransferHandler.Context transferContext = TransferHandler.Context.create(false, false, REIRuntime.getInstance().getPreviousContainerScreen(), display); + DefaultCategoryHandler legacyHandler = null; + for (TransferHandler handler : TransferHandlerRegistry.getInstance()) { + if (handler instanceof DefaultCategoryHandler) { + legacyHandler = (DefaultCategoryHandler) handler; + } else { + TransferHandler.ApplicabilityResult result = handler.checkApplicable(transferContext); + if (result.isSuccessful()) { + if (handler instanceof TransferHandlerMeta) { + return extractIngredients(((TransferHandlerMeta) handler).getAvailableIngredients(transferContext)); + } else { + return CraftableFilter.INSTANCE.getInvStacks(); + } + } + } + } + + if (legacyHandler != null) { + TransferHandler.ApplicabilityResult result = legacyHandler.checkApplicable(transferContext); + if (result.isSuccessful()) { + return CraftableFilter.INSTANCE.getInvStacks(); + } + } + + return null; + } + + private static Long2LongMap extractIngredients(Iterable ingredients) { + EntryDefinition definition = VanillaEntryTypes.ITEM.getDefinition(); + + Long2LongMap map = new Long2LongOpenHashMap(); + for (ItemStack stack : ingredients) { + if (!stack.isEmpty()) { + long hash = definition.hash(null, stack, ComparisonContext.FUZZY); + long newCount = map.getOrDefault(hash, 0) + Math.max(0, stack.getCount()); + map.put(hash, newCount); + } + } + return map; + } +} diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/entrylist/EntryListSearchManager.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/entrylist/EntryListSearchManager.java index d7f353a52..c1bd3338e 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/entrylist/EntryListSearchManager.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/entrylist/EntryListSearchManager.java @@ -23,10 +23,9 @@ package me.shedaniel.rei.impl.client.gui.widget.entrylist; +import com.google.common.base.Predicates; import com.google.common.base.Stopwatch; import com.google.common.collect.Iterators; -import it.unimi.dsi.fastutil.longs.LongOpenHashSet; -import it.unimi.dsi.fastutil.longs.LongSet; import me.shedaniel.rei.api.client.config.ConfigManager; import me.shedaniel.rei.api.client.config.ConfigObject; import me.shedaniel.rei.api.client.gui.config.EntryPanelOrdering; @@ -36,10 +35,10 @@ import me.shedaniel.rei.api.client.view.Views; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; -import me.shedaniel.rei.api.common.util.EntryStacks; import me.shedaniel.rei.impl.client.config.collapsible.CollapsibleConfigManager; import me.shedaniel.rei.impl.client.search.AsyncSearchManager; import me.shedaniel.rei.impl.client.search.collapsed.CollapsedEntriesCache; +import me.shedaniel.rei.impl.client.view.ViewsImpl; import me.shedaniel.rei.impl.common.InternalLogger; import me.shedaniel.rei.impl.common.entry.type.EntryRegistryImpl; import me.shedaniel.rei.impl.common.entry.type.collapsed.CollapsedStack; @@ -72,13 +71,7 @@ public class EntryListSearchManager { private final AsyncSearchManager searchManager = new AsyncSearchManager(EntryListSearchManager::getAllEntriesContextually, () -> { boolean checkCraftable = ConfigManager.getInstance().isCraftableOnlyEnabled(); - LongSet workingItems = checkCraftable ? new LongOpenHashSet() : null; - if (checkCraftable) { - for (EntryStack stack : Views.getInstance().findCraftableEntriesByMaterials()) { - workingItems.add(EntryStacks.hashExact(stack)); - } - } - return checkCraftable ? stack -> workingItems.contains(stack.hashExact()) : stack -> true; + return checkCraftable ? ((ViewsImpl) Views.getInstance()).getCraftableEntriesPredicate() : Predicates.alwaysTrue(); }, HashedEntryStackWrapper::normalize); private static List getAllEntriesContextually(SearchFilter filter) { diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/search/AsyncSearchManager.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/search/AsyncSearchManager.java index 381831c42..2d9f1c870 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/search/AsyncSearchManager.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/search/AsyncSearchManager.java @@ -124,6 +124,10 @@ public CompletableFuture, SearchFilter>> .thenApply(entry -> { this.last = entry; return entry; + }) + .exceptionally(throwable -> { + InternalLogger.getInstance().error("Error while searching", throwable); + return new AbstractMap.SimpleImmutableEntry<>(List.of(), filter); }); } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java index 79d27d9fd..2d2e6fada 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java @@ -23,23 +23,18 @@ package me.shedaniel.rei.impl.client.view; +import com.google.common.base.Predicates; import com.google.common.base.Stopwatch; import com.google.common.collect.Iterables; import com.google.common.collect.Maps; -import it.unimi.dsi.fastutil.longs.Long2LongMap; -import it.unimi.dsi.fastutil.longs.Long2LongMaps; -import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import it.unimi.dsi.fastutil.objects.ReferenceLinkedOpenHashSet; -import me.shedaniel.rei.api.client.REIRuntime; import me.shedaniel.rei.api.client.config.ConfigObject; import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; import me.shedaniel.rei.api.client.registry.display.DisplayCategory; import me.shedaniel.rei.api.client.registry.display.DisplayRegistry; import me.shedaniel.rei.api.client.registry.display.DynamicDisplayGenerator; -import me.shedaniel.rei.api.client.registry.transfer.TransferHandler; -import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerRegistry; import me.shedaniel.rei.api.client.view.ViewSearchBuilder; import me.shedaniel.rei.api.client.view.Views; import me.shedaniel.rei.api.common.category.CategoryIdentifier; @@ -47,40 +42,28 @@ import me.shedaniel.rei.api.common.display.DisplayMerger; import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.entry.EntryStack; -import me.shedaniel.rei.api.common.entry.comparison.ComparisonContext; -import me.shedaniel.rei.api.common.entry.type.EntryDefinition; -import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; import me.shedaniel.rei.api.common.plugins.PluginManager; -import me.shedaniel.rei.api.common.transfer.info.MenuInfo; -import me.shedaniel.rei.api.common.transfer.info.MenuInfoRegistry; -import me.shedaniel.rei.api.common.transfer.info.MenuSerializationContext; -import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor; import me.shedaniel.rei.api.common.util.CollectionUtils; import me.shedaniel.rei.api.common.util.EntryIngredients; import me.shedaniel.rei.api.common.util.EntryStacks; -import me.shedaniel.rei.impl.client.gui.craftable.CraftableFilter; +import me.shedaniel.rei.impl.client.gui.craftable.CraftableFilterCalculator; import me.shedaniel.rei.impl.client.gui.widget.AutoCraftingEvaluator; import me.shedaniel.rei.impl.client.registry.display.DisplayRegistryImpl; import me.shedaniel.rei.impl.client.registry.display.DisplaysHolder; import me.shedaniel.rei.impl.client.util.CrashReportUtils; import me.shedaniel.rei.impl.common.InternalLogger; +import me.shedaniel.rei.impl.common.util.HashedEntryStackWrapper; import me.shedaniel.rei.impl.display.DisplaySpec; -import me.shedaniel.rei.plugin.autocrafting.DefaultCategoryHandler; import net.minecraft.CrashReport; import net.minecraft.ReportedException; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; -import net.minecraft.client.player.LocalPlayer; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.inventory.AbstractContainerMenu; -import net.minecraft.world.item.ItemStack; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.function.Consumer; import java.util.function.Function; -import java.util.stream.Collectors; +import java.util.function.Predicate; @ApiStatus.Internal public class ViewsImpl implements Views { @@ -411,122 +394,12 @@ public Optional> generate(ViewSearchBuilder builder) { }; } - @Override - public Collection> findCraftableEntriesByMaterials() { + public Predicate getCraftableEntriesPredicate() { if (PluginManager.areAnyReloading()) { - return Collections.emptySet(); - } - - AbstractContainerMenu menu = Minecraft.getInstance().player.containerMenu; - Set> craftables = new HashSet<>(); - AbstractContainerScreen containerScreen = REIRuntime.getInstance().getPreviousContainerScreen(); - - for (Map.Entry, List> entry : DisplayRegistry.getInstance().getAll().entrySet()) { - MenuSerializationContext context = createLegacyContext(menu, entry.getKey().cast()); - - List displays = entry.getValue(); - for (Display display : displays) { - try { - TransferHandler.Context transferContext = TransferHandler.Context.create(false, false, containerScreen, display); - boolean successful = matchesLegacyRequirements(menu, context, display); - - if (!successful) { - for (TransferHandler handler : TransferHandlerRegistry.getInstance()) { - if (!(handler instanceof DefaultCategoryHandler) && handler.checkApplicable(transferContext).isSuccessful()) { - if (handler.handle(transferContext).isSuccessful()) { - successful = true; - break; - } - } - } - } - - if (!successful) continue; - - display.getOutputEntries().stream().flatMap(Collection::stream) - .collect(Collectors.toCollection(() -> craftables)); - } catch (Throwable t) { - InternalLogger.getInstance().warn("Error while checking if display is craftable", t); - } - } - } - return craftables; - } - - private static MenuSerializationContext createLegacyContext(AbstractContainerMenu menu, CategoryIdentifier categoryIdentifier) { - class InfoSerializationContext implements MenuSerializationContext { - @Override - public AbstractContainerMenu getMenu() { - return menu; - } - - @Override - public LocalPlayer getPlayerEntity() { - return Minecraft.getInstance().player; - } - - @Override - public CategoryIdentifier getCategoryIdentifier() { - return categoryIdentifier; - } - } - - return new InfoSerializationContext(); - } - - private static boolean matchesLegacyRequirements(AbstractContainerMenu menu, - MenuSerializationContext context, - Display display) { - MenuInfo info = menu != null ? - MenuInfoRegistry.getInstance().getClient(display, context, menu) - : null; - - if (menu != null && info == null) { - return false; - } - - Iterable inputSlots = info != null ? Iterables.concat(info.getInputSlots(context.withDisplay(display)), info.getInventorySlots(context.withDisplay(display))) : Collections.emptySet(); - int slotsCraftable = 0; - boolean containsNonEmpty = false; - List requiredInput = display.getRequiredEntries(); - Long2LongMap invCount = new Long2LongOpenHashMap(info == null ? CraftableFilter.INSTANCE.getInvStacks() : Long2LongMaps.EMPTY_MAP); - for (SlotAccessor inputSlot : inputSlots) { - ItemStack stack = inputSlot.getItemStack(); - - EntryDefinition definition; - try { - definition = VanillaEntryTypes.ITEM.getDefinition(); - } catch (NullPointerException e) { - break; - } - - if (!stack.isEmpty()) { - long hash = definition.hash(null, stack, ComparisonContext.FUZZY); - long newCount = invCount.get(hash) + Math.max(0, stack.getCount()); - invCount.put(hash, newCount); - } - } - - for (EntryIngredient slot : requiredInput) { - if (slot.isEmpty()) { - slotsCraftable++; - continue; - } - for (EntryStack slotPossible : slot) { - if (slotPossible.getType() != VanillaEntryTypes.ITEM) continue; - ItemStack stack = slotPossible.castValue(); - long hashFuzzy = EntryStacks.hashFuzzy(slotPossible); - long availableAmount = invCount.get(hashFuzzy); - if (availableAmount >= stack.getCount()) { - invCount.put(hashFuzzy, availableAmount - stack.getCount()); - containsNonEmpty = true; - slotsCraftable++; - break; - } - } + return Predicates.alwaysTrue(); } - return slotsCraftable == display.getRequiredEntries().size() && containsNonEmpty; + return new CraftableFilterCalculator(); } private static boolean isStackWorkStationOfCategory(CategoryRegistry.CategoryConfiguration category, EntryStack stack) {