From 1b80c1b6ebfb1bd5d7705814ac2c7606283b176b Mon Sep 17 00:00:00 2001 From: Alex <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Thu, 16 May 2024 22:28:23 +0200 Subject: [PATCH] Fix writing mobeffectinstances (#814) --- .../game/item/component/FoodProperties.java | 2 +- .../game/item/component/ItemCodecHelper.java | 26 ++++++++++++------- .../game/item/component/MobEffectDetails.java | 5 +--- .../item/component/MobEffectInstance.java | 13 ++++++++++ .../game/item/component/PotionContents.java | 2 +- 5 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/MobEffectInstance.java diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/FoodProperties.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/FoodProperties.java index 8a3614ee6..ab9f973a8 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/FoodProperties.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/FoodProperties.java @@ -17,7 +17,7 @@ public class FoodProperties { @Data @AllArgsConstructor public static class PossibleEffect { - private final MobEffectDetails effect; + private final MobEffectInstance effect; private final float probability; } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/ItemCodecHelper.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/ItemCodecHelper.java index 80b76383a..e426343c2 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/ItemCodecHelper.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/ItemCodecHelper.java @@ -256,10 +256,10 @@ public PotionContents readPotionContents(ByteBuf buf) { int potionId = buf.readBoolean() ? this.readVarInt(buf) : -1; int customColor = buf.readBoolean() ? buf.readInt() : -1; - List customEffects = new ArrayList<>(); + List customEffects = new ArrayList<>(); int effectCount = this.readVarInt(buf); for (int i = 0; i < effectCount; i++) { - customEffects.add(this.readEffectDetails(buf)); + customEffects.add(this.readEffectInstance(buf)); } return new PotionContents(potionId, customColor, customEffects); } @@ -280,8 +280,8 @@ public void writePotionContents(ByteBuf buf, PotionContents contents) { } this.writeVarInt(buf, contents.getCustomEffects().size()); - for (MobEffectDetails customEffect : contents.getCustomEffects()) { - this.writeEffectDetails(buf, customEffect); + for (MobEffectInstance customEffect : contents.getCustomEffects()) { + this.writeEffectInstance(buf, customEffect); } } @@ -294,7 +294,7 @@ public FoodProperties readFoodProperties(ByteBuf buf) { List effects = new ArrayList<>(); int effectCount = this.readVarInt(buf); for (int i = 0; i < effectCount; i++) { - effects.add(new FoodProperties.PossibleEffect(this.readEffectDetails(buf), buf.readFloat())); + effects.add(new FoodProperties.PossibleEffect(this.readEffectInstance(buf), buf.readFloat())); } return new FoodProperties(nutrition, saturationModifier, canAlwaysEat, eatSeconds, effects); @@ -308,24 +308,32 @@ public void writeFoodProperties(ByteBuf buf, FoodProperties properties) { this.writeVarInt(buf, properties.getEffects().size()); for (FoodProperties.PossibleEffect effect : properties.getEffects()) { - this.writeEffectDetails(buf, effect.getEffect()); + this.writeEffectInstance(buf, effect.getEffect()); buf.writeFloat(effect.getProbability()); } } - public MobEffectDetails readEffectDetails(ByteBuf buf) { + public MobEffectInstance readEffectInstance(ByteBuf buf) { Effect effect = this.readEffect(buf); + return new MobEffectInstance(effect, this.readEffectDetails(buf)); + } + + public MobEffectDetails readEffectDetails(ByteBuf buf) { int amplifier = this.readVarInt(buf); int duration = this.readVarInt(buf); boolean ambient = buf.readBoolean(); boolean showParticles = buf.readBoolean(); boolean showIcon = buf.readBoolean(); MobEffectDetails hiddenEffect = this.readNullable(buf, this::readEffectDetails); - return new MobEffectDetails(effect, amplifier, duration, ambient, showParticles, showIcon, hiddenEffect); + return new MobEffectDetails(amplifier, duration, ambient, showParticles, showIcon, hiddenEffect); + } + + public void writeEffectInstance(ByteBuf buf, MobEffectInstance instance) { + this.writeEffect(buf, instance.getEffect()); + this.writeEffectDetails(buf, instance.getDetails()); } public void writeEffectDetails(ByteBuf buf, MobEffectDetails details) { - this.writeEffect(buf, details.getEffect()); this.writeVarInt(buf, details.getAmplifier()); this.writeVarInt(buf, details.getDuration()); buf.writeBoolean(details.isAmbient()); diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/MobEffectDetails.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/MobEffectDetails.java index 2f6e32ef4..29406e782 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/MobEffectDetails.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/MobEffectDetails.java @@ -2,18 +2,15 @@ import lombok.AllArgsConstructor; import lombok.Data; -import lombok.NonNull; -import org.geysermc.mcprotocollib.protocol.data.game.entity.Effect; import org.jetbrains.annotations.Nullable; @Data @AllArgsConstructor public class MobEffectDetails { - private final @NonNull Effect effect; private final int amplifier; private final int duration; private final boolean ambient; private final boolean showParticles; private final boolean showIcon; private final @Nullable MobEffectDetails hiddenEffect; -} \ No newline at end of file +} diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/MobEffectInstance.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/MobEffectInstance.java new file mode 100644 index 000000000..9084f146b --- /dev/null +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/MobEffectInstance.java @@ -0,0 +1,13 @@ +package org.geysermc.mcprotocollib.protocol.data.game.item.component; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NonNull; +import org.geysermc.mcprotocollib.protocol.data.game.entity.Effect; + +@Data +@AllArgsConstructor +public class MobEffectInstance { + private final @NonNull Effect effect; + private final @NonNull MobEffectDetails details; +} diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/PotionContents.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/PotionContents.java index e2cb2a73b..dd7fbe4eb 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/PotionContents.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/PotionContents.java @@ -10,5 +10,5 @@ public class PotionContents { private final int potionId; private final int customColor; - private final List customEffects; + private final List customEffects; }