Skip to content

Commit

Permalink
Make heart types an extensible enum and add an event to modify them (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gingershaped authored Jun 4, 2024
1 parent 4a7d634 commit b13c445
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
41 changes: 34 additions & 7 deletions patches/net/minecraft/client/gui/Gui.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,42 @@
this.toolHighlightTimer = (int)(40.0 * this.minecraft.options.notificationDisplayTime().get());
} else if (this.toolHighlightTimer > 0) {
this.toolHighlightTimer--;
@@ -1287,6 +_,11 @@
p_282761_.drawString(font, SAVING_TEXT, p_282761_.guiWidth() - j - 10, p_282761_.guiHeight() - 15, k);
}
@@ -1289,8 +_,13 @@
}
+ }
+
}
+ @org.jetbrains.annotations.ApiStatus.Internal
+ public void initModdedOverlays() {
+ this.layerManager.initModdedLayers();
}
+ }
+
@OnlyIn(Dist.CLIENT)
- public static enum HeartType {
+ public static enum HeartType implements net.neoforged.neoforge.common.IExtensibleEnum {
CONTAINER(
new ResourceLocation("hud/heart/container"),
new ResourceLocation("hud/heart/container_blinking"),
@@ -1406,8 +_,23 @@
} else {
gui$hearttype = NORMAL;
}
+ gui$hearttype = net.neoforged.neoforge.event.EventHooks.firePlayerHeartTypeEvent(p_168733_, gui$hearttype);

return gui$hearttype;
+ }
+
+ public static HeartType create(
+ String name,
+ ResourceLocation full,
+ ResourceLocation fullBlinking,
+ ResourceLocation half,
+ ResourceLocation halfBlinking,
+ ResourceLocation hardcoreFull,
+ ResourceLocation hardcoreFullBlinking,
+ ResourceLocation hardcoreHalf,
+ ResourceLocation hardcoreHalfBlinking
+ ) {
+ throw new IllegalStateException("Enum not extended");
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/net/neoforged/neoforge/event/EventHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.advancements.AdvancementHolder;
import net.minecraft.advancements.AdvancementProgress;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
Expand Down Expand Up @@ -144,6 +145,7 @@
import net.neoforged.neoforge.event.entity.player.PlayerDestroyItemEvent;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.event.entity.player.PlayerFlyableFallEvent;
import net.neoforged.neoforge.event.entity.player.PlayerHeartTypeEvent;
import net.neoforged.neoforge.event.entity.player.PlayerRespawnPositionEvent;
import net.neoforged.neoforge.event.entity.player.PlayerSetSpawnEvent;
import net.neoforged.neoforge.event.entity.player.PlayerSpawnPhantomsEvent;
Expand Down Expand Up @@ -881,6 +883,18 @@ public static void firePlayerSmeltedEvent(Player player, ItemStack smelted) {
NeoForge.EVENT_BUS.post(new PlayerEvent.ItemSmeltedEvent(player, smelted));
}

/**
* Called by {@link Gui.HeartType#forPlayer} to allow for modification of the displayed heart type in the
* health bar.
*
* @param player The local {@link Player}
* @param heartType The {@link Gui.HeartType} which would be displayed by vanilla
* @return The heart type which should be displayed
*/
public static Gui.HeartType firePlayerHeartTypeEvent(Player player, Gui.HeartType heartType) {
return NeoForge.EVENT_BUS.post(new PlayerHeartTypeEvent(player, heartType)).getType();
}

/**
* Fires {@link EntityTickEvent.Pre}. Called from the head of {@link LivingEntity#tick()}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.event.entity.player;

import net.minecraft.client.gui.Gui;
import net.minecraft.world.entity.player.Player;

/**
* Fired by {@link Gui.HeartType#forPlayer} to allow mods to change the heart sprite which is displayed in the player's
* health bar.
*
* <p>
* This event is fired only on the client.
*/
public class PlayerHeartTypeEvent extends PlayerEvent {
private final Gui.HeartType originalType;
private Gui.HeartType type;

public PlayerHeartTypeEvent(Player player, Gui.HeartType type) {
super(player);
this.type = type;
this.originalType = type;
}

/**
* @return The original heart type which would be displayed by vanilla.
*/
public Gui.HeartType getOriginalType() {
return originalType;
}

/**
* @return The heart type which will be displayed on the health bar.
*/
public Gui.HeartType getType() {
return type;
}

/**
* Set the heart sprite which will be displayed on the {@link Player}'s health bar.
*
* @param type The {@link Gui.HeartType} to display
*/
public void setType(Gui.HeartType type) {
this.type = type;
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ protected net.minecraft.client.gui.Gui renderPortalOverlay(Lnet/minecraft/client
public net.minecraft.client.gui.Gui renderHotbar(FLnet/minecraft/client/gui/GuiGraphics;)V # renderHotbar
public net.minecraft.client.gui.Gui renderEffects(Lnet/minecraft/client/gui/GuiGraphics;)V # renderEffects
protected net.minecraft.client.gui.Gui drawBackdrop(Lnet/minecraft/client/gui/GuiGraphics;Lnet/minecraft/client/gui/Font;III)V # drawBackdrop
public net.minecraft.client.gui.Gui$HeartType
protected net.minecraft.client.gui.components.AbstractButton SPRITES
protected net.minecraft.client.gui.components.AbstractSelectionList$Entry list # list
protected net.minecraft.client.gui.components.AbstractSliderButton getSprite()Lnet/minecraft/resources/ResourceLocation; # getSprite
Expand Down

0 comments on commit b13c445

Please sign in to comment.