diff options
| author | shedaniel <daniel@shedaniel.me> | 2021-12-12 23:20:43 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2021-12-12 23:22:34 +0800 |
| commit | 616bd6af839373c95d9191cf808da0446963d3eb (patch) | |
| tree | 7b5b4a5b7f546162956001608de5113ae8f9384d /api/src/main/java/me | |
| parent | b31529de8b342fb6176ace18f2560a7508fdcdca (diff) | |
| download | RoughlyEnoughItems-616bd6af839373c95d9191cf808da0446963d3eb.tar.gz RoughlyEnoughItems-616bd6af839373c95d9191cf808da0446963d3eb.tar.bz2 RoughlyEnoughItems-616bd6af839373c95d9191cf808da0446963d3eb.zip | |
Add entry settings adapter
Diffstat (limited to 'api/src/main/java/me')
5 files changed, 114 insertions, 2 deletions
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 8c4eb908d..d5d2aeaae 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<T> extends TextRepresentable, Renderer { @Nullable ResourceLocation getIdentifier(); + @Nullable + String getContainingNamespace(); + boolean isEmpty(); /** @@ -216,6 +219,8 @@ public interface EntryStack<T> extends TextRepresentable, Renderer { @Environment(EnvType.CLIENT) public static Settings<BiFunction<EntryStack<?>, Tooltip, Tooltip>> TOOLTIP_PROCESSOR; @Environment(EnvType.CLIENT) + public static Settings<BiFunction<EntryStack<?>, String, String>> CONTAINING_NS; + @Environment(EnvType.CLIENT) public static Settings<Function<EntryStack<?>, List<Component>>> TOOLTIP_APPEND_EXTRA; @Environment(EnvType.CLIENT) public static Settings<Float> FLUID_RENDER_RATIO; @@ -224,6 +229,7 @@ public interface EntryStack<T> 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 <T> the entry type + * @param <S> the setting type + * @see EntryStack.Settings + */ +@FunctionalInterface +@ApiStatus.Experimental +public interface EntrySettingsAdapter<T, S> { + static <T, S> EntrySettingsAdapter<T, S> 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<T> entry, EntryStack.Settings<S> 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<REIPlugin<?>> { + static EntrySettingsAdapterRegistry getInstance() { + return PluginManager.getInstance().get(EntrySettingsAdapterRegistry.class); + } + + <T, S> void register(EntryType<T> type, EntryStack.Settings<S> settings, EntrySettingsAdapter<T, S> provider); + + @Nullable + <T, S> S adapt(EntryStack<T> stack, EntryStack.Settings<S> 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 9ac8607af..9b7afa48f 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<T> { EntryRenderer<T> 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 @@ -83,6 +82,25 @@ public interface EntryDefinition<T> { ResourceLocation getIdentifier(EntryStack<T> 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<T> 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. * Empty entries will be treated equally to {@link EntryStack#empty()}. 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 108b293ac..5841fcf45 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<P extends REIPlugin<?>> extends Comparable<REIPlugin< } /** + * Registers new entry settings adapters + * + * @param registry the entry settings adapters registry + */ + @ApiStatus.OverrideOnly + @ApiStatus.Experimental + default void registerEntrySettingsAdapters(EntrySettingsAdapterRegistry registry) { + } + + /** * Registers item comparators for identifying variants of {@link net.minecraft.world.item.ItemStack}. * * @see ItemComparatorRegistry |
