From 1ae7bcc4d0fbbdc3496323045299f0e95e8f686d Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Thu, 4 Mar 2021 08:28:07 +1100 Subject: [PATCH] #795: Add methods to modify the rate of regeneration and starvation By: Martoph --- paper-server/nms-patches/FoodMetaData.patch | 36 +++++++---- .../craftbukkit/entity/CraftHumanEntity.java | 60 +++++++++++++++++++ .../craftbukkit/entity/CraftPlayer.java | 30 ---------- 3 files changed, 86 insertions(+), 40 deletions(-) diff --git a/paper-server/nms-patches/FoodMetaData.patch b/paper-server/nms-patches/FoodMetaData.patch index 054cc9be7a..178068a8ef 100644 --- a/paper-server/nms-patches/FoodMetaData.patch +++ b/paper-server/nms-patches/FoodMetaData.patch @@ -1,10 +1,15 @@ --- a/net/minecraft/server/FoodMetaData.java +++ b/net/minecraft/server/FoodMetaData.java -@@ -6,9 +6,17 @@ +@@ -6,9 +6,22 @@ public float saturationLevel = 5.0F; public float exhaustionLevel; private int foodTickTimer; -+ private EntityHuman entityhuman; // CraftBukkit ++ // CraftBukkit start ++ private EntityHuman entityhuman; ++ public int saturatedRegenRate = 10; ++ public int unsaturatedRegenRate = 80; ++ public int starvationRate = 80; ++ // CraftBukkit end private int e = 20; - public FoodMetaData() {} @@ -19,26 +24,26 @@ public void eat(int i, float f) { this.foodLevel = Math.min(i + this.foodLevel, 20); -@@ -18,8 +26,17 @@ +@@ -18,8 +31,17 @@ public void a(Item item, ItemStack itemstack) { if (item.isFood()) { FoodInfo foodinfo = item.getFoodInfo(); + // CraftBukkit start + int oldFoodLevel = foodLevel; - -- this.eat(foodinfo.getNutrition(), foodinfo.getSaturationModifier()); ++ + org.bukkit.event.entity.FoodLevelChangeEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callFoodLevelChangeEvent(entityhuman, foodinfo.getNutrition() + oldFoodLevel, itemstack); + + if (!event.isCancelled()) { + this.eat(event.getFoodLevel() - oldFoodLevel, foodinfo.getSaturationModifier()); + } -+ + +- this.eat(foodinfo.getNutrition(), foodinfo.getSaturationModifier()); + ((EntityPlayer) entityhuman).getBukkitEntity().sendHealthUpdate(); + // CraftBukkit end } } -@@ -33,7 +50,15 @@ +@@ -33,7 +55,15 @@ if (this.saturationLevel > 0.0F) { this.saturationLevel = Math.max(this.saturationLevel - 1.0F, 0.0F); } else if (enumdifficulty != EnumDifficulty.PEACEFUL) { @@ -55,8 +60,12 @@ } } -@@ -44,15 +69,17 @@ - if (this.foodTickTimer >= 10) { +@@ -41,23 +71,25 @@ + + if (flag && this.saturationLevel > 0.0F && entityhuman.eJ() && this.foodLevel >= 20) { + ++this.foodTickTimer; +- if (this.foodTickTimer >= 10) { ++ if (this.foodTickTimer >= this.saturatedRegenRate) { // CraftBukkit float f = Math.min(this.saturationLevel, 6.0F); - entityhuman.heal(f / 6.0F); @@ -68,12 +77,19 @@ } } else if (flag && this.foodLevel >= 18 && entityhuman.eJ()) { ++this.foodTickTimer; - if (this.foodTickTimer >= 80) { +- if (this.foodTickTimer >= 80) { - entityhuman.heal(1.0F); - this.a(6.0F); ++ if (this.foodTickTimer >= this.unsaturatedRegenRate) { // CraftBukkit - add regen rate manipulation + entityhuman.heal(1.0F, org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.SATIATED); // CraftBukkit - added RegainReason + // this.a(6.0F); CraftBukkit - EntityExhaustionEvent + entityhuman.applyExhaustion(6.0f, org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.REGEN); // CraftBukkit - EntityExhaustionEvent this.foodTickTimer = 0; } } else if (this.foodLevel <= 0) { + ++this.foodTickTimer; +- if (this.foodTickTimer >= 80) { ++ if (this.foodTickTimer >= this.starvationRate) { // CraftBukkit - add regen rate manipulation + if (entityhuman.getHealth() > 10.0F || enumdifficulty == EnumDifficulty.HARD || entityhuman.getHealth() > 1.0F && enumdifficulty == EnumDifficulty.NORMAL) { + entityhuman.damageEntity(DamageSource.STARVE, 1.0F); + } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java index 96b55867d8..3ea267a786 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java @@ -574,4 +574,64 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity { public boolean dropItem(boolean dropAll) { return getHandle().dropItem(dropAll); } + + @Override + public float getExhaustion() { + return getHandle().getFoodData().exhaustionLevel; + } + + @Override + public void setExhaustion(float value) { + getHandle().getFoodData().exhaustionLevel = value; + } + + @Override + public float getSaturation() { + return getHandle().getFoodData().saturationLevel; + } + + @Override + public void setSaturation(float value) { + getHandle().getFoodData().saturationLevel = value; + } + + @Override + public int getFoodLevel() { + return getHandle().getFoodData().foodLevel; + } + + @Override + public void setFoodLevel(int value) { + getHandle().getFoodData().foodLevel = value; + } + + @Override + public int getSaturatedRegenRate() { + return getHandle().getFoodData().saturatedRegenRate; + } + + @Override + public void setSaturatedRegenRate(int i) { + getHandle().getFoodData().saturatedRegenRate = i; + } + + @Override + public int getUnsaturatedRegenRate() { + return getHandle().getFoodData().unsaturatedRegenRate; + } + + @Override + public void setUnsaturatedRegenRate(int i) { + getHandle().getFoodData().unsaturatedRegenRate = i; + } + + @Override + public int getStarvationRate() { + return getHandle().getFoodData().starvationRate; + } + + @Override + public void setStarvationRate(int i) { + getHandle().getFoodData().starvationRate = i; + } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index a499b17eab..31877a21cc 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -1032,36 +1032,6 @@ public class CraftPlayer extends CraftHumanEntity implements Player { getHandle().playerConnection.sendPacket(packet); } - @Override - public float getExhaustion() { - return getHandle().getFoodData().exhaustionLevel; - } - - @Override - public void setExhaustion(float value) { - getHandle().getFoodData().exhaustionLevel = value; - } - - @Override - public float getSaturation() { - return getHandle().getFoodData().saturationLevel; - } - - @Override - public void setSaturation(float value) { - getHandle().getFoodData().saturationLevel = value; - } - - @Override - public int getFoodLevel() { - return getHandle().getFoodData().foodLevel; - } - - @Override - public void setFoodLevel(int value) { - getHandle().getFoodData().foodLevel = value; - } - @Nullable private static WeakReference getPluginWeakReference(@Nullable Plugin plugin) { return (plugin == null) ? null : pluginWeakReferences.computeIfAbsent(plugin, WeakReference::new);