Skip to content

Commit

Permalink
clean up code and add more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
olim88 committed Oct 17, 2024
1 parent 8f57113 commit e4da759
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
45 changes: 26 additions & 19 deletions src/main/java/de/hysky/skyblocker/skyblock/HealthBars.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,20 @@ public static void onEntityDespawn(Entity entity, ClientWorld clientWorld) {
}
}

/**
* Processes armourtand updates and if it's a mob with health get the value of its health and save it the hashmap
*
* @param armorStand updated armourstand
*/
public static void HeathBar(ArmorStandEntity armorStand) {
if (!armorStand.isInvisible() || !armorStand.hasCustomName() || !armorStand.isCustomNameVisible() || !SkyblockerConfigManager.get().uiAndVisuals.healthBars.enabled) {
return;
}

//check if armour stand is dead and remove it from list
if (armorStand.isDead()) {
System.out.println("somthing");
healthValues.removeFloat(armorStand);
mobStartingHealth.removeInt(armorStand);

return;
}

Expand Down Expand Up @@ -102,12 +105,10 @@ public static void HeathBar(ArmorStandEntity armorStand) {
if (removeValue && i < parts.size() - 3 && parts.get(i).getString().equals(healthMatcher.group(1)) && parts.get(i + 1).getString().equals("/") && parts.get(i + 2).getString().equals(healthMatcher.group(4)) && parts.get(i + 3).getString().equals("❤")) {
continue;
}
//remove slash from max
//remove slash from max and skip over the value
if (removeMax && i < parts.size() - 2 && parts.get(i).getString().equals("/") && parts.get(i + 1).getString().equals(healthMatcher.group(4)) && parts.get(i + 2).getString().equals("❤")) {
continue;
}
//remove max
if (removeMax && i < parts.size() - 1 && parts.get(i).getString().equals(healthMatcher.group(4)) && parts.get(i + 1).getString().equals("❤")) {
//remove the value as well
i += 1;
continue;
}
//if both enabled remove "❤"
Expand All @@ -119,9 +120,13 @@ public static void HeathBar(ArmorStandEntity armorStand) {
armorStand.setCustomName(cleanedText);
}

/**
* Processes armour stands that only have a health value and no max health
*
* @param armorStand armorstand to check the name of
*/
private static void HealthOnlyCheck(ArmorStandEntity armorStand) {
//todo setting for this
if (!SkyblockerConfigManager.get().uiAndVisuals.healthBars.applyToHealthOnlyMobs || armorStand.getCustomName() == null) {
if (!SkyblockerConfigManager.get().uiAndVisuals.healthBars.applyToHealthOnlyMobs || armorStand.getCustomName() == null) {
return;
}
Matcher healthOnlyMatcher = HEALTH_ONLY_PATTERN.matcher(armorStand.getCustomName().getString());
Expand All @@ -135,7 +140,7 @@ private static void HealthOnlyCheck(ArmorStandEntity armorStand) {

//if it's a new health only armor stand add to starting health lookup (not always full health if already damaged but best that can be done)
if (!mobStartingHealth.containsKey(armorStand)) {
mobStartingHealth.put(armorStand,currentHealth);
mobStartingHealth.put(armorStand, currentHealth);
}

//add to health bar values
Expand All @@ -149,13 +154,10 @@ private static void HealthOnlyCheck(ArmorStandEntity armorStand) {
MutableText cleanedText = Text.empty();
List<Text> parts = armorStand.getCustomName().getSiblings();
for (int i = 0; i < parts.size(); i++) {
//remove value from name
//remove value from name and skip over heart
if (i < parts.size() - 1 && parts.get(i).getString().equals(healthOnlyMatcher.group(1)) && parts.get(i + 1).getString().equals("❤")) {
continue;
}

//remove "❤"
if (parts.get(i).getString().equals("❤")) {
//skip the heart
i += 1;
continue;
}
cleanedText.append(parts.get(i));
Expand All @@ -175,16 +177,21 @@ private static void render(WorldRenderContext context) {
Color barColor = SkyblockerConfigManager.get().uiAndVisuals.healthBars.barColor;
boolean hideFullHealth = SkyblockerConfigManager.get().uiAndVisuals.healthBars.hideFullHealth;
float scale = SkyblockerConfigManager.get().uiAndVisuals.healthBars.scale;
float tickDelta = context.tickCounter().getTickDelta(false);

for (Object2FloatMap.Entry<ArmorStandEntity> healthValue : healthValues.object2FloatEntrySet()) {
//if the health bar is full and the setting is enabled to hide it stop rendering it
if (hideFullHealth && healthValue.getFloatValue() == 1) {
float health = healthValue.getFloatValue();
if (hideFullHealth && health == 1) {
continue;
}

ArmorStandEntity armorStand = healthValue.getKey();
// Render the health bar texture with scaling based on health percentage
RenderHelper.renderTextureQuad(context, armorStand.getCameraPosVec(context.tickCounter().getTickDelta(false)).add(0, 0.25 - (0.1f * scale), 0), scale, 0.1f * scale, 1f, 1f, new Vec3d(-0.5f * scale, 0, 0), HEALTH_BAR_BACKGROUND_TEXTURE, barColor, false);
RenderHelper.renderTextureQuad(context, armorStand.getCameraPosVec(context.tickCounter().getTickDelta(false)).add(0, 0.25 - (0.1f * scale), 0), healthValue.getFloatValue() * scale, 0.1f * scale, healthValue.getFloatValue(), 1f, new Vec3d(-0.5f * scale, 0, 0.003f), HEALTH_BAR_TEXTURE, barColor, false);
float width = scale;
float height = scale * 0.1f;
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, false);
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, false);
}
}
}
15 changes: 14 additions & 1 deletion src/main/java/de/hysky/skyblocker/utils/render/RenderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,20 @@ public static void renderQuad(WorldRenderContext context, Vec3d[] points, float[
RenderSystem.depthFunc(GL11.GL_LEQUAL);
}

public static void renderTextureQuad(WorldRenderContext context, Vec3d pos, float width, float height, float textureWidth, float textureHeight, Vec3d renderOffset, Identifier texture, Color shaderColor, boolean throughWalls) {
/**
* Renders a texture in world space facing the player (like a name tag)
* @param context world render context
* @param pos world position
* @param width rendered width
* @param height rendered height
* @param textureWidth amount of texture rendered width
* @param textureHeight amount of texture rendered height
* @param renderOffset offset once it's been placed in the world facing the player
* @param texture reference to texture to render
* @param shaderColor color to apply to the texture
* @param throughWalls if it should render though walls
*/
public static void renderTextureInWorld(WorldRenderContext context, Vec3d pos, float width, float height, float textureWidth, float textureHeight, Vec3d renderOffset, Identifier texture, Color shaderColor, boolean throughWalls) {
Matrix4f positionMatrix = new Matrix4f();
Camera camera = context.camera();
Vec3d cameraPos = camera.getPos();
Expand Down

0 comments on commit e4da759

Please sign in to comment.