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;
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;
}

View File

@ -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;

View File

@ -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<MapDecoration> decorations = new ArrayList<>();
private List<MapDecoration> 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<MapDecoration> {
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");
}
}
}
}

View File

@ -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();
}
}

View File

@ -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 extends PublicCloneable> T optionalClone(@Nullable T object) {
return object != null ? (T) object.clone() : null;
}
@NotNull
public static <T extends PublicCloneable> CopyOnWriteArrayList cloneCopyOnWriteArrayList(@NotNull List<T> list) {
CopyOnWriteArrayList<T> result = new CopyOnWriteArrayList<>();