From b657842bcddcb65fb658d4cd9835e7fa15e1c236 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sun, 16 May 2021 17:45:22 +0800 Subject: Update JEI API to 7.6.4 & Fix #520 --- .../me/shedaniel/rei/RoughlyEnoughItemsCore.java | 10 ++- .../comparison/EntryComparatorRegistryImpl.java | 78 ++++++++++++++++++++++ .../comparison/FluidComparatorRegistryImpl.java | 41 ++++++++++++ .../comparison/ItemComparatorRegistryImpl.java | 46 +------------ .../plugin/client/entry/FluidEntryDefinition.java | 14 +--- 5 files changed, 133 insertions(+), 56 deletions(-) create mode 100644 runtime/src/main/java/me/shedaniel/rei/impl/common/entry/comparison/EntryComparatorRegistryImpl.java create mode 100644 runtime/src/main/java/me/shedaniel/rei/impl/common/entry/comparison/FluidComparatorRegistryImpl.java (limited to 'runtime') diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java index 8112b7630..312957985 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java @@ -52,6 +52,8 @@ import me.shedaniel.rei.api.client.registry.screen.OverlayDecider; import me.shedaniel.rei.api.client.registry.screen.ScreenRegistry; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.comparison.EntryComparatorRegistry; +import me.shedaniel.rei.api.common.entry.comparison.FluidComparatorRegistry; import me.shedaniel.rei.api.common.entry.comparison.ItemComparatorRegistry; import me.shedaniel.rei.api.common.entry.type.BuiltinEntryTypes; import me.shedaniel.rei.api.common.entry.type.EntryDefinition; @@ -85,6 +87,8 @@ import me.shedaniel.rei.impl.common.display.DisplaySerializerRegistryImpl; import me.shedaniel.rei.impl.common.entry.EmptyEntryStack; import me.shedaniel.rei.impl.common.entry.EntryIngredientImpl; import me.shedaniel.rei.impl.common.entry.TypedEntryStack; +import me.shedaniel.rei.impl.common.entry.comparison.EntryComparatorRegistryImpl; +import me.shedaniel.rei.impl.common.entry.comparison.FluidComparatorRegistryImpl; import me.shedaniel.rei.impl.common.entry.comparison.ItemComparatorRegistryImpl; import me.shedaniel.rei.impl.common.entry.comparison.NbtHasherProviderImpl; import me.shedaniel.rei.impl.common.entry.type.EntryRegistryImpl; @@ -234,10 +238,11 @@ public class RoughlyEnoughItemsCore { REIPlugin.class, UnaryOperator.identity(), usedTime -> { - RoughlyEnoughItemsCore.LOGGER.info("Reloaded Plugin Manager [%s] with %d entry types, %d item comparators and %d fluid support providers in %dms.", + RoughlyEnoughItemsCore.LOGGER.info("Reloaded Plugin Manager [%s] with %d entry types, %d item comparators, %d fluid comparators and %d fluid support providers in %dms.", REIPlugin.class.getSimpleName(), EntryTypeRegistry.getInstance().values().size(), ItemComparatorRegistry.getInstance().comparatorSize(), + FluidComparatorRegistry.getInstance().comparatorSize(), FluidSupportProvider.getInstance().size(), usedTime ); @@ -245,6 +250,7 @@ public class RoughlyEnoughItemsCore { new EntryTypeRegistryImpl(), new RecipeManagerContextImpl<>(RecipeManagerContextImpl.supplier()), new ItemComparatorRegistryImpl(), + new FluidComparatorRegistryImpl(), new DisplaySerializerRegistryImpl(), new FluidSupportProviderImpl()), "commonPluginManager"); Internals.attachInstanceSupplier(new PluginManagerImpl<>( @@ -381,10 +387,10 @@ public class RoughlyEnoughItemsCore { new ViewsImpl(), new SearchProviderImpl(), new ConfigManagerImpl(), + new EntryRegistryImpl(), new CategoryRegistryImpl(), new DisplayRegistryImpl(), new ScreenRegistryImpl(), - new EntryRegistryImpl(), new FavoriteEntryTypeRegistryImpl(), new SubsetsRegistryImpl(), new TransferHandlerRegistryImpl(), diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/comparison/EntryComparatorRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/comparison/EntryComparatorRegistryImpl.java new file mode 100644 index 000000000..1304e7c5d --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/comparison/EntryComparatorRegistryImpl.java @@ -0,0 +1,78 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 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.common.entry.comparison; + +import me.shedaniel.rei.api.common.entry.comparison.ComparisonContext; +import me.shedaniel.rei.api.common.entry.comparison.EntryComparator; +import me.shedaniel.rei.api.common.entry.comparison.EntryComparatorRegistry; +import me.shedaniel.rei.api.common.plugins.REIPlugin; +import net.minecraft.core.Registry; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.ApiStatus; + +import java.util.IdentityHashMap; +import java.util.Map; + +@ApiStatus.Internal +public abstract class EntryComparatorRegistryImpl implements EntryComparatorRegistry { + private static final Logger LOGGER = LogManager.getLogger(EntryComparatorRegistryImpl.class); + private final Map> comparators = new IdentityHashMap<>(); + + @Override + public void register(EntryComparator comparator, S entry) { + EntryComparator put = this.comparators.put(entry, comparator); + if (put != null) { + LOGGER.warn("[REI] Overriding " + put + "entry comparator with " + comparator + "for " + entry + "! This may result in unwanted comparisons!"); + } + } + + @Override + public void startReload() { + comparators.clear(); + } + + public abstract S getEntry(T stack); + + @Override + public long hashOf(ComparisonContext context, T stack) { + EntryComparator comparator = comparators.get(getEntry(stack)); + if (comparator != null) { + return comparator.hash(context, stack); + } + return 1; + } + + @Override + public boolean containsComparator(S item) { + return comparators.containsKey(item); + } + + @Override + public int comparatorSize() { + return this.comparators.size(); + } +} diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/comparison/FluidComparatorRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/comparison/FluidComparatorRegistryImpl.java new file mode 100644 index 000000000..67cceb6dd --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/comparison/FluidComparatorRegistryImpl.java @@ -0,0 +1,41 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 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.common.entry.comparison; + +import me.shedaniel.architectury.fluid.FluidStack; +import me.shedaniel.rei.api.common.entry.comparison.FluidComparatorRegistry; +import me.shedaniel.rei.api.common.plugins.REIPlugin; +import net.minecraft.world.level.material.Fluid; + +public class FluidComparatorRegistryImpl extends EntryComparatorRegistryImpl implements FluidComparatorRegistry { + @Override + public Fluid getEntry(FluidStack stack) { + return stack.getFluid(); + } + + @Override + public void acceptPlugin(REIPlugin plugin) { + plugin.registerFluidComparators(this); + } +} diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/comparison/ItemComparatorRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/comparison/ItemComparatorRegistryImpl.java index ed3eb1fba..e1c7b7a3b 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/comparison/ItemComparatorRegistryImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/comparison/ItemComparatorRegistryImpl.java @@ -23,59 +23,19 @@ package me.shedaniel.rei.impl.common.entry.comparison; -import me.shedaniel.rei.api.common.entry.comparison.ComparisonContext; -import me.shedaniel.rei.api.common.entry.comparison.ItemComparator; import me.shedaniel.rei.api.common.entry.comparison.ItemComparatorRegistry; import me.shedaniel.rei.api.common.plugins.REIPlugin; -import net.minecraft.core.Registry; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.jetbrains.annotations.ApiStatus; -import java.util.IdentityHashMap; -import java.util.Map; - -@ApiStatus.Internal -public class ItemComparatorRegistryImpl implements ItemComparatorRegistry { - private static final Logger LOGGER = LogManager.getLogger(ItemComparatorRegistryImpl.class); - private final Map comparators = new IdentityHashMap<>(); - +public class ItemComparatorRegistryImpl extends EntryComparatorRegistryImpl implements ItemComparatorRegistry { @Override - public void register(ItemComparator comparator, Item item) { - ItemComparator put = this.comparators.put(item, comparator); - if (put != null) { - LOGGER.warn("[REI] Overriding " + put + "item comparator with " + comparator + "for " + Registry.ITEM.getKey(item) + "! This may result in unwanted comparisons!"); - } - } - - @Override - public void startReload() { - comparators.clear(); + public Item getEntry(ItemStack stack) { + return stack.getItem(); } @Override public void acceptPlugin(REIPlugin plugin) { plugin.registerItemComparators(this); } - - @Override - public long hashOf(ComparisonContext context, ItemStack stack) { - ItemComparator comparator = comparators.get(stack.getItem()); - if (comparator != null) { - return comparator.hash(context, stack); - } - return 1; - } - - @Override - public boolean containsComparator(Item item) { - return comparators.containsKey(item); - } - - @Override - public int comparatorSize() { - return this.comparators.size(); - } } diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java b/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java index 148eba7b4..c42ac3341 100644 --- a/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java +++ b/runtime/src/main/java/me/shedaniel/rei/plugin/client/entry/FluidEntryDefinition.java @@ -40,10 +40,10 @@ import me.shedaniel.rei.api.client.util.SpriteRenderer; import me.shedaniel.rei.api.common.entry.EntrySerializer; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.entry.comparison.ComparisonContext; +import me.shedaniel.rei.api.common.entry.comparison.FluidComparatorRegistry; import me.shedaniel.rei.api.common.entry.type.EntryDefinition; import me.shedaniel.rei.api.common.entry.type.EntryType; import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; -import me.shedaniel.rei.api.common.util.EntryStacks; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.ChatFormatting; @@ -62,7 +62,6 @@ import net.minecraft.tags.TagCollection; import net.minecraft.tags.TagContainer; import net.minecraft.util.Mth; import net.minecraft.world.inventory.InventoryMenu; -import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.material.FlowingFluid; import net.minecraft.world.level.material.Fluid; import org.jetbrains.annotations.Nullable; @@ -70,7 +69,6 @@ import org.jetbrains.annotations.Nullable; import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -128,7 +126,7 @@ public class FluidEntryDefinition implements EntryDefinition, EntryS public long hash(EntryStack entry, FluidStack value, ComparisonContext context) { int code = 1; code = 31 * code + value.getFluid().hashCode(); - code = 31 * code + (context.isFuzzy() || !value.hasTag() ? 0 : value.getTag().hashCode()); + code = 31 * code + Long.hashCode(FluidComparatorRegistry.getInstance().hashOf(context, value)); return code; } @@ -136,13 +134,7 @@ public class FluidEntryDefinition implements EntryDefinition, EntryS public boolean equals(FluidStack o1, FluidStack o2, ComparisonContext context) { if (o1.getFluid() != o2.getFluid()) return false; - return context.isFuzzy() || isTagEqual(o1, o2); - } - - private boolean isTagEqual(FluidStack o1, FluidStack o2) { - CompoundTag tag1 = o1.getTag(); - CompoundTag tag2 = o2.getTag(); - return Objects.equals(tag1, tag2); + return FluidComparatorRegistry.getInstance().hashOf(context, o1) == FluidComparatorRegistry.getInstance().hashOf(context, o2); } @Override -- cgit