diff options
| author | shedaniel <daniel@shedaniel.me> | 2021-06-30 22:34:51 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2021-06-30 22:34:51 +0800 |
| commit | 828247bd6266556bbdd3c1ba41a2d0909c9f68b8 (patch) | |
| tree | 1aae621724d18791070f3c9e271c9795569e7865 | |
| parent | 4cbdc5d77a1406ef6d8b350ddedce987cbed3647 (diff) | |
| download | RoughlyEnoughItems-828247bd6266556bbdd3c1ba41a2d0909c9f68b8.tar.gz RoughlyEnoughItems-828247bd6266556bbdd3c1ba41a2d0909c9f68b8.tar.bz2 RoughlyEnoughItems-828247bd6266556bbdd3c1ba41a2d0909c9f68b8.zip | |
Improve ComparisonContext matching
10 files changed, 59 insertions, 26 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/SimpleDisplayRenderer.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/SimpleDisplayRenderer.java index 95fc9ef6b..246a32979 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/SimpleDisplayRenderer.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/SimpleDisplayRenderer.java @@ -51,7 +51,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; public class SimpleDisplayRenderer extends DisplayRenderer implements WidgetHolder { - protected static final Comparator<EntryStack<?>> ENTRY_COMPARATOR = Comparator.comparingLong(EntryStacks::hashExact); protected static final ResourceLocation CHEST_GUI_TEXTURE = new ResourceLocation("roughlyenoughitems", "textures/gui/recipecontainer.png"); protected List<Slot> inputWidgets; protected List<Slot> outputWidgets; diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/entry/EntryRegistry.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/entry/EntryRegistry.java index 6c439a43c..b4d206910 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/registry/entry/EntryRegistry.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/entry/EntryRegistry.java @@ -183,4 +183,14 @@ public interface EntryRegistry extends Reloadable<REIClientPlugin> { * @return whether it was successful to remove any entry */ boolean removeEntryExactHashIf(LongPredicate predicate); + + /** + * Removes entries from the entry list, if it matches the predicate. + * This method is usually faster than {@link #removeEntryIf(Predicate)} + * due to its fast comparison. + * + * @param predicate a predicate which returns {@code true} for the entries to be removed + * @return whether it was successful to remove any entry + */ + boolean removeEntryFuzzyHashIf(LongPredicate predicate); } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/entry/comparison/ComparisonContext.java b/api/src/main/java/me/shedaniel/rei/api/common/entry/comparison/ComparisonContext.java index 8e2917dbc..1094a88e3 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/entry/comparison/ComparisonContext.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/comparison/ComparisonContext.java @@ -25,11 +25,21 @@ package me.shedaniel.rei.api.common.entry.comparison; public enum ComparisonContext { /** - * Should only compare the type of the object, normalized stacks may still not be the same. + * Should only compare the type of the object. + * <p> + * The fuzzy context type denotes that the equivalent stacks should be <b>primarily</b> the same. + * <p> + * For example, enchantment books of different enchantments should be different within this context, + * while tools with different damage values and different enchantments should be treated as the same within this context. + * Skulker boxes with different content should be different within this context. */ FUZZY(false), /** - * Should compare the nbt and the type of the object, normalized stacks should be exactly the same. + * Should compare the nbt and the type of the object. + * <p> + * The exact context type denotes that the equivalent stacks should be <b>functionally</b> the same. + * <p> + * For example, tools with different damage values and different enchantments should be treated as different within this context. */ EXACT(true); diff --git a/api/src/main/java/me/shedaniel/rei/api/common/entry/comparison/EntryComparatorRegistry.java b/api/src/main/java/me/shedaniel/rei/api/common/entry/comparison/EntryComparatorRegistry.java index 7c051bac1..fecdabacf 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/entry/comparison/EntryComparatorRegistry.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/comparison/EntryComparatorRegistry.java @@ -30,9 +30,6 @@ import me.shedaniel.rei.api.common.registry.Reloadable; * Registry for registering custom methods for identifying variants of {@link T}. * The default comparator is {@link EntryComparator#noop()} when fuzzy, which does not compare the NBT of the entries; * and nbt when exact. - * - * <p> - * This comparator is used when the comparison context is {@link ComparisonContext#EXACT}. */ public interface EntryComparatorRegistry<T, S> extends Reloadable<REIPlugin<?>> { void register(EntryComparator<T> comparator, S entry); diff --git a/api/src/main/java/me/shedaniel/rei/api/common/util/EntryStacks.java b/api/src/main/java/me/shedaniel/rei/api/common/util/EntryStacks.java index 10c3aaa6d..91805a6ed 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/util/EntryStacks.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/util/EntryStacks.java @@ -114,10 +114,11 @@ public final class EntryStacks { } /** - * Compares equality for the {@link ComparisonContext#EXACT} context, stacks that equal should share the same normalized stack. + * Compares equality for the {@link ComparisonContext#EXACT} context. * <p> - * For example, enchantment books of different enchantments will not be equal under this context. - * However, difference between the amount of objects in a stack will not affect the result. + * The exact context type denotes that the equivalent stacks should be <b>functionally</b> the same. + * <p> + * For example, tools with different damage values and different enchantments should be treated as different within this context. * * @param left the first stack to compare * @param right the second stack to compare @@ -128,10 +129,13 @@ public final class EntryStacks { } /** - * Compares equality for the {@link ComparisonContext#FUZZY} context, stacks that equal may not share the same normalized stack. - * This result is less specific, mainly used for fuzzy matching between different stacks. + * Compares equality for the {@link ComparisonContext#FUZZY} context. + * <p> + * The fuzzy context type denotes that the equivalent stacks should be <b>primarily</b> the same. * <p> - * For example, enchantment books of different enchantments should still be equal under this context. + * For example, enchantment books of different enchantments should be different within this context, + * while tools with different damage values and different enchantments should be treated as the same within this context. + * Skulker boxes with different content should be different within this context. * * @param left the first stack to compare * @param right the second stack to compare @@ -154,10 +158,11 @@ public final class EntryStacks { } /** - * Hash Code of the {@link ComparisonContext#EXACT} context, stacks with the same hash code should share the same normalized stack. + * Hash Code of the {@link ComparisonContext#EXACT} context. * <p> - * For example, enchantment books of different enchantments will not receive the same hash code under this context. - * However, difference between the amount of objects in a stack will not affect the hash code. + * The exact context type denotes that the equivalent stacks should be <b>functionally</b> the same. + * <p> + * For example, tools with different damage values and different enchantments should be treated as different within this context. * * @param stack the stack to hash code * @param <T> the type of the stack @@ -169,10 +174,13 @@ public final class EntryStacks { } /** - * Hash Code of the {@link ComparisonContext#FUZZY} context, stacks with the same hash code may not share the same normalized stack. - * This hash is less specific, mainly used for fuzzy matching between different stacks. + * Hash Code of the {@link ComparisonContext#FUZZY} context. + * <p> + * The fuzzy context type denotes that the equivalent stacks should be <b>primarily</b> the same. * <p> - * For example, enchantment books of different enchantments should still receive the same hash code under this context. + * For example, enchantment books of different enchantments should be different within this context, + * while tools with different damage values and different enchantments should be treated as the same within this context. + * Skulker boxes with different content should be different within this context. * * @param stack the stack to hash code * @param <T> the type of the stack 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 cc834728e..9932e2bcc 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 @@ -235,7 +235,7 @@ public class ClientHelperImpl implements ClientHelper { return Minecraft.getInstance().player.getInventory().compartments.stream() .flatMap(Collection::stream) .map(EntryStacks::of) - .mapToLong(EntryStacks::hashExact) + .mapToLong(EntryStacks::hashFuzzy) .collect(LongOpenHashSet::new, LongOpenHashSet::add, LongOpenHashSet::addAll); } 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 7e4e90178..0bd0765d8 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 @@ -67,11 +67,11 @@ public class CraftableFilter { } public boolean matches(EntryStack<?> stack, Iterable<SlotAccessor> inputSlots) { - if (invStacks.contains(EntryStacks.hashExact(stack))) return true; + if (invStacks.contains(EntryStacks.hashFuzzy(stack))) return true; if (stack.getType() != VanillaEntryTypes.ITEM) return false; for (SlotAccessor slot : inputSlots) { EntryStack<?> itemStack = EntryStacks.of(slot.getItemStack()); - if (!itemStack.isEmpty() && EntryStacks.equalsExact(itemStack, stack)) { + if (!itemStack.isEmpty() && EntryStacks.equalsFuzzy(itemStack, stack)) { return true; } } 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 12ba2ab3d..a75e75698 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 @@ -89,7 +89,7 @@ public class ViewsImpl implements Views { for (List<? extends EntryStack<?>> results : display.getOutputEntries()) { for (EntryStack<?> otherEntry : results) { for (EntryStack<?> stack : recipesFor) { - if (EntryStacks.equalsExact(otherEntry, stack)) { + if (EntryStacks.equalsFuzzy(otherEntry, stack)) { set.add(display); break back; } @@ -102,7 +102,7 @@ public class ViewsImpl implements Views { for (List<? extends EntryStack<?>> input : display.getInputEntries()) { for (EntryStack<?> otherEntry : input) { for (EntryStack<?> stack : usagesFor) { - if (EntryStacks.equalsExact(otherEntry, stack)) { + if (EntryStacks.equalsFuzzy(otherEntry, stack)) { set.add(display); break back; } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java index a36d386e6..07c0e618c 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java @@ -218,4 +218,13 @@ public class EntryRegistryImpl implements EntryRegistry { return entries.removeIf(stack -> predicate.test(EntryStacks.hashExact(stack))); } } + + @Override + public boolean removeEntryFuzzyHashIf(LongPredicate predicate) { + if (reloading) { + return reloadingRegistry.removeIf(wrapper -> predicate.test(EntryStacks.hashFuzzy(wrapper.unwrap()))); + } else { + return entries.removeIf(stack -> predicate.test(EntryStacks.hashFuzzy(stack))); + } + } } diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientRuntimePlugin.java b/runtime/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientRuntimePlugin.java index a87cfc722..5334fbea5 100644 --- a/runtime/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientRuntimePlugin.java +++ b/runtime/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientRuntimePlugin.java @@ -192,11 +192,11 @@ public class DefaultClientRuntimePlugin implements REIClientPlugin { private static class EntryStackFavoriteEntry extends FavoriteEntry { private static final Function<EntryStack<?>, String> CANCEL_FLUID_AMOUNT = s -> null; private final EntryStack<?> stack; - private final long hashIgnoreAmount; + private final long hash; public EntryStackFavoriteEntry(EntryStack<?> stack) { this.stack = stack.normalize(); - this.hashIgnoreAmount = EntryStacks.hashExact(this.stack); + this.hash = EntryStacks.hashExact(this.stack); } @Override @@ -230,7 +230,7 @@ public class DefaultClientRuntimePlugin implements REIClientPlugin { @Override public long hashIgnoreAmount() { - return hashIgnoreAmount; + return hash; } @Override |
