Skip to content

Commit

Permalink
Fix health.formatted, percentage for players, Fixes #1351
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 23, 2016
1 parent e13792b commit 6c0f937
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
14 changes: 14 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
@@ -1,6 +1,7 @@
package net.aufdemrand.denizen.objects;

import net.aufdemrand.denizen.flags.FlagManager;
import net.aufdemrand.denizen.objects.properties.entity.EntityHealth;
import net.aufdemrand.denizen.scripts.commands.core.FailCommand;
import net.aufdemrand.denizen.scripts.commands.core.FinishCommand;
import net.aufdemrand.denizen.scripts.commands.player.SidebarCommand;
Expand Down Expand Up @@ -1144,6 +1145,19 @@ else if (attribute.startsWith("uuid") && !isOnline())
}

// Same with health tags
if (attribute.startsWith("health.formatted")) {
return EntityHealth.getHealthFormatted(new dEntity(getPlayerEntity()), attribute);
}

if (attribute.startsWith("health.percentage")) {
double maxHealth = getPlayerEntity().getMaxHealth();
if (attribute.hasContext(2)) {
maxHealth = attribute.getIntContext(2);
}
return new Element((getPlayerEntity().getHealth() / maxHealth) * 100)
.getAttribute(attribute.fulfill(2));
}

if (attribute.startsWith("health.max")) {
return new Element(getMaxHealth()).getAttribute(attribute.fulfill(2));
}
Expand Down
Expand Up @@ -54,6 +54,29 @@ public String getPropertyId() {
return "health_data";
}

public static String getHealthFormatted(dEntity entity, Attribute attribute) {
double maxHealth = entity.getLivingEntity().getMaxHealth();
if (attribute.hasContext(2)) {
maxHealth = attribute.getIntContext(2);
}
if ((float) entity.getLivingEntity().getHealth() / maxHealth < .10) {
return new Element("dying").getAttribute(attribute.fulfill(2));
}
else if ((float) entity.getLivingEntity().getHealth() / maxHealth < .40) {
return new Element("seriously wounded").getAttribute(attribute.fulfill(2));
}
else if ((float) entity.getLivingEntity().getHealth() / maxHealth < .75) {
return new Element("injured").getAttribute(attribute.fulfill(2));
}
else if ((float) entity.getLivingEntity().getHealth() / maxHealth < 1) {
return new Element("scraped").getAttribute(attribute.fulfill(2));
}

else {
return new Element("healthy").getAttribute(attribute.fulfill(2));
}
}


///////////
// dObject Attributes
Expand All @@ -75,26 +98,7 @@ public String getAttribute(Attribute attribute) {
// May be 'dying', 'seriously wounded', 'injured', 'scraped', or 'healthy'.
// -->
if (attribute.startsWith("health.formatted")) {
double maxHealth = entity.getLivingEntity().getMaxHealth();
if (attribute.hasContext(2)) {
maxHealth = attribute.getIntContext(2);
}
if ((float) entity.getLivingEntity().getHealth() / maxHealth < .10) {
return new Element("dying").getAttribute(attribute.fulfill(2));
}
else if ((float) entity.getLivingEntity().getHealth() / maxHealth < .40) {
return new Element("seriously wounded").getAttribute(attribute.fulfill(2));
}
else if ((float) entity.getLivingEntity().getHealth() / maxHealth < .75) {
return new Element("injured").getAttribute(attribute.fulfill(2));
}
else if ((float) entity.getLivingEntity().getHealth() / maxHealth < 1) {
return new Element("scraped").getAttribute(attribute.fulfill(2));
}

else {
return new Element("healthy").getAttribute(attribute.fulfill(2));
}
return getHealthFormatted(entity, attribute);
}

// <--[tag]
Expand Down
Expand Up @@ -75,7 +75,6 @@ public class IntHolder {
public void setBlocksDelayed(final Location loc, final Runnable runme) {
final IntHolder index = new IntHolder();
final long goal = (long) (x_width * y_length * z_height);

new BukkitRunnable() {
@Override
public void run() {
Expand All @@ -90,7 +89,9 @@ public void run() {
return;
}
}
runme.run();
if (runme != null) {
runme.run();
}
cancel();

}
Expand Down

0 comments on commit 6c0f937

Please sign in to comment.