From 8fe6355975d2e3403555f67f5b0917401b5bd559 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sun, 12 Dec 2021 23:20:43 +0800 Subject: Add entry settings adapter --- .../shedaniel/rei/api/common/entry/EntryStack.java | 6 +++ .../entry/settings/EntrySettingsAdapter.java | 56 ++++++++++++++++++++++ .../settings/EntrySettingsAdapterRegistry.java | 21 ++++++++ .../rei/api/common/entry/type/EntryDefinition.java | 22 ++++++++- .../rei/api/common/plugins/REIPlugin.java | 11 +++++ 5 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/common/entry/settings/EntrySettingsAdapter.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/common/entry/settings/EntrySettingsAdapterRegistry.java (limited to 'api/src/main/java') diff --git a/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java b/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java index 6ce45d45e..fd20f33a7 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java @@ -121,6 +121,9 @@ public interface EntryStack extends TextRepresentable, Renderer { @Nullable ResourceLocation getIdentifier(); + @Nullable + String getContainingNamespace(); + boolean isEmpty(); /** @@ -218,6 +221,8 @@ public interface EntryStack extends TextRepresentable, Renderer { @Environment(EnvType.CLIENT) public static Settings, Tooltip, Tooltip>> TOOLTIP_PROCESSOR; @Environment(EnvType.CLIENT) + public static Settings, String, String>> CONTAINING_NS; + @Environment(EnvType.CLIENT) public static Settings, List>> TOOLTIP_APPEND_EXTRA; @Environment(EnvType.CLIENT) public static Settings FLUID_RENDER_RATIO; @@ -226,6 +231,7 @@ public interface EntryStack extends TextRepresentable, Renderer { EnvExecutor.runInEnv(Env.CLIENT, () -> () -> { RENDERER = new Settings<>(stack -> EntryRendererRegistry.getInstance().get(stack)); TOOLTIP_PROCESSOR = new Settings<>((stack, tooltip) -> tooltip); + CONTAINING_NS = new Settings<>((stack, ns) -> ns); TOOLTIP_APPEND_EXTRA = new Settings<>(stack -> Collections.emptyList()); FLUID_RENDER_RATIO = new Settings<>(1.0F); }); diff --git a/api/src/main/java/me/shedaniel/rei/api/common/entry/settings/EntrySettingsAdapter.java b/api/src/main/java/me/shedaniel/rei/api/common/entry/settings/EntrySettingsAdapter.java new file mode 100644 index 000000000..b802b033b --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/settings/EntrySettingsAdapter.java @@ -0,0 +1,56 @@ +/* + * 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.api.common.entry.settings; + +import me.shedaniel.rei.api.common.entry.EntryStack; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +/** + * A functional interface to adapt {@link EntryStack.Settings} for {@link EntryStack}. + * + * @param the entry type + * @param the setting type + * @see EntryStack.Settings + */ +@FunctionalInterface +@ApiStatus.Experimental +public interface EntrySettingsAdapter { + static EntrySettingsAdapter empty() { + return (entry, settings, value) -> value; + } + + /** + * Modifies the settings of the entry stack. + * + * @param entry the entry stack, do not store or cache this + * @param settings the settings instance + * @param value the value to adapt, could be {@code null} + * @return the adapted value, could be {@code null} + */ + @Nullable + S provide(EntryStack entry, EntryStack.Settings settings, @Nullable S value); +} diff --git a/api/src/main/java/me/shedaniel/rei/api/common/entry/settings/EntrySettingsAdapterRegistry.java b/api/src/main/java/me/shedaniel/rei/api/common/entry/settings/EntrySettingsAdapterRegistry.java new file mode 100644 index 000000000..b0d7122eb --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/settings/EntrySettingsAdapterRegistry.java @@ -0,0 +1,21 @@ +package me.shedaniel.rei.api.common.entry.settings; + +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.type.EntryType; +import me.shedaniel.rei.api.common.plugins.PluginManager; +import me.shedaniel.rei.api.common.plugins.REIPlugin; +import me.shedaniel.rei.api.common.registry.Reloadable; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +@ApiStatus.Experimental +public interface EntrySettingsAdapterRegistry extends Reloadable> { + static EntrySettingsAdapterRegistry getInstance() { + return PluginManager.getInstance().get(EntrySettingsAdapterRegistry.class); + } + + void register(EntryType type, EntryStack.Settings settings, EntrySettingsAdapter provider); + + @Nullable + S adapt(EntryStack stack, EntryStack.Settings settings, @Nullable S value); +} diff --git a/api/src/main/java/me/shedaniel/rei/api/common/entry/type/EntryDefinition.java b/api/src/main/java/me/shedaniel/rei/api/common/entry/type/EntryDefinition.java index 343a3491c..0e188b91c 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/entry/type/EntryDefinition.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/type/EntryDefinition.java @@ -72,8 +72,7 @@ public interface EntryDefinition { EntryRenderer getRenderer(); /** - * Returns the identifier for an entry, used in identifier search argument type, - * and appending the mod name to the tooltip. + * Returns the identifier for an entry, used in identifier search argument type. * * @param entry the entry * @param value the value of the entry @@ -82,6 +81,25 @@ public interface EntryDefinition { @Nullable ResourceLocation getIdentifier(EntryStack entry, T value); + /** + * Returns the container namespace of the entry, used for determining the + * responsible mod for the entry. + * + * @param entry the entry + * @param value the value of the entry + * @return the identifier for an entry + */ + @Nullable + default String getContainingNamespace(EntryStack entry, T value) { + ResourceLocation identifier = getIdentifier(entry, value); + + if (identifier == null) { + return null; + } else { + return identifier.getNamespace(); + } + } + /** * Returns whether the entry is empty, empty entries are not displayed, * and are considered invalid. diff --git a/api/src/main/java/me/shedaniel/rei/api/common/plugins/REIPlugin.java b/api/src/main/java/me/shedaniel/rei/api/common/plugins/REIPlugin.java index 6485fc2bc..3df36c94e 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/plugins/REIPlugin.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/plugins/REIPlugin.java @@ -27,6 +27,7 @@ import me.shedaniel.rei.api.client.plugins.REIClientPlugin; import me.shedaniel.rei.api.common.display.DisplaySerializerRegistry; 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.settings.EntrySettingsAdapterRegistry; import me.shedaniel.rei.api.common.entry.type.EntryTypeRegistry; import me.shedaniel.rei.api.common.fluid.FluidSupportProvider; import me.shedaniel.rei.api.common.registry.ReloadStage; @@ -64,6 +65,16 @@ public interface REIPlugin

> extends Comparable