Jump to content

Recommended Posts

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

×   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

    • seguridad seguridad
    • I am creating a server with mods but when i try tostart it it say in the logs:   [29Jan2025 20:36:50.715] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file /server/libraries/net/minecraftforge/fmlcore/1.20.1-47.3.27/fmlcore-1.20.1-47.3.27.jar is missing mods.toml file 159[29Jan2025 20:36:50.717] [main/DEBUG] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Considering mod file candidate /server/libraries/net/minecraftforge/javafmllanguage/1.20.1-47.3.27/javafmllanguage-1.20.1-47.3.27.jar 160[29Jan2025 20:36:50.717] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file /server/libraries/net/minecraftforge/javafmllanguage/1.20.1-47.3.27/javafmllanguage-1.20.1-47.3.27.jar is missing mods.toml file 161[29Jan2025 20:36:50.718] [main/DEBUG] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Considering mod file candidate /server/libraries/net/minecraftforge/lowcodelanguage/1.20.1-47.3.27/lowcodelanguage-1.20.1-47.3.27.jar 162[29Jan2025 20:36:50.718] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file /server/libraries/net/minecraftforge/lowcodelanguage/1.20.1-47.3.27/lowcodelanguage-1.20.1-47.3.27.jar is missing mods.toml file 163[29Jan2025 20:36:50.719] [main/DEBUG] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Considering mod file candidate /server/libraries/net/minecraftforge/mclanguage/1.20.1-47.3.27/mclanguage-1.20.1-47.3.27.jar 164[29Jan2025 20:36:50.719] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file /server/libraries/net/minecraftforge/mclanguage/1.20.1-47.3.27/mclanguage-1.20.1-47.3.27.jar is missing mods.toml file
    • How do you configure the entity reach of a custom weapon? Asking for 1.21 Minecraft parchment
    • This topic is over a year old. If you are having an issue, please read the FAQ for the proper way to post logs, and create your own thread.
    • hello i have been trying to put this specific pack for a while now but ive been hit with a new error everytime. no matter if its a previous version or if i change the pack format it will just not work please help latest log:https://mclo.gs/b6Qra5d <----- this is with pack version 1.20 no changes done to it debug log: https://mclo.gs/jczkfRK
  • Topics

×
×
  • Create New...

Important Information

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