From 1eed4cdcea68a9b305120b51390a18868c550ef2 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Fri, 22 Oct 2021 15:15:08 +0800 Subject: Fix #645 --- .../api/client/entry/renderer/EntryRenderer.java | 8 ++- .../entry/renderer/EntryRendererProvider.java | 54 +++++++++++++++++ .../entry/renderer/EntryRendererRegistry.java | 67 ++++++++++++++++++++++ .../entry/renderer/ForwardingEntryRenderer.java | 50 ++++++++++++++++ .../rei/api/client/plugins/REIClientPlugin.java | 11 ++++ .../shedaniel/rei/api/common/entry/EntryStack.java | 3 +- 6 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererProvider.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererRegistry.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/ForwardingEntryRenderer.java (limited to 'api/src') 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 3943a76eb..43819351a 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 @@ -40,9 +40,10 @@ import org.jetbrains.annotations.Nullable; * * @param the entry type * @see BatchedEntryRenderer + * @see EntryRendererRegistry */ @Environment(EnvType.CLIENT) -public interface EntryRenderer { +public interface EntryRenderer extends EntryRendererProvider { static EntryRenderer empty() { return ClientInternals.getEmptyEntryRenderer(); } @@ -58,4 +59,9 @@ public interface EntryRenderer { default EntryRenderer cast() { return (EntryRenderer) this; } + + @Override + default EntryRenderer provide(EntryStack entry, EntryRenderer last) { + return this; + } } \ No newline at end of file diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererProvider.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererProvider.java new file mode 100644 index 000000000..659df5cff --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererProvider.java @@ -0,0 +1,54 @@ +/* + * 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.client.entry.renderer; + +import me.shedaniel.rei.api.common.entry.EntryStack; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; + +/** + * A functional interface to provide {@link EntryRenderer} for {@link EntryStack}. + * + * @param the entry type + * @see EntryRenderer + */ +@FunctionalInterface +@Environment(EnvType.CLIENT) +public interface EntryRendererProvider { + static EntryRendererProvider empty() { + return EntryRenderer.empty(); + } + + /** + * Returns a new {@link EntryRenderer} for a specific {@link EntryStack}, + * a previous {@link EntryRenderer} will be provided, this may be an empty renderer. + * {@code null} is not an accepted value, return the previous renderer if this provider + * does not modify the renderer. + * + * @param entry the entry stack to render for, do not store or cache this + * @param last the previous entry renderer + * @return the new entry renderer, {@code null} is not accepted here + */ + EntryRenderer provide(EntryStack entry, EntryRenderer last); +} 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 new file mode 100644 index 000000000..95b8981b3 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererRegistry.java @@ -0,0 +1,67 @@ +/* + * 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.client.entry.renderer; + +import me.shedaniel.math.Point; +import me.shedaniel.rei.api.client.gui.widgets.Tooltip; +import me.shedaniel.rei.api.client.plugins.REIClientPlugin; +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.registry.Reloadable; +import org.jetbrains.annotations.ApiStatus; +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} + */ +@ApiStatus.Experimental +public interface EntryRendererRegistry extends Reloadable { + static EntryRendererRegistry getInstance() { + return PluginManager.getClientInstance().get(EntryRendererRegistry.class); + } + + void register(EntryType type, EntryRendererProvider provider); + + default void transformTooltip(EntryType type, TooltipTransformer transformer) { + register(type, (entry, last) -> { + return new ForwardingEntryRenderer(last) { + @Override + @Nullable + public Tooltip getTooltip(EntryStack entry, Point mouse) { + return transformer.transform(entry, mouse, super.getTooltip(entry, mouse)); + } + }; + }); + } + + EntryRenderer get(EntryStack stack); + + @FunctionalInterface + interface TooltipTransformer { + @Nullable + Tooltip transform(EntryStack entry, Point mouse, @Nullable Tooltip tooltip); + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/ForwardingEntryRenderer.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/ForwardingEntryRenderer.java new file mode 100644 index 000000000..2c56d5176 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/ForwardingEntryRenderer.java @@ -0,0 +1,50 @@ +/* + * 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.client.entry.renderer; + +import com.mojang.blaze3d.vertex.PoseStack; +import me.shedaniel.math.Point; +import me.shedaniel.math.Rectangle; +import me.shedaniel.rei.api.client.gui.widgets.Tooltip; +import me.shedaniel.rei.api.common.entry.EntryStack; +import org.jetbrains.annotations.Nullable; + +public abstract class ForwardingEntryRenderer implements EntryRenderer { + protected EntryRenderer next; + + public ForwardingEntryRenderer(EntryRenderer next) { + this.next = next; + } + + @Override + public void render(EntryStack entry, PoseStack matrices, Rectangle bounds, int mouseX, int mouseY, float delta) { + this.next.render(entry, matrices, bounds, mouseX, mouseY, delta); + } + + @Override + @Nullable + public Tooltip getTooltip(EntryStack entry, Point mouse) { + return this.next.getTooltip(entry, mouse); + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/plugins/REIClientPlugin.java b/api/src/main/java/me/shedaniel/rei/api/client/plugins/REIClientPlugin.java index e65ccc4e1..a6be0beeb 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/plugins/REIClientPlugin.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/plugins/REIClientPlugin.java @@ -23,6 +23,7 @@ package me.shedaniel.rei.api.client.plugins; +import me.shedaniel.rei.api.client.entry.renderer.EntryRendererRegistry; import me.shedaniel.rei.api.client.favorites.FavoriteEntryType; import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; import me.shedaniel.rei.api.client.registry.display.DisplayRegistry; @@ -38,6 +39,16 @@ import org.jetbrains.annotations.ApiStatus; @Environment(EnvType.CLIENT) public interface REIClientPlugin extends REIPlugin { + /** + * Registers new entry renderers + * + * @param registry the entry renderer registry + */ + @ApiStatus.OverrideOnly + @ApiStatus.Experimental + default void registerEntryRenderers(EntryRendererRegistry registry) { + } + /** * Registers new categories * 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 76ff50f98..74529de5c 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 dev.architectury.utils.EnvExecutor; 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.EntryRendererRegistry; import me.shedaniel.rei.api.client.gui.Renderer; import me.shedaniel.rei.api.client.gui.widgets.Tooltip; import me.shedaniel.rei.api.common.entry.type.EntryDefinition; @@ -182,7 +183,7 @@ public interface EntryStack extends TextRepresentable, Renderer { static { EnvExecutor.runInEnv(Env.CLIENT, () -> () -> { - RENDERER = new Settings<>(stack -> stack.getDefinition().getRenderer()); + RENDERER = new Settings<>(stack -> EntryRendererRegistry.getInstance().get(stack)); TOOLTIP_PROCESSOR = new Settings<>((stack, tooltip) -> tooltip); TOOLTIP_APPEND_EXTRA = new Settings<>(stack -> Collections.emptyList()); FLUID_RENDER_RATIO = new Settings<>(1.0F); -- cgit