Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.15.2] Colouring a rendered GUI using code

Featured Replies

Posted

Hello! Like the title says im trying to change the color of my gui using code. My rendered gui is a custom health bar and im trying to change its color. I want to (in the future) make the color change based on my health but i cant manage to making it change from its default color of white.

 

Any help is appreciated! thanks

Spoiler

 


package com.electrosphere.projectx.client.gui.Health;

import com.electrosphere.projectx.ElectroCore;
import com.electrosphere.projectx.init.SoundInit;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;

import net.minecraft.client.Minecraft;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.client.event.GuiScreenEvent.BackgroundDrawnEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.client.gui.GuiUtils;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;

@SuppressWarnings("unused")
@Mod.EventBusSubscriber(modid = ElectroCore.MOD_ID, bus = Bus.FORGE, value = Dist.CLIENT)
public class HealthBar {
	private static final ResourceLocation bar0 = new ResourceLocation(ElectroCore.MOD_ID,
			"textures/gui/healthbar0.png");
	private static final ResourceLocation bar1 = new ResourceLocation(ElectroCore.MOD_ID,
			"textures/gui/healthbar1.png");
	private static final ResourceLocation bar2 = new ResourceLocation(ElectroCore.MOD_ID,
			"textures/gui/healthbar2.png");
	private static final ResourceLocation bar3 = new ResourceLocation(ElectroCore.MOD_ID,
			"textures/gui/healthbar3.png");
	private static final ResourceLocation bar4 = new ResourceLocation(ElectroCore.MOD_ID,
			"textures/gui/healthbar4.png");

//	int texW = 250;
	int texH = 15;
	int barW = 240;
	Minecraft mc = Minecraft.getInstance();

	int i = 0;
	int j = 240;
	int SmoothCurrentHealth = 240;

	@SubscribeEvent(priority = EventPriority.LOWEST)
	public void OverlayHealthBar(RenderGameOverlayEvent event) {

		if (mc.player.isCreative() == false) {
			if (event.getType().equals(RenderGameOverlayEvent.ElementType.TEXT)) {

				
//###################################### UNITS #################################################################################			
				float UnitHealth = (float) barW / (float) mc.player.getMaxHealth();
				int NewCurrentHealth = (int) mc.player.getHealth() * 12;

				int hp0 = 0;
				int hp1 = 0; //future addition
				int hp2 = 0; //future addition
				int hp3 = 0; //future addition
				int hp4 = 0; //future addition

//###################################### SMOOTH HEALTH HANDLER #############################################################				
				
				if (NewCurrentHealth < SmoothCurrentHealth) {
					SmoothCurrentHealth--;
					if (SmoothCurrentHealth == NewCurrentHealth) {
						SmoothCurrentHealth = (int) NewCurrentHealth;
					}
				} else if (NewCurrentHealth > SmoothCurrentHealth) {
					SmoothCurrentHealth++;
				}

				
//###################################### Health Bar renderer ###########################################################
				RenderSystem.enableBlend();
              	//RenderSystem.color4f(1, 1, 220, 1); <-- THIS DOESNT WORK
				if (NewCurrentHealth >= 0 && NewCurrentHealth <= 240) {
					hp0 = NewCurrentHealth;
					Minecraft.getInstance().textureManager.bindTexture(bar0);
					GuiUtils.drawTexturedModalRect(0, 3, 0, texH, SmoothCurrentHealth, texH, 0);

					mc.fontRenderer.drawStringWithShadow(String.valueOf(NewCurrentHealth), 10, 69, 0xFF00FF3A); // DEBUG
					mc.fontRenderer.drawStringWithShadow(String.valueOf(SmoothCurrentHealth), 10, 89, 0xFF00FF3A); // DEBUG

					
//##################################### LOW HP SOUND MAKER ############################################################
					if (NewCurrentHealth <= 60 && NewCurrentHealth >= 0) {
						mc.world.playMovingSound(mc.player, mc.player, SoundInit.LOWHP.get(), SoundCategory.AMBIENT, 1,
								1);

					}

				}

			}
		}

	}

//###################################### HEALTH BAR EVENT CANCELER ###################################################
	@SubscribeEvent
	public void CancelHealthBar(RenderGameOverlayEvent.Pre event) {
		if (event.getType() == (RenderGameOverlayEvent.ElementType.HEALTH)) {
			event.setCanceled(false);
		}
	}

}

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.