From 69d47921916599a3af0ed0528bbf1a1d4bc1b704 Mon Sep 17 00:00:00 2001 From: themode Date: Tue, 15 Dec 2020 03:29:47 +0100 Subject: [PATCH] WIP collection deep cloning. Starting with PotionMeta --- .../server/item/metadata/PotionMeta.java | 17 +++++++--- .../server/potion/CustomPotionEffect.java | 33 ++++++++++++++++++- .../server/utils/clone/CloneUtils.java | 19 +++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 src/main/java/net/minestom/server/utils/clone/CloneUtils.java diff --git a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java index bd7f60ff8..cb3b8f5b1 100644 --- a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java @@ -4,7 +4,9 @@ import net.minestom.server.chat.ChatColor; import net.minestom.server.potion.CustomPotionEffect; import net.minestom.server.potion.PotionType; import net.minestom.server.registry.Registries; +import net.minestom.server.utils.clone.CloneUtils; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTList; import org.jglrxavpok.hephaistos.nbt.NBTTypes; @@ -22,7 +24,9 @@ import java.util.concurrent.CopyOnWriteArrayList; public class PotionMeta extends ItemMeta { private PotionType potionType; - private final List customPotionEffects = new CopyOnWriteArrayList<>(); + + // Not final because of #clone() + private List customPotionEffects = new CopyOnWriteArrayList<>(); private boolean hasColor; private byte red, green, blue; @@ -32,6 +36,7 @@ public class PotionMeta extends ItemMeta { * * @return the potion type */ + @Nullable public PotionType getPotionType() { return potionType; } @@ -41,15 +46,16 @@ public class PotionMeta extends ItemMeta { * * @param potionType the new potion type */ - public void setPotionType(PotionType potionType) { + public void setPotionType(@Nullable PotionType potionType) { this.potionType = potionType; } /** * Get a list of {@link CustomPotionEffect}. * - * @return the custom potion effects + * @return the custom potion effect list */ + @NotNull public List getCustomPotionEffects() { return customPotionEffects; } @@ -75,7 +81,8 @@ public class PotionMeta extends ItemMeta { @Override public boolean hasNbt() { - return potionType != null; + return potionType != null || + !customPotionEffects.isEmpty(); } @Override @@ -155,7 +162,7 @@ public class PotionMeta extends ItemMeta { public ItemMeta clone() { PotionMeta potionMeta = (PotionMeta) super.clone(); potionMeta.potionType = potionType; - potionMeta.customPotionEffects.addAll(customPotionEffects); + potionMeta.customPotionEffects = CloneUtils.cloneCopyOnWriteArrayList(customPotionEffects); potionMeta.hasColor = hasColor; potionMeta.red = red; diff --git a/src/main/java/net/minestom/server/potion/CustomPotionEffect.java b/src/main/java/net/minestom/server/potion/CustomPotionEffect.java index 450f00919..d84daa1f1 100644 --- a/src/main/java/net/minestom/server/potion/CustomPotionEffect.java +++ b/src/main/java/net/minestom/server/potion/CustomPotionEffect.java @@ -1,9 +1,16 @@ package net.minestom.server.potion; +import net.minestom.server.utils.clone.PublicCloneable; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; + /** * Represents a custom effect in {@link net.minestom.server.item.metadata.PotionMeta}. + *

+ * This is an immutable class. */ -public class CustomPotionEffect { +public class CustomPotionEffect implements PublicCloneable { private final byte id; private final byte amplifier; @@ -45,4 +52,28 @@ public class CustomPotionEffect { public boolean showIcon() { return showIcon; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CustomPotionEffect that = (CustomPotionEffect) o; + return id == that.id && amplifier == that.amplifier && duration == that.duration && ambient == that.ambient && showParticles == that.showParticles && showIcon == that.showIcon; + } + + @Override + public int hashCode() { + return Objects.hash(id, amplifier, duration, ambient, showParticles, showIcon); + } + + @NotNull + @Override + public CustomPotionEffect clone() { + try { + return (CustomPotionEffect) super.clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + throw new IllegalStateException("Weird thing happened"); + } + } } diff --git a/src/main/java/net/minestom/server/utils/clone/CloneUtils.java b/src/main/java/net/minestom/server/utils/clone/CloneUtils.java new file mode 100644 index 000000000..26689dfc0 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/clone/CloneUtils.java @@ -0,0 +1,19 @@ +package net.minestom.server.utils.clone; + +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +public final class CloneUtils { + + @NotNull + public static CopyOnWriteArrayList cloneCopyOnWriteArrayList(@NotNull List list) { + CopyOnWriteArrayList result = new CopyOnWriteArrayList<>(); + for (T element : list) { + result.add((T) element.clone()); + } + return result; + } + +}