PaperMC/paper-server/nms-patches/FoodMetaData.patch
CraftBukkit/Spigot 1ae7bcc4d0 #795: Add methods to modify the rate of regeneration and starvation
By: Martoph <sager1018@gmail.com>
2021-03-04 08:28:07 +11:00

95 lines
4.7 KiB
Diff

--- a/net/minecraft/server/FoodMetaData.java
+++ b/net/minecraft/server/FoodMetaData.java
@@ -6,9 +6,22 @@
public float saturationLevel = 5.0F;
public float exhaustionLevel;
private int foodTickTimer;
+ // 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() {}
+ public FoodMetaData() { throw new AssertionError("Whoopsie, we missed the bukkit."); } // CraftBukkit start - throw an error
+
+ // CraftBukkit start - added EntityHuman constructor
+ public FoodMetaData(EntityHuman entityhuman) {
+ org.apache.commons.lang.Validate.notNull(entityhuman);
+ this.entityhuman = entityhuman;
+ }
+ // CraftBukkit end
public void eat(int i, float f) {
this.foodLevel = Math.min(i + this.foodLevel, 20);
@@ -18,8 +31,17 @@
public void a(Item item, ItemStack itemstack) {
if (item.isFood()) {
FoodInfo foodinfo = item.getFoodInfo();
+ // CraftBukkit start
+ int oldFoodLevel = foodLevel;
+
+ 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 +55,15 @@
if (this.saturationLevel > 0.0F) {
this.saturationLevel = Math.max(this.saturationLevel - 1.0F, 0.0F);
} else if (enumdifficulty != EnumDifficulty.PEACEFUL) {
- this.foodLevel = Math.max(this.foodLevel - 1, 0);
+ // CraftBukkit start
+ org.bukkit.event.entity.FoodLevelChangeEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callFoodLevelChangeEvent(entityhuman, Math.max(this.foodLevel - 1, 0));
+
+ if (!event.isCancelled()) {
+ this.foodLevel = event.getFoodLevel();
+ }
+
+ ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutUpdateHealth(((EntityPlayer) entityhuman).getBukkitEntity().getScaledHealth(), this.foodLevel, this.saturationLevel));
+ // CraftBukkit end
}
}
@@ -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);
- this.a(f);
+ entityhuman.heal(f / 6.0F, org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.SATIATED); // CraftBukkit - added RegainReason
+ // this.a(f); CraftBukkit - EntityExhaustionEvent
+ entityhuman.applyExhaustion(f, org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.REGEN); // CraftBukkit - EntityExhaustionEvent
this.foodTickTimer = 0;
}
} else if (flag && this.foodLevel >= 18 && entityhuman.eJ()) {
++this.foodTickTimer;
- 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);
}