From 616bd6af839373c95d9191cf808da0446963d3eb 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 +++++ .../me/shedaniel/rei/RoughlyEnoughItemsCore.java | 2 + .../search/argument/type/ModArgumentType.java | 6 +-- .../rei/impl/common/entry/AbstractEntryStack.java | 24 ++++++---- .../settings/EntrySettingsAdapterRegistryImpl.java | 51 ++++++++++++++++++++ 9 files changed, 186 insertions(+), 13 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 create mode 100644 runtime/src/main/java/me/shedaniel/rei/impl/common/entry/settings/EntrySettingsAdapterRegistryImpl.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 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 extends TextRepresentable, Renderer { @Nullable ResourceLocation getIdentifier(); + @Nullable + String getContainingNamespace(); + boolean isEmpty(); /** @@ -216,6 +219,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; @@ -224,6 +229,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 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 { 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 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; @@ -63,6 +64,16 @@ public interface REIPlugin

> extends Comparable(RecipeManagerContextImpl.supplier()), new ItemComparatorRegistryImpl(), new FluidComparatorRegistryImpl(), diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/type/ModArgumentType.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/type/ModArgumentType.java index efea0ffac..cbcf6fd4b 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/type/ModArgumentType.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/search/argument/type/ModArgumentType.java @@ -64,9 +64,9 @@ public final class ModArgumentType extends ArgumentType data, EntryStack stack, String searchText, Unit filterData) { if (data.getValue() == null) { - ResourceLocation id = stack.getIdentifier(); - data.setValue(id != null ? new ModInfoPair( - id.getNamespace(), + String containingNs = stack.getContainingNamespace(); + data.setValue(containingNs != null ? new ModInfoPair( + containingNs, null ) : ModInfoPair.EMPTY); } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java index 1d3486971..c31b54cf9 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java @@ -35,6 +35,7 @@ import me.shedaniel.rei.api.client.ClientHelper; import me.shedaniel.rei.api.client.gui.Renderer; import me.shedaniel.rei.api.client.gui.widgets.Tooltip; import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.settings.EntrySettingsAdapterRegistry; import me.shedaniel.rei.api.common.util.EntryStacks; import me.shedaniel.rei.api.common.util.FormattingUtils; import me.shedaniel.rei.impl.client.util.CrashReportUtils; @@ -126,6 +127,12 @@ public abstract class AbstractEntryStack implements EntryStack, Renderer { return getDefinition().getIdentifier(this, getValue()); } + @Override + @Nullable + public String getContainingNamespace() { + return get(Settings.CONTAINING_NS).apply(this, getDefinition().getContainingNamespace(this, getValue())); + } + @Override public boolean isEmpty() { return getDefinition().isEmpty(this, getValue()); @@ -163,11 +170,12 @@ public abstract class AbstractEntryStack implements EntryStack, Renderer { @Override public T get(Settings settings) { - Object o = this.settings == null ? null : this.settings.get(settings.getId()); + T o = this.settings == null ? null : (T) this.settings.get(settings.getId()); + o = EntrySettingsAdapterRegistry.getInstance().adapt(this, settings, o); if (o == null) { - return settings.getDefaultValue(); + o = settings.getDefaultValue(); } - return (T) o; + return o; } @Override @@ -191,13 +199,13 @@ public abstract class AbstractEntryStack implements EntryStack, Renderer { tooltip.getValue().addAllTexts(get(Settings.TOOLTIP_APPEND_EXTRA).apply(this)); tooltip.setValue(get(Settings.TOOLTIP_PROCESSOR).apply(this, tooltip.getValue())); if (tooltip.getValue() == null) return null; - ResourceLocation location = getIdentifier(); + String containingNs = getContainingNamespace(); if (appendModName) { - if (location != null) { - ClientHelper.getInstance().appendModIdToTooltips(tooltip.getValue(), location.getNamespace()); + if (containingNs != null) { + ClientHelper.getInstance().appendModIdToTooltips(tooltip.getValue(), containingNs); } - } else { - final String modName = ClientHelper.getInstance().getModFromModId(location.getNamespace()); + } else if (containingNs != null) { + final String modName = ClientHelper.getInstance().getModFromModId(containingNs); Iterator iterator = tooltip.getValue().entries().iterator(); while (iterator.hasNext()) { Tooltip.Entry s = iterator.next(); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/settings/EntrySettingsAdapterRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/settings/EntrySettingsAdapterRegistryImpl.java new file mode 100644 index 000000000..11385492c --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/settings/EntrySettingsAdapterRegistryImpl.java @@ -0,0 +1,51 @@ +package me.shedaniel.rei.impl.common.entry.settings; + +import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; +import me.shedaniel.rei.api.client.entry.renderer.EntryRenderer; +import me.shedaniel.rei.api.client.entry.renderer.EntryRendererProvider; +import me.shedaniel.rei.api.client.plugins.REIClientPlugin; +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.settings.EntrySettingsAdapter; +import me.shedaniel.rei.api.common.entry.settings.EntrySettingsAdapterRegistry; +import me.shedaniel.rei.api.common.entry.type.EntryType; +import me.shedaniel.rei.api.common.plugins.REIPlugin; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +public class EntrySettingsAdapterRegistryImpl implements EntrySettingsAdapterRegistry { + private final Map, Multimap, EntrySettingsAdapter>> providers = new HashMap<>(); + + @Override + public void register(EntryType type, EntryStack.Settings settings, EntrySettingsAdapter provider) { + Multimap, EntrySettingsAdapter> multimap = this.providers.computeIfAbsent(settings, $ -> Multimaps.newMultimap(new Reference2ObjectOpenHashMap<>(), ArrayList::new)); + multimap.put(type, provider); + } + + @Override + @Nullable + public S adapt(EntryStack stack, EntryStack.Settings settings, @Nullable S value) { + Multimap, EntrySettingsAdapter> multimap = providers.get(settings); + if (multimap != null) { + for (EntrySettingsAdapter adapter : (Collection>) (Collection>) multimap.get(stack.getType())) { + value = adapter.provide(stack, settings, value); + } + } + return value; + } + + @Override + public void startReload() { + providers.clear(); + } + + @Override + public void acceptPlugin(REIPlugin plugin) { + plugin.registerEntrySettingsAdapters(this); + } +} -- cgit