Skip to content

Commit

Permalink
[1.21] Add GatherEffectScreenTooltipsEvent (#1240)
Browse files Browse the repository at this point in the history
This new event allows changing the tooltip shown when hovering a potion effect in the player inventory.
  • Loading branch information
Shadows-of-Fire authored Jul 7, 2024
1 parent 6bd9373 commit 901e469
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
this.renderBackgrounds(p_281945_, i, k, iterable, flag);
this.renderIcons(p_281945_, i, k, iterable, flag);
if (flag) {
@@ -74,6 +_,8 @@
this.getEffectName(mobeffectinstance),
MobEffectUtil.formatDuration(mobeffectinstance, 1.0F, this.minecraft.level.tickRateManager().tickrate())
);
+ // Neo: Allow mods to adjust the tooltip shown when hovering a mob effect.
+ list = net.neoforged.neoforge.client.ClientHooks.getEffectTooltip(this, mobeffectinstance, list);
p_281945_.renderTooltip(this.font, list, Optional.empty(), p_282601_, p_282335_);
}
}
@@ -99,6 +_,11 @@
int i = this.topPos;

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/neoforged/neoforge/client/ClientHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.TitleScreen;
import net.minecraft.client.gui.screens.inventory.EffectRenderingInventoryScreen;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipPositioner;
import net.minecraft.client.model.HumanoidModel;
Expand Down Expand Up @@ -143,6 +144,7 @@
import net.neoforged.neoforge.client.event.ComputeFovModifierEvent;
import net.neoforged.neoforge.client.event.CustomizeGuiOverlayEvent;
import net.neoforged.neoforge.client.event.EntityRenderersEvent;
import net.neoforged.neoforge.client.event.GatherEffectScreenTooltipsEvent;
import net.neoforged.neoforge.client.event.InputEvent;
import net.neoforged.neoforge.client.event.ModelEvent;
import net.neoforged.neoforge.client.event.MovementInputUpdateEvent;
Expand Down Expand Up @@ -1055,4 +1057,20 @@ public static <T> RegistryLookup<T> resolveLookup(ResourceKey<? extends Registry
}
return null;
}

/**
* Fires the {@link GatherEffectScreenTooltipsEvent} and returns the resulting tooltip lines.
* <p>
* Called from {@link EffectRenderingInventoryScreen#renderEffects} just before {@link GuiGraphics#renderTooltip(Font, List, Optional, int, int)} is called.
*
* @param screen The screen rendering the tooltip.
* @param effectInst The effect instance whose tooltip is being rendered.
* @param tooltip An immutable list containing the existing tooltip lines, which consist of the name and the duration.
* @return The new tooltip lines, modified by the event.
*/
public static List<Component> getEffectTooltip(EffectRenderingInventoryScreen<?> screen, MobEffectInstance effectInst, List<Component> tooltip) {
var event = new GatherEffectScreenTooltipsEvent(screen, effectInst, tooltip);
NeoForge.EVENT_BUS.post(event);
return event.getTooltip();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.client.event;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.gui.screens.inventory.EffectRenderingInventoryScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.world.effect.MobEffectInstance;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.Event;

/**
* This event is called when an {@link EffectRenderingInventoryScreen} draws the tooltip lines for a hovered {@link MobEffectInstance}.
* It can be used to modify the tooltip.
* <p>
* This event is only fired on the {@linkplain Dist#CLIENT physical client}.
*/
public class GatherEffectScreenTooltipsEvent extends Event {
protected final EffectRenderingInventoryScreen<?> screen;
protected final MobEffectInstance effectInst;
protected final List<Component> tooltip;

public GatherEffectScreenTooltipsEvent(EffectRenderingInventoryScreen<?> screen, MobEffectInstance effectInst, List<Component> tooltip) {
this.screen = screen;
this.effectInst = effectInst;
this.tooltip = new ArrayList<>(tooltip);
}

/**
* @return The screen which will be rendering the tooltip lines.
*/
public EffectRenderingInventoryScreen<?> getScreen() {
return this.screen;
}

/**
* @return The effect whose tooltip is being drawn.
*/
public MobEffectInstance getEffectInstance() {
return this.effectInst;
}

/**
* @return A mutable list of tooltip lines.
*/
public List<Component> getTooltip() {
return this.tooltip;
}
}

0 comments on commit 901e469

Please sign in to comment.