diff --git a/patches/net/minecraft/client/gui/GuiGraphics.java.patch b/patches/net/minecraft/client/gui/GuiGraphics.java.patch index 60a00e141e..caca78e363 100644 --- a/patches/net/minecraft/client/gui/GuiGraphics.java.patch +++ b/patches/net/minecraft/client/gui/GuiGraphics.java.patch @@ -155,8 +155,9 @@ int i1 = vector2ic.y(); this.pose.pushPose(); int j1 = 400; -+ net.neoforged.neoforge.client.event.RenderTooltipEvent.Color colorEvent = net.neoforged.neoforge.client.ClientHooks.onRenderTooltipColor(this.tooltipStack, this, l, i1, preEvent.getFont(), p_282615_); - TooltipRenderUtil.renderTooltipBackground(this, l, i1, i, j, 400, p_371327_); +- TooltipRenderUtil.renderTooltipBackground(this, l, i1, i, j, 400, p_371327_); ++ var textureEvent = net.neoforged.neoforge.client.ClientHooks.onRenderTooltipTexture(this.tooltipStack, this, l, i1, preEvent.getFont(), p_282615_, p_371327_); ++ TooltipRenderUtil.renderTooltipBackground(this, l, i1, i, j, 400, textureEvent.getTexture()); this.pose.translate(0.0F, 0.0F, 400.0F); int k1 = i1; diff --git a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java index a1b1166a57..0132b8a863 100644 --- a/src/main/java/net/neoforged/neoforge/client/ClientHooks.java +++ b/src/main/java/net/neoforged/neoforge/client/ClientHooks.java @@ -774,11 +774,8 @@ public static RenderTooltipEvent.Pre onRenderTooltipPre(ItemStack stack, GuiGrap return preEvent; } - // TODO porting: potentially replace with a background texture event - public static RenderTooltipEvent.Color onRenderTooltipColor(ItemStack stack, GuiGraphics graphics, int x, int y, Font font, List components) { - var colorEvent = new RenderTooltipEvent.Color(stack, graphics, x, y, font, 0xf0100010, 0x505000FF, 0x5028007f, components); - NeoForge.EVENT_BUS.post(colorEvent); - return colorEvent; + public static RenderTooltipEvent.Texture onRenderTooltipTexture(ItemStack stack, GuiGraphics graphics, int x, int y, Font font, List components, @Nullable ResourceLocation texture) { + return NeoForge.EVENT_BUS.post(new RenderTooltipEvent.Texture(stack, graphics, x, y, font, components, texture)); } public static List gatherTooltipComponents(ItemStack stack, List textElements, int mouseX, int screenWidth, int screenHeight, Font fallbackFont) { diff --git a/src/main/java/net/neoforged/neoforge/client/event/RenderTooltipEvent.java b/src/main/java/net/neoforged/neoforge/client/event/RenderTooltipEvent.java index e7f7c84657..1fff84cf30 100644 --- a/src/main/java/net/neoforged/neoforge/client/event/RenderTooltipEvent.java +++ b/src/main/java/net/neoforged/neoforge/client/event/RenderTooltipEvent.java @@ -12,7 +12,9 @@ import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipPositioner; +import net.minecraft.core.component.DataComponents; import net.minecraft.network.chat.FormattedText; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.ItemStack; import net.neoforged.bus.api.Event; @@ -21,6 +23,7 @@ import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.entity.player.ItemTooltipEvent; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; /** * Fired during tooltip rendering. @@ -28,7 +31,7 @@ * * @see RenderTooltipEvent.GatherComponents * @see RenderTooltipEvent.Pre - * @see RenderTooltipEvent.Color + * @see RenderTooltipEvent.Texture */ public abstract class RenderTooltipEvent extends Event { protected final ItemStack itemStack; @@ -99,7 +102,7 @@ public Font getFont() { * *

This event is {@linkplain ICancellableEvent cancellable}, and does not {@linkplain HasResult have a result}. * If this event is cancelled, then the list of components will be empty, causing the tooltip to not be rendered and - * the corresponding {@link RenderTooltipEvent.Pre} and {@link RenderTooltipEvent.Color} to not be fired.

+ * the corresponding {@link RenderTooltipEvent.Pre} and {@link RenderTooltipEvent.Texture} to not be fired.

* *

This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus}, * only on the {@linkplain LogicalSide#CLIENT logical client}.

@@ -179,7 +182,7 @@ public void setMaxWidth(int maxWidth) { * *

This event is {@linkplain ICancellableEvent cancellable}, and does not {@linkplain HasResult have a result}. * If this event is cancelled, then the tooltip will not be rendered and the corresponding - * {@link RenderTooltipEvent.Color} will not be fired.

+ * {@link RenderTooltipEvent.Texture} will not be fired.

* *

This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus}, * only on the {@linkplain LogicalSide#CLIENT logical client}.

@@ -247,136 +250,51 @@ public void setY(int y) { } /** - * Fired when the colours for the tooltip background are determined. - * This can be used to modify the background color and the border's gradient colors. + * Fired when the textures for the tooltip background are determined. + * This can be used to modify the background and frame texture. * - *

This event is not {@linkplain ICancellableEvent cancellable}, and does not {@linkplain HasResult have a result}.

+ *

This event is not {@linkplain ICancellableEvent cancellable}.

* *

This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus}, * only on the {@linkplain LogicalSide#CLIENT logical client}.

*/ - public static class Color extends RenderTooltipEvent { - private final int originalBackground; - private final int originalBorderStart; - private final int originalBorderEnd; - private int backgroundStart; - private int backgroundEnd; - private int borderStart; - private int borderEnd; + public static class Texture extends RenderTooltipEvent { + @Nullable + private final ResourceLocation originalTexture; + @Nullable + private ResourceLocation texture; @ApiStatus.Internal - public Color(ItemStack stack, GuiGraphics graphics, int x, int y, Font fr, int background, int borderStart, int borderEnd, List components) { - super(stack, graphics, x, y, fr, components); - this.originalBackground = background; - this.originalBorderStart = borderStart; - this.originalBorderEnd = borderEnd; - this.backgroundStart = background; - this.backgroundEnd = background; - this.borderStart = borderStart; - this.borderEnd = borderEnd; - } - - /** - * {@return the gradient start color for the tooltip background (top edge)} - */ - public int getBackgroundStart() { - return backgroundStart; - } - - /** - * {@return the gradient end color for the tooltip background (bottom edge)} - */ - public int getBackgroundEnd() { - return backgroundEnd; - } - - /** - * Sets the new color for the tooltip background. This sets both the gradient start and end color for the - * background to this color. - * - * @param background the new color for the tooltip background - */ - public void setBackground(int background) { - this.backgroundStart = background; - this.backgroundEnd = background; - } - - /** - * Sets the new start color for the gradient of the tooltip background (top edge). - * - * @param backgroundStart the new start color for the tooltip background - */ - public void setBackgroundStart(int backgroundStart) { - this.backgroundStart = backgroundStart; - } - - /** - * Sets the new end color for the gradient of the tooltip background (bottom edge). - * - * @param backgroundEnd the new end color for the tooltip background - */ - public void setBackgroundEnd(int backgroundEnd) { - this.backgroundEnd = backgroundEnd; - } - - /** - * {@return the gradient start color for the tooltip border (top edge)} - */ - public int getBorderStart() { - return borderStart; - } - - /** - * Sets the new start color for the gradient of the tooltip border (top edge). - * - * @param borderStart the new start color for the tooltip border - */ - public void setBorderStart(int borderStart) { - this.borderStart = borderStart; - } - - /** - * {@return the gradient end color for the tooltip border (bottom edge)} - */ - public int getBorderEnd() { - return borderEnd; - } - - /** - * Sets the new end color for the gradient of the tooltip border (bottom edge). - * - * @param borderEnd the new end color for the tooltip border - */ - public void setBorderEnd(int borderEnd) { - this.borderEnd = borderEnd; - } - - /** - * {@return the original tooltip background's gradient start color (top edge)} - */ - public int getOriginalBackgroundStart() { - return originalBackground; + public Texture(ItemStack stack, GuiGraphics graphics, int x, int y, Font font, List components, @Nullable ResourceLocation texture) { + super(stack, graphics, x, y, font, components); + this.originalTexture = texture; + this.texture = texture; } /** - * {@return the original tooltip background's gradient end color (bottom edge)} + * {@return the original texture location given to the tooltip render method (may originate from {@link DataComponents#TOOLTIP_STYLE})} */ - public int getOriginalBackgroundEnd() { - return originalBackground; + @Nullable + public ResourceLocation getOriginalTexture() { + return originalTexture; } /** - * {@return the original tooltip border's gradient start color (top edge)} + * {@return the texture location that will be used to render the tooltip} */ - public int getOriginalBorderStart() { - return originalBorderStart; + @Nullable + public ResourceLocation getTexture() { + return texture; } /** - * {@return the original tooltip border's gradient end color (bottom edge)} + * Set the texture to use for the tooltip background and frame or {@code null} to use the default textures. + *

+ * The given {@link ResourceLocation} will be prefixed with {@code tooltip/} and suffixed with {@code _background} + * and {@code _frame} to determine the background and frame texture respectively */ - public int getOriginalBorderEnd() { - return originalBorderEnd; + public void setTexture(@Nullable ResourceLocation texture) { + this.texture = texture; } } } diff --git a/tests/src/main/java/net/neoforged/neoforge/oldtest/client/CustomTooltipTest.java b/tests/src/main/java/net/neoforged/neoforge/oldtest/client/CustomTooltipTest.java index 1057944878..cb83eda472 100644 --- a/tests/src/main/java/net/neoforged/neoforge/oldtest/client/CustomTooltipTest.java +++ b/tests/src/main/java/net/neoforged/neoforge/oldtest/client/CustomTooltipTest.java @@ -18,6 +18,7 @@ import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; @@ -115,6 +116,8 @@ public static void onRegisterClientTooltipComponentFactories(RegisterClientToolt } private static class ClientEventHandler { + private static final ResourceLocation TEXTURE = ResourceLocation.fromNamespaceAndPath(ID, "test"); + @SubscribeEvent public static void gatherTooltips(RenderTooltipEvent.GatherComponents event) { if (event.getItemStack().getItem() == Items.STICK) { @@ -126,12 +129,9 @@ public static void gatherTooltips(RenderTooltipEvent.GatherComponents event) { } @SubscribeEvent - public static void preTooltip(RenderTooltipEvent.Color event) { + public static void preTooltip(RenderTooltipEvent.Texture event) { if (event.getItemStack().getItem() == Items.APPLE) { - event.setBackgroundStart(0xFF0000FF); - event.setBackgroundEnd(0xFFFFFF00); - event.setBorderStart(0xFFFF0000); - event.setBorderEnd(0xFF000011); + event.setTexture(TEXTURE); } } } diff --git a/tests/src/main/resources/assets/custom_tooltip_test/textures/gui/sprites/tooltip/test_background.png b/tests/src/main/resources/assets/custom_tooltip_test/textures/gui/sprites/tooltip/test_background.png new file mode 100644 index 0000000000..1f5f803bf5 Binary files /dev/null and b/tests/src/main/resources/assets/custom_tooltip_test/textures/gui/sprites/tooltip/test_background.png differ diff --git a/tests/src/main/resources/assets/custom_tooltip_test/textures/gui/sprites/tooltip/test_background.png.mcmeta b/tests/src/main/resources/assets/custom_tooltip_test/textures/gui/sprites/tooltip/test_background.png.mcmeta new file mode 100644 index 0000000000..c0435eb685 --- /dev/null +++ b/tests/src/main/resources/assets/custom_tooltip_test/textures/gui/sprites/tooltip/test_background.png.mcmeta @@ -0,0 +1,11 @@ +{ + "gui": { + "scaling": { + "type": "nine_slice", + "width": 100, + "height": 100, + "border": 9, + "stretch_inner": true + } + } +} diff --git a/tests/src/main/resources/assets/custom_tooltip_test/textures/gui/sprites/tooltip/test_frame.png b/tests/src/main/resources/assets/custom_tooltip_test/textures/gui/sprites/tooltip/test_frame.png new file mode 100644 index 0000000000..34c5e37f1a Binary files /dev/null and b/tests/src/main/resources/assets/custom_tooltip_test/textures/gui/sprites/tooltip/test_frame.png differ diff --git a/tests/src/main/resources/assets/custom_tooltip_test/textures/gui/sprites/tooltip/test_frame.png.mcmeta b/tests/src/main/resources/assets/custom_tooltip_test/textures/gui/sprites/tooltip/test_frame.png.mcmeta new file mode 100644 index 0000000000..e107d241a7 --- /dev/null +++ b/tests/src/main/resources/assets/custom_tooltip_test/textures/gui/sprites/tooltip/test_frame.png.mcmeta @@ -0,0 +1,11 @@ +{ + "gui": { + "scaling": { + "type": "nine_slice", + "width": 100, + "height": 100, + "border": 10, + "stretch_inner": true + } + } +}