Skip to content

Commit

Permalink
Merge pull request #1057 from booky10/fix/potion-contents-component
Browse files Browse the repository at this point in the history
Fix potion contents item component for 1.21.2/1.21.3
  • Loading branch information
booky10 authored Nov 7, 2024
2 parents 4cf7069 + b14dfc7 commit 6f490a7
Showing 1 changed file with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.github.retrooper.packetevents.protocol.component.builtin.item;

import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.potion.Potion;
import com.github.retrooper.packetevents.protocol.potion.PotionEffect;
import com.github.retrooper.packetevents.protocol.potion.Potions;
Expand All @@ -32,28 +33,44 @@ public class ItemPotionContents {
private @Nullable Potion potion;
private @Nullable Integer customColor;
private List<PotionEffect> customEffects;
private @Nullable String customName;

public ItemPotionContents(
@Nullable Potion potion,
@Nullable Integer customColor,
List<PotionEffect> customEffects
) {
this(potion, customColor, customEffects, null);
}

public ItemPotionContents(
@Nullable Potion potion,
@Nullable Integer customColor,
List<PotionEffect> customEffects,
@Nullable String customName
) {
this.potion = potion;
this.customColor = customColor;
this.customEffects = customEffects;
this.customName = customName;
}

public static ItemPotionContents read(PacketWrapper<?> wrapper) {
Potion potionId = wrapper.readOptional(ew -> ew.readMappedEntity(Potions::getById));
Integer customColor = wrapper.readOptional(PacketWrapper::readInt);
List<PotionEffect> customEffects = wrapper.readList(PotionEffect::read);
return new ItemPotionContents(potionId, customColor, customEffects);
String customName = wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_21_2)
? wrapper.readOptional(PacketWrapper::readString) : null;
return new ItemPotionContents(potionId, customColor, customEffects, customName);
}

public static void write(PacketWrapper<?> wrapper, ItemPotionContents contents) {
wrapper.writeOptional(contents.potion, PacketWrapper::writeMappedEntity);
wrapper.writeOptional(contents.customColor, PacketWrapper::writeInt);
wrapper.writeList(contents.customEffects, PotionEffect::write);
if (wrapper.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_21_2)) {
wrapper.writeOptional(contents.customName, PacketWrapper::writeString);
}
}

public @Nullable Potion getPotion() {
Expand Down Expand Up @@ -84,18 +101,27 @@ public void setCustomEffects(List<PotionEffect> customEffects) {
this.customEffects = customEffects;
}

public @Nullable String getCustomName() {
return this.customName;
}

public void setCustomName(@Nullable String customName) {
this.customName = customName;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof ItemPotionContents)) return false;
ItemPotionContents that = (ItemPotionContents) obj;
if (!Objects.equals(this.potion, that.potion)) return false;
if (!Objects.equals(this.customColor, that.customColor)) return false;
return this.customEffects.equals(that.customEffects);
if (!this.customEffects.equals(that.customEffects)) return false;
return Objects.equals(this.customName, that.customName);
}

@Override
public int hashCode() {
return Objects.hash(this.potion, this.customColor, this.customEffects);
return Objects.hash(this.potion, this.customColor, this.customEffects, this.customName);
}
}

0 comments on commit 6f490a7

Please sign in to comment.