Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 92 additions & 62 deletions src/main/java/net/torocraft/torohealth/bars/BarState.java
Original file line number Diff line number Diff line change
@@ -1,85 +1,115 @@
package net.torocraft.torohealth.bars;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.MathHelper;
import net.minecraft.client.MinecraftClient;
import net.torocraft.torohealth.ToroHealth;

public class BarState {

public final LivingEntity entity;

public float health;
public float previousHealth;
public float previousHealthDisplay;
public float previousHealthDelay;
public int lastDmg;
public int lastDmgCumulative;
public float lastHealth;
public float lastDmgDelay;
private float animationSpeed = 0;

private static final float HEALTH_INDICATOR_DELAY = 10;
public final Integer entityID;

public float health;
public float previousHealthDisplay;
public float previousHealthDelay;
public int lastDmg;
public int lastDmgCumulative;
public float lastHealth;
public float lastDmgDelay;
private float animationSpeed;

private static final float HEALTH_INDICATOR_DELAY = 10;


private BarState(Integer id, float health){
this.entityID = id;
this.health = health;
this.previousHealthDisplay = health;
this.lastDmg = 0;
this.lastDmgCumulative = 0;
this.lastHealth = health;
this.lastDmgDelay = 0;
this.animationSpeed = 0;
}

public BarState(LivingEntity entity) {
this.entity = entity;
}
public static BarState create(Integer id){
MinecraftClient client = MinecraftClient.getInstance();
assert client.world != null;
Entity entity = client.world.getEntityById(id);
if (entity instanceof LivingEntity living) {
float currentHealth = Math.min(living.getHealth(), living.getMaxHealth());
return new BarState(id, currentHealth);
}
return null;
}

public void tick() {
health = Math.min(entity.getHealth(), entity.getMaxHealth());
incrementTimers();

if (lastHealth < 0.1) {
reset();
public void tick() {
MinecraftClient client = MinecraftClient.getInstance();
assert client.world != null;
LivingEntity entity = (LivingEntity) client.world.getEntityById(entityID);

} else if (lastHealth != health) {
handleHealthChange();
if (entity != null){
health = Math.min(entity.getHealth(), entity.getMaxHealth());
incrementTimers();

} else if (lastDmgDelay == 0.0F) {
reset();
}
if (lastHealth < 0.1) {
reset();

updateAnimations();
}
} else if (lastHealth != health) {
handleHealthChange();

private void reset() {
lastHealth = health;
lastDmg = 0;
lastDmgCumulative = 0;
}
} else if (lastDmgDelay == 0.0F) {
reset();
}

private void incrementTimers() {
if (this.lastDmgDelay > 0) {
this.lastDmgDelay--;
updateAnimations();
}
}
if (this.previousHealthDelay > 0) {
this.previousHealthDelay--;
}
}

private void handleHealthChange() {
lastDmg = MathHelper.ceil(lastHealth) - MathHelper.ceil(health);
lastDmgCumulative += lastDmg;
private void reset() {
lastHealth = health;
lastDmg = 0;
lastDmgCumulative = 0;
}

lastDmgDelay = HEALTH_INDICATOR_DELAY * 2;
lastHealth = health;
if (ToroHealth.CONFIG.particle.show) {
BarStates.PARTICLES.add(new BarParticle(entity, lastDmg));
private void incrementTimers() {
if (this.lastDmgDelay > 0) {
this.lastDmgDelay--;
}
if (this.previousHealthDelay > 0) {
this.previousHealthDelay--;
}
}
}

private void updateAnimations() {
if (previousHealthDelay > 0) {
float diff = previousHealthDisplay - health;
if (diff > 0) {
animationSpeed = diff / 10f;
}
} else if (previousHealthDelay < 1 && previousHealthDisplay > health) {
previousHealthDisplay -= animationSpeed;
} else {
previousHealthDisplay = health;
previousHealth = health;
previousHealthDelay = HEALTH_INDICATOR_DELAY;

private void handleHealthChange() {
lastDmg = MathHelper.ceil(lastHealth) - MathHelper.ceil(health);
lastDmgCumulative += lastDmg;

lastDmgDelay = HEALTH_INDICATOR_DELAY * 2;
lastHealth = health;
if (ToroHealth.CONFIG.particle.show) {
MinecraftClient client = MinecraftClient.getInstance();
assert client.world != null;
LivingEntity entity = (LivingEntity) client.world.getEntityById(entityID);
if (entity != null) {
BarStates.PARTICLES.add(new BarParticle(entity, lastDmg));
}
}
}
}

private void updateAnimations() {
if (previousHealthDelay > 0) {
float diff = previousHealthDisplay - health;
if (diff > 0) {
animationSpeed = diff / 10f;
}
} else if (previousHealthDelay < 1 && previousHealthDisplay > health) {
previousHealthDisplay -= animationSpeed;
} else {
previousHealthDisplay = health;
previousHealthDelay = HEALTH_INDICATOR_DELAY;
}
}
}
7 changes: 5 additions & 2 deletions src/main/java/net/torocraft/torohealth/bars/BarStates.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public static BarState getState(LivingEntity entity) {
int id = entity.getId();
BarState state = STATES.get(id);
if (state == null) {
state = new BarState(entity);
STATES.put(id, state);
state = BarState.create(id);
if (state != null) {
STATES.put(id, state);
}
}
return state;
}
Expand Down Expand Up @@ -49,6 +51,7 @@ private static boolean stateExpired(Map.Entry<Integer, BarState> entry) {
}

MinecraftClient minecraft = MinecraftClient.getInstance();
assert minecraft.world != null;
Entity entity = minecraft.world.getEntityById(entry.getKey());

if (!(entity instanceof LivingEntity)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public static void render(MatrixStack matrix, LivingEntity entity, double x, dou
: ToroHealth.CONFIG.bar.foeColorSecondary;

BarState state = BarStates.getState(entity);
if (state == null) {
return;
}

float percent = Math.min(1, Math.min(state.health, entity.getMaxHealth()) / entity.getMaxHealth());
float percent2 = Math.min(state.previousHealthDisplay, entity.getMaxHealth()) / entity.getMaxHealth();
Expand Down