Skip to content

Commit

Permalink
add suggested changes
Browse files Browse the repository at this point in the history
add fading between colours and fix problems
  • Loading branch information
olim88 committed Nov 3, 2024
1 parent b111cde commit 08873e0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.collapsed(true)
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("skyblocker.config.uiAndVisuals.healthBars.enabled"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.uiAndVisuals.healthBars.enabled.@Tooltip")))
.binding(defaults.uiAndVisuals.healthBars.enabled,
() -> config.uiAndVisuals.healthBars.enabled,
newValue -> config.uiAndVisuals.healthBars.enabled = newValue)
Expand Down Expand Up @@ -488,10 +489,27 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.controller(ConfigUtils::createBooleanController)
.build())
.option(Option.<Color>createBuilder()
.name(Text.translatable("skyblocker.config.uiAndVisuals.healthBars.barColor"))
.binding(defaults.uiAndVisuals.healthBars.barColor,
() -> config.uiAndVisuals.healthBars.barColor,
newValue -> config.uiAndVisuals.healthBars.barColor = newValue)
.name(Text.translatable("skyblocker.config.uiAndVisuals.healthBars.fullBarColor"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.uiAndVisuals.healthBars.fullBarColor.@Tooltip")))
.binding(defaults.uiAndVisuals.healthBars.fullBarColor,
() -> config.uiAndVisuals.healthBars.fullBarColor,
newValue -> config.uiAndVisuals.healthBars.fullBarColor = newValue)
.controller(ColorControllerBuilder::create)
.build())
.option(Option.<Color>createBuilder()
.name(Text.translatable("skyblocker.config.uiAndVisuals.healthBars.halfBarColor"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.uiAndVisuals.healthBars.halfBarColor.@Tooltip")))
.binding(defaults.uiAndVisuals.healthBars.halfBarColor,
() -> config.uiAndVisuals.healthBars.halfBarColor,
newValue -> config.uiAndVisuals.healthBars.halfBarColor = newValue)
.controller(ColorControllerBuilder::create)
.build())
.option(Option.<Color>createBuilder()
.name(Text.translatable("skyblocker.config.uiAndVisuals.healthBars.emptyBarColor"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.uiAndVisuals.healthBars.emptyBarColor.@Tooltip")))
.binding(defaults.uiAndVisuals.healthBars.emptyBarColor,
() -> config.uiAndVisuals.healthBars.emptyBarColor,
newValue -> config.uiAndVisuals.healthBars.emptyBarColor = newValue)
.controller(ColorControllerBuilder::create)
.build())
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,14 @@ public static class HealthBars {
@SerialEntry
public boolean hideFullHealth = false;


@SerialEntry
public Color fullBarColor = new Color(0x00FF00);

@SerialEntry
public Color halfBarColor = new Color(0xFF4600);

@SerialEntry
public Color barColor = new Color(0xFFFFFF);
public Color emptyBarColor = new Color(0xFF0000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public abstract class ClientPlayNetworkHandlerMixin {
EggFinder.checkIfEgg(armorStandEntity);
try { //Prevent packet handling fails if something goes wrong so that entity trackers still update, just without compact damage numbers
CompactDamage.compactDamage(armorStandEntity);
HealthBars.HeathBar(armorStandEntity);
HealthBars.heathBar(armorStandEntity);
} catch (Exception e) {
LOGGER.error("[Skyblocker Compact Damage] Failed to compact damage number", e);
}
Expand Down
57 changes: 48 additions & 9 deletions src/main/java/de/hysky/skyblocker/skyblock/HealthBars.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void onEntityDespawn(Entity entity, ClientWorld clientWorld) {
*
* @param armorStand updated armorstand
*/
public static void HeathBar(ArmorStandEntity armorStand) {
public static void heathBar(ArmorStandEntity armorStand) {
if (!armorStand.isInvisible() || !armorStand.hasCustomName() || !armorStand.isCustomNameVisible() || !SkyblockerConfigManager.get().uiAndVisuals.healthBars.enabled) {
return;
}
Expand All @@ -82,7 +82,7 @@ public static void HeathBar(ArmorStandEntity armorStand) {
Matcher healthMatcher = HEALTH_PATTERN.matcher(armorStand.getCustomName().getString());
//if a health ratio can not be found send onto health only pattern
if (!healthMatcher.find()) {
HealthOnlyCheck(armorStand);
healthOnlyCheck(armorStand);
return;
}

Expand All @@ -103,10 +103,9 @@ public static void HeathBar(ArmorStandEntity armorStand) {
List<Text> parts = armorStand.getCustomName().getSiblings();
//loop though name and add every part to a new text skipping over the hidden health values
int healthStartIndex = -1;
System.out.println(healthMatcher.group(0).toString()+"testing");
for (int i = 0; i < parts.size(); i++) {
//remove value from name
if (i < parts.size() - 4 && StringUtils.join(parts.subList(i+1, i + 5).stream().map(Text::getString).toArray(), "").equals(healthMatcher.group(0))) {
if (i < parts.size() - 4 && StringUtils.join(parts.subList(i + 1, i + 5).stream().map(Text::getString).toArray(), "").equals(healthMatcher.group(0))) {
healthStartIndex = i;
}
if (healthStartIndex != -1) {
Expand Down Expand Up @@ -150,7 +149,7 @@ public static void HeathBar(ArmorStandEntity armorStand) {
*
* @param armorStand armorstand to check the name of
*/
private static void HealthOnlyCheck(ArmorStandEntity armorStand) {
private static void healthOnlyCheck(ArmorStandEntity armorStand) {
if (!SkyblockerConfigManager.get().uiAndVisuals.healthBars.applyToHealthOnlyMobs || armorStand.getCustomName() == null) {
return;
}
Expand Down Expand Up @@ -181,7 +180,7 @@ private static void HealthOnlyCheck(ArmorStandEntity armorStand) {
//loop though name and add every part to a new text skipping over the health value
for (int i = 0; i < parts.size(); i++) {
//skip space before value, value and heart from name
if (i < parts.size() - 2 && StringUtils.join(parts.subList(i+1, i + 3).stream().map(Text::getString).toArray(), "").equals(healthOnlyMatcher.group(0))) {
if (i < parts.size() - 2 && StringUtils.join(parts.subList(i + 1, i + 3).stream().map(Text::getString).toArray(), "").equals(healthOnlyMatcher.group(0))) {
//skip the heart
i += 2;
continue;
Expand All @@ -200,7 +199,9 @@ private static void render(WorldRenderContext context) {
if (!SkyblockerConfigManager.get().uiAndVisuals.healthBars.enabled || healthValues.isEmpty()) {
return;
}
Color barColor = SkyblockerConfigManager.get().uiAndVisuals.healthBars.barColor;
Color fullColor = SkyblockerConfigManager.get().uiAndVisuals.healthBars.fullBarColor;
Color halfColor = SkyblockerConfigManager.get().uiAndVisuals.healthBars.halfBarColor;
Color emptyColor = SkyblockerConfigManager.get().uiAndVisuals.healthBars.emptyBarColor;
boolean hideFullHealth = SkyblockerConfigManager.get().uiAndVisuals.healthBars.hideFullHealth;
float scale = SkyblockerConfigManager.get().uiAndVisuals.healthBars.scale;
float tickDelta = context.tickCounter().getTickDelta(false);
Expand All @@ -219,9 +220,47 @@ private static void render(WorldRenderContext context) {
if (!armorStand.shouldRenderName()) {
return;
}
//gets the mixed color of the health bar
Color mixedColor = fadeBetweenThreeColors(emptyColor, halfColor, fullColor, health);
// Render the health bar texture with scaling based on health percentage
RenderHelper.renderTextureInWorld(context, armorStand.getCameraPosVec(tickDelta).add(0, 0.25 - height, 0), width, height, 1f, 1f, new Vec3d(width * -0.5f, 0, 0), HEALTH_BAR_BACKGROUND_TEXTURE, barColor, true);
RenderHelper.renderTextureInWorld(context, armorStand.getCameraPosVec(tickDelta).add(0, 0.25 - height, 0), width * health, height, health, 1f, new Vec3d(width * -0.5f, 0, 0.003f), HEALTH_BAR_TEXTURE, barColor, true);
RenderHelper.renderTextureInWorld(context, armorStand.getCameraPosVec(tickDelta).add(0, 0.25 - height, 0), width, height, 1f, 1f, new Vec3d(width * -0.5f, 0, 0), HEALTH_BAR_BACKGROUND_TEXTURE, mixedColor, true);
RenderHelper.renderTextureInWorld(context, armorStand.getCameraPosVec(tickDelta).add(0, 0.25 - height, 0), width * health, height, health, 1f, new Vec3d(width * -0.5f, 0, 0.003f), HEALTH_BAR_TEXTURE, mixedColor, true);
}
}


/**
* Interoperates between 3 colors
*
* @param color1 full color
* @param color2 half color
* @param color3 empty color
* @param t position between the colors from 0-1
* @return the interpolated color
*/
public static Color fadeBetweenThreeColors(Color color1, Color color2, Color color3, double t) {
Color startColor;
Color endColor;
double subT;

if (t <= 0.5) {
// First half: color1 to color2
startColor = color1;
endColor = color2;
subT = t / 0.5;
} else {
// Second half: color2 to color3
startColor = color2;
endColor = color3;
subT = (t - 0.5) / 0.5;
}

// Interpolate each RGB component
int red = (int) (startColor.getRed() + (endColor.getRed() - startColor.getRed()) * subT);
int green = (int) (startColor.getGreen() + (endColor.getGreen() - startColor.getGreen()) * subT);
int blue = (int) (startColor.getBlue() + (endColor.getBlue() - startColor.getBlue()) * subT);

return new Color(red, green, blue);
}

}
8 changes: 7 additions & 1 deletion src/main/resources/assets/skyblocker/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@

"skyblocker.config.uiAndVisuals.healthBars": "Custom Mob Health Bars",
"skyblocker.config.uiAndVisuals.healthBars.enabled": "Enabled",
"skyblocker.config.uiAndVisuals.healthBars.enabled.@Tooltip": "(only updates when health updates)",
"skyblocker.config.uiAndVisuals.healthBars.scale": "Scale",
"skyblocker.config.uiAndVisuals.healthBars.removeHealthFromName": "Remove Health From Name",
"skyblocker.config.uiAndVisuals.healthBars.removeHealthFromName.@Tooltip": "Remove the current health of mobs from their nametag",
Expand All @@ -763,7 +764,12 @@
"skyblocker.config.uiAndVisuals.healthBars.applyToHealthOnlyMobs.@Tooltip": "Also add health bars to mobs with only a current health value (will sometimes be wrong if mob is damaged before loading in)",
"skyblocker.config.uiAndVisuals.healthBars.hideFullHealth": "Hide Full Health Bars",
"skyblocker.config.uiAndVisuals.healthBars.hideFullHealth.@Tooltip": "Don't show the health bar when its full",
"skyblocker.config.uiAndVisuals.healthBars.barColor": "Health Bar Color",
"skyblocker.config.uiAndVisuals.healthBars.fullBarColor": "Full Health bar Color",
"skyblocker.config.uiAndVisuals.healthBars.fullBarColor.@Tooltip": "Color of health bar when mob is at full hp",
"skyblocker.config.uiAndVisuals.healthBars.halfBarColor": "Half Health Bar Color",
"skyblocker.config.uiAndVisuals.healthBars.halfBarColor.@Tooltip": "Color of health bar when mob is at half hp",
"skyblocker.config.uiAndVisuals.healthBars.emptyBarColor": "Empty Health Bar Color",
"skyblocker.config.uiAndVisuals.healthBars.emptyBarColor.@Tooltip": "Color of health bar when mob is at full hp",

"skyblocker.config.uiAndVisuals.inputCalculator": "Input Calculator",
"skyblocker.config.uiAndVisuals.inputCalculator.enabled": "Enable Sign Calculator",
Expand Down

0 comments on commit 08873e0

Please sign in to comment.