From 62fa5b26a7513a6287bc43c5b7a1f99af3c2e9fb Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 26 Jul 2026 13:46:12 -0400 Subject: [PATCH] Split hotbar rendering into components, allow replacing renderers (#10869) --- .../net/minecraft/client/gui/Gui.java.patch | 17 ++++- .../client/gui/overlay/ForgeLayeredDraw.java | 65 ++++++++++++++++++- .../modify_overlay_test/replace_renderer.json | 7 ++ .../debug/client/ModifyOverlayTest.java | 15 +++++ 4 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 src/test/generated/modify_overlay_test/data/forge/test_instance/modify_overlay_test/replace_renderer.json diff --git a/patches/minecraft/net/minecraft/client/gui/Gui.java.patch b/patches/minecraft/net/minecraft/client/gui/Gui.java.patch index bd12945d45..bf09645a09 100644 --- a/patches/minecraft/net/minecraft/client/gui/Gui.java.patch +++ b/patches/minecraft/net/minecraft/client/gui/Gui.java.patch @@ -54,9 +54,24 @@ p_282812_.blitSprite(RenderPipelines.GUI_TEXTURED, getMobEffectSprite(holder), k + 3, l + 3, 18, 18, ARGB.white(f)); } } -@@ -603,6 +_,10 @@ +@@ -602,7 +_,25 @@ + } } ++ // Forge: Maintain vanilla parity during FLD#BACKGROUND ++ public final void updateContextualInfo(final GuiGraphics graphics, DeltaTracker deltaTracker) { ++ var nextContextualInfo = this.nextContextualInfoState(); ++ if (nextContextualInfo != this.contextualInfoBar.getKey()) { ++ this.contextualInfoBar = Pair.of(nextContextualInfo, this.contextualInfoBarRenderers.get(nextContextualInfo).get()); ++ } ++ ++ this.contextualInfoBar.getValue().renderBackground(graphics, deltaTracker); ++ } ++ // Forge: Same as above but for FLD#CONTEXTUAL_INFO ++ public final void extractContextualInfoState(final GuiGraphics graphics, DeltaTracker deltaTracker) { ++ this.contextualInfoBar.getValue().render(graphics, deltaTracker); ++ } ++ public void renderSelectedItemName(GuiGraphics p_283501_) { + renderSelectedItemName(p_283501_, 0); + } diff --git a/src/main/java/net/minecraftforge/client/gui/overlay/ForgeLayeredDraw.java b/src/main/java/net/minecraftforge/client/gui/overlay/ForgeLayeredDraw.java index 895473f94a..64df8f136f 100644 --- a/src/main/java/net/minecraftforge/client/gui/overlay/ForgeLayeredDraw.java +++ b/src/main/java/net/minecraftforge/client/gui/overlay/ForgeLayeredDraw.java @@ -13,6 +13,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.resources.ResourceLocation; +import net.minecraft.client.gui.contextualbar.ContextualBarRenderer; import net.minecraftforge.client.event.ForgeEventFactoryClient; import net.minecraftforge.client.event.AddGuiOverlayLayersEvent; import org.jetbrains.annotations.ApiStatus; @@ -42,14 +43,29 @@ public final class ForgeLayeredDraw implements ForgeLayer { private final List bakedLayers = new ArrayList<>(); private final ResourceLocation name; + // Begin pre-sleep overlay public static final ResourceLocation PRE_SLEEP_STACK = ResourceLocation.withDefaultNamespace("pre_sleep_phase"); public static final ResourceLocation CAMERA_OVERLAY = ResourceLocation.withDefaultNamespace("camera_overlay"); public static final ResourceLocation CROSSHAIR = ResourceLocation.withDefaultNamespace("crosshair"); public static final ResourceLocation CHANGE_STRATUM = ResourceLocation.withDefaultNamespace("stratum_change"); + + // Begin hotbar public static final ResourceLocation HOTBAR_AND_DECOS = ResourceLocation.withDefaultNamespace("hotbar"); + public static final ResourceLocation ITEM_HOTBAR = ResourceLocation.withDefaultNamespace("item_hotbar"); + public static final ResourceLocation SPECTATOR_HOTBAR = ResourceLocation.withDefaultNamespace("spectator_hotbar"); + public static final ResourceLocation HEALTH_BAR = ResourceLocation.withDefaultNamespace("health_bar"); + public static final ResourceLocation VEHICLE_HEALTH = ResourceLocation.withDefaultNamespace("vehicle_health"); + public static final ResourceLocation BACKGROUND = ResourceLocation.withDefaultNamespace("background"); // Any layer needing contextual info should order at some point after this layer. + public static final ResourceLocation EXPERIENCE_LEVEL = ResourceLocation.withDefaultNamespace("experience_level"); + public static final ResourceLocation CONTEXTUAL_INFO = ResourceLocation.withDefaultNamespace("contextual_info"); + public static final ResourceLocation SELECTED_ITEM_NAME = ResourceLocation.withDefaultNamespace("selected_item_name"); + public static final ResourceLocation SPECTATOR_ACTION = ResourceLocation.withDefaultNamespace("spectator_action"); + + // End hotbar public static final ResourceLocation POTION_EFFECTS = ResourceLocation.withDefaultNamespace("potion_effects"); public static final ResourceLocation BOSS_OVERLAY = ResourceLocation.withDefaultNamespace("boss_overlay"); - + // End pre-sleep overlay + // Begin post-sleep overlay public static final ResourceLocation POST_SLEEP_STACK = ResourceLocation.withDefaultNamespace("post_sleep_phase"); public static final ResourceLocation DEMO_OVERLAY = ResourceLocation.withDefaultNamespace("demo"); public static final ResourceLocation DEBUG_OVERLAY = ResourceLocation.withDefaultNamespace("debug"); @@ -59,6 +75,7 @@ public final class ForgeLayeredDraw implements ForgeLayer { public static final ResourceLocation CHAT_OVERLAY = ResourceLocation.withDefaultNamespace("chat_overlay"); public static final ResourceLocation TAB_LIST = ResourceLocation.withDefaultNamespace("tab_list"); public static final ResourceLocation SUBTITLE_OVERLAY = ResourceLocation.withDefaultNamespace("subtitle"); + // End post-sleep overlay public static final ResourceLocation VANILLA_ROOT = ResourceLocation.withDefaultNamespace("vanilla_root"); public static final ResourceLocation SLEEP_OVERLAY = ResourceLocation.withDefaultNamespace("sleep_overlay"); @@ -91,6 +108,17 @@ public final class ForgeLayeredDraw implements ForgeLayer { return this; } + /** + * Adds a full draw stack and assumes condition is always true. + * Use {@linkplain ForgeLayeredDraw#putAbove} and {@linkplain ForgeLayeredDraw#putBelow} for fine location adjustment. + * @param name RL of the name to identify this stack with. + * @param layeredDraw the draw stack + * @return this + */ + public ForgeLayeredDraw add(ResourceLocation name, ForgeLayeredDraw layeredDraw) { + return add(name, layeredDraw, () -> true); + } + /** * Add a layer to the layer list. This layer will be at the end of the list, which means * it will be rendered last (on top) of already added layers. @@ -283,6 +311,28 @@ public final class ForgeLayeredDraw implements ForgeLayer { return this; } + /** + * Replaces the renderer of a single layer and logs whodunnit, this is not recommended for obvious reasons. + * Will not work if target is a ForgeLayeredDraw + * Prefer {@linkplain ForgeLayeredDraw#addConditionTo} + * @param expectedLocation Layer stack where the target should be. + * @param targetLayer Target whose renderer should be replaced. + * @param replacementRenderer Renderer to use instead. + * @return this + */ + public ForgeLayeredDraw replace(ResourceLocation expectedLocation, ResourceLocation targetLayer, ForgeLayer replacementRenderer) { + locateStack(expectedLocation).ifPresentOrElse((stack) -> { + if (stack.namedLayers.get(targetLayer) != null) { + stack.namedLayers.put(targetLayer, replacementRenderer); + LogUtils.getLogger().debug("ForgeLayer {} in {} was replaced by {}.", targetLayer, expectedLocation, replacementRenderer); + } else { + LogUtils.getLogger().debug("ForgeLayer {} in {} was attempted to be replaced by {}, but it did not exist.", targetLayer, expectedLocation, replacementRenderer); + } + }, () -> stackNotPresentWarning(expectedLocation)); + + return this; + } + /** * Propagate the layer order down to the inner render list after providing modders an opportunity to alter the list as they wish. * @apiNote Modders should NEVER be calling this method. @@ -385,11 +435,22 @@ public final class ForgeLayeredDraw implements ForgeLayer { @ApiStatus.Internal public static void init(Gui gui, Minecraft minecraft) { + BooleanSupplier spectator = () -> minecraft.gameMode.isAlwaysFlying(); + var hotbarCluster = new ForgeLayeredDraw(HOTBAR_AND_DECOS) + .addWithCondition(SPECTATOR_HOTBAR, (gg, dt) -> gui.getSpectatorGui().renderHotbar(gg), spectator) + .addWithCondition(ITEM_HOTBAR, gui::renderItemHotbar, () -> !spectator.getAsBoolean()) + .addWithCondition(HEALTH_BAR, (gg, dt) -> gui.renderPlayerHealth(gg), () -> minecraft.gameMode.canHurtPlayer()) + .add(VEHICLE_HEALTH, (gg, dt) -> gui.renderVehicleHealth(gg)) + .add(BACKGROUND, gui::updateContextualInfo) + .addWithCondition(EXPERIENCE_LEVEL, (gg, dt) -> ContextualBarRenderer.renderExperienceLevel(gg, minecraft.font, minecraft.player.experienceLevel), () -> minecraft.gameMode.hasExperience() && minecraft.player.experienceLevel > 0) + .add(CONTEXTUAL_INFO, gui::extractContextualInfoState) + .addWithCondition(SELECTED_ITEM_NAME, (gg, dt) -> gui.renderSelectedItemName(gg), () -> !spectator.getAsBoolean()) + .addWithCondition(SPECTATOR_ACTION, (gg, dt) -> gui.getSpectatorGui().renderAction(gg), spectator); var preSleepDraw = new ForgeLayeredDraw(PRE_SLEEP_STACK) .add(CAMERA_OVERLAY, gui::renderCameraOverlays) .add(CROSSHAIR, gui::renderCrosshair) .add(CHANGE_STRATUM, (gg, dt) -> gg.nextStratum()) - .add(HOTBAR_AND_DECOS, gui::renderHotbarAndDecorations) + .add(HOTBAR_AND_DECOS, hotbarCluster) .add(POTION_EFFECTS, gui::renderEffects) .add(BOSS_OVERLAY, gui::renderBossOverlay); var postSleepDraw = new ForgeLayeredDraw(POST_SLEEP_STACK) diff --git a/src/test/generated/modify_overlay_test/data/forge/test_instance/modify_overlay_test/replace_renderer.json b/src/test/generated/modify_overlay_test/data/forge/test_instance/modify_overlay_test/replace_renderer.json new file mode 100644 index 0000000000..f018811795 --- /dev/null +++ b/src/test/generated/modify_overlay_test/data/forge/test_instance/modify_overlay_test/replace_renderer.json @@ -0,0 +1,7 @@ +{ + "type": "minecraft:function", + "environment": "minecraft:default", + "function": "forge:modify_overlay_test/replace_renderer", + "max_ticks": 100, + "structure": "forge:empty3x3x3" +} \ No newline at end of file diff --git a/src/test/java/net/minecraftforge/debug/client/ModifyOverlayTest.java b/src/test/java/net/minecraftforge/debug/client/ModifyOverlayTest.java index 67a6d923a9..f92a237234 100644 --- a/src/test/java/net/minecraftforge/debug/client/ModifyOverlayTest.java +++ b/src/test/java/net/minecraftforge/debug/client/ModifyOverlayTest.java @@ -5,6 +5,7 @@ package net.minecraftforge.debug.client; +import net.minecraft.client.Minecraft; import net.minecraft.gametest.framework.GameTestHelper; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.client.event.AddGuiOverlayLayersEvent; @@ -39,6 +40,12 @@ public class ModifyOverlayTest extends BaseTestMod { private static final ForgeLayer layerC = (gg,tr) -> {}; private static final ResourceLocation layerCName = name("layer_c"); + private static final IForgeGameTestHelper.BoolFlag detectReplaceFlag = new IForgeGameTestHelper.BoolFlag("det_replace_flag"); + // Wrapper renderer since we just need to change the callee location to test + private static final ForgeLayer replacementRenderer = (gg, tracker) -> { + Minecraft.getInstance().gui.renderEffects(gg, tracker); + detectReplaceFlag.set(true); + }; private static final IForgeGameTestHelper.BoolFlag detectConditionFlag = new IForgeGameTestHelper.BoolFlag("det_cond_flag"); private static final IForgeGameTestHelper.BoolFlag detectConditionStackFlag = new IForgeGameTestHelper.BoolFlag("det_cond_stack_flag"); @@ -119,6 +126,13 @@ public class ModifyOverlayTest extends BaseTestMod { }); } + @GameTest + public static void replace_renderer(GameTestHelper helper) { + helper.assertTrue(detectReplaceFlag.getBool(), "Replace flag was not set."); + detectReplaceFlag.set(false); + helper.succeed(); + } + private void overlayTestListener(AddGuiOverlayLayersEvent event) { drawStack = event.getLayeredDraw(); var layeredDraw = event.getLayeredDraw(); @@ -128,6 +142,7 @@ public class ModifyOverlayTest extends BaseTestMod { // Layers have to be present to be ordered against, of course, but we tested for that already above. pEffects.addAbove(layerBName, POTION_EFFECTS, layerB); pEffects.addAbove(layerCName, layerBName, layerC); + layeredDraw.replace(PRE_SLEEP_STACK, POTION_EFFECTS, replacementRenderer); layeredDraw.addBelow(PRE_SLEEP_STACK, layerAName, layerBName, layerA); layeredDraw.addConditionTo(PRE_SLEEP_STACK, BOSS_OVERLAY, () -> { if (enableConditionFlag.getBool()) {