[TUTORIAL] Percentage Based Bar In Overlay Tutorial - Health Bar Example

Started by Rikurob on Mon, 08/22/2022 - 07:18

Topic category: User side tutorials

Last seen on 07:52, 21. Nov 2022
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
[TUTORIAL] Percentage Based Bar In Overlay Tutorial - Health Bar Example
Mon, 08/22/2022 - 14:34 (edited)

I have seen a lot of people asking for this and I recently figured it out myself. So making a bar in an overlay is actually relatively easy with custom code. By following this tutorial, you can make a bar based on health. You can change the health values to whatever values you see fit.

 

Step 1: Make your bar.png and your barframe.png

Image - My Bar

Image - My Bar Frame

Step 2:Make your overlay and place your bar/frame where you would like it to go, as well as adding your conditional statement for the bar to appear. (You can always edit the position later in the code need be, which I didn't do for the tutorial but I highly recommend doing it to get the positioning right for all scales until mcreator fixes the way it handles scaling in screens.)

 

Step 3: Open your OverlayNameOverlay.java and declare 2 doubles and an integer under the eventHandler class declaration. Four this tutorial the 2 doubles will be your health, and max health. Your integer will always be the bar length and it will be equal to one double divided by the other to get the ratio for the bar.

Under This:

        if (event.getType() == RenderGameOverlayEvent.ElementType.ALL) {

Declare your variables.

Variable Declarations (My Example):

double health = (entity.getHealth());
double maxHealth = (entity.getMaxHealth());
int barlength=(int)Math.round((86)*(health/maxHealth));

Replace the "86" with the initial length of your bar.png.

Step 4: Find the lines where your pictures are set them to be the lines below, replacing values where you need to for your positions, and pictures

Render Bar Code:

if (true) {
                Minecraft.getInstance().font.draw(event.getMatrixStack(), "" + (int)health, posX - 12, posY + 77, -12829636); 
                //Gets Health as Integer next to the bar

                RenderSystem.setShaderTexture(0, new ResourceLocation("tester:textures/barframe.png"));
                Minecraft.getInstance().gui.blit(event.getMatrixStack(), posX + 0, posY + 77, 0, 0, 90, 11, 90, 11);
                //Renders Bar Frame

                RenderSystem.setShaderTexture(0, new ResourceLocation("tester:textures/bartest.png"));
                Minecraft.getInstance().gui.blit(event.getMatrixStack(), posX + 2, posY + 79, 0, 0, barlength, 7, 86, 7);
                //Renders Bar of length "barlength"

            }

Replace the positions except for "barlength" with your values.

Step 5(Optional): I Suggest adding the following statement at the begining of the Overlay, but it is not required. under the eventHandler class declaration

if (entity == null)
            return;

 

Note: Mcreator has some issues with the default placement of overlays and guis when it comes to scaling. Until this is fixed to get the proper spot for each scale requires different custom code that you can find in another tutorial. This is only how to make the bar.

Edited by Rikurob on Mon, 08/22/2022 - 14:34
Last seen on 07:52, 21. Nov 2022
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Examples of it working …
Mon, 08/22/2022 - 07:23

Examples of it working (Placement could be better in my example, I did it quick for the tutorial)

 

 

Image

 

Image

 

Last seen on 07:52, 21. Nov 2022
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Using this technique, I am…
Mon, 08/22/2022 - 07:26

Using this technique, I am working on a Custom Bars mod for customizable Bars to replace Minecraft's default bars, similar to how Classic Bars used to work, and going to add extra bars that the player can set different nbt values in a configuration folder for customization, as well as built in compatibility for mods like survive!

Last seen on 07:52, 21. Nov 2022
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Also, I chose an arbitray…
Mon, 08/22/2022 - 07:30

Also, I chose an arbitray length of 86 for this bar to show it will work for any value, however it is best to use a value that is a multiple of your max value, as that always return an integer when getting the ratio and multiplying it by that value, as rounding it brings it to the nearest pixel. For Example, for health you should always use a multiple of 20 as the player's max health is 20 and the it will always be on the correct pixel.

Last seen on 07:52, 21. Nov 2022
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Sorry I forgot, add these…
Mon, 08/22/2022 - 08:02

Sorry I forgot, add these imports to the beginning of the java file:

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
Last seen on 07:52, 21. Nov 2022
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Scratch that last thing I…
Mon, 08/22/2022 - 08:07

Scratch that last thing I lied that was only for what I was currently working on oops sorry.

Last seen on 07:52, 21. Nov 2022
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
double saturation =…
Mon, 08/22/2022 - 08:12
            double saturation = saturation;
            double maxSaturation = 20;
            int barLength=0;
            if (saturation<=20){
                barLength=(int)Math.round((80)*(air/maxAir));
            }
            else{
                barLength=80
            }

This is an example of a code segment used if the value can be over the max value to determine barLength

Example, saturation can be more than the max foodvalue, but you only want to show up to 20 saturation

Last seen on 07:52, 21. Nov 2022
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
I didn't change air to…
Mon, 08/22/2022 - 08:15

I didn't change air to saturation there yet but same idea.

Last seen on 07:52, 21. Nov 2022
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Solved the GUI Scaling issue…
Mon, 08/22/2022 - 14:58

Solved the GUI Scaling issue. Set the pos y=h instead of h/2, then edit your values accordingly

Last seen on 07:30, 24. Sep 2022
Joined Mar 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Nice. Can used to make a…
Sat, 08/27/2022 - 21:31

Nice. Can used to make a accurate custom furnace

Last seen on 13:03, 29. Oct 2022
Joined Jul 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Is There A Way To Do This…
Wed, 08/31/2022 - 21:14

Is There A Way To Do This For A GUI?

If So Could You Please Make A Tutorial

Last seen on 13:03, 29. Oct 2022
Joined Jul 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
im trying to make a…
Wed, 08/31/2022 - 21:16

im trying to make a nutrients gui for my crops mod im using custom vars 

Last seen on 07:52, 21. Nov 2022
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Yeah its the same principle,…
Wed, 08/31/2022 - 23:23

Yeah its the same principle, GUI's are a little different is all, and the scaling is a little different with mcreator as well. I can make a tutorial for it. That being said, for now atleast, you could follow this tutorial and set your overlay to overlay over the GUI screen only!