diff options
| author | shedaniel <daniel@shedaniel.me> | 2021-03-19 19:16:12 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2021-03-19 19:16:12 +0800 |
| commit | d21a84ac2e07fe30685f1d703481425b0f01c93d (patch) | |
| tree | 4089e62da73a4346de41b486603c3316f0201cc8 | |
| parent | 43ae584e12c7ed0755301eb34223f644a717183e (diff) | |
| download | RoughlyEnoughItems-d21a84ac2e07fe30685f1d703481425b0f01c93d.tar.gz RoughlyEnoughItems-d21a84ac2e07fe30685f1d703481425b0f01c93d.tar.bz2 RoughlyEnoughItems-d21a84ac2e07fe30685f1d703481425b0f01c93d.zip | |
Implement ItemComparatorRegistry as a replacement for previous check tags and check amount flags
Signed-off-by: shedaniel <daniel@shedaniel.me>
55 files changed, 649 insertions, 402 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/api/gui/SimpleDisplayRenderer.java b/api/src/main/java/me/shedaniel/rei/api/gui/SimpleDisplayRenderer.java index 50d44339e..404fab43a 100644 --- a/api/src/main/java/me/shedaniel/rei/api/gui/SimpleDisplayRenderer.java +++ b/api/src/main/java/me/shedaniel/rei/api/gui/SimpleDisplayRenderer.java @@ -81,12 +81,12 @@ public class SimpleDisplayRenderer extends DisplayRenderer { public static boolean equalsList(EntryIngredient left, EntryIngredient right) { IntSet leftBytes = new IntOpenHashSet(left.size()); for (EntryStack<?> entryStack : left) { - leftBytes.add(EntryStacks.hashIgnoreCount(entryStack)); + leftBytes.add(EntryStacks.hashExact(entryStack)); } if (leftBytes.size() > right.size()) return false; IntSet rightBytes = new IntOpenHashSet(right.size()); for (EntryStack<?> entryStack : right) { - rightBytes.add(EntryStacks.hashIgnoreCount(entryStack)); + rightBytes.add(EntryStacks.hashExact(entryStack)); if (rightBytes.size() > leftBytes.size()) return false; } @@ -149,5 +149,4 @@ public class SimpleDisplayRenderer extends DisplayRenderer { public int getItemsPerLine() { return Mth.floor((getWidth() - 4f) / 18f); } - } diff --git a/api/src/main/java/me/shedaniel/rei/api/ingredient/EntryStack.java b/api/src/main/java/me/shedaniel/rei/api/ingredient/EntryStack.java index d39bd4dde..d083b425b 100644 --- a/api/src/main/java/me/shedaniel/rei/api/ingredient/EntryStack.java +++ b/api/src/main/java/me/shedaniel/rei/api/ingredient/EntryStack.java @@ -27,15 +27,16 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.mojang.serialization.Dynamic; import com.mojang.serialization.JsonOps; -import me.shedaniel.architectury.platform.Platform; import me.shedaniel.rei.api.gui.Renderer; -import me.shedaniel.rei.api.ingredient.entry.*; -import me.shedaniel.rei.api.ingredient.util.EntryStacks; +import me.shedaniel.rei.api.ingredient.entry.EntrySerializer; +import me.shedaniel.rei.api.ingredient.entry.comparison.ComparisonContext; +import me.shedaniel.rei.api.ingredient.entry.renderer.EntryRenderer; +import me.shedaniel.rei.api.ingredient.entry.type.EntryDefinition; +import me.shedaniel.rei.api.ingredient.entry.type.EntryType; import me.shedaniel.rei.api.util.TextRepresentable; import me.shedaniel.rei.impl.Internals; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.resources.language.I18n; import net.minecraft.nbt.NbtOps; import net.minecraft.nbt.TagParser; import net.minecraft.network.chat.Component; @@ -109,7 +110,8 @@ public interface EntryStack<T> extends TextRepresentable, Renderer { } default EntryRenderer<T> getRenderer() { - return getDefinition().getRenderer(); + EntryRenderer<?> renderer = get(Settings.RENDER).apply(this); + return renderer == null ? EntryRenderer.empty() : renderer.cast(); } Optional<ResourceLocation> getIdentifier(); @@ -151,16 +153,12 @@ public interface EntryStack<T> extends TextRepresentable, Renderer { public static final Supplier<Boolean> TRUE = () -> true; public static final Supplier<Boolean> FALSE = () -> false; - public static final Settings<Supplier<Boolean>> RENDER = new Settings<>(TRUE); - @Deprecated - public static final Settings<Supplier<Boolean>> CHECK_TAGS = new Settings<>(FALSE); - @Deprecated - public static final Settings<Supplier<Boolean>> CHECK_AMOUNT = new Settings<>(FALSE); + public static final Function<EntryStack<?>, EntryRenderer<?>> DEFAULT_RENDERER = stack -> stack.getDefinition().getRenderer(); + public static final Settings<Function<EntryStack<?>, EntryRenderer<?>>> RENDER = new Settings<>(DEFAULT_RENDERER); @Deprecated public static final Settings<Supplier<Boolean>> TOOLTIP_ENABLED = new Settings<>(TRUE); @Deprecated public static final Settings<Supplier<Boolean>> TOOLTIP_APPEND_MOD = new Settings<>(TRUE); - public static final Settings<Supplier<Boolean>> RENDER_COUNTS = new Settings<>(TRUE); @Deprecated public static final Settings<Function<EntryStack<?>, List<Component>>> TOOLTIP_APPEND_EXTRA = new Settings<>(stack -> Collections.emptyList()); @Deprecated @@ -190,16 +188,6 @@ public interface EntryStack<T> extends TextRepresentable, Renderer { public short getId() { return id; } - - public static class Fluid { - private static final String FLUID_AMOUNT = Platform.isForge() ? "tooltip.rei.fluid_amount.forge" : "tooltip.rei.fluid_amount"; - // Return null to disable - public static final Settings<Function<EntryStack<?>, String>> AMOUNT_TOOLTIP = new Settings<>(stack -> I18n.get(FLUID_AMOUNT, EntryStacks.simplifyAmount(stack.cast()).getValue().getAmount())); - - private Fluid() { - } - } - } @ApiStatus.NonExtendable diff --git a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/ComparisonContext.java b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/comparison/ComparisonContext.java index 4b30e44c1..3a958adb5 100644 --- a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/ComparisonContext.java +++ b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/comparison/ComparisonContext.java @@ -21,39 +21,29 @@ * SOFTWARE. */ -package me.shedaniel.rei.api.ingredient.entry; +package me.shedaniel.rei.api.ingredient.entry.comparison; public enum ComparisonContext { /** - * Should only compare the type of the object + * Should only compare the type of the object, normalized stacks may still not be the same. */ - FUZZY(true, true), + FUZZY(false), /** - * Should compare the nbt and the type of the object + * Should compare the nbt and the type of the object, normalized stacks should be exactly the same. */ - IGNORE_COUNT(true, false), - /** - * Should compare the amount and the type of the object - */ - IGNORE_NBT(true, false), - /** - * Should compare the amount, the nbt and the type of the object - */ - EXACT(false, false); + EXACT(true); - boolean ignoresCount; - boolean ignoresNbt; + boolean exact; - ComparisonContext(boolean ignoresCount, boolean ignoresNbt) { - this.ignoresCount = ignoresCount; - this.ignoresNbt = ignoresNbt; + ComparisonContext(boolean exact) { + this.exact = exact; } - public boolean isIgnoresCount() { - return ignoresCount; + public boolean isExact() { + return exact; } - public boolean isIgnoresNbt() { - return ignoresNbt; + public boolean isFuzzy() { + return !exact; } }
\ No newline at end of file diff --git a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/comparison/ItemComparator.java b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/comparison/ItemComparator.java new file mode 100644 index 000000000..03f9511c4 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/comparison/ItemComparator.java @@ -0,0 +1,67 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020 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.ingredient.entry.comparison; + +import me.shedaniel.rei.impl.Internals; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.Tag; +import net.minecraft.world.item.ItemStack; + +import java.util.Objects; +import java.util.function.ToLongFunction; + +/** + * Hasher implementation for {@link ItemStack}. + */ +@FunctionalInterface +public interface ItemComparator { + static ItemComparator noop() { + return stack -> 1; + } + + static ItemComparator itemNbt() { + ToLongFunction<Tag> nbtHasher = nbtHasher("Count"); + return stack -> { + CompoundTag tag = stack.getTag(); + return tag == null ? 0L : nbtHasher.applyAsLong(tag); + }; + } + + static ToLongFunction<Tag> nbtHasher(String... ignoredKeys) { + return Internals.getNbtHasher(ignoredKeys); + } + + long hash(ItemStack stack); + + default ItemComparator then(ItemComparator other) { + Objects.requireNonNull(other); + + return stack -> { + long hash = 1L; + hash = hash * 31 + hash(stack); + hash = hash * 31 + other.hash(stack); + return hash; + }; + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/comparison/ItemComparatorRegistry.java b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/comparison/ItemComparatorRegistry.java new file mode 100644 index 000000000..ea97a57ac --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/comparison/ItemComparatorRegistry.java @@ -0,0 +1,62 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020 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.ingredient.entry.comparison; + +import me.shedaniel.rei.api.plugins.PluginManager; +import me.shedaniel.rei.api.registry.Reloadable; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; + +/** + * Registry for registering custom methods for identifying variants of {@link net.minecraft.world.item.ItemStack}. + * The default comparator is {@link ItemComparator#noop()}, which does not compare the NBT of the items. + * <p> + * This comparator is used when the comparison context is {@link ComparisonContext#EXACT}. + */ +public interface ItemComparatorRegistry extends Reloadable { + /** + * @return the instance of {@link ItemComparatorRegistry} + */ + static ItemComparatorRegistry getInstance() { + return PluginManager.getInstance().get(ItemComparatorRegistry.class); + } + + void register(ItemComparator comparator, Item item); + + default void register(ItemComparator comparator, Item... items) { + for (Item item : items) { + register(comparator, item); + } + } + + default void registerNbt(Item item) { + register(ItemComparator.itemNbt(), item); + } + + default void registerNbt(Item... items) { + register(ItemComparator.itemNbt(), items); + } + + long hashOf(ItemStack stack); +} diff --git a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/AbstractEntryRenderer.java b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/renderer/AbstractEntryRenderer.java index bbc52436b..c868b44dc 100644 --- a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/AbstractEntryRenderer.java +++ b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/renderer/AbstractEntryRenderer.java @@ -21,7 +21,7 @@ * SOFTWARE. */ -package me.shedaniel.rei.api.ingredient.entry; +package me.shedaniel.rei.api.ingredient.entry.renderer; import net.minecraft.client.gui.GuiComponent; diff --git a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/BatchEntryRenderer.java b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/renderer/BatchEntryRenderer.java index 16660f336..343a2d4ac 100644 --- a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/BatchEntryRenderer.java +++ b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/renderer/BatchEntryRenderer.java @@ -21,7 +21,7 @@ * SOFTWARE. */ -package me.shedaniel.rei.api.ingredient.entry; +package me.shedaniel.rei.api.ingredient.entry.renderer; import com.mojang.blaze3d.vertex.PoseStack; import me.shedaniel.math.Rectangle; diff --git a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/EntryRenderer.java b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/renderer/EntryRenderer.java index 3ff850bb3..0ab20098d 100644 --- a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/EntryRenderer.java +++ b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/renderer/EntryRenderer.java @@ -21,18 +21,29 @@ * SOFTWARE. */ -package me.shedaniel.rei.api.ingredient.entry; +package me.shedaniel.rei.api.ingredient.entry.renderer; import com.mojang.blaze3d.vertex.PoseStack; import me.shedaniel.math.Point; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.gui.widgets.Tooltip; import me.shedaniel.rei.api.ingredient.EntryStack; +import me.shedaniel.rei.impl.Internals; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; public interface EntryRenderer<T> { + static <T> EntryRenderer<T> empty() { + return Internals.getEmptyEntryRenderer(); + } + void render(EntryStack<T> entry, PoseStack matrices, Rectangle bounds, int mouseX, int mouseY, float delta); @Nullable Tooltip getTooltip(EntryStack<T> entry, Point mouse); + + @ApiStatus.NonExtendable + default <O> EntryRenderer<O> cast() { + return (EntryRenderer<O>) this; + } }
\ No newline at end of file diff --git a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/BuiltinEntryTypes.java b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/type/BuiltinEntryTypes.java index 935418904..72d535846 100644 --- a/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/BuiltinEntryTypes.java +++ b/api/src/main/java/me/shedaniel/rei/api/ingredient/entry/type/BuiltinEntryTypes.java @@ -21,7 +21,7 @@ * SOFTWARE. */ -package me.shedaniel.rei.api.ingredient.entry; +package me.shedaniel.rei.api.ingredient.entry.type; import me.shedaniel.rei.api.gui.Renderer; import me.shedaniel.rei.impl.Internals; @@ -33,6 +33,7 @@ import org.jetbrains.annotations.ApiStatus; public interface BuiltinEntryTypes { ResourceLocation EMPTY_ID = new ResourceLocation("empty"); ResourceLocation RENDERING_ID = new ResourceLocation("rendering"); + EntryType<Unit> EMPTY = Internals.getEntryStackProvider().emptyType(EMPTY_ID); |
