Use CloneUtils for deep copy

This commit is contained in:
themode 2020-12-15 03:40:07 +01:00
parent 17fc4fc7e6
commit a3613bff89
5 changed files with 31 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package net.minestom.server.item.metadata; package net.minestom.server.item.metadata;
import net.minestom.server.utils.Position; import net.minestom.server.utils.Position;
import net.minestom.server.utils.clone.CloneUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -95,7 +96,7 @@ public class CompassMeta extends ItemMeta {
CompassMeta compassMeta = (CompassMeta) super.clone(); CompassMeta compassMeta = (CompassMeta) super.clone();
compassMeta.lodestoneTracked = lodestoneTracked; compassMeta.lodestoneTracked = lodestoneTracked;
compassMeta.lodestoneDimension = lodestoneDimension; compassMeta.lodestoneDimension = lodestoneDimension;
compassMeta.lodestonePosition = lodestonePosition != null ? lodestonePosition.clone() : null; compassMeta.lodestonePosition = CloneUtils.optionalClone(lodestonePosition);
return compassMeta; return compassMeta;
} }

View File

@ -4,6 +4,7 @@ import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material; import net.minestom.server.item.Material;
import net.minestom.server.registry.Registries; import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NBTUtils; import net.minestom.server.utils.NBTUtils;
import net.minestom.server.utils.clone.CloneUtils;
import net.minestom.server.utils.validate.Check; import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -183,9 +184,9 @@ public class CrossbowMeta extends ItemMeta {
public ItemMeta clone() { public ItemMeta clone() {
CrossbowMeta crossbowMeta = (CrossbowMeta) super.clone(); CrossbowMeta crossbowMeta = (CrossbowMeta) super.clone();
crossbowMeta.triple = triple; crossbowMeta.triple = triple;
crossbowMeta.projectile1 = projectile1 == null ? null : projectile1.clone(); crossbowMeta.projectile1 = CloneUtils.optionalClone(projectile1);
crossbowMeta.projectile2 = projectile2 == null ? null : projectile2.clone(); crossbowMeta.projectile2 = CloneUtils.optionalClone(projectile2);
crossbowMeta.projectile3 = projectile3 == null ? null : projectile3.clone(); crossbowMeta.projectile3 = CloneUtils.optionalClone(projectile3);
crossbowMeta.charged = charged; crossbowMeta.charged = charged;

View File

@ -1,19 +1,21 @@
package net.minestom.server.item.metadata; package net.minestom.server.item.metadata;
import net.minestom.server.chat.ChatColor; 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.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList; import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTTypes; import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class MapMeta extends ItemMeta { public class MapMeta extends ItemMeta {
private int mapId; private int mapId;
private int mapScaleDirection = 1; private int mapScaleDirection = 1;
private List<MapDecoration> decorations = new ArrayList<>(); private List<MapDecoration> decorations = new CopyOnWriteArrayList<>();
private ChatColor mapColor = ChatColor.NO_COLOR; private ChatColor mapColor = ChatColor.NO_COLOR;
public MapMeta() { public MapMeta() {
@ -198,18 +200,18 @@ public class MapMeta extends ItemMeta {
MapMeta mapMeta = (MapMeta) super.clone(); MapMeta mapMeta = (MapMeta) super.clone();
mapMeta.setMapId(mapId); mapMeta.setMapId(mapId);
mapMeta.setMapScaleDirection(mapScaleDirection); mapMeta.setMapScaleDirection(mapScaleDirection);
mapMeta.decorations.addAll(decorations); mapMeta.decorations = CloneUtils.cloneCopyOnWriteArrayList(decorations);
mapMeta.setMapColor(mapColor); mapMeta.setMapColor(mapColor);
return mapMeta; return mapMeta;
} }
public static class MapDecoration { public static class MapDecoration implements PublicCloneable<MapDecoration> {
private final String id; private final String id;
private final byte type; private final byte type;
private final byte x, z; private final byte x, z;
private final double rotation; 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.id = id;
this.type = type; this.type = type;
this.x = x; this.x = x;
@ -262,6 +264,17 @@ public class MapMeta extends ItemMeta {
public double getRotation() { public double getRotation() {
return rotation; return rotation;
} }
@NotNull
@Override
public MapDecoration clone() {
try {
return (MapDecoration) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new IllegalStateException("Something weird happened");
}
}
} }
} }

View File

@ -69,9 +69,6 @@ public class WritableBookMeta extends ItemMeta {
@NotNull @NotNull
@Override @Override
public ItemMeta clone() { public ItemMeta clone() {
WritableBookMeta writableBookMeta = (WritableBookMeta) super.clone(); return super.clone();
writableBookMeta.pages.addAll(pages);
return writableBookMeta;
} }
} }

View File

@ -1,12 +1,18 @@
package net.minestom.server.utils.clone; package net.minestom.server.utils.clone;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
public final class CloneUtils { public final class CloneUtils {
@Nullable
public static <T extends PublicCloneable> T optionalClone(@Nullable T object) {
return object != null ? (T) object.clone() : null;
}
@NotNull @NotNull
public static <T extends PublicCloneable> CopyOnWriteArrayList cloneCopyOnWriteArrayList(@NotNull List<T> list) { public static <T extends PublicCloneable> CopyOnWriteArrayList cloneCopyOnWriteArrayList(@NotNull List<T> list) {
CopyOnWriteArrayList<T> result = new CopyOnWriteArrayList<>(); CopyOnWriteArrayList<T> result = new CopyOnWriteArrayList<>();