Skip to content

Commit

Permalink
Allow offline food level, fix offline health
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Dec 22, 2016
1 parent 6b484d4 commit 8faa913
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
Expand Up @@ -28,6 +28,8 @@
* @author one4me
*/

import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.NMSVersion;
import net.aufdemrand.denizen.nms.util.jnbt.CompoundTag;
import net.aufdemrand.denizen.nms.util.jnbt.DoubleTag;
import net.aufdemrand.denizen.nms.util.jnbt.FloatTag;
Expand Down Expand Up @@ -105,11 +107,13 @@ public void setLocation(Location location) {
}

public float getHealthFloat() {
return this.compound.getFloat("HealF");
String id = NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2) ? "Health" : "HealF";
return this.compound.getFloat(id);
}

public void setHealthFloat(float input) {
this.compound = compound.createBuilder().putFloat("HealF", input).build();
String id = NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2) ? "Health" : "HealF";
this.compound = compound.createBuilder().putFloat(id, input).build();
if (this.autosave) {
savePlayerData();
}
Expand Down
32 changes: 25 additions & 7 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
Expand Up @@ -304,6 +304,15 @@ public double getMaxHealth() {
}
}

public int getFoodLevel() {
if (isOnline()) {
return getPlayerEntity().getFoodLevel();
}
else {
return getNBTEditor().getFoodLevel();
}
}

public dLocation getEyeLocation() {
if (isOnline()) {
return new dLocation(getPlayerEntity().getEyeLocation());
Expand Down Expand Up @@ -511,6 +520,15 @@ public void setMaxHealth(double maxHealth) {
}
}

public void setFoodLevel(int foodLevel) {
if (isOnline()) {
getPlayerEntity().setFoodLevel(foodLevel);
}
else {
getNBTEditor().setFoodLevel(foodLevel);
}
}

public void setLevel(int level) {
if (isOnline()) {
getPlayerEntity().setLevel(level);
Expand Down Expand Up @@ -1817,19 +1835,19 @@ else if (isOnline()) {
if (attribute.hasContext(2)) {
maxHunger = attribute.getIntContext(2);
}
if (getPlayerEntity().getFoodLevel() / maxHunger < .10) {
int foodLevel = getFoodLevel();
if (foodLevel / maxHunger < .10) {
return new Element("starving").getAttribute(attribute.fulfill(2));
}
else if (getPlayerEntity().getFoodLevel() / maxHunger < .40) {
else if (foodLevel / maxHunger < .40) {
return new Element("famished").getAttribute(attribute.fulfill(2));
}
else if (getPlayerEntity().getFoodLevel() / maxHunger < .75) {
else if (foodLevel / maxHunger < .75) {
return new Element("parched").getAttribute(attribute.fulfill(2));
}
else if (getPlayerEntity().getFoodLevel() / maxHunger < 1) {
else if (foodLevel / maxHunger < 1) {
return new Element("hungry").getAttribute(attribute.fulfill(2));
}

else {
return new Element("healthy").getAttribute(attribute.fulfill(2));
}
Expand All @@ -1853,7 +1871,7 @@ else if (getPlayerEntity().getFoodLevel() / maxHunger < 1) {
// Returns the current food level of the player.
// -->
if (attribute.startsWith("food_level")) {
return new Element(getPlayerEntity().getFoodLevel())
return new Element(getFoodLevel())
.getAttribute(attribute.fulfill(1));
}

Expand Down Expand Up @@ -2308,7 +2326,7 @@ public void adjust(Mechanism mechanism) {
// <p@player.food_level>
// -->
if (mechanism.matches("food_level") && mechanism.requireInteger()) {
getPlayerEntity().setFoodLevel(value.asInt());
setFoodLevel(value.asInt());
}

// <--[mechanism]
Expand Down

0 comments on commit 8faa913

Please sign in to comment.