Jump to content

Regarding custom HUD elements (replacing vanilla)


Zer0HD2

Recommended Posts

I'm currently working on a mod which will eventually modify a player's maximum health depending on a level, and replace the default GUI element with a custom health bar. I currently have the following working;

 

- Vanilla health bar no longer renders

- My custom health bar is rendering (in the center of the screen, this is one of my questions)

- My custom health bar is displaying the correct amount of health ((current health / max health) * bar width)

 

However, I have a few problems/questions:

 

Firstly, the hunger bar no longer renders. This is not a huge issue, as I can just make it render manually, but I'd rather not cancel that particular part of the RenderGameOverlayEvent. Is there any way to only cancel the rendering of the health bar, not the hunger bar?

 

Second, how would I go about finding the X and Y coords of the top-left corner of the VANILLA health (and hunger) bar? As in, the location on the screen where it will render. I currently am using event.resolution.getScaledHeight() / 2 (and width for the x position) to get the center of the screen, but thanks to the helpful variable naming in Minecraft's code, I haven't been able to figure out how they get the position on the screen.

 

Lastly, how exactly would I go about updating the player's maximum health, and do I need to store this in their extended properties (and use packets to sync between client and server)? I already have extended properties set up for the levelling system, and can grab the user's properties (level, exp, etc) at any time.

 

Thanks,

 

Zer0

Link to comment
Share on other sites

When you are drawing something, the point (0,0) is the top left corner. If you were having your health bar drawn in the middle, then it must have been drawing at width / 2 and height / 2

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

When you are drawing something, the point (0,0) is the top left corner. If you were having your health bar drawn in the middle, then it must have been drawing at width / 2 and height / 2

 

That is precisely what I said in my first post. I had it there for testing. I've got the positioning sorted now, anyway.

Link to comment
Share on other sites

You never said that. I was just pointing out what I hadn't read in your post.

 

All crap aside, here is what you want for one of your questions:

@SubscribeEvent
public void on(RenderGameOverlayEvent.PRE event)
{
        if (event.type.equals(RenderGameOverlayEvent.ElementType.HEALTH)
        {
                event.setCanceled(true); // Cancels the rendering of the HEALTH bar. Take a look at all the different ElementType's
        }
}

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

Thanks, but my problem is not cancelling the rendering of the health bar - it's stopping that from cancelling the rendering of the hunger bar. Right now if I stop the health bar from rendering, the hunger bar doesn't render either. I would prefer to implement a custom hunger bar, but I've been unable to get it to display the correct value. What I wanted to know was how to stop the event from cancelling when it renders the hunger bar.

Link to comment
Share on other sites

>.> I said look into the element types didn't I? Yes? Change HEALTH to FOOD and it will only cancel the food bar. Use HEALTH and it will only cancel the health bar.

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

Okay. First, I apologize for want to punch your face in. I get exasperated easily.

 

Secondly. If you use this code:


@SubscribeEvent
public void on(RenderGameOverlayEvent.PRE event)
{
        if (event.type.equals(RenderGameOverlayEvent.ElementType.HEALTH)
        {
                event.setCanceled(true); // Cancels the rendering of the HEALTH bar. Take a look at all the different ElementType's
        }
}

 

Only the health bar should be canceled, not the food bar. That is what you were asking, no?

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

@SideOnly(Side.CLIENT)
public class GuiHealthBar extends Gui 
{
private Minecraft mc;
private static final ResourceLocation texLoc = new ResourceLocation("mchardcore", "textures/gui/health_bar.png");

public GuiHealthBar(Minecraft mc)
{
	super();
	this.mc = mc;
}

@SubscribeEvent
public void onRenderHealthBar(RenderGameOverlayEvent.Pre event)
{
	if(event.type == ElementType.HEALTH)
	{
		event.setCanceled(true);

		EntityPlayer player = this.mc.thePlayer;

		float health = player.getHealth();
		float maxHealth = player.getMaxHealth();

		int healthBarWidth = (int)(((float)health/maxHealth)*79);

		int xPos = event.resolution.getScaledWidth() / 2 - 91;
		int yPos = event.resolution.getScaledHeight() - 40;

		GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
		GL11.glDisable(GL11.GL_LIGHTING);

		this.mc.getTextureManager().bindTexture(texLoc);

		this.drawTexturedModalRect(xPos, yPos, 0, 0, 81, 9);
		this.drawTexturedModalRect(xPos+1, yPos+1, 0, 9, healthBarWidth, 7);

	}
}
}

Link to comment
Share on other sites

Try putting a else statement after the if statement which does event.setCanceled(false); so it de-cancels the rendering if it isn't the health bar.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Yep, I tried adding an else statement - still cancels the rendering of everything after the health bar.

 

Kwibble: I did try changing it to the if statement you provided, but it made no difference, so I changed it back to that purely because it's easier to read.

Link to comment
Share on other sites

So I decided that rather than trying to get the default hunger bar to render, I would create a new HUD instead (seems weird that this was actually easier than getting the default elements to render). However, I now have a new fun little bug. Where the old health and hunger bars would have been, I now have weird green shapes that look like they would have been the outlines for the default bars.

 

ii6dT0q.jpg

 

They are coloured green because I set the colour of the string (number in the HUD) to green, as when I change that colour, the colour of the phantom health bar changes too. Therefore, it must be rendering after the event I have cancelled, as the string is the last thing I render to prevent it from turning the armour and air bars green (the grey one is my new armour bar, turns out that got cancelled too).

 

Here is the code for the GUI class: http://pastebin.com/D2jrErz8

 

Does anyone know why this happens or how to fix it?

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.