craftbukkit/nms-patches/net/minecraft/world/food/FoodMetaData.patch

107 lines
5 KiB
Diff
Raw Normal View History

2021-03-16 09:00:00 +11:00
--- a/net/minecraft/world/food/FoodMetaData.java
+++ b/net/minecraft/world/food/FoodMetaData.java
2023-03-15 03:30:00 +11:00
@@ -7,15 +7,33 @@
2021-03-16 09:00:00 +11:00
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameRules;
+// CraftBukkit start
+import net.minecraft.network.protocol.game.PacketPlayOutUpdateHealth;
+import net.minecraft.server.level.EntityPlayer;
+// CraftBukkit end
+
public class FoodMetaData {
public int foodLevel = 20;
public float saturationLevel = 5.0F;
public float exhaustionLevel;
2021-06-11 15:00:00 +10:00
private int tickTimer;
+ // CraftBukkit start
+ private EntityHuman entityhuman;
+ public int saturatedRegenRate = 10;
+ public int unsaturatedRegenRate = 80;
+ public int starvationRate = 80;
+ // CraftBukkit end
2021-06-11 15:00:00 +10:00
private int lastFoodLevel = 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
2024-04-24 01:15:00 +10:00
private void add(int i, float f) {
this.foodLevel = Math.min(i + this.foodLevel, 20);
2024-04-24 01:15:00 +10:00
@@ -30,7 +48,17 @@
FoodInfo foodinfo = (FoodInfo) itemstack.get(DataComponents.FOOD);
if (foodinfo != null) {
- this.add(foodinfo.nutrition(), foodinfo.saturation());
2019-04-23 12:00:00 +10:00
+ // CraftBukkit start
+ int oldFoodLevel = foodLevel;
2024-04-24 01:15:00 +10:00
+
+ org.bukkit.event.entity.FoodLevelChangeEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callFoodLevelChangeEvent(entityhuman, foodinfo.nutrition() + oldFoodLevel, itemstack);
+
2019-04-23 12:00:00 +10:00
+ if (!event.isCancelled()) {
2024-04-24 01:15:00 +10:00
+ this.add(event.getFoodLevel() - oldFoodLevel, foodinfo.saturation());
2019-04-23 12:00:00 +10:00
+ }
2021-03-16 09:00:00 +11:00
+
2019-04-23 12:00:00 +10:00
+ ((EntityPlayer) entityhuman).getBukkitEntity().sendHealthUpdate();
+ // CraftBukkit end
}
2019-04-23 12:00:00 +10:00
}
2024-04-24 01:15:00 +10:00
@@ -44,7 +72,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();
+ }
+
2021-11-22 09:00:00 +11:00
+ ((EntityPlayer) entityhuman).connection.send(new PacketPlayOutUpdateHealth(((EntityPlayer) entityhuman).getBukkitEntity().getScaledHealth(), this.foodLevel, this.saturationLevel));
+ // CraftBukkit end
}
}
2024-04-24 01:15:00 +10:00
@@ -52,23 +88,25 @@
2021-11-22 09:00:00 +11:00
if (flag && this.saturationLevel > 0.0F && entityhuman.isHurt() && this.foodLevel >= 20) {
2021-06-11 15:00:00 +10:00
++this.tickTimer;
- if (this.tickTimer >= 10) {
+ if (this.tickTimer >= this.saturatedRegenRate) { // CraftBukkit
2016-11-17 12:41:03 +11:00
float f = Math.min(this.saturationLevel, 6.0F);
2016-11-17 12:41:03 +11:00
- entityhuman.heal(f / 6.0F);
2021-11-22 09:00:00 +11:00
- this.addExhaustion(f);
2016-11-17 12:41:03 +11:00
+ entityhuman.heal(f / 6.0F, org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.SATIATED); // CraftBukkit - added RegainReason
2021-11-22 09:00:00 +11:00
+ // this.addExhaustion(f); CraftBukkit - EntityExhaustionEvent
+ entityhuman.causeFoodExhaustion(f, org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.REGEN); // CraftBukkit - EntityExhaustionEvent
2021-06-11 15:00:00 +10:00
this.tickTimer = 0;
}
2021-11-22 09:00:00 +11:00
} else if (flag && this.foodLevel >= 18 && entityhuman.isHurt()) {
2021-06-11 15:00:00 +10:00
++this.tickTimer;
- if (this.tickTimer >= 80) {
- entityhuman.heal(1.0F);
2021-11-22 09:00:00 +11:00
- this.addExhaustion(6.0F);
2021-06-11 15:00:00 +10:00
+ if (this.tickTimer >= this.unsaturatedRegenRate) { // CraftBukkit - add regen rate manipulation
+ entityhuman.heal(1.0F, org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.SATIATED); // CraftBukkit - added RegainReason
2021-03-04 08:28:05 +11:00
+ // this.a(6.0F); CraftBukkit - EntityExhaustionEvent
2021-11-22 09:00:00 +11:00
+ entityhuman.causeFoodExhaustion(6.0f, org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.REGEN); // CraftBukkit - EntityExhaustionEvent
2021-06-11 15:00:00 +10:00
this.tickTimer = 0;
}
2021-03-04 08:28:05 +11:00
} else if (this.foodLevel <= 0) {
2021-06-11 15:00:00 +10:00
++this.tickTimer;
- if (this.tickTimer >= 80) {
+ if (this.tickTimer >= this.starvationRate) { // CraftBukkit - add regen rate manipulation
if (entityhuman.getHealth() > 10.0F || enumdifficulty == EnumDifficulty.HARD || entityhuman.getHealth() > 1.0F && enumdifficulty == EnumDifficulty.NORMAL) {
2023-03-15 03:30:00 +11:00
entityhuman.hurt(entityhuman.damageSources().starve(), 1.0F);
}