Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix health regen + hunger falling when food is disabled #1856

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
57 changes: 32 additions & 25 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,7 @@ protected void processMovement(int tickDiff) {
}
}

Location from = new Location(
Location from = new Location(
this.lastX,
this.lastY,
this.lastZ,
Expand Down Expand Up @@ -1511,25 +1511,23 @@ protected void processMovement(int tickDiff) {
else this.speed.setComponents(0, 0, 0);
}

if (!revert && (this.isFoodEnabled() || this.getServer().getDifficulty() == 0)) {
if (!revert) {
if ((this.isSurvival() || this.isAdventure())/* && !this.getRiddingOn() instanceof Entity*/) {

//UpdateFoodExpLevel
if (distance >= 0.05) {
double jump = 0;
double swimming = this.isInsideOfWater() ? 0.015 * distance : 0;
if (swimming != 0) distance = 0;
if (this.isSprinting()) { //Running
if (this.inAirTicks == 3 && swimming == 0) {
jump = 0.7;
}
this.getFoodData().updateFoodExpLevel(0.06 * distance + jump + swimming);
} else {
if (this.inAirTicks == 3 && swimming == 0) {
jump = 0.2;
}
this.getFoodData().updateFoodExpLevel(0.01 * distance + jump + swimming);
double jump = 0;
double swimming = this.isInsideOfWater() ? 0.01 * distance : 0;
if (swimming != 0) distance = 0;
if (this.isSprinting()) { //Running
if (this.inAirTicks == 3 && swimming == 0) {
jump = 0.2;
}
this.getFoodData().updateFoodExpLevel(0.1 * distance + jump + swimming);
} else {
if (this.inAirTicks == 3 && swimming == 0) {
jump = 0.05;
}
this.getFoodData().updateFoodExpLevel(jump + swimming);
}
}
}
Expand Down Expand Up @@ -1638,15 +1636,23 @@ public boolean onUpdate(int currentTick) {

this.entityBaseTick(tickDiff);

if (this.getServer().getDifficulty() == 0 && this.level.getGameRules().getBoolean(GameRule.NATURAL_REGENERATION)) {
if (this.getHealth() < this.getMaxHealth() && this.ticksLived % 20 == 0) {
this.heal(1);
}
if (this.getHealth() < this.getMaxHealth() && !this.server.isHardcore() && this.level.getGameRules().getBoolean(GameRule.NATURAL_REGENERATION)) {
if (this.getServer().getDifficulty() == 0) {
if (this.ticksLived % 20 == 0) {
this.heal(1);
}

PlayerFood foodData = this.getFoodData();
PlayerFood foodData = this.getFoodData();
if (foodData.getLevel() < 20 && this.ticksLived % 10 == 0) {
foodData.addFoodLevel(1, 0);
}
}

if (foodData.getLevel() < 20 && this.ticksLived % 10 == 0) {
foodData.addFoodLevel(1, 0);
if (this.ticksLived % 80 == 0 && this.getFoodData().getLevel() >= 18) {
this.heal(1);
if (this.getServer().getDifficulty() > 0) {
this.getFoodData().updateFoodExpLevel(0.6);
}
}
}

Expand Down Expand Up @@ -3102,7 +3108,7 @@ public void onCompletion(Server server) {

if (this.canInteract(blockVector.add(0.5, 0.5, 0.5), this.isCreative() ? 13 : 7) && (i = this.level.useBreakOn(blockVector.asVector3(), face, i, this, true)) != null) {
if (this.isSurvival()) {
this.getFoodData().updateFoodExpLevel(0.025);
this.getFoodData().updateFoodExpLevel(0.005);
if (!i.equals(oldItem) || i.getCount() != oldItem.getCount()) {
inventory.setItemInHand(i);
inventory.sendHeldItem(this.getViewers().values());
Expand Down Expand Up @@ -4168,14 +4174,15 @@ public boolean attack(EntityDamageEvent source) {
if (source instanceof EntityDamageByEntityEvent) {
Entity damager = ((EntityDamageByEntityEvent) source).getDamager();
if (damager instanceof Player) {
((Player) damager).getFoodData().updateFoodExpLevel(0.3);
((Player) damager).getFoodData().updateFoodExpLevel(0.1);
}
}
EntityEventPacket pk = new EntityEventPacket();
pk.eid = this.id;
pk.event = EntityEventPacket.HURT_ANIMATION;
this.dataPacket(pk);
}
this.getFoodData().updateFoodExpLevel(0.1);
return true;
} else {
return false;
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/cn/nukkit/PlayerFood.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public void useHunger(int amount) {
float newSfl = sfl - amount;
if (newSfl < 0) newSfl = 0;
this.setFoodSaturationLevel(newSfl);
this.sendFoodLevel();
} else {
this.setLevel(foodLevel - amount);
}
Expand Down Expand Up @@ -171,12 +172,16 @@ public void update(int tickDiff) {
}

public void updateFoodExpLevel(double use) {
if (!this.getPlayer().isFoodEnabled()) return;
if (Server.getInstance().getDifficulty() == 0) return;
if (this.getPlayer().hasEffect(Effect.SATURATION)) return;
this.foodExpLevel += use;
if (this.foodExpLevel > 4) {
this.useHunger(1);
if (!this.getPlayer().isFoodEnabled()) {
// Hack to get around the client reducing food despite us not sending the attribute
this.sendFoodLevel();
} else {
this.useHunger(1);
}
this.foodExpLevel = 0;
}
}
Expand Down