Skip to content

Commit

Permalink
Update FoodProperties serialization to 1.21.2
Browse files Browse the repository at this point in the history
  • Loading branch information
booky10 committed Oct 19, 2024
1 parent e00d582 commit 1cb2219
Showing 1 changed file with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,13 +38,29 @@ public class FoodProperties {
private List<PossibleEffect> effects;
private @Nullable ItemStack usingConvertsTo;

public FoodProperties(
int nutrition, float saturation, boolean canAlwaysEat
) {
this(nutrition, saturation, canAlwaysEat, 1.6f, Collections.emptyList());
}

/**
* <code>eatSeconds</code> and <code>effects</code>
* have been moved to {@link ItemConsumable} in 1.21.2
*/
@ApiStatus.Obsolete
public FoodProperties(
int nutrition, float saturation, boolean canAlwaysEat,
float eatSeconds, List<PossibleEffect> effects
) {
this(nutrition, saturation, canAlwaysEat, eatSeconds, effects, null);
}

/**
* <code>eatSeconds</code>, <code>effects</code> and <code>usingConvertsTo</code>
* have been moved to {@link ItemConsumable} in 1.21.2
*/
@ApiStatus.Obsolete
public FoodProperties(
int nutrition, float saturation, boolean canAlwaysEat, float eatSeconds,
List<PossibleEffect> effects, @Nullable ItemStack usingConvertsTo
Expand All @@ -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;
Expand All @@ -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() {
Expand All @@ -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<PossibleEffect> getEffects() {
return this.effects;
}

/**
* Has been moved to {@link ItemConsumable} in 1.21.2
*/
@ApiStatus.Obsolete
public void setEffects(List<PossibleEffect> 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;
}
Expand All @@ -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;
Expand Down

0 comments on commit 1cb2219

Please sign in to comment.