diff --git a/src/main/java/net/minestom/server/item/metadata/CompassMeta.java b/src/main/java/net/minestom/server/item/metadata/CompassMeta.java index 8a0e007a9..da5424d51 100644 --- a/src/main/java/net/minestom/server/item/metadata/CompassMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/CompassMeta.java @@ -1,6 +1,7 @@ package net.minestom.server.item.metadata; import net.minestom.server.utils.Position; +import net.minestom.server.utils.clone.CloneUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; @@ -95,7 +96,7 @@ public class CompassMeta extends ItemMeta { CompassMeta compassMeta = (CompassMeta) super.clone(); compassMeta.lodestoneTracked = lodestoneTracked; compassMeta.lodestoneDimension = lodestoneDimension; - compassMeta.lodestonePosition = lodestonePosition != null ? lodestonePosition.clone() : null; + compassMeta.lodestonePosition = CloneUtils.optionalClone(lodestonePosition); return compassMeta; } diff --git a/src/main/java/net/minestom/server/item/metadata/CrossbowMeta.java b/src/main/java/net/minestom/server/item/metadata/CrossbowMeta.java index 075f69427..72ba1c539 100644 --- a/src/main/java/net/minestom/server/item/metadata/CrossbowMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/CrossbowMeta.java @@ -4,6 +4,7 @@ import net.minestom.server.item.ItemStack; import net.minestom.server.item.Material; import net.minestom.server.registry.Registries; import net.minestom.server.utils.NBTUtils; +import net.minestom.server.utils.clone.CloneUtils; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; import org.jglrxavpok.hephaistos.nbt.NBTCompound; @@ -183,9 +184,9 @@ public class CrossbowMeta extends ItemMeta { public ItemMeta clone() { CrossbowMeta crossbowMeta = (CrossbowMeta) super.clone(); crossbowMeta.triple = triple; - crossbowMeta.projectile1 = projectile1 == null ? null : projectile1.clone(); - crossbowMeta.projectile2 = projectile2 == null ? null : projectile2.clone(); - crossbowMeta.projectile3 = projectile3 == null ? null : projectile3.clone(); + crossbowMeta.projectile1 = CloneUtils.optionalClone(projectile1); + crossbowMeta.projectile2 = CloneUtils.optionalClone(projectile2); + crossbowMeta.projectile3 = CloneUtils.optionalClone(projectile3); crossbowMeta.charged = charged; diff --git a/src/main/java/net/minestom/server/item/metadata/MapMeta.java b/src/main/java/net/minestom/server/item/metadata/MapMeta.java index 64993e97e..e16aef362 100644 --- a/src/main/java/net/minestom/server/item/metadata/MapMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/MapMeta.java @@ -1,19 +1,21 @@ package net.minestom.server.item.metadata; import net.minestom.server.chat.ChatColor; +import net.minestom.server.utils.clone.CloneUtils; +import net.minestom.server.utils.clone.PublicCloneable; import org.jetbrains.annotations.NotNull; import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTList; import org.jglrxavpok.hephaistos.nbt.NBTTypes; -import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; public class MapMeta extends ItemMeta { private int mapId; private int mapScaleDirection = 1; - private List decorations = new ArrayList<>(); + private List decorations = new CopyOnWriteArrayList<>(); private ChatColor mapColor = ChatColor.NO_COLOR; public MapMeta() { @@ -198,18 +200,18 @@ public class MapMeta extends ItemMeta { MapMeta mapMeta = (MapMeta) super.clone(); mapMeta.setMapId(mapId); mapMeta.setMapScaleDirection(mapScaleDirection); - mapMeta.decorations.addAll(decorations); + mapMeta.decorations = CloneUtils.cloneCopyOnWriteArrayList(decorations); mapMeta.setMapColor(mapColor); return mapMeta; } - public static class MapDecoration { + public static class MapDecoration implements PublicCloneable { private final String id; private final byte type; private final byte x, z; private final double rotation; - public MapDecoration(String id, byte type, byte x, byte z, double rotation) { + public MapDecoration(@NotNull String id, byte type, byte x, byte z, double rotation) { this.id = id; this.type = type; this.x = x; @@ -262,6 +264,17 @@ public class MapMeta extends ItemMeta { public double getRotation() { return rotation; } + + @NotNull + @Override + public MapDecoration clone() { + try { + return (MapDecoration) super.clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + throw new IllegalStateException("Something weird happened"); + } + } } } diff --git a/src/main/java/net/minestom/server/item/metadata/WritableBookMeta.java b/src/main/java/net/minestom/server/item/metadata/WritableBookMeta.java index f5d74f5c5..3958baf59 100644 --- a/src/main/java/net/minestom/server/item/metadata/WritableBookMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/WritableBookMeta.java @@ -69,9 +69,6 @@ public class WritableBookMeta extends ItemMeta { @NotNull @Override public ItemMeta clone() { - WritableBookMeta writableBookMeta = (WritableBookMeta) super.clone(); - writableBookMeta.pages.addAll(pages); - - return writableBookMeta; + return super.clone(); } } diff --git a/src/main/java/net/minestom/server/utils/clone/CloneUtils.java b/src/main/java/net/minestom/server/utils/clone/CloneUtils.java index 26689dfc0..f0f9f58fc 100644 --- a/src/main/java/net/minestom/server/utils/clone/CloneUtils.java +++ b/src/main/java/net/minestom/server/utils/clone/CloneUtils.java @@ -1,12 +1,18 @@ package net.minestom.server.utils.clone; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public final class CloneUtils { + @Nullable + public static T optionalClone(@Nullable T object) { + return object != null ? (T) object.clone() : null; + } + @NotNull public static CopyOnWriteArrayList cloneCopyOnWriteArrayList(@NotNull List list) { CopyOnWriteArrayList result = new CopyOnWriteArrayList<>();