net.minecraft.world.food

This commit is contained in:
Jake Potrebic 2024-12-14 11:29:09 -08:00
parent 3cce21ddce
commit e28654cfb2
No known key found for this signature in database
GPG key ID: ECE0B3C133C016C5
3 changed files with 39 additions and 68 deletions

View file

@ -1,22 +1,6 @@
--- a/net/minecraft/world/food/FoodData.java --- a/net/minecraft/world/food/FoodData.java
+++ b/net/minecraft/world/food/FoodData.java +++ b/net/minecraft/world/food/FoodData.java
@@ -1,11 +1,14 @@ @@ -12,6 +_,11 @@
package net.minecraft.world.food;
+import net.minecraft.world.level.GameRules;
import net.minecraft.nbt.CompoundTag;
+import net.minecraft.network.protocol.game.ClientboundSetHealthPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.Difficulty;
-import net.minecraft.world.level.GameRules;
+import net.minecraft.world.item.ItemStack;
+// CraftBukkit end
public class FoodData {
@@ -13,6 +16,11 @@
public float saturationLevel = 5.0F; public float saturationLevel = 5.0F;
public float exhaustionLevel; public float exhaustionLevel;
private int tickTimer; private int tickTimer;
@ -26,33 +10,30 @@
+ public int starvationRate = 80; + public int starvationRate = 80;
+ // CraftBukkit end + // CraftBukkit end
public FoodData() {} private void add(int foodLevel, float saturationLevel) {
this.foodLevel = Mth.clamp(foodLevel + this.foodLevel, 0, 20);
@@ -29,6 +37,20 @@ @@ -26,6 +_,17 @@
this.add(foodComponent.nutrition(), foodComponent.saturation()); this.add(foodProperties.nutrition(), foodProperties.saturation());
} }
+ // CraftBukkit start + // CraftBukkit start
+ public void eat(FoodProperties foodinfo, ItemStack itemstack, ServerPlayer entityplayer) { + public void eat(FoodProperties foodProperties, net.minecraft.world.item.ItemStack stack, ServerPlayer serverPlayer) {
+ int oldFoodLevel = this.foodLevel; + int oldFoodLevel = this.foodLevel;
+ + org.bukkit.event.entity.FoodLevelChangeEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callFoodLevelChangeEvent(serverPlayer, foodProperties.nutrition() + oldFoodLevel, stack);
+ org.bukkit.event.entity.FoodLevelChangeEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callFoodLevelChangeEvent(entityplayer, foodinfo.nutrition() + oldFoodLevel, itemstack);
+
+ if (!event.isCancelled()) { + if (!event.isCancelled()) {
+ this.add(event.getFoodLevel() - oldFoodLevel, foodinfo.saturation()); + this.add(event.getFoodLevel() - oldFoodLevel, foodProperties.saturation());
+ } + }
+ + serverPlayer.getBukkitEntity().sendHealthUpdate();
+ entityplayer.getBukkitEntity().sendHealthUpdate();
+ } + }
+ // CraftBukkit end + // CraftBukkit end
+ +
public void tick(ServerPlayer player) { public void tick(ServerPlayer player) {
ServerLevel worldserver = player.serverLevel(); ServerLevel serverLevel = player.serverLevel();
Difficulty enumdifficulty = worldserver.getDifficulty(); Difficulty difficulty = serverLevel.getDifficulty();
@@ -38,7 +60,15 @@ @@ -34,29 +_,39 @@
if (this.saturationLevel > 0.0F) { if (this.saturationLevel > 0.0F) {
this.saturationLevel = Math.max(this.saturationLevel - 1.0F, 0.0F); this.saturationLevel = Math.max(this.saturationLevel - 1.0F, 0.0F);
} else if (enumdifficulty != Difficulty.PEACEFUL) { } else if (difficulty != Difficulty.PEACEFUL) {
- this.foodLevel = Math.max(this.foodLevel - 1, 0); - this.foodLevel = Math.max(this.foodLevel - 1, 0);
+ // CraftBukkit start + // CraftBukkit start
+ org.bukkit.event.entity.FoodLevelChangeEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callFoodLevelChangeEvent(player, Math.max(this.foodLevel - 1, 0)); + org.bukkit.event.entity.FoodLevelChangeEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callFoodLevelChangeEvent(player, Math.max(this.foodLevel - 1, 0));
@ -61,28 +42,26 @@
+ this.foodLevel = event.getFoodLevel(); + this.foodLevel = event.getFoodLevel();
+ } + }
+ +
+ player.connection.send(new ClientboundSetHealthPacket(player.getBukkitEntity().getScaledHealth(), this.foodLevel, this.saturationLevel)); + player.connection.send(new net.minecraft.network.protocol.game.ClientboundSetHealthPacket(player.getBukkitEntity().getScaledHealth(), this.foodLevel, this.saturationLevel));
+ // CraftBukkit end + // CraftBukkit end
} }
} }
@@ -46,23 +76,25 @@ boolean _boolean = serverLevel.getGameRules().getBoolean(GameRules.RULE_NATURAL_REGENERATION);
if (_boolean && this.saturationLevel > 0.0F && player.isHurt() && this.foodLevel >= 20) {
if (flag && this.saturationLevel > 0.0F && player.isHurt() && this.foodLevel >= 20) { this.tickTimer++;
++this.tickTimer;
- if (this.tickTimer >= 10) { - if (this.tickTimer >= 10) {
+ if (this.tickTimer >= this.saturatedRegenRate) { // CraftBukkit + if (this.tickTimer >= this.saturatedRegenRate) { // CraftBukkit
float f = Math.min(this.saturationLevel, 6.0F); float min = Math.min(this.saturationLevel, 6.0F);
- player.heal(min / 6.0F);
- player.heal(f / 6.0F); - this.addExhaustion(min);
- this.addExhaustion(f); + player.heal(min / 6.0F, org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.SATIATED, true); // CraftBukkit - added RegainReason // Paper - This is fast regen
+ player.heal(f / 6.0F, org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.SATIATED, true); // CraftBukkit - added RegainReason // Paper - This is fast regen + // this.addExhaustion(min); CraftBukkit - EntityExhaustionEvent
+ // this.addExhaustion(f); CraftBukkit - EntityExhaustionEvent + player.causeFoodExhaustion(min, org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.REGEN); // CraftBukkit - EntityExhaustionEvent
+ player.causeFoodExhaustion(f, org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.REGEN); // CraftBukkit - EntityExhaustionEvent
this.tickTimer = 0; this.tickTimer = 0;
} }
} else if (flag && this.foodLevel >= 18 && player.isHurt()) { } else if (_boolean && this.foodLevel >= 18 && player.isHurt()) {
++this.tickTimer; this.tickTimer++;
- if (this.tickTimer >= 80) { - if (this.tickTimer >= 80) {
- player.heal(1.0F); - player.heal(1.0F);
- this.addExhaustion(6.0F); - this.addExhaustion(6.0F);
@ -93,9 +72,9 @@
this.tickTimer = 0; this.tickTimer = 0;
} }
} else if (this.foodLevel <= 0) { } else if (this.foodLevel <= 0) {
++this.tickTimer; this.tickTimer++;
- if (this.tickTimer >= 80) { - if (this.tickTimer >= 80) {
+ if (this.tickTimer >= this.starvationRate) { // CraftBukkit - add regen rate manipulation + if (this.tickTimer >= this.starvationRate) { // CraftBukkit - add regen rate manipulation
if (player.getHealth() > 10.0F || enumdifficulty == Difficulty.HARD || player.getHealth() > 1.0F && enumdifficulty == Difficulty.NORMAL) { if (player.getHealth() > 10.0F || difficulty == Difficulty.HARD || player.getHealth() > 1.0F && difficulty == Difficulty.NORMAL) {
player.hurtServer(worldserver, player.damageSources().starve(), 1.0F); player.hurtServer(serverLevel, player.damageSources().starve(), 1.0F);
} }

View file

@ -0,0 +1,11 @@
--- a/net/minecraft/world/food/FoodProperties.java
+++ b/net/minecraft/world/food/FoodProperties.java
@@ -41,7 +_,7 @@
RandomSource random = entity.getRandom();
level.playSound(null, entity.getX(), entity.getY(), entity.getZ(), consumable.sound().value(), SoundSource.NEUTRAL, 1.0F, random.triangle(1.0F, 0.4F));
if (entity instanceof Player player) {
- player.getFoodData().eat(this);
+ player.getFoodData().eat(this, stack, (net.minecraft.server.level.ServerPlayer) player); // CraftBukkit
level.playSound(
null, player.getX(), player.getY(), player.getZ(), SoundEvents.PLAYER_BURP, SoundSource.PLAYERS, 0.5F, Mth.randomBetween(random, 0.9F, 1.0F)
);

View file

@ -1,19 +0,0 @@
--- a/net/minecraft/world/food/FoodProperties.java
+++ b/net/minecraft/world/food/FoodProperties.java
@@ -5,6 +5,7 @@
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
+import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
@@ -31,7 +32,7 @@
world.playSound((Player) null, user.getX(), user.getY(), user.getZ(), (SoundEvent) consumable.sound().value(), SoundSource.NEUTRAL, 1.0F, randomsource.triangle(1.0F, 0.4F));
if (user instanceof Player entityhuman) {
- entityhuman.getFoodData().eat(this);
+ entityhuman.getFoodData().eat(this, stack, (ServerPlayer) entityhuman); // CraftBukkit
world.playSound((Player) null, entityhuman.getX(), entityhuman.getY(), entityhuman.getZ(), SoundEvents.PLAYER_BURP, SoundSource.PLAYERS, 0.5F, Mth.randomBetween(randomsource, 0.9F, 1.0F));
}