From b13cdac76a661effc3f20eee4fbfe0de5090141d Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sun, 25 Dec 2022 00:10:44 +0800 Subject: Add helper methods from ClientEntryStacks into EntryStack --- .../api/client/entry/renderer/EntryRenderer.java | 2 +- .../entry/renderer/EntryRendererRegistry.java | 2 +- .../rei/api/client/util/ClientEntryStacks.java | 61 +++++++++++++++++- .../shedaniel/rei/api/common/entry/EntryStack.java | 75 +++++++++++++++++++--- 4 files changed, 128 insertions(+), 12 deletions(-) (limited to 'api/src/main/java') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRenderer.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRenderer.java index 81bb56d15..03503919a 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRenderer.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRenderer.java @@ -36,7 +36,7 @@ import org.jetbrains.annotations.Nullable; /** * A renderer to render a {@link EntryStack}. - * Use {@link me.shedaniel.rei.api.client.util.ClientEntryStacks#setRenderer} to change the {@link EntryRenderer} for a {@link EntryStack}. + * Use {@link EntryStack#withRenderer} to change the {@link EntryRenderer} for a {@link EntryStack}. * * @param the entry type * @see BatchedEntryRenderer diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererRegistry.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererRegistry.java index f1dbe8d7b..0f3f18c6f 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererRegistry.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererRegistry.java @@ -36,7 +36,7 @@ import org.jetbrains.annotations.Nullable; /** * Registry to transform {@link EntryRenderer} for stacks at a global level. - * For specific stacks, you can use {@link me.shedaniel.rei.api.client.util.ClientEntryStacks#setRenderer} + * For specific stacks, you can use {@link EntryStack#withRenderer} */ @ApiStatus.Experimental public interface EntryRendererRegistry extends Reloadable { diff --git a/api/src/main/java/me/shedaniel/rei/api/client/util/ClientEntryStacks.java b/api/src/main/java/me/shedaniel/rei/api/client/util/ClientEntryStacks.java index 5fa855549..48ce5985a 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/util/ClientEntryStacks.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/util/ClientEntryStacks.java @@ -25,10 +25,16 @@ package me.shedaniel.rei.api.client.util; import dev.architectury.fluid.FluidStack; import me.shedaniel.rei.api.client.entry.renderer.EntryRenderer; +import me.shedaniel.rei.api.client.entry.renderer.EntryRendererProvider; +import me.shedaniel.rei.api.client.entry.renderer.EntryRendererRegistry; import me.shedaniel.rei.api.client.entry.type.BuiltinClientEntryTypes; import me.shedaniel.rei.api.client.gui.Renderer; import me.shedaniel.rei.api.client.gui.widgets.Tooltip; +import me.shedaniel.rei.api.client.gui.widgets.TooltipContext; import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.type.EntryDefinition; +import me.shedaniel.rei.api.common.entry.type.EntryType; +import net.minecraft.network.chat.Component; import java.util.function.BiFunction; import java.util.function.Function; @@ -44,22 +50,75 @@ public final class ClientEntryStacks { return EntryStack.of(BuiltinClientEntryTypes.RENDERING, renderer); } + /** + * Sets a renderer for the {@link EntryStack}. This will override the default renderer from the {@link EntryDefinition}. + *

+ * You can transform the renderer on a {@link EntryType} level + * using {@link EntryRendererRegistry#register(EntryType, EntryRendererProvider)}. + * + * @return the {@link EntryStack} + * @see EntryStack#getRenderer() for how the tooltip is resolved + * @see EntryRenderer#empty() for an empty renderer + * @deprecated use {@link EntryStack#withRenderer(EntryRenderer)} with {@link EntryRenderer#empty()} instead + */ + @Deprecated(forRemoval = true) public static EntryStack setNotRenderer(EntryStack stack) { return setRenderer(stack, EntryRenderer.empty()); } + /** + * Sets a renderer for the {@link EntryStack}. This will override the default renderer from the {@link EntryDefinition}. + *

+ * You can transform the renderer on a {@link EntryType} level + * using {@link EntryRendererRegistry#register(EntryType, EntryRendererProvider)}. + * + * @param renderer the new renderer to use + * @return the {@link EntryStack} + * @see EntryStack#getRenderer() for how the tooltip is resolved + * @see EntryRenderer#empty() for an empty renderer + * @deprecated use {@link EntryStack#withRenderer(EntryRenderer)} instead + */ + @Deprecated(forRemoval = true) public static EntryStack setRenderer(EntryStack stack, EntryRenderer renderer) { return stack.setting(EntryStack.Settings.RENDERER, s -> renderer).cast(); } + /** + * Sets a renderer for the {@link EntryStack}. This will override the default renderer from the {@link EntryDefinition}. + *

+ * You can transform the renderer on a {@link EntryType} level + * using {@link EntryRendererRegistry#register(EntryType, EntryRendererProvider)}. + * + * @param rendererProvider the new renderer to use + * @return the {@link EntryStack} + * @see EntryStack#getRenderer() for how the tooltip is resolved + * @see EntryRenderer#empty() for an empty renderer + * @deprecated use {@link EntryStack#withRenderer(Function)} instead + */ + @Deprecated(forRemoval = true) @SuppressWarnings("rawtypes") public static EntryStack setRenderer(EntryStack stack, Function, EntryRenderer> rendererProvider) { return stack.setting(EntryStack.Settings.RENDERER, (Function) rendererProvider).cast(); } + /** + * Sets a tooltip processor to the {@link EntryStack}. The processor will be used to modify the tooltip. + *

+ * You can transform the tooltip on a {@link EntryType} level + * using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}. + *

+ * To append to the tooltip, use {@link EntryStack#tooltip(Component...)} instead. + * + * @param stack the stack to set the tooltip processor to + * @param processor the processor to modify the tooltips + * @return the {@link EntryStack} + * @see EntryStack#getTooltip(TooltipContext, boolean) for how the tooltip is resolved + * @deprecated use {@link EntryStack#tooltipProcessor(BiFunction)} instead + */ @SuppressWarnings("rawtypes") + @Deprecated(forRemoval = true) public static EntryStack setTooltipProcessor(EntryStack stack, BiFunction, Tooltip, Tooltip> processor) { - return stack.setting(EntryStack.Settings.TOOLTIP_PROCESSOR, (BiFunction) processor).cast(); + return stack.tooltipProcessor((BiFunction) processor); } public static EntryStack setFluidRenderRatio(EntryStack stack, float ratio) { 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 9acf0c8ee..4b79fce3f 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 @@ -27,6 +27,7 @@ import dev.architectury.utils.Env; import dev.architectury.utils.EnvExecutor; import me.shedaniel.rei.api.client.config.ConfigObject; import me.shedaniel.rei.api.client.entry.renderer.EntryRenderer; +import me.shedaniel.rei.api.client.entry.renderer.EntryRendererProvider; import me.shedaniel.rei.api.client.entry.renderer.EntryRendererRegistry; import me.shedaniel.rei.api.client.gui.Renderer; import me.shedaniel.rei.api.client.gui.widgets.Tooltip; @@ -194,7 +195,7 @@ public interface EntryStack extends TextRepresentable, Renderer { * using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}. *

* This tooltip can be appended by {@link EntryStack#tooltip(Component...)}, - * and further processed by {@link me.shedaniel.rei.api.client.util.ClientEntryStacks#setTooltipProcessor(EntryStack, BiFunction)}. + * and further processed by {@link EntryStack#tooltipProcessor(BiFunction)} instead. * * @param context the tooltip context * @param appendModName whether to append the mod name @@ -214,7 +215,7 @@ public interface EntryStack extends TextRepresentable, Renderer { * using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}. *

* This tooltip can be appended by {@link EntryStack#tooltip(Component...)}, - * and further processed by {@link me.shedaniel.rei.api.client.util.ClientEntryStacks#setTooltipProcessor(EntryStack, BiFunction)}. + * and further processed by {@link EntryStack#tooltipProcessor(BiFunction)} instead. * * @param context the tooltip context * @return the tooltip, can be {@code null} @@ -257,7 +258,7 @@ public interface EntryStack extends TextRepresentable, Renderer { * then is processed by {@link EntryRendererRegistry}. *

* To modify the renderer at a per stack level, - * use {@link me.shedaniel.rei.api.client.util.ClientEntryStacks#setRenderer(EntryStack, EntryRenderer)}. + * use {@link EntryStack#withRenderer(EntryRenderer)} instead. * * @return the {@link EntryRenderer} of this {@link EntryStack} */ @@ -405,7 +406,7 @@ public interface EntryStack extends TextRepresentable, Renderer { *

* It is generally not recommended to use this method, but to instead use the helper * methods such as {@link EntryStack#tooltip(Component...)} and - * the methods in {@link me.shedaniel.rei.api.client.util.ClientEntryStacks}. + * the methods in {@link EntryStack#tooltipProcessor(BiFunction)} instead. * * @param settings the setting to apply * @param value the value of the setting to apply @@ -459,7 +460,7 @@ public interface EntryStack extends TextRepresentable, Renderer { * You can transform the tooltip on a {@link EntryType} level * using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}. *

- * To modify the tooltip, use {@link me.shedaniel.rei.api.client.util.ClientEntryStacks#setTooltipProcessor(EntryStack, BiFunction)} instead. + * To modify the tooltip, use {@link EntryStack#tooltipProcessor(BiFunction)} instead. * * @param tooltips the tooltips to append * @return this {@link EntryStack} @@ -476,7 +477,7 @@ public interface EntryStack extends TextRepresentable, Renderer { * You can transform the tooltip on a {@link EntryType} level * using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}. *

- * To modify the tooltip, use {@link me.shedaniel.rei.api.client.util.ClientEntryStacks#setTooltipProcessor(EntryStack, BiFunction)} instead. + * To modify the tooltip, use {@link EntryStack#tooltipProcessor(BiFunction)} instead. * * @param tooltips the tooltips to append * @return this {@link EntryStack} @@ -493,7 +494,7 @@ public interface EntryStack extends TextRepresentable, Renderer { * You can transform the tooltip on a {@link EntryType} level * using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}. *

- * To modify the tooltip, use {@link me.shedaniel.rei.api.client.util.ClientEntryStacks#setTooltipProcessor(EntryStack, BiFunction)} instead. + * To modify the tooltip, use {@link EntryStack#tooltipProcessor(BiFunction)} instead. * * @param tooltipProvider the provider for the tooltips to append * @return this {@link EntryStack} @@ -504,6 +505,56 @@ public interface EntryStack extends TextRepresentable, Renderer { return setting(Settings.TOOLTIP_APPEND_EXTRA, tooltipProvider); } + /** + * Sets a tooltip processor to this {@link EntryStack}. The processor will be used to modify the tooltip. + *

+ * You can transform the tooltip on a {@link EntryType} level + * using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}. + *

+ * To append to the tooltip, use {@link EntryStack#tooltip(Component...)} instead. + * + * @param tooltipProcessor the processor to modify the tooltips + * @return this {@link EntryStack} + * @see EntryStack#getTooltip(TooltipContext, boolean) for how the tooltip is resolved + */ + @SuppressWarnings("rawtypes") + @Environment(EnvType.CLIENT) + default EntryStack tooltipProcessor(BiFunction, Tooltip, Tooltip> tooltipProcessor) { + return setting(EntryStack.Settings.TOOLTIP_PROCESSOR, (BiFunction) tooltipProcessor).cast(); + } + + /** + * Sets a renderer for this {@link EntryStack}. This will override the default renderer from the {@link EntryDefinition}. + *

+ * You can transform the renderer on a {@link EntryType} level + * using {@link EntryRendererRegistry#register(EntryType, EntryRendererProvider)}. + * + * @param renderer the new renderer to use + * @return this {@link EntryStack} + * @see EntryStack#getRenderer() for how the tooltip is resolved + * @see EntryRenderer#empty() for an empty renderer + */ + @Environment(EnvType.CLIENT) + default EntryStack withRenderer(EntryRenderer renderer) { + return setting(Settings.RENDERER, $ -> renderer).cast(); + } + + /** + * Sets a renderer for this {@link EntryStack}. This will override the default renderer from the {@link EntryDefinition}. + *

+ * You can transform the renderer on a {@link EntryType} level + * using {@link EntryRendererRegistry#register(EntryType, EntryRendererProvider)}. + * + * @param renderer the new renderer to use + * @return this {@link EntryStack} + * @see EntryStack#getRenderer() for how the tooltip is resolved + */ + @SuppressWarnings("rawtypes") + @Environment(EnvType.CLIENT) + default EntryStack withRenderer(Function, EntryRenderer> renderer) { + return setting(Settings.RENDERER, (Function) renderer).cast(); + } + /** * Returns the cheated stack of this {@link EntryStack}. * @@ -512,6 +563,12 @@ public interface EntryStack extends TextRepresentable, Renderer { */ EntryStack cheatsAs(); + /** + * Settings for {@link EntryStack}s. + * Please consider using the utility methods in {@link EntryStack} instead of using this class directly. + * + * @param the type of the setting + */ @Deprecated class Settings { @ApiStatus.Internal @@ -543,8 +600,8 @@ public interface EntryStack extends TextRepresentable, Renderer { }); } - private R defaultValue; - private short id; + private final R defaultValue; + private final short id; @ApiStatus.Internal public Settings(R defaultValue) { -- cgit