Skip to content

Commit

Permalink
Fix writing mobeffectinstances (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE authored May 16, 2024
1 parent d54eefc commit 1b80c1b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<MobEffectDetails> customEffects = new ArrayList<>();
List<MobEffectInstance> 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);
}
Expand All @@ -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);
}
}

Expand All @@ -294,7 +294,7 @@ public FoodProperties readFoodProperties(ByteBuf buf) {
List<FoodProperties.PossibleEffect> 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);
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
public class PotionContents {
private final int potionId;
private final int customColor;
private final List<MobEffectDetails> customEffects;
private final List<MobEffectInstance> customEffects;
}

0 comments on commit 1b80c1b

Please sign in to comment.