Jump to content

(UNSOLVED) [1.7.10/1.8] Help replacing the in-game HUD?


Ms_Raven

Recommended Posts

I want to replace the health/hunger/oxygen/armour/xp/jump bars with my own.

 

But I can't figure out how to cancel only those specific render types.

 

I try to check if it's rendering those types, load mine then cancel the event but nothing renders. Not mine, not the ones I want cancelled, not even the crosshair, inventory or pause menu.

 

How do I do this right?

 

public class HeadsUpDisplay extends Gui
{

private Minecraft mc;
private static final ResourceLocation texturepath = new ResourceLocation(This.MODID, "hud.png");
protected static final RenderItem itemRenderer = new RenderItem();

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

@SubscribeEvent
public void renderHUD(RenderGameOverlayEvent.Pre event)
{
	if (event.isCancelable() || event.type == ElementType.HEALTH || event.type == ElementType.FOOD
			|| event.type == ElementType.AIR || event.type == ElementType.HOTBAR || event.type == ElementType.EXPERIENCE
			|| event.type == ElementType.JUMPBAR)
	{
		render();
		event.setCanceled(true);
	}

}

private void render()
{

	EntityPlayer player = mc.thePlayer;
	FontRenderer fontrenderer = this.mc.fontRenderer;
	InventoryPlayer inventoryplayer = this.mc.thePlayer.inventory;

	int xPos = (mc.displayWidth / 2) - 90;
	int yPos = mc.displayHeight - 60;

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

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

	// Render main HUD background
	this.drawTexturedModalRect(xPos, yPos, 0, 0, 180, 54);

	// Render health
	int health = (int) (player.getHealth() * 2);
	int maxHealth = (int) (player.getMaxHealth() * 2);

	int healthWidth = (health / maxHealth) * 71;

	this.drawTexturedModalRect(xPos + 4, yPos + 21, 0, 185, healthWidth, 9);

	if (health <= 1)
	{
		this.drawTexturedModalRect(xPos + 66, yPos + 13, 4, 57, 4, 10);
	}
	String hp = health + "/" + maxHealth;
	int w1 = fontrenderer.getStringWidth(hp) / 2;
	fontrenderer.drawStringWithShadow(hp, xPos + 122, yPos + 13, 0x000000);

	// Render hunger
	int hunger = player.getFoodStats().getFoodLevel();

	int hungerWidth = (hunger / 20) * 71;

	this.drawTexturedModalRect(xPos + 105, yPos + 21, 9, 185, hungerWidth, 9);

	if (hunger <= 1)
	{
		this.drawTexturedModalRect(xPos + 110, yPos + 13, 0, 57, 4, 10);
	}

	String hDisplay = hunger + "/" + 20;
	int w2 = fontrenderer.getStringWidth(hDisplay) / 2;
	fontrenderer.drawStringWithShadow(hDisplay, xPos + 56, yPos + 13, 0x000000);

	// Render hotbar item selector
	this.drawTexturedModalRect(xPos + 6 + inventoryplayer.currentItem * 20, yPos - 34, 8, 57, 8, 7);

	// Render hotbar icons
	this.mc.mcProfiler.startSection("actionBar");
	GL11.glEnable(GL12.GL_RESCALE_NORMAL);
	RenderHelper.enableGUIStandardItemLighting();

	for (int i = 0; i < 9; ++i)
	{
		this.renderInventorySlot(i, xPos + i * 20, yPos + 37, 1);
	}

	RenderHelper.disableStandardItemLighting();
	GL11.glDisable(GL12.GL_RESCALE_NORMAL);
	this.mc.mcProfiler.endSection();
	GL11.glDisable(GL11.GL_BLEND);

	// Render horse jump bar
	if (this.mc.thePlayer.isRidingHorse())
	{
		this.mc.mcProfiler.startSection("jumpBar");
		float jump = this.mc.thePlayer.getHorseJumpPower();
		int jumpMax = 148;

		this.drawTexturedModalRect(xPos + 16, yPos - 19, 0, 90, (int) ((jump / jumpMax) * jumpMax), 19);

		if (jump > 0)
		{
			this.drawTexturedModalRect(xPos + 20, yPos + 8, 0, 109, 140, 4);
		}

		this.mc.mcProfiler.endSection();
	}

	// Render xp bar
	int xp = (int) player.experience;
	int toNextLevel = player.xpBarCap();
	int level = player.experienceLevel;

	String s = "" + level;
	int w3 = fontrenderer.getStringWidth(s) / 2;
	fontrenderer.drawStringWithShadow(s, xPos + 90, yPos + 20, 0x000000);

	if (level > 0)
	{
		this.drawTexturedModalRect(xPos + 20, yPos + 4, 20, 57, (xp / toNextLevel) * 140, 4);
	}

	// Render armour
	// Render oxygen

}

protected void renderInventorySlot(int slot, int x, int y, float tick)
{
	ItemStack itemstack = this.mc.thePlayer.inventory.mainInventory[slot];

	if (itemstack != null)
	{
		float f1 = (float) itemstack.animationsToGo - tick;

		if (f1 > 0.0F)
		{
			GL11.glPushMatrix();
			float f2 = 1.0F + f1 / 5.0F;
			GL11.glTranslatef((float) (x + , (float) (y + 12), 0.0F);
			GL11.glScalef(1.0F / f2, (f2 + 1.0F) / 2.0F, 1.0F);
			GL11.glTranslatef((float) (-(x + ), (float) (-(y + 12)), 0.0F);
		}

		itemRenderer.renderItemAndEffectIntoGUI(this.mc.fontRenderer, this.mc.getTextureManager(), itemstack, x, y);

		if (f1 > 0.0F)
		{
			GL11.glPopMatrix();
		}

		itemRenderer.renderItemOverlayIntoGUI(this.mc.fontRenderer, this.mc.getTextureManager(), itemstack, x, y);
	}
}
}

Link to comment
Share on other sites

RenderGameOverlayEvent#isCancable()

wil always return true, so you will always cancel rendering for everything.

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

Okay that was dumb.

 

But now after fixing that my HUD still won't render.

 

	@SubscribeEvent
public void renderHUD(RenderGameOverlayEvent.Pre event)
{
	if (event.isCancelable())
	{
		if (event.type == ElementType.HEALTH || event.type == ElementType.FOOD || event.type == ElementType.AIR
				|| event.type == ElementType.HOTBAR || event.type == ElementType.EXPERIENCE
				|| event.type == ElementType.JUMPBAR)
		{
			render();
			event.setCanceled(true);
		}
	}
}

 

If the event is cancelled after it renders or if I don't cancel it at all, it just shows a really glitchy-looking black-and-white version of the vanilla HUD.

 

kimf9cv.png

 

If the event is cancelled before it renders, the vanilla HUD doesn't render but mine still doesn't either.

 

Link to comment
Share on other sites

Hey so what I have found is that the rendering needs to be cancelled in a specific order and reimplemented in a specific order. In order to cancel Hunger, Health, Exp, and the storage...

 

@SubscribeEvent(priority=EventPriority.NORMAL)
public void onRenderPre(RenderGameOverlayEvent.Pre event) {

	if (event.type == ElementType.HOTBAR) {
		event.setCanceled(true);
		return;
	}

	if (event.type == ElementType.FOOD) {
		event.setCanceled(true);
		return;
	}
	if (event.type == ElementType.EXPERIENCE) {
		event.setCanceled(true);
		return;
	}

	if (event.type == ElementType.HEALTH) {
		event.setCanceled(true);
		return;
	}

   ///Im not positive if you need this statement but I kept it in otherwise I noticed weird glitches with my custom stuff
	if (event.type != ElementType.FOOD) {
			return;
	}
}

 

then in the RenderGameOverlay Post event render your new custom elements in the same order as above. Note, I am unsure where the air bar fits in I havent messed with it yet, lemme know if you figure that out. If you mess up the order you get weird glitches I found where some of your custom stuff will look weird. Im not sure why this happens but this seems to work perfectly for me.

 

@SubscribeEvent(priority=EventPriority.NORMAL)
public void RenderBattleGUI(RenderGameOverlayEvent.Post event) {

	if (event.type != ElementType.CROSSHAIRS) {
		return;
	}

	////////////////Render Custom Hot Bar////////////////////////////////////////////	


	////////////////Render Custom Food Bar////////////////////////////////////////////


	///////////////////Render Custom ExpBar////////////////////////////////////////////


	////////////////Render Custom HealthBar////////////////////////////////////////////

}

 

Also ---------------> Hit Thanks if you dont mind :)

Link to comment
Share on other sites

I render my custom gui hud elements as so in the event you see above

 

                GL11.glEnable(GL11.GL_BLEND);
	GL11.glDisable(GL11.GL_DEPTH_TEST);
	GL11.glDepthMask(false);
	GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	GL11.glDisable(GL11.GL_ALPHA_TEST);

                        ...//Custom Stuff...

                  ///Then the stuff you care about///

                 /////////ExpBar////////////////////////////////////////////
	this.mc.getTextureManager().bindTexture(expBarTexture);
	int expBarWidth = (int)(((float) (((int)expGained) -this.getExpFromLevel(id, level))/ (this.getExpFromLevel(id, level + 1)-this.getExpFromLevel(id, level))) * pixelWidthOfTexture);
	drawTexturedModalRect(width/2 -106, height- 32, 0, 0, 77, 9);
	drawTexturedModalRect(width/2 -106 + 2, height- 32 + 1, 0, 9, expBarWidth, 5);

	////////////////HealthBar////////////////////////////////////////////
	this.mc.getTextureManager().bindTexture(healthBarTexture);
	int healthBarWidth = (int)(((float) (int)this.mc.thePlayer.getHealth() / hpMax) * pixelWidthOfTexture);
	drawTexturedModalRect(width/2 -106, height- 40, 0, 0, 77, 9);
	drawTexturedModalRect(width/2 -106 + 2, height- 40 + 1, 0, 19, healthBarWidth, 5);
               
                  ...//.etc etc....

                  ///more custom stuff///

                GL11.glDisable(GL11.GL_BLEND);
	GL11.glEnable(GL11.GL_DEPTH_TEST);
	GL11.glDepthMask(true);

 

Again The Thank you btn is ---> that a way ish :)

Link to comment
Share on other sites

Unfortunately that still doesn't work. Nothing renders.

 

@SubscribeEvent
public void clear(RenderGameOverlayEvent.Pre event)
{
	if (event.type == ElementType.HOTBAR)
	{
		event.setCanceled(true);
		return;
	}

	if (event.type == ElementType.FOOD)
	{
		event.setCanceled(true);
		return;
	}
	if (event.type == ElementType.EXPERIENCE)
	{
		event.setCanceled(true);
		return;
	}

	if (event.type == ElementType.HEALTH)
	{
		event.setCanceled(true);
		return;
	}
	if (event.type == ElementType.JUMPBAR)
	{
		event.setCanceled(true);
		return;
	}
	if (event.type == ElementType.AIR)
	{
		event.setCanceled(true);
		return;
	}
	if (event.type == ElementType.ARMOR)
	{
		event.setCanceled(true);
		return;
	}

	if (event.type != ElementType.FOOD)
	{
		return;
	}
}

@SubscribeEvent
public void render(RenderGameOverlayEvent.Post event)
{
	EntityPlayer player = mc.thePlayer;
	FontRenderer fontrenderer = this.mc.fontRenderer;
	InventoryPlayer inventoryplayer = this.mc.thePlayer.inventory;

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

	int xPos = (mc.displayWidth / 2) - 90;
	int yPos = mc.displayHeight - 60;

	GL11.glEnable(GL11.GL_BLEND);
	GL11.glDisable(GL11.GL_DEPTH_TEST);
	GL11.glDepthMask(false);
	GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	GL11.glDisable(GL11.GL_ALPHA_TEST);

	// HOTBAR - ICONS
	this.mc.mcProfiler.startSection("actionBar");
	GL11.glEnable(GL12.GL_RESCALE_NORMAL);
	RenderHelper.enableGUIStandardItemLighting();

	for (int i = 0; i < 9; ++i)
	{
		this.renderInventorySlot(i, xPos + i * 20, yPos + 37, 1);
	}

	RenderHelper.disableStandardItemLighting();
	GL11.glDisable(GL12.GL_RESCALE_NORMAL);
	this.mc.mcProfiler.endSection();
	GL11.glDisable(GL11.GL_BLEND);

	// HOTBAR - SELECTOR
	this.drawTexturedModalRect(xPos + 6 + inventoryplayer.currentItem * 20, yPos - 34, 8, 57, 8, 7);

	if (!mc.thePlayer.capabilities.isCreativeMode)
	{
		// HUD BACKGROUND
		this.drawTexturedModalRect(xPos, yPos, 0, 0, 180, 54);

		// FOOD BAR
		int hunger = player.getFoodStats().getFoodLevel();

		int hungerWidth = (hunger / 20) * 71;

		this.drawTexturedModalRect(xPos + 105, yPos + 21, 9, 185, hungerWidth, 9);

		if (hunger <= 1)
		{
			this.drawTexturedModalRect(xPos + 110, yPos + 13, 0, 57, 4, 10);
		}

		String hDisplay = hunger + "/" + 20;
		int w2 = fontrenderer.getStringWidth(hDisplay) / 2;
		fontrenderer.drawStringWithShadow(hDisplay, xPos + 56, yPos + 13, 0x000000);

		// EXPERIENCE BAR
		int xp = (int) player.experience;
		int toNextLevel = player.xpBarCap();
		int level = player.experienceLevel;

		String s = "" + level;
		int w3 = fontrenderer.getStringWidth(s) / 2;
		fontrenderer.drawStringWithShadow(s, xPos + 90, yPos + 20, 0x000000);

		if (level > 0)
		{
			this.drawTexturedModalRect(xPos + 20, yPos + 4, 20, 57, (xp / toNextLevel) * 140, 4);
		}

		// HEALTH BAR
		int health = (int) (player.getHealth() * 2);
		int maxHealth = (int) (player.getMaxHealth() * 2);

		int healthWidth = (health / maxHealth) * 71;

		this.drawTexturedModalRect(xPos + 4, yPos + 21, 0, 185, healthWidth, 9);

		if (health <= 1)
		{
			this.drawTexturedModalRect(xPos + 66, yPos + 13, 4, 57, 4, 10);
		}
		String hp = health + "/" + maxHealth;
		int w1 = fontrenderer.getStringWidth(hp) / 2;
		fontrenderer.drawStringWithShadow(hp, xPos + 122, yPos + 13, 0x000000);

		// HORSE BAR
		if (this.mc.thePlayer.isRidingHorse())
		{
			this.mc.mcProfiler.startSection("jumpBar");
			float jump = this.mc.thePlayer.getHorseJumpPower();
			int jumpMax = 148;

			this.drawTexturedModalRect(xPos + 16, yPos - 19, 0, 90, (int) ((jump / jumpMax) * jumpMax), 19);

			if (jump > 0)
			{
				this.drawTexturedModalRect(xPos + 20, yPos + 8, 0, 109, 140, 4);
			}

			this.mc.mcProfiler.endSection();
		}

		// OXYGEN
		// ARMOUR
	}
}

Link to comment
Share on other sites

Uh, if it wasn't being registered then nothing would happen. As I've said several times, nothing renders, not mine or the vanilla.

So the pre-event is clearly being called. The post-event that's supposed to render mine is being called, for some reason it's not doing anything.

Link to comment
Share on other sites

Please understand that the event is posted for each element type, with different rendering state.

If you cancel the event with element type == ALL, NOTHING is rendered.

You can cancel any number of them, but you need to choose ONE element type to render ONCE.

See GuiInGameForge to check the rendering states.

Link to comment
Share on other sites

This isn't hard at all just remember:

 

1.) You should cancel this event in Pre

 

2) Cancelling "ElementType.ALL" will disable every HUD rendered element

 

Take a look at this example:

 

public void onEvent(RenderGameOverlayEvent.Pre event)
{
    switch(event.type)
    {
        case HOTBAR : event.setCanceled(event.isCancelable); break;
        case ANOTHERELEMENT : yourRenderMethod(); event.setCanceled(event.isCancelable); break;
        default : break;
    }
}

 

EDIT: Depending on your rendering you should probably render your new widgets / icons in current / post. You can also decide when to render it based on when other elements render, shown below:

 

public void onEvent(RenderGameOverlayEvent event)
{
    if(event.type.equals(ElementType.EXPERIENCE)
    {
        yourRenderMethodOnExperienceBar();
    }
}

Development of Plugins [2012 - 2014] Development of Mods [2012 - Current]

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.