From dd758e2bbbdac32599eb75de740acf4f83aad2d9 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sun, 25 Dec 2022 00:10:44 +0800 Subject: [PATCH] Add helper methods from ClientEntryStacks into EntryStack --- .../client/entry/renderer/EntryRenderer.java | 2 +- .../entry/renderer/EntryRendererRegistry.java | 2 +- .../api/client/util/ClientEntryStacks.java | 61 +++++++++++++- .../rei/api/common/entry/EntryStack.java | 79 ++++++++++++++++--- .../entrylist/PaginatedEntryListWidget.java | 3 +- 5 files changed, 131 insertions(+), 16 deletions(-) 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 c51309603..f9c66ed5e 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 @@ -37,7 +37,7 @@ /** * 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 efb3882d1..af100c222 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 @@ /** * 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 b08645e52..775d150dd 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 @@ 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 static EntryStack of(Renderer renderer) { 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 0dcb10eda..d344ffa1c 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 @@ -28,6 +28,7 @@ import me.shedaniel.math.Point; 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; @@ -195,7 +196,7 @@ default boolean supportSaving() { * 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 mouse the mouse position * @param appendModName whether to append the mod name @@ -219,7 +220,7 @@ default Tooltip getTooltip(Point mouse, boolean appendModName) { * 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 @@ -239,7 +240,7 @@ default Tooltip getTooltip(Point mouse, boolean appendModName) { * 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 mouse the mouse position * @return the tooltip, can be {@code null} @@ -261,7 +262,7 @@ default Tooltip getTooltip(Point mouse) { * 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} @@ -304,7 +305,7 @@ default Class getValueType() { * 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} */ @@ -452,7 +453,7 @@ default R castValue() { *

* 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 @@ -506,7 +507,7 @@ default R castValue() { * 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} @@ -523,7 +524,7 @@ default EntryStack tooltip(Component... tooltips) { * 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} @@ -540,7 +541,7 @@ default EntryStack tooltip(List tooltips) { * 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} @@ -551,6 +552,56 @@ default EntryStack tooltip(Function, List> tooltipPr 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}. * @@ -559,6 +610,12 @@ default EntryStack tooltip(Function, List> tooltipPr */ 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 @@ -590,8 +647,8 @@ class Settings { }); } - private R defaultValue; - private short id; + private final R defaultValue; + private final short id; @ApiStatus.Internal public Settings(R defaultValue) { diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/entrylist/PaginatedEntryListWidget.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/entrylist/PaginatedEntryListWidget.java index fb7c864c5..5c4914ebb 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/entrylist/PaginatedEntryListWidget.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/entrylist/PaginatedEntryListWidget.java @@ -34,7 +34,6 @@ import me.shedaniel.rei.api.client.gui.widgets.Button; import me.shedaniel.rei.api.client.gui.widgets.Widget; import me.shedaniel.rei.api.client.gui.widgets.Widgets; -import me.shedaniel.rei.api.client.util.ClientEntryStacks; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.util.CollectionUtils; import me.shedaniel.rei.impl.client.ClientHelperImpl; @@ -90,7 +89,7 @@ protected void renderEntries(boolean fastEntryRendering, PoseStack matrices, int CachedEntryListRender.Sprite sprite = CachedEntryListRender.get(entry.getCurrentEntry()); if (sprite != null) { CachingEntryRenderer renderer = new CachingEntryRenderer(sprite, this::getBlitOffset); - entry.our = ClientEntryStacks.setRenderer(entry.getCurrentEntry().copy().cast(), stack -> renderer); + entry.our = entry.getCurrentEntry().copy().cast().withRenderer(stack -> renderer); } } }