From 1cb2219dbb0fc7e23367ec0b2d1c2d2e574e701f Mon Sep 17 00:00:00 2001 From: booky10 Date: Sat, 19 Oct 2024 02:09:40 +0200 Subject: [PATCH] Update FoodProperties serialization to 1.21.2 --- .../builtin/item/FoodProperties.java | 63 +++++++++++++++++-- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/com/github/retrooper/packetevents/protocol/component/builtin/item/FoodProperties.java b/api/src/main/java/com/github/retrooper/packetevents/protocol/component/builtin/item/FoodProperties.java index d4ca6e486..6eca57e13 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/protocol/component/builtin/item/FoodProperties.java +++ b/api/src/main/java/com/github/retrooper/packetevents/protocol/component/builtin/item/FoodProperties.java @@ -22,8 +22,10 @@ import com.github.retrooper.packetevents.protocol.item.ItemStack; import com.github.retrooper.packetevents.protocol.potion.PotionEffect; import com.github.retrooper.packetevents.wrapper.PacketWrapper; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import java.util.Collections; import java.util.List; import java.util.Objects; @@ -36,6 +38,17 @@ public class FoodProperties { private List effects; private @Nullable ItemStack usingConvertsTo; + public FoodProperties( + int nutrition, float saturation, boolean canAlwaysEat + ) { + this(nutrition, saturation, canAlwaysEat, 1.6f, Collections.emptyList()); + } + + /** + * eatSeconds and effects + * have been moved to {@link ItemConsumable} in 1.21.2 + */ + @ApiStatus.Obsolete public FoodProperties( int nutrition, float saturation, boolean canAlwaysEat, float eatSeconds, List effects @@ -43,6 +56,11 @@ public FoodProperties( this(nutrition, saturation, canAlwaysEat, eatSeconds, effects, null); } + /** + * eatSeconds, effects and usingConvertsTo + * have been moved to {@link ItemConsumable} in 1.21.2 + */ + @ApiStatus.Obsolete public FoodProperties( int nutrition, float saturation, boolean canAlwaysEat, float eatSeconds, List effects, @Nullable ItemStack usingConvertsTo @@ -59,6 +77,9 @@ public static FoodProperties read(PacketWrapper wrapper) { int nutrition = wrapper.readVarInt(); float saturation = wrapper.readFloat(); boolean canAlwaysEat = wrapper.readBoolean(); + if (wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_21_2)) { + return new FoodProperties(nutrition, saturation, canAlwaysEat); + } float eatSeconds = wrapper.readFloat(); ItemStack usingConvertsTo = wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_21) ? wrapper.readOptional(PacketWrapper::readItemStack) : null; @@ -70,11 +91,13 @@ public static void write(PacketWrapper wrapper, FoodProperties props) { wrapper.writeVarInt(props.nutrition); wrapper.writeFloat(props.saturation); wrapper.writeBoolean(props.canAlwaysEat); - wrapper.writeFloat(props.eatSeconds); - if (wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_21)) { - wrapper.writeOptional(props.usingConvertsTo, PacketWrapper::writeItemStack); + if (wrapper.getServerVersion().isOlderThan(ServerVersion.V_1_21_2)) { + wrapper.writeFloat(props.eatSeconds); + if (wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_21)) { + wrapper.writeOptional(props.usingConvertsTo, PacketWrapper::writeItemStack); + } + wrapper.writeList(props.effects, PossibleEffect::write); } - wrapper.writeList(props.effects, PossibleEffect::write); } public int getNutrition() { @@ -101,30 +124,58 @@ public void setCanAlwaysEat(boolean canAlwaysEat) { this.canAlwaysEat = canAlwaysEat; } + /** + * Has been moved to {@link ItemConsumable} in 1.21.2 + */ + @ApiStatus.Obsolete public float getEatSeconds() { return this.eatSeconds; } + /** + * Has been moved to {@link ItemConsumable} in 1.21.2 + */ + @ApiStatus.Obsolete public void setEatSeconds(float eatSeconds) { this.eatSeconds = eatSeconds; } + /** + * Has been moved to {@link ItemConsumable} in 1.21.2 + */ + @ApiStatus.Obsolete public void addEffect(PossibleEffect effect) { this.effects.add(effect); } + /** + * Has been moved to {@link ItemConsumable} in 1.21.2 + */ + @ApiStatus.Obsolete public List getEffects() { return this.effects; } + /** + * Has been moved to {@link ItemConsumable} in 1.21.2 + */ + @ApiStatus.Obsolete public void setEffects(List effects) { this.effects = effects; } + /** + * Has been moved to {@link ItemConsumable} in 1.21.2 + */ + @ApiStatus.Obsolete public @Nullable ItemStack getUsingConvertsTo() { return this.usingConvertsTo; } + /** + * Has been moved to {@link ItemConsumable} in 1.21.2 + */ + @ApiStatus.Obsolete public void setUsingConvertsTo(@Nullable ItemStack usingConvertsTo) { this.usingConvertsTo = usingConvertsTo; } @@ -147,6 +198,10 @@ public int hashCode() { return Objects.hash(this.nutrition, this.saturation, this.canAlwaysEat, this.eatSeconds, this.effects, this.usingConvertsTo); } + /** + * Has been moved to {@link ItemConsumable} in 1.21.2 + */ + @ApiStatus.Obsolete public static class PossibleEffect { private PotionEffect effect;