Fix ItemStack context in RenderTooltipEvent.Pre (#10655) (#10658)

This commit is contained in:
Matt authored and GitHub committed 2025-11-14 07:08:10 -08:00
1 parent 60110cd916
commit fae5c92daf
7 files changed
+151 -22

No files matched your search

@@ -636,8 +636,8 @@ public class ForgeHooksClient {
return stackFont == null ? fallbackFont : stackFont;
}
public static @Nullable RenderTooltipEvent.Pre onRenderTooltipPre(@NotNull ItemStack stack, GuiGraphics graphics, int x, int y, int screenWidth, int screenHeight, @NotNull List<ClientTooltipComponent> components, @NotNull Font fallbackFont, @NotNull ClientTooltipPositioner positioner) {
var preEvent = new RenderTooltipEvent.Pre(stack, graphics, x, y, screenWidth, screenHeight, getTooltipFont(stack, fallbackFont), components, positioner);
public static @Nullable RenderTooltipEvent.Pre onRenderTooltipPre(@NotNull ItemStack stack, GuiGraphics graphics, int x, int y, int screenWidth, int screenHeight, @NotNull List<ClientTooltipComponent> components, @NotNull Font fallbackFont, @NotNull ClientTooltipPositioner positioner, @Nullable ResourceLocation background) {
var preEvent = new RenderTooltipEvent.Pre(stack, graphics, x, y, screenWidth, screenHeight, getTooltipFont(stack, fallbackFont), components, positioner, background);
return RenderTooltipEvent.Pre.BUS.post(preEvent) ? null : preEvent;
}
@@ -35,7 +35,6 @@ import java.util.List;
*
* @see RenderTooltipEvent.GatherComponents
* @see RenderTooltipEvent.Pre
* @see RenderTooltipEvent.Background
*/
public abstract sealed class RenderTooltipEvent extends MutableEvent implements InheritableEvent {
public static final EventBus<RenderTooltipEvent> BUS = EventBus.create(RenderTooltipEvent.class);
@@ -117,12 +116,11 @@ public abstract sealed class RenderTooltipEvent extends MutableEvent implements
* Fired when a tooltip gathers the {@link TooltipComponent}s to be rendered, before any text wrapping or processing.
* The list of components and the maximum width of the tooltip can be modified through this event.
*
* <p>This event is {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}.
* <p>This event is {@linkplain Cancellable cancellable}.
* 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.Background} to not be fired.</p>
* the corresponding {@link RenderTooltipEvent.Pre} to not be fired.</p>
*
* <p>This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
* Fired only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
public static final class GatherComponents extends MutableEvent implements Cancellable {
public static final CancellableEventBus<GatherComponents> BUS = CancellableEventBus.create(GatherComponents.class);
@@ -204,14 +202,12 @@ public abstract sealed class RenderTooltipEvent extends MutableEvent implements
/**
* Fired <b>before</b> the tooltip is rendered.
* This can be used to modify the positioning and font of the tooltip.
* This can be used to modify the positioning, font, and background of the tooltip.
*
* <p>This event is {@linkplain Cancelable 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.Background} will not be fired.</p>
* <p>This event is {@linkplain Cancellable cancellable}.
* If this event is cancelled, then the tooltip will not be rendered.
*
* <p>This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
* Fired only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
public static final class Pre extends RenderTooltipEvent implements Cancellable {
public static final CancellableEventBus<Pre> BUS = CancellableEventBus.create(Pre.class);
@@ -219,14 +215,20 @@ public abstract sealed class RenderTooltipEvent extends MutableEvent implements
private final int screenWidth;
private final int screenHeight;
private final ClientTooltipPositioner positioner;
@Nullable
private ResourceLocation background;
@Nullable
private final ResourceLocation originalBackground;
@ApiStatus.Internal
public Pre(@NotNull ItemStack stack, GuiGraphics graphics, int x, int y, int screenWidth, int screenHeight, @NotNull Font font, @NotNull List<ClientTooltipComponent> components, @NotNull ClientTooltipPositioner positioner)
public Pre(@NotNull ItemStack stack, GuiGraphics graphics, int x, int y, int screenWidth, int screenHeight, @NotNull Font font, @NotNull List<ClientTooltipComponent> components, @NotNull ClientTooltipPositioner positioner, @Nullable ResourceLocation background)
{
super(stack, graphics, x, y, font, components);
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
this.positioner = positioner;
this.background = background;
this.originalBackground = background;
}
/**
@@ -282,17 +284,40 @@ public abstract sealed class RenderTooltipEvent extends MutableEvent implements
{
this.y = y;
}
}
/**
* @return the potentially modified background's prefix, can be null for default
*/
public @Nullable ResourceLocation getBackground() {
return background;
}
/**
* Sets the new prefix for the background texture
*/
public void setBackground(@Nullable ResourceLocation background) {
this.background = background;
}
/**
* @return the original tooltip background's prefix, can be null for default
*/
public @Nullable ResourceLocation getOriginalBackground() {
return originalBackground;
}
}
/**
* Fired when the tooltip background prefix is determined.
* This can be used to modify the textures to be used for the tooltip background.
*
* <p>This event is not {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}.</p>
* <p>This event is not {@linkplain Cancellable cancellable}.</p>
*
* <p>This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
* only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*
* @deprecated Behavior will be merged into {@link RenderTooltipEvent.Pre}.
*/
@Deprecated(forRemoval=true, since="1.21.10")
public static final class Background extends RenderTooltipEvent {
public static final EventBus<Background> BUS = EventBus.create(Background.class);