Skip to content

Commit

Permalink
Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Apr 13, 2021
1 parent f7ba70c commit 4c55432
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ loader_version=0.11.3
fabric_version=0.32.5+1.16

# Mod Properties
mod_version = 1.0.1
mod_version = 1.0.2
maven_group = org.samo_lego
archives_base_name = healthcare

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface HealthbarPreferences {
* @param maxHealth max health
* @return formatted mutable text with health info
*/
MutableText getHealth(float health, float maxHealth);
MutableText getHealthbarText(float health, float maxHealth);

void setEnabled(boolean enabled);
boolean isEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ public void setHealthbarStyle(Enum<HealthbarStyle> healthbarStyle) {
}

@Override
public MutableText getHealth(float health, float maxHealth) {
public MutableText getHealthbarText(float health, float maxHealth) {
if(health < 0.0F) {
health = 0.0F;
}
if(maxHealth <= 0.0F) {
maxHealth = 1.0F;
}
if(health > maxHealth) {
maxHealth = health;
}

String first, second;
if(this.healthbarStyle.equals(HealthbarStyle.NUMBER)) {
// Number
Expand All @@ -55,21 +59,22 @@ public MutableText getHealth(float health, float maxHealth) {
int heartCount, fullHearts;
char full, empty;
if(this.healthbarStyle.equals(HealthbarStyle.LINES)) {
heartCount = maxHealth < 20 ? (int) maxHealth : 20;
fullHearts = (int) Math.ceil(health * heartCount / maxHealth);
heartCount = maxHealth < 20 ? (int) Math.ceil(maxHealth) : 20;

empty = '|';
full = '|';
} else { // Hearts
// We ceil the number to not show 0 hearts if entity has like 0.2f health
int length = this.healthbarStyle == HealthbarStyle.CUSTOM ? customLength : 10;
heartCount = maxHealth < length ? (int) maxHealth : length;
fullHearts = (int) Math.ceil(health * heartCount / maxHealth);
heartCount = maxHealth < length ? (int) Math.ceil(maxHealth) : length;

full = (char) (this.healthbarStyle.equals(HealthbarStyle.HEARTS) ? 9829 : this.customFullChar); // ♥ or custom
empty = (char) (this.healthbarStyle.equals(HealthbarStyle.HEARTS) ? 9825 : this.customEmptyChar); // ♡ or custom
}

// Hearts that should be colored red
fullHearts = (int) Math.ceil(health * heartCount / maxHealth);

first = new String(new char[fullHearts]).replace('\0', full);
second = new String(new char[heartCount - fullHearts]).replace('\0', empty);
}
Expand Down Expand Up @@ -119,7 +124,8 @@ public int getCustomFullChar() {

@Override
public void setCustomLength(int length) {
this.customLength = length;
if(length >= 0) // Don't allow negative length
this.customLength = length;
}

@Override
Expand All @@ -132,6 +138,7 @@ private void writeCustomDataToTag(CompoundTag tag, CallbackInfo ci) {
CompoundTag healthbar = new CompoundTag();
healthbar.putString("Style", this.healthbarStyle.toString());
healthbar.putBoolean("Enabled", this.enabled);
healthbar.putBoolean("AlwaysVisible", this.alwaysVisible);
if(this.healthbarStyle.equals(HealthbarStyle.CUSTOM)) {
healthbar.putInt("CustomFullChar", this.customFullChar);
healthbar.putInt("CustomEmptyChar", this.customEmptyChar);
Expand All @@ -146,6 +153,7 @@ private void readCustomDataFromTag(CompoundTag tag, CallbackInfo ci) {
CompoundTag healthbar = tag.getCompound("Healthbar");
this.healthbarStyle = HealthbarStyle.valueOf(healthbar.getString("Style"));
this.enabled = healthbar.getBoolean("Enabled");
this.alwaysVisible = healthbar.getBoolean("AlwaysVisible");

if(this.healthbarStyle.equals(HealthbarStyle.CUSTOM)) {
this.customFullChar = healthbar.getInt("CustomFullChar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void onPacketSend(Packet<?> packet, GenericFutureListener<? extends Futu
// @SpaceClouds42 saved me here, `.copy()` after getting custom name is essential!
MutableText name = entity.hasCustomName() ? entity.getCustomName().copy() : new TranslatableText(entity.getType().getTranslationKey());

Text healthbar = ((HealthbarPreferences) this.player).getHealth(health, maxHealth);
Text healthbar = ((HealthbarPreferences) this.player).getHealthbarText(health, maxHealth);
DataTracker.Entry<Optional<Text>> healthTag = new DataTracker.Entry<>(EntityAccessor.getCUSTOM_NAME(), Optional.of(name.append(" ").append(healthbar)));

Collections.addAll(trackedValues, visibleTag, healthTag);
Expand Down

0 comments on commit 4c55432

Please sign in to comment.