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 | |
| parent | b31529de8b342fb6176ace18f2560a7508fdcdca (diff) | |
| download | RoughlyEnoughItems-616bd6af839373c95d9191cf808da0446963d3eb.tar.gz RoughlyEnoughItems-616bd6af839373c95d9191cf808da0446963d3eb.tar.bz2 RoughlyEnoughItems-616bd6af839373c95d9191cf808da0446963d3eb.zip | |
Add entry settings adapter
9 files changed, 186 insertions, 13 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 diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java index 654552028..9fc7a2b96 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCore.java @@ -49,6 +49,7 @@ import me.shedaniel.rei.impl.common.entry.EntryStackProviderImpl; 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.settings.EntrySettingsAdapterRegistryImpl; import me.shedaniel.rei.impl.common.entry.type.EntryTypeRegistryImpl; import me.shedaniel.rei.impl.common.fluid.FluidSupportProviderImpl; import me.shedaniel.rei.impl.common.logging.FileLogger; @@ -106,6 +107,7 @@ public class RoughlyEnoughItemsCore { ); }, new EntryTypeRegistryImpl(), + new EntrySettingsAdapterRegistryImpl(), new RecipeManagerContextImpl<>(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<Unit, ModArgumentType.@N @Override public boolean matches(Mutable<@Nullable ModInfoPair> 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; @@ -127,6 +128,12 @@ public abstract class AbstractEntryStack<A> implements EntryStack<A>, Renderer { } @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<A> implements EntryStack<A>, Renderer { @Override public <T> T get(Settings<T> 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<A> implements EntryStack<A>, 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<Tooltip.Entry> 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<EntryStack.Settings<?>, Multimap<EntryType<?>, EntrySettingsAdapter<?, ?>>> providers = new HashMap<>(); + + @Override + public <T,S> void register(EntryType<T> type, EntryStack.Settings<S> settings, EntrySettingsAdapter<T,S> provider) { + Multimap<EntryType<?>, EntrySettingsAdapter<?, ?>> multimap = this.providers.computeIfAbsent(settings, $ -> Multimaps.newMultimap(new Reference2ObjectOpenHashMap<>(), ArrayList::new)); + multimap.put(type, provider); + } + + @Override + @Nullable + public <T,S> S adapt(EntryStack<T> stack, EntryStack.Settings<S> settings, @Nullable S value) { + Multimap<EntryType<?>, EntrySettingsAdapter<?, ?>> multimap = providers.get(settings); + if (multimap != null) { + for (EntrySettingsAdapter<T, S> adapter : (Collection<EntrySettingsAdapter<T, S>>) (Collection<? extends EntrySettingsAdapter<?, ?>>) 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); + } +} |
