|
| 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.client.gui.craftable; |
| 25 | + |
| 26 | +import com.google.common.base.Suppliers; |
| 27 | +import it.unimi.dsi.fastutil.longs.*; |
| 28 | +import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; |
| 29 | +import me.shedaniel.rei.api.client.REIRuntime; |
| 30 | +import me.shedaniel.rei.api.client.registry.display.DisplayRegistry; |
| 31 | +import me.shedaniel.rei.api.client.registry.transfer.TransferHandler; |
| 32 | +import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerMeta; |
| 33 | +import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerRegistry; |
| 34 | +import me.shedaniel.rei.api.common.display.Display; |
| 35 | +import me.shedaniel.rei.api.common.entry.EntryIngredient; |
| 36 | +import me.shedaniel.rei.api.common.entry.EntryStack; |
| 37 | +import me.shedaniel.rei.api.common.entry.comparison.ComparisonContext; |
| 38 | +import me.shedaniel.rei.api.common.entry.type.EntryDefinition; |
| 39 | +import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; |
| 40 | +import me.shedaniel.rei.api.common.util.EntryStacks; |
| 41 | +import me.shedaniel.rei.impl.client.registry.display.DisplayCache; |
| 42 | +import me.shedaniel.rei.impl.client.registry.display.DisplayRegistryImpl; |
| 43 | +import me.shedaniel.rei.impl.common.util.HashedEntryStackWrapper; |
| 44 | +import me.shedaniel.rei.plugin.autocrafting.DefaultCategoryHandler; |
| 45 | +import net.minecraft.world.item.ItemStack; |
| 46 | +import org.jetbrains.annotations.Nullable; |
| 47 | + |
| 48 | +import java.util.Collections; |
| 49 | +import java.util.List; |
| 50 | +import java.util.Set; |
| 51 | +import java.util.function.Predicate; |
| 52 | +import java.util.function.Supplier; |
| 53 | + |
| 54 | +public class CraftableFilterCalculator implements Predicate<HashedEntryStackWrapper> { |
| 55 | + private final Supplier<DisplayCache> displayCache = Suppliers.memoize(() -> ((DisplayRegistryImpl) DisplayRegistry.getInstance()).displaysHolder().cache()); |
| 56 | + private Set<Display> checkedCraftableDisplays = Collections.synchronizedSet(new ReferenceOpenHashSet<>()); |
| 57 | + private Set<Display> checkedUncraftableDisplays = Collections.synchronizedSet(new ReferenceOpenHashSet<>()); |
| 58 | + private LongSet checkedCraftableEntries = LongSets.synchronize(new LongOpenHashSet()); |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean test(HashedEntryStackWrapper wrapper) { |
| 62 | + EntryStack<?> stack = wrapper.unwrap(); |
| 63 | + if (stack.getType() != VanillaEntryTypes.ITEM || stack.isEmpty()) return false; |
| 64 | + if (checkedCraftableEntries.contains(wrapper.hashExact())) return true; |
| 65 | + DisplayCache cache = this.displayCache.get(); |
| 66 | + for (Display display : cache.getAllDisplaysByOutputs(List.of(stack))) { |
| 67 | + if (checkCraftableCachedByResult(display)) return true; |
| 68 | + } |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + private boolean checkCraftableCachedByDisplay(Display display) { |
| 73 | + if (checkedCraftableDisplays.contains(display)) return true; |
| 74 | + if (checkedUncraftableDisplays.contains(display)) return false; |
| 75 | + boolean checkCraftable = checkCraftable(display); |
| 76 | + if (checkCraftable) { |
| 77 | + checkedCraftableDisplays.add(display); |
| 78 | + } else { |
| 79 | + checkedUncraftableDisplays.add(display); |
| 80 | + } |
| 81 | + return checkCraftable; |
| 82 | + } |
| 83 | + |
| 84 | + private boolean checkCraftableCachedByResult(Display display) { |
| 85 | + boolean checkCraftable = checkCraftableCachedByDisplay(display); |
| 86 | + if (checkCraftable) { |
| 87 | + for (EntryIngredient ingredient : display.getOutputEntries()) { |
| 88 | + for (EntryStack<?> stack : ingredient) { |
| 89 | + if (stack.getType() != VanillaEntryTypes.ITEM || stack.isEmpty()) continue; |
| 90 | + checkedCraftableEntries.add(EntryStacks.hashExact(stack)); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return checkCraftable; |
| 95 | + } |
| 96 | + |
| 97 | + private boolean checkCraftable(Display display) { |
| 98 | + @Nullable Long2LongMap ingredients = chooseHandler(display); |
| 99 | + if (ingredients == null) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + List<EntryIngredient> requiredEntries = display.getRequiredEntries(); |
| 104 | + if (requiredEntries.isEmpty()) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + int slotsCraftable = 0; |
| 109 | + boolean containsNonEmpty = false; |
| 110 | + |
| 111 | + for (EntryIngredient slot : requiredEntries) { |
| 112 | + if (slot.isEmpty()) { |
| 113 | + slotsCraftable++; |
| 114 | + continue; |
| 115 | + } |
| 116 | + for (EntryStack<?> slotPossible : slot) { |
| 117 | + if (slotPossible.getType() != VanillaEntryTypes.ITEM) continue; |
| 118 | + ItemStack stack = slotPossible.castValue(); |
| 119 | + long hashFuzzy = EntryStacks.hashFuzzy(slotPossible); |
| 120 | + long availableAmount = ingredients.get(hashFuzzy); |
| 121 | + if (availableAmount >= stack.getCount()) { |
| 122 | + ingredients.put(hashFuzzy, availableAmount - stack.getCount()); |
| 123 | + containsNonEmpty = true; |
| 124 | + slotsCraftable++; |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return slotsCraftable == requiredEntries.size() && containsNonEmpty; |
| 131 | + } |
| 132 | + |
| 133 | + @Nullable |
| 134 | + public Long2LongMap chooseHandler(Display display) { |
| 135 | + TransferHandler.Context transferContext = TransferHandler.Context.create(false, false, REIRuntime.getInstance().getPreviousContainerScreen(), display); |
| 136 | + DefaultCategoryHandler legacyHandler = null; |
| 137 | + for (TransferHandler handler : TransferHandlerRegistry.getInstance()) { |
| 138 | + if (handler instanceof DefaultCategoryHandler) { |
| 139 | + legacyHandler = (DefaultCategoryHandler) handler; |
| 140 | + } else { |
| 141 | + TransferHandler.ApplicabilityResult result = handler.checkApplicable(transferContext); |
| 142 | + if (result.isSuccessful()) { |
| 143 | + if (handler instanceof TransferHandlerMeta) { |
| 144 | + return extractIngredients(((TransferHandlerMeta) handler).getAvailableIngredients(transferContext)); |
| 145 | + } else { |
| 146 | + return CraftableFilter.INSTANCE.getInvStacks(); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if (legacyHandler != null) { |
| 153 | + TransferHandler.ApplicabilityResult result = legacyHandler.checkApplicable(transferContext); |
| 154 | + if (result.isSuccessful()) { |
| 155 | + return CraftableFilter.INSTANCE.getInvStacks(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + private static Long2LongMap extractIngredients(Iterable<ItemStack> ingredients) { |
| 163 | + EntryDefinition<ItemStack> definition = VanillaEntryTypes.ITEM.getDefinition(); |
| 164 | + |
| 165 | + Long2LongMap map = new Long2LongOpenHashMap(); |
| 166 | + for (ItemStack stack : ingredients) { |
| 167 | + if (!stack.isEmpty()) { |
| 168 | + long hash = definition.hash(null, stack, ComparisonContext.FUZZY); |
| 169 | + long newCount = map.getOrDefault(hash, 0) + Math.max(0, stack.getCount()); |
| 170 | + map.put(hash, newCount); |
| 171 | + } |
| 172 | + } |
| 173 | + return map; |
| 174 | + } |
| 175 | +} |
0 commit comments