From 0501f24bafd7bada65e685c0cb4354d4b90ebbfd Mon Sep 17 00:00:00 2001 From: WillQi Date: Sat, 19 Jun 2021 20:36:59 -0600 Subject: [PATCH 1/6] Implement natural regeneration for non-peaceful gamemodes --- src/main/java/cn/nukkit/Player.java | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/cn/nukkit/Player.java b/src/main/java/cn/nukkit/Player.java index fa43cdc50a9..3432682a80d 100644 --- a/src/main/java/cn/nukkit/Player.java +++ b/src/main/java/cn/nukkit/Player.java @@ -1638,15 +1638,20 @@ 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.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); } } @@ -4794,6 +4799,9 @@ public boolean isFoodEnabled() { } public void setFoodEnabled(boolean foodEnabled) { + if (foodEnabled == true) { + System.out.println(12); + } this.foodEnabled = foodEnabled; } From c61227def03d96caec3666c53e50b1f258e2ce62 Mon Sep 17 00:00:00 2001 From: WillQi Date: Sat, 19 Jun 2021 20:38:19 -0600 Subject: [PATCH 2/6] Do not regen for hardcore servers --- src/main/java/cn/nukkit/Player.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/cn/nukkit/Player.java b/src/main/java/cn/nukkit/Player.java index 3432682a80d..83c099e4256 100644 --- a/src/main/java/cn/nukkit/Player.java +++ b/src/main/java/cn/nukkit/Player.java @@ -1638,7 +1638,7 @@ public boolean onUpdate(int currentTick) { this.entityBaseTick(tickDiff); - if (this.getHealth() < this.getMaxHealth() && this.level.getGameRules().getBoolean(GameRule.NATURAL_REGENERATION)) { + 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); From 9253853a93f6ae92f2d7869a7c54fe52744995a7 Mon Sep 17 00:00:00 2001 From: WillQi Date: Sat, 19 Jun 2021 20:45:28 -0600 Subject: [PATCH 3/6] Whoops, removed debug code I had --- src/main/java/cn/nukkit/Player.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/cn/nukkit/Player.java b/src/main/java/cn/nukkit/Player.java index 83c099e4256..23a64f03319 100644 --- a/src/main/java/cn/nukkit/Player.java +++ b/src/main/java/cn/nukkit/Player.java @@ -4799,9 +4799,6 @@ public boolean isFoodEnabled() { } public void setFoodEnabled(boolean foodEnabled) { - if (foodEnabled == true) { - System.out.println(12); - } this.foodEnabled = foodEnabled; } From 729b64316b3559d091c3d88e146a59cb680023bb Mon Sep 17 00:00:00 2001 From: WillQi Date: Sun, 20 Jun 2021 17:39:39 -0600 Subject: [PATCH 4/6] Add hunger dealt when hit + fix client taking hunger when food is disabled --- src/main/java/cn/nukkit/Player.java | 1 + src/main/java/cn/nukkit/PlayerFood.java | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/cn/nukkit/Player.java b/src/main/java/cn/nukkit/Player.java index fa43cdc50a9..383c1bc0c48 100644 --- a/src/main/java/cn/nukkit/Player.java +++ b/src/main/java/cn/nukkit/Player.java @@ -4171,6 +4171,7 @@ public boolean attack(EntityDamageEvent source) { ((Player) damager).getFoodData().updateFoodExpLevel(0.3); } } + this.getFoodData().updateFoodExpLevel(0.1); EntityEventPacket pk = new EntityEventPacket(); pk.eid = this.id; pk.event = EntityEventPacket.HURT_ANIMATION; diff --git a/src/main/java/cn/nukkit/PlayerFood.java b/src/main/java/cn/nukkit/PlayerFood.java index 40ceb867c9b..1c1757466fd 100644 --- a/src/main/java/cn/nukkit/PlayerFood.java +++ b/src/main/java/cn/nukkit/PlayerFood.java @@ -171,12 +171,15 @@ 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()) { + this.sendFoodLevel(); + } else { + this.useHunger(1); + } this.foodExpLevel = 0; } } From 3a455a3b88bd751be7b370546a8668a726b16ce4 Mon Sep 17 00:00:00 2001 From: WillQi Date: Mon, 21 Jun 2021 14:44:09 -0600 Subject: [PATCH 5/6] Fix outdated hunger values + fix clientside hunger desync --- src/main/java/cn/nukkit/Player.java | 34 ++++++++++++------------- src/main/java/cn/nukkit/PlayerFood.java | 1 + 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/main/java/cn/nukkit/Player.java b/src/main/java/cn/nukkit/Player.java index 383c1bc0c48..5d9b2bfb6ff 100644 --- a/src/main/java/cn/nukkit/Player.java +++ b/src/main/java/cn/nukkit/Player.java @@ -1456,7 +1456,7 @@ protected void processMovement(int tickDiff) { } } - Location from = new Location( + Location from = new Location( this.lastX, this.lastY, this.lastZ, @@ -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); } } } @@ -3102,7 +3100,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()); @@ -4168,7 +4166,7 @@ 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); } } this.getFoodData().updateFoodExpLevel(0.1); diff --git a/src/main/java/cn/nukkit/PlayerFood.java b/src/main/java/cn/nukkit/PlayerFood.java index 1c1757466fd..7064b8b8ffa 100644 --- a/src/main/java/cn/nukkit/PlayerFood.java +++ b/src/main/java/cn/nukkit/PlayerFood.java @@ -176,6 +176,7 @@ public void updateFoodExpLevel(double use) { this.foodExpLevel += use; if (this.foodExpLevel > 4) { if (!this.getPlayer().isFoodEnabled()) { + // Hack to get around the client reducing food despite us not sending the attribute this.sendFoodLevel(); } else { this.useHunger(1); From b3c0f33b02bb5212a5df7fa9d41cbdbb8381c472 Mon Sep 17 00:00:00 2001 From: WillQi Date: Mon, 21 Jun 2021 16:18:40 -0600 Subject: [PATCH 6/6] fix hunger desync when player still has saturation + updated hunger values --- src/main/java/cn/nukkit/Player.java | 5 ++++- src/main/java/cn/nukkit/PlayerFood.java | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/cn/nukkit/Player.java b/src/main/java/cn/nukkit/Player.java index 7f25de77ac2..e3319c480d5 100644 --- a/src/main/java/cn/nukkit/Player.java +++ b/src/main/java/cn/nukkit/Player.java @@ -1650,6 +1650,9 @@ public boolean onUpdate(int currentTick) { if (this.ticksLived % 80 == 0 && this.getFoodData().getLevel() >= 18) { this.heal(1); + if (this.getServer().getDifficulty() > 0) { + this.getFoodData().updateFoodExpLevel(0.6); + } } } @@ -4174,12 +4177,12 @@ public boolean attack(EntityDamageEvent source) { ((Player) damager).getFoodData().updateFoodExpLevel(0.1); } } - this.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; diff --git a/src/main/java/cn/nukkit/PlayerFood.java b/src/main/java/cn/nukkit/PlayerFood.java index 7064b8b8ffa..a895cee7e47 100644 --- a/src/main/java/cn/nukkit/PlayerFood.java +++ b/src/main/java/cn/nukkit/PlayerFood.java @@ -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); }