diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-04-13 16:35:59 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-04-13 16:37:03 +0800 |
| commit | 79b8d8d0d9774a6f23579cb5452b44b61437a305 (patch) | |
| tree | c30a2cd881575da479845b8f91ae20cc1f5c5281 /runtime/src/main/java | |
| parent | 7353154ff0e24606e864147c034acd29056e2161 (diff) | |
| download | RoughlyEnoughItems-79b8d8d0d9774a6f23579cb5452b44b61437a305.tar.gz RoughlyEnoughItems-79b8d8d0d9774a6f23579cb5452b44b61437a305.tar.bz2 RoughlyEnoughItems-79b8d8d0d9774a6f23579cb5452b44b61437a305.zip | |
Make craftable filter check count
Diffstat (limited to 'runtime/src/main/java')
4 files changed, 45 insertions, 25 deletions
diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/ClientHelperImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/ClientHelperImpl.java index f3b18ba39..8f29b8989 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/ClientHelperImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/ClientHelperImpl.java @@ -27,8 +27,9 @@ import com.google.common.base.Suppliers; import dev.architectury.networking.NetworkManager; import dev.architectury.platform.Platform; import io.netty.buffer.Unpooled; -import it.unimi.dsi.fastutil.longs.LongOpenHashSet; -import it.unimi.dsi.fastutil.longs.LongSet; +import it.unimi.dsi.fastutil.longs.Long2LongMap; +import it.unimi.dsi.fastutil.longs.Long2LongMaps; +import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; import me.shedaniel.rei.RoughlyEnoughItemsNetwork; import me.shedaniel.rei.api.client.ClientHelper; import me.shedaniel.rei.api.client.config.ConfigManager; @@ -40,6 +41,8 @@ import me.shedaniel.rei.api.client.registry.display.DisplayCategory; import me.shedaniel.rei.api.client.view.ViewSearchBuilder; import me.shedaniel.rei.api.common.category.CategoryIdentifier; 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.api.common.util.FormattingUtils; @@ -54,6 +57,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; import net.minecraft.client.gui.screens.inventory.CreativeModeInventoryScreen; +import net.minecraft.core.NonNullList; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.resources.ResourceLocation; @@ -221,17 +225,22 @@ public class ClientHelperImpl implements ClientHelper { } @ApiStatus.Internal - public LongSet _getInventoryItemsTypes() { + public Long2LongMap _getInventoryItemsTypes() { + EntryDefinition<ItemStack> definition; try { - VanillaEntryTypes.ITEM.getDefinition(); + definition = VanillaEntryTypes.ITEM.getDefinition(); } catch (NullPointerException e) { - return new LongOpenHashSet(); + return Long2LongMaps.EMPTY_MAP; + } + Long2LongOpenHashMap map = new Long2LongOpenHashMap(); + for (NonNullList<ItemStack> compartment : Minecraft.getInstance().player.getInventory().compartments) { + for (ItemStack stack : compartment) { + long hash = definition.hash(null, stack, ComparisonContext.FUZZY); + long newCount = map.getOrDefault(hash, 0) + Math.max(0, stack.getCount()); + map.put(hash, newCount); + } } - return Minecraft.getInstance().player.getInventory().compartments.stream() - .flatMap(Collection::stream) - .map(EntryStacks::of) - .mapToLong(EntryStacks::hashFuzzy) - .collect(LongOpenHashSet::new, LongOpenHashSet::add, LongOpenHashSet::addAll); + return map; } @ApiStatus.Internal diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java index a34423cb0..73a5bc946 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java @@ -138,7 +138,7 @@ public class ScreenOverlayImpl extends ScreenOverlay { public void tick() { if (REIRuntimeImpl.getSearchField() != null) { REIRuntimeImpl.getSearchField().tick(); - if (Minecraft.getInstance().player != null && !PluginManager.areAnyReloading()) { + if (Minecraft.getInstance().player != null && !PluginManager.areAnyReloading() && Minecraft.getInstance().player.tickCount % 20 == 0) { CraftableFilter.INSTANCE.tick(); } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/craftable/CraftableFilter.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/craftable/CraftableFilter.java index 59c23cca0..3912829d1 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/craftable/CraftableFilter.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/craftable/CraftableFilter.java @@ -23,20 +23,19 @@ package me.shedaniel.rei.impl.client.gui.craftable; -import it.unimi.dsi.fastutil.longs.LongOpenHashSet; -import it.unimi.dsi.fastutil.longs.LongSet; -import it.unimi.dsi.fastutil.longs.LongSets; +import it.unimi.dsi.fastutil.longs.Long2LongMap; +import it.unimi.dsi.fastutil.longs.Long2LongMaps; +import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor; import me.shedaniel.rei.api.common.util.EntryStacks; import me.shedaniel.rei.impl.client.ClientHelperImpl; -import net.minecraft.client.Minecraft; public class CraftableFilter { public static final CraftableFilter INSTANCE = new CraftableFilter(); private boolean dirty = false; - private LongSet invStacks = new LongOpenHashSet(); + private Long2LongMap invStacks = new Long2LongOpenHashMap(); public void markDirty() { dirty = true; @@ -53,28 +52,28 @@ public class CraftableFilter { public void tick() { if (dirty) return; - LongSet currentStacks; + Long2LongMap currentStacks; try { currentStacks = ClientHelperImpl.getInstance()._getInventoryItemsTypes(); } catch (Throwable throwable) { throwable.printStackTrace(); - currentStacks = LongSets.EMPTY_SET; + currentStacks = Long2LongMaps.EMPTY_MAP; } if (!currentStacks.equals(this.invStacks)) { - invStacks = new LongOpenHashSet(currentStacks); + invStacks = currentStacks; markDirty(); } } - public boolean matches(EntryStack<?> stack, Iterable<SlotAccessor> inputSlots) { - if (invStacks.contains(EntryStacks.hashFuzzy(stack))) return true; - if (stack.getType() != VanillaEntryTypes.ITEM) return false; + public int matches(EntryStack<?> stack, Iterable<SlotAccessor> inputSlots, long currentUsed) { + long availableAmount = invStacks.get(EntryStacks.hashFuzzy(stack)); + if (availableAmount > currentUsed) return 1; for (SlotAccessor slot : inputSlots) { EntryStack<?> itemStack = EntryStacks.of(slot.getItemStack()); if (!itemStack.isEmpty() && EntryStacks.equalsFuzzy(itemStack, stack)) { - return true; + return 2; } } - return false; + return 0; } } 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 4b2ce42e1..857f3aaea 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 @@ -27,6 +27,8 @@ import com.google.common.base.Stopwatch; import com.google.common.collect.Iterables; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import it.unimi.dsi.fastutil.longs.Long2LongMap; +import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; import me.shedaniel.rei.RoughlyEnoughItemsCore; import me.shedaniel.rei.api.client.config.ConfigObject; import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; @@ -40,6 +42,7 @@ import me.shedaniel.rei.api.common.display.Display; 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.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; @@ -374,13 +377,22 @@ public class ViewsImpl implements Views { Iterable<SlotAccessor> inputSlots = info != null ? info.getInputSlots(context.withDisplay(display)) : Collections.emptySet(); int slotsCraftable = 0; List<EntryIngredient> requiredInput = display.getRequiredEntries(); + Long2LongMap usedCount = new Long2LongOpenHashMap(); for (EntryIngredient slot : requiredInput) { if (slot.isEmpty()) { slotsCraftable++; continue; } for (EntryStack<?> slotPossible : slot) { - if (CraftableFilter.INSTANCE.matches(slotPossible, inputSlots)) { + if (slotPossible.getType() != VanillaEntryTypes.ITEM) continue; + long hashFuzzy = EntryStacks.hashFuzzy(slotPossible); + long currentUsed = usedCount.get(hashFuzzy); + int matches = CraftableFilter.INSTANCE.matches(slotPossible, inputSlots, currentUsed); + if (matches != 0) { + if (matches == 1) { + usedCount.put(hashFuzzy, currentUsed + 1); + } + slotsCraftable++; break; } |
