Skip to content

Commit

Permalink
Add helper methods from ClientEntryStacks into EntryStack
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Dec 24, 2022
1 parent 5307680 commit dd758e2
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> the entry type
* @see BatchedEntryRenderer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<REIClientPlugin> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}.
* <p>
* 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 <T> EntryStack<T> setNotRenderer(EntryStack<? extends T> stack) {
return setRenderer(stack, EntryRenderer.empty());
}

/**
* Sets a renderer for the {@link EntryStack}. This will override the default renderer from the {@link EntryDefinition}.
* <p>
* 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 <T> EntryStack<T> setRenderer(EntryStack<? extends T> stack, EntryRenderer<? extends T> 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}.
* <p>
* 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 <T> EntryStack<T> setRenderer(EntryStack<? extends T> stack, Function<EntryStack<T>, EntryRenderer<? extends T>> 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.
* <p>
* You can transform the tooltip on a {@link EntryType} level
* using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}.
* <p>
* 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 <T> EntryStack<T> setTooltipProcessor(EntryStack<? extends T> stack, BiFunction<EntryStack<T>, Tooltip, Tooltip> processor) {
return stack.setting(EntryStack.Settings.TOOLTIP_PROCESSOR, (BiFunction) processor).cast();
return stack.tooltipProcessor((BiFunction) processor);
}

public static EntryStack<FluidStack> setFluidRenderRatio(EntryStack<FluidStack> stack, float ratio) {
Expand Down
79 changes: 68 additions & 11 deletions api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -195,7 +196,7 @@ default boolean supportSaving() {
* using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}.
* <p>
* 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
Expand All @@ -219,7 +220,7 @@ default Tooltip getTooltip(Point mouse, boolean appendModName) {
* using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}.
* <p>
* 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
Expand All @@ -239,7 +240,7 @@ default Tooltip getTooltip(Point mouse, boolean appendModName) {
* using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}.
* <p>
* 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}
Expand All @@ -261,7 +262,7 @@ default Tooltip getTooltip(Point mouse) {
* using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}.
* <p>
* 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}
Expand Down Expand Up @@ -304,7 +305,7 @@ default Class<T> getValueType() {
* then is processed by {@link EntryRendererRegistry}.
* <p>
* 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}
*/
Expand Down Expand Up @@ -452,7 +453,7 @@ default <R> R castValue() {
* <p>
* 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
Expand Down Expand Up @@ -506,7 +507,7 @@ default <R> R castValue() {
* You can transform the tooltip on a {@link EntryType} level
* using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}.
* <p>
* 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}
Expand All @@ -523,7 +524,7 @@ default EntryStack<T> tooltip(Component... tooltips) {
* You can transform the tooltip on a {@link EntryType} level
* using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}.
* <p>
* 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}
Expand All @@ -540,7 +541,7 @@ default EntryStack<T> tooltip(List<Component> tooltips) {
* You can transform the tooltip on a {@link EntryType} level
* using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}.
* <p>
* 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}
Expand All @@ -551,6 +552,56 @@ default EntryStack<T> tooltip(Function<EntryStack<?>, List<Component>> 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.
* <p>
* You can transform the tooltip on a {@link EntryType} level
* using {@link EntryRendererRegistry#transformTooltip(EntryType, EntryRendererRegistry.TooltipTransformer)}.
* <p>
* 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<T> tooltipProcessor(BiFunction<EntryStack<T>, 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}.
* <p>
* 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<T> withRenderer(EntryRenderer<? super T> renderer) {
return setting(Settings.RENDERER, $ -> renderer).cast();
}

/**
* Sets a renderer for this {@link EntryStack}. This will override the default renderer from the {@link EntryDefinition}.
* <p>
* 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<T> withRenderer(Function<EntryStack<T>, EntryRenderer<? super T>> renderer) {
return setting(Settings.RENDERER, (Function) renderer).cast();
}

/**
* Returns the cheated stack of this {@link EntryStack}.
*
Expand All @@ -559,6 +610,12 @@ default EntryStack<T> tooltip(Function<EntryStack<?>, List<Component>> tooltipPr
*/
EntryStack<ItemStack> cheatsAs();

/**
* Settings for {@link EntryStack}s.
* Please consider using the utility methods in {@link EntryStack} instead of using this class directly.
*
* @param <R> the type of the setting
*/
@Deprecated
class Settings<R> {
@ApiStatus.Internal
Expand Down Expand Up @@ -590,8 +647,8 @@ class Settings<R> {
});
}

private R defaultValue;
private short id;
private final R defaultValue;
private final short id;

@ApiStatus.Internal
public Settings(R defaultValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit dd758e2

Please sign in to comment.