mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-27 02:21:38 +01:00
WIP collection deep cloning. Starting with PotionMeta
This commit is contained in:
parent
f2c586177f
commit
69d4792191
@ -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<CustomPotionEffect> customPotionEffects = new CopyOnWriteArrayList<>();
|
||||
|
||||
// Not final because of #clone()
|
||||
private List<CustomPotionEffect> 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<CustomPotionEffect> 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;
|
||||
|
@ -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}.
|
||||
* <p>
|
||||
* This is an immutable class.
|
||||
*/
|
||||
public class CustomPotionEffect {
|
||||
public class CustomPotionEffect implements PublicCloneable<CustomPotionEffect> {
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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 <T extends PublicCloneable> CopyOnWriteArrayList cloneCopyOnWriteArrayList(@NotNull List<T> list) {
|
||||
CopyOnWriteArrayList<T> result = new CopyOnWriteArrayList<>();
|
||||
for (T element : list) {
|
||||
result.add((T) element.clone());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user