Jump to content

Rendering an overlay over the health bar [1.12.2]


theishiopian

Recommended Posts

I have a mod called immortality that add a potion that keeps the player's health from dropping below 20%. The mod itself works just fine, but I want to add an overlay effect to the health bar to show the player how low their health can go, specifically by covering 20% of the health bar (usually 2 hearts) in custom hearts, kinda like how poison turns your hearts green. I have some rendering code, but its not rendering anything.

 

Here's the event handler Im using, I've confirmed it works:

 	private static Minecraft mc = Minecraft.getMinecraft();
	private static final ImmortalityBarRenderer renderer = new ImmortalityBarRenderer(mc);
    
    @SubscribeEvent//render immortal hearts over player hearts
	public void onDrawBar(RenderGameOverlayEvent.Post event)
	{
		EntityPlayerSP entityPlayerSP = Minecraft.getMinecraft().player;
		
		if (entityPlayerSP == null) return;
		
		if(event.getType() == ElementType.HEALTH)
		{
			renderer.renderStatusBar(event.getResolution().getScaledWidth(), event.getResolution().getScaledHeight());
		}
	}

 

And here is my renderer class, I have confirmed that renderStatusBar is getting called, which means its something in my render code:

	public class ImmortalityBarRenderer extends Gui
    {
		private static final ResourceLocation overlay = new ResourceLocation(Immortality.MODID +":textures/gui/immortal_heart.png");
	
		private Minecraft mc;
		

		public ImmortalityBarRenderer(Minecraft mc) 
		{
			this.mc = mc;
		}
		
		

		public void renderStatusBar(int screenWidth, int screenHeight)
		{
			
			World world = mc.world;
			EntityPlayer player = mc.player;
			final int barHeight = 9;
			final int barWidth = (int) (9*((Math.ceil(player.getMaxHealth()/5))/2));
			final int expBarSpace = 3;
			
			//push matrix
			GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
		    GL11.glPushMatrix();
		    
			    GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
			    mc.renderEngine.bindTexture(overlay);
			    
			    final int vanillaExpLeftX = screenWidth / 2 - 91; // leftmost edge of the experience bar
			    final int vanillaExpTopY = screenHeight - 32 + 3;  // top of the experience bar
			    
			    GL11.glTranslatef(vanillaExpLeftX, vanillaExpTopY - expBarSpace - barHeight, 0);
			    
			    drawTexturedModalRect(0, 0, 0, 0, barWidth, barHeight);
		    
		    GL11.glPopAttrib();
		    GL11.glPopMatrix();
		}
    }

 

I know next to nothing about rendering on the HUD, I've just been replicating examples I've seen. If I've done something obviously wrong, please point it out so I don't do it again.

Link to comment
Share on other sites

4 hours ago, theishiopian said:

I have some rendering code, but its not rendering anything.

Have you confirmed that your render code is executing? Is the event being called? Is the event ever have the exact fields (Does the Post event ever fire with ElementType.HEALTH)?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

34 minutes ago, theishiopian said:

As I said in my post, I have confirmed the event is firing, and that the render code is being called.

My bad. Try removing your translate call.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

10 hours ago, theishiopian said:

mc.renderEngine.bindTexture(overlay);

Instead do mc.getTextureManager().bindTexture(...)

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Just now, theishiopian said:

That makes sense, always better to use a getter instead of the field. Regardless, it did not fix the issue. 

Switch your barWidth to a constant.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

53 minutes ago, theishiopian said:

That did not help either. As a side note, I need to be able to vary the amount of hearts the bar covers since the effect is based on a percentage of your health, is there a better way to do that?

What you are doing seems appropriate.

Can you post your image you are trying to render. And have you refreshed your workspace since you added it.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Ok, progress! Im now seeing 6 dark red rectangles on the oposite side of the HUD from the health bar, that twitch occasionally.2019-08-08_01_52_16.png.9bd6b56cf59ad2b27ca362d175f43351.png

 

code used:

		public void renderStatusBar(int screenWidth, int screenHeight)
		{
			//System.out.println("called");
			World world = mc.world;
			EntityPlayer player = mc.player;
			final int barHeight = 9;
			//final int barWidth = (int) (9*((Math.ceil(player.getMaxHealth()/5))/2));
			final int barWidth = 18;
			final int expBarSpace = 3;

		    mc.getTextureManager().bindTexture(overlay);
		    final int vanillaExpLeftX = screenWidth / 2 - 91; // leftmost edge of the experience bar
		    final int vanillaExpTopY = screenHeight - 32 + 3;  // top of the experience bar
		    
		    //GL11.glTranslatef(vanillaExpLeftX, vanillaExpTopY - expBarSpace - barHeight, 0);
		    
		    //drawTexturedModalRect(0, 0, 0, 0, barWidth, barHeight);
		    this.drawModalRectWithCustomSizedTexture(0, 0, 0, 0, 9, 9, 9, 9);
		}

 

I should clarify that this is still not the desired effect, but at least its rendering now.

 

EDIT:

I just noticed the icons rendering at the top of the screen! Now i just gotta translate them to the right spot. And get rid of those artifacts on the HUD.

Edited by theishiopian
clarification.
Link to comment
Share on other sites

Ah, that makes a lot of sense. I figured I would have to clean up after myself. Also, whats the best way to make my hearts jiggle when the player is on low health, along with the vanilla ones?

 

EDIT: regarding translation, thats what I meant, yeah.

EDIT2: rebinding the icon textures fixed the hunger bar.

Edited by theishiopian
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.