Skip to content

Commit afa6162

Browse files
committed
fix:random fix
1 parent 8f286c8 commit afa6162

6 files changed

Lines changed: 100 additions & 167 deletions

File tree

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,110 @@
11
--- a/net/minecraft/world/food/FoodData.java
22
+++ b/net/minecraft/world/food/FoodData.java
3-
@@ -5,17 +_,31 @@
3+
@@ -1,10 +_,14 @@
4+
package net.minecraft.world.food;
5+
6+
import net.minecraft.nbt.CompoundTag;
7+
+import net.minecraft.network.protocol.game.ClientboundSetHealthPacket;
8+
+import net.minecraft.server.level.ServerPlayer;
9+
import net.minecraft.util.Mth;
410
import net.minecraft.world.Difficulty;
511
import net.minecraft.world.entity.player.Player;
12+
+import net.minecraft.world.item.ItemStack;
613
import net.minecraft.world.level.GameRules;
7-
+import org.taiyitistmc.injection.world.food.InjectionFoodData;
14+
+import org.bukkit.craftbukkit.event.CraftEventFactory;
815

9-
-public class FoodData {
10-
+public class FoodData implements InjectionFoodData {
16+
public class FoodData {
1117
public int foodLevel = 20;
12-
public float saturationLevel;
13-
public float exhaustionLevel;
18+
@@ -13,10 +_,22 @@
1419
private int tickTimer;
1520
private int lastFoodLevel = 20;
21+
1622
+ // CraftBukkit start
17-
+ private Player entityhuman;
23+
+ public Player entityhuman = null;
1824
+ public int saturatedRegenRate = 10;
1925
+ public int unsaturatedRegenRate = 80;
2026
+ public int starvationRate = 80;
2127
+ // CraftBukkit end
22-
28+
+
2329
public FoodData() {
2430
this.saturationLevel = 5.0F;
2531
}
32+
33+
+ public FoodData player(Player player) {
34+
+ this.entityhuman = player;
35+
+ return this;
36+
+ }
37+
+
38+
private void add(int p_340988_, float p_340961_) {
39+
this.foodLevel = Mth.clamp(p_340988_ + this.foodLevel, 0, 20);
40+
this.saturationLevel = Mth.clamp(p_340961_ + this.saturationLevel, 0.0F, (float)this.foodLevel);
41+
@@ -30,37 +_,65 @@
42+
this.add(p_347533_.nutrition(), p_347533_.saturation());
43+
}
44+
45+
+ // CraftBukkit start
46+
+ public void eat(ItemStack itemstack, FoodProperties foodinfo) {
47+
+ int oldFoodLevel = this.foodLevel;
48+
+
49+
+ org.bukkit.event.entity.FoodLevelChangeEvent event = CraftEventFactory.callFoodLevelChangeEvent(this.entityhuman, foodinfo.nutrition() + oldFoodLevel, itemstack);
2650
+
27-
+ // CraftBukkit start - added EntityHuman constructor
28-
+ public FoodData(Player entityhuman) {
29-
+ org.apache.commons.lang3.Validate.notNull(entityhuman);
30-
+ this.entityhuman = entityhuman;
51+
+ if (!event.isCancelled()) {
52+
+ this.add(event.getFoodLevel() - oldFoodLevel, foodinfo.saturation());
53+
+ }
54+
+
55+
+ ((ServerPlayer) this.entityhuman).getBukkitEntity().sendHealthUpdate();
3156
+ }
3257
+ // CraftBukkit end
58+
+
59+
+
60+
public void tick(Player p_38711_) {
61+
Difficulty difficulty = p_38711_.level().getDifficulty();
62+
+ if (entityhuman == null) entityhuman = p_38711_;
63+
this.lastFoodLevel = this.foodLevel;
64+
if (this.exhaustionLevel > 4.0F) {
65+
this.exhaustionLevel -= 4.0F;
66+
if (this.saturationLevel > 0.0F) {
67+
this.saturationLevel = Math.max(this.saturationLevel - 1.0F, 0.0F);
68+
} else if (difficulty != Difficulty.PEACEFUL) {
69+
- this.foodLevel = Math.max(this.foodLevel - 1, 0);
70+
+ // CraftBukkit start
71+
+ org.bukkit.event.entity.FoodLevelChangeEvent event = CraftEventFactory.callFoodLevelChangeEvent(p_38711_, Math.max(this.foodLevel - 1, 0));
72+
+
73+
+ if (!event.isCancelled()) {
74+
+ this.foodLevel = event.getFoodLevel();
75+
+ }
76+
+
77+
+ ((ServerPlayer) p_38711_).connection.send(new ClientboundSetHealthPacket(((ServerPlayer) p_38711_).getBukkitEntity().getScaledHealth(), this.foodLevel, this.saturationLevel));
78+
+ // CraftBukkit end
79+
}
80+
}
3381

34-
private void add(int p_340988_, float p_340961_) {
35-
this.foodLevel = Mth.clamp(p_340988_ + this.foodLevel, 0, 20);
82+
boolean flag = p_38711_.level().getGameRules().getBoolean(GameRules.RULE_NATURAL_REGENERATION);
83+
if (flag && this.saturationLevel > 0.0F && p_38711_.isHurt() && this.foodLevel >= 20) {
84+
this.tickTimer++;
85+
- if (this.tickTimer >= 10) {
86+
+ if (this.tickTimer >= this.saturatedRegenRate) { // CraftBukkit
87+
float f = Math.min(this.saturationLevel, 6.0F);
88+
+ p_38711_.pushHealReason(org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.SATIATED);
89+
p_38711_.heal(f / 6.0F);
90+
+ p_38711_.pushExhaustReason(org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.REGEN); // CraftBukkit - EntityExhaustionEvent
91+
this.addExhaustion(f);
92+
this.tickTimer = 0;
93+
}
94+
} else if (flag && this.foodLevel >= 18 && p_38711_.isHurt()) {
95+
this.tickTimer++;
96+
- if (this.tickTimer >= 80) {
97+
+ if (this.tickTimer >= this.unsaturatedRegenRate) { // CraftBukkit - add regen rate manipulation
98+
+ p_38711_.pushHealReason(org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.SATIATED);
99+
p_38711_.heal(1.0F);
100+
+ p_38711_.pushExhaustReason(org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.REGEN); // CraftBukkit - EntityExhaustionEvent
101+
this.addExhaustion(6.0F);
102+
this.tickTimer = 0;
103+
}
104+
} else if (this.foodLevel <= 0) {
105+
this.tickTimer++;
106+
- if (this.tickTimer >= 80) {
107+
+ if (this.tickTimer >= this.starvationRate) { // CraftBukkit - add regen rate manipulation
108+
if (p_38711_.getHealth() > 10.0F || difficulty == Difficulty.HARD || p_38711_.getHealth() > 1.0F && difficulty == Difficulty.NORMAL) {
109+
p_38711_.hurt(p_38711_.damageSources().starve(), 1.0F);
110+
}

src/main/java/org/taiyitistmc/mixin/server/level/MixinServerPlayer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public abstract class MixinServerPlayer extends Player implements InjectionServe
127127
public long timeOffset = 0;
128128
public WeatherType weather = null;
129129
public boolean relativeTime = true;
130-
public String locale = null; // CraftBukkit - add, lowercase // Paper - default to null
130+
public String locale = "en_us"; // CraftBukkit - add, lowercase
131131
public CraftPlayer.TransferCookieConnection transferCookieConnection;
132132
@Shadow
133133
private ResourceKey<Level> respawnDimension;
@@ -483,7 +483,7 @@ public void reset() {
483483
this.setRemainingFireTicks(0);
484484
this.resetFallDistance();
485485
this.foodData = new FoodData();
486-
this.foodData.setEntityhuman((ServerPlayer) (Object) this);
486+
this.foodData.entityhuman = ((ServerPlayer) (Object) this);
487487
this.experienceLevel = this.newLevel;
488488
this.totalExperience = this.newTotalExp;
489489
this.experienceProgress = 0.0f;

src/main/java/org/taiyitistmc/mixin/server/network/MixinServerHandshakePacketListenerImpl.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.taiyitistmc.mixin.server.network;
22

3-
import com.google.gson.Gson;
43
import java.net.InetAddress;
54
import java.net.InetSocketAddress;
65
import java.util.HashMap;
6+
77
import net.minecraft.network.Connection;
88
import net.minecraft.network.chat.Component;
99
import net.minecraft.network.protocol.handshake.ClientIntentionPacket;
@@ -24,19 +24,20 @@
2424
@Mixin(ServerHandshakePacketListenerImpl.class)
2525
public abstract class MixinServerHandshakePacketListenerImpl implements ServerHandshakePacketListener {
2626

27-
// CraftBukkit start - add fields
28-
private static final HashMap<InetAddress, Long> throttleTracker = new HashMap<InetAddress, Long>();
29-
// CraftBukkit end
30-
private static final Gson gson = new Gson();
31-
private static final java.util.regex.Pattern HOST_PATTERN = java.util.regex.Pattern.compile("[0-9a-f\\.:]{0,45}");
32-
private static int throttleCounter = 0;
3327
@Shadow
3428
@Final
3529
private Connection connection;
3630
@Shadow
3731
@Final
3832
private MinecraftServer server;
3933

34+
@Shadow
35+
private static int throttleCounter;
36+
37+
@Shadow
38+
@Final
39+
private static HashMap<InetAddress, Long> throttleTracker;
40+
4041
@Inject(method = "handleIntention", at = @At("HEAD"))
4142
private void taiyitist$setHostName(ClientIntentionPacket packet, CallbackInfo ci) {
4243
this.connection.hostname = packet.hostName() + ":" + packet.port(); // CraftBukkit - set hostname

src/main/java/org/taiyitistmc/mixin/world/entity/player/MixinPlayer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected MixinPlayer(EntityType<? extends LivingEntity> entityType, Level level
100100

101101
@Inject(method = "<init>", at = @At("RETURN"))
102102
private void taiyitist$init(CallbackInfo ci) {
103-
this.foodData.setEntityhuman((Player) (Object) this);
103+
this.foodData.entityhuman = ((Player) (Object) this);
104104
this.enderChestInventory.setOwner(this.getBukkitEntity());
105105
}
106106

src/main/java/org/taiyitistmc/mixin/world/food/MixinFoodData.java

Lines changed: 0 additions & 142 deletions
This file was deleted.

src/main/resources/neotaiyitist.mixins.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@
139139
"world.entity.npc.MixinWanderingTraderSpawner",
140140
"world.entity.player.MixinInventory",
141141
"world.entity.player.MixinPlayer",
142-
"world.food.MixinFoodData",
143142
"world.inventory.MixinAbstractFurnaceMenu",
144143
"world.inventory.MixinAnvilMenu",
145144
"world.inventory.MixinBeaconMenu",

0 commit comments

Comments
 (0)