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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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