PublicCloneable and support for ItemMeta#clone

This commit is contained in:
Felix Cravic 2020-12-09 21:15:02 +01:00
parent cea3b8b593
commit f39f6444d7
17 changed files with 284 additions and 229 deletions

View File

@ -1,5 +1,6 @@
package net.minestom.server.data;
import net.minestom.server.utils.clone.PublicCloneable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -11,7 +12,7 @@ import java.util.Set;
* <p>
* See {@link DataImpl} for the default implementation.
*/
public abstract class Data implements Cloneable {
public abstract class Data implements PublicCloneable<Data> {
public static final Data EMPTY = new Data() {
@Override

View File

@ -16,6 +16,7 @@ import net.minestom.server.registry.Registries;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Direction;
import net.minestom.server.utils.NBTUtils;
import net.minestom.server.utils.clone.PublicCloneable;
import net.minestom.server.utils.ownership.OwnershipHandler;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
@ -35,7 +36,7 @@ import java.util.*;
* Here a non-exhaustive list of what you can do to update the item:
* {@link PlayerInventory#refreshSlot(short)}, {@link Inventory#refreshSlot(short)} or a raw {@link SetSlotPacket}.
*/
public class ItemStack implements DataContainer {
public class ItemStack implements DataContainer, PublicCloneable<ItemStack> {
public static final OwnershipHandler<Data> DATA_OWNERSHIP = new OwnershipHandler();
public static final String OWNERSHIP_DATA_KEY = "ownership_identifier";
@ -551,38 +552,52 @@ public class ItemStack implements DataContainer {
}
/**
* Copies this item stack.
* @deprecated use {@link #clone()}
*/
@Deprecated
@NotNull
public synchronized ItemStack copy() {
return clone();
}
/**
* Clones this item stack.
* <p>
* Be aware that the identifier ({@link #getIdentifier()}) will change.
*
* @return a cloned item stack with a different identifier
*/
@NotNull
public synchronized ItemStack copy() {
ItemStack itemStack = new ItemStack(material, amount, damage);
itemStack.setDisplayName(displayName);
itemStack.setUnbreakable(unbreakable);
if (lore != null) {
itemStack.setLore(new ArrayList<>(lore));
@Override
public ItemStack clone() {
try {
ItemStack itemStack = (ItemStack) super.clone();
itemStack.setDisplayName(displayName);
itemStack.setUnbreakable(unbreakable);
if (lore != null) {
itemStack.setLore(new ArrayList<>(lore));
}
if (stackingRule != null) {
itemStack.setStackingRule(stackingRule);
}
itemStack.enchantmentMap = new HashMap<>(enchantmentMap);
itemStack.attributes = new ArrayList<>(attributes);
itemStack.hideFlag = hideFlag;
itemStack.customModelData = customModelData;
if (itemMeta != null)
itemStack.itemMeta = itemMeta.clone();
final Data data = getData();
if (data != null)
itemStack.setData(data.clone());
return itemStack;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
if (stackingRule != null) {
itemStack.setStackingRule(stackingRule);
}
itemStack.enchantmentMap = new HashMap<>(enchantmentMap);
itemStack.attributes = new ArrayList<>(attributes);
itemStack.hideFlag = hideFlag;
itemStack.customModelData = customModelData;
if (itemMeta != null)
itemStack.itemMeta = itemMeta.copy();
final Data data = getData();
if (data != null)
itemStack.setData(data.clone());
return itemStack;
}
@Nullable

View File

@ -3,7 +3,7 @@ package net.minestom.server.item.metadata;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
public class CompassMeta implements ItemMeta {
public class CompassMeta extends ItemMeta {
private boolean lodestoneTracked;
private String lodestoneDimension;
@ -84,7 +84,7 @@ public class CompassMeta implements ItemMeta {
@Override
public void write(@NotNull NBTCompound compound) {
compound.setByte("LodestoneTracked", (byte) (lodestoneTracked ? 1 : 0));
if(lodestoneDimension != null) {
if (lodestoneDimension != null) {
compound.setString("LodestoneDimension", lodestoneDimension);
}
@ -99,8 +99,8 @@ public class CompassMeta implements ItemMeta {
@NotNull
@Override
public ItemMeta copy() {
CompassMeta compassMeta = new CompassMeta();
public ItemMeta clone() {
CompassMeta compassMeta = (CompassMeta) super.clone();
compassMeta.lodestoneTracked = lodestoneTracked;
compassMeta.lodestoneDimension = lodestoneDimension;
compassMeta.x = x;

View File

@ -10,7 +10,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
public class CrossbowMeta implements ItemMeta {
public class CrossbowMeta extends ItemMeta {
private boolean triple;
private ItemStack projectile1, projectile2, projectile3;
@ -180,12 +180,12 @@ public class CrossbowMeta implements ItemMeta {
@NotNull
@Override
public ItemMeta copy() {
CrossbowMeta crossbowMeta = new CrossbowMeta();
public ItemMeta clone() {
CrossbowMeta crossbowMeta = (CrossbowMeta) super.clone();
crossbowMeta.triple = triple;
crossbowMeta.projectile1 = projectile1 == null ? null : projectile1.copy();
crossbowMeta.projectile2 = projectile2 == null ? null : projectile2.copy();
crossbowMeta.projectile3 = projectile3 == null ? null : projectile3.copy();
crossbowMeta.projectile1 = projectile1 == null ? null : projectile1.clone();
crossbowMeta.projectile2 = projectile2 == null ? null : projectile2.clone();
crossbowMeta.projectile3 = projectile3 == null ? null : projectile3.clone();
crossbowMeta.charged = charged;

View File

@ -9,7 +9,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class EnchantedBookMeta implements ItemMeta {
public class EnchantedBookMeta extends ItemMeta {
private final Map<Enchantment, Short> storedEnchantmentMap = new HashMap<>();
@ -85,8 +85,8 @@ public class EnchantedBookMeta implements ItemMeta {
@NotNull
@Override
public ItemMeta copy() {
EnchantedBookMeta enchantedBookMeta = new EnchantedBookMeta();
public ItemMeta clone() {
EnchantedBookMeta enchantedBookMeta = (EnchantedBookMeta) super.clone();
enchantedBookMeta.storedEnchantmentMap.putAll(storedEnchantmentMap);
return enchantedBookMeta;

View File

@ -3,7 +3,7 @@ package net.minestom.server.item.metadata;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
public class FireworkMeta implements ItemMeta {
public class FireworkMeta extends ItemMeta {
private boolean flicker;
private boolean trail;
@ -36,7 +36,7 @@ public class FireworkMeta implements ItemMeta {
@NotNull
@Override
public ItemMeta copy() {
public ItemMeta clone() {
return null;
}

View File

@ -1,20 +1,21 @@
package net.minestom.server.item.metadata;
import net.minestom.server.item.ItemStack;
import net.minestom.server.utils.clone.PublicCloneable;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
/**
* Represents nbt data only available for a type of item.
*/
public interface ItemMeta {
public abstract class ItemMeta implements PublicCloneable<ItemMeta> {
/**
* Gets if this meta object contains any useful data to send to the client.
*
* @return true if this item has nbt data, false otherwise
*/
boolean hasNbt();
public abstract boolean hasNbt();
/**
* Gets if the two ItemMeta are similar.
@ -24,7 +25,7 @@ public interface ItemMeta {
* @param itemMeta the second item meta to check
* @return true if the two meta are similar, false otherwise
*/
boolean isSimilar(@NotNull ItemMeta itemMeta);
public abstract boolean isSimilar(@NotNull ItemMeta itemMeta);
/**
* Reads nbt data from a compound.
@ -34,23 +35,23 @@ public interface ItemMeta {
*
* @param compound the compound containing the data
*/
void read(@NotNull NBTCompound compound);
public abstract void read(@NotNull NBTCompound compound);
/**
* Writes nbt data to a compound.
*
* @param compound the compound receiving the item meta data
*/
void write(@NotNull NBTCompound compound);
public abstract void write(@NotNull NBTCompound compound);
/**
* Copies this item meta.
* <p>
* Used by {@link ItemStack#copy()}.
*
* @return the cloned item meta
*/
@NotNull
ItemMeta copy();
@Override
public ItemMeta clone() {
try {
return (ItemMeta) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new IllegalStateException("Weird thing happened");
}
}
}

View File

@ -4,151 +4,165 @@ import net.minestom.server.chat.ChatColor;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
/** Represents the item meat for leather armor parts. */
public class LeatherArmorMeta implements ItemMeta {
/**
* Represents the item meta for leather armor parts.
*/
public class LeatherArmorMeta extends ItemMeta {
private static final int BIT_MASK = 0xFF;
private static final int BIT_MASK = 0xFF;
private boolean modified;
private byte red;
private byte green;
private byte blue;
private boolean modified;
private byte red;
private byte green;
private byte blue;
/**
* Sets the color of the leather armor piece.
*
* @param color the color of the leather armor
*/
public void setColor(ChatColor color) {
// TODO using "CHAT color" is pretty weird, maybe that the class should be renamed to "Color"
this.red = color.getRed();
this.green = color.getGreen();
this.blue = color.getBlue();
this.modified = true;
}
/**
* Changes the color of the leather armor piece.
*
* @param red The red color of the leather armor piece.
* @param green The green color of the leather armor piece.
* @param blue The blue color of the leather armor piece.
*/
public void setColor(byte red, byte green, byte blue) {
this.red = red;
this.green = green;
this.blue = blue;
this.modified = true;
}
/** Resets the color to the default leather one. */
public void reset() {
this.red = 0;
this.green = 0;
this.blue = 0;
this.modified = false;
}
/**
* Gets the red component.
*
* @return the red component
*/
public int getRed() {
return BIT_MASK & red;
}
/**
* Gets the green component.
*
* @return the green component
*/
public int getGreen() {
return BIT_MASK & green;
}
/**
* Gets the blue component.
*
* @return the blue component
*/
public int getBlue() {
return BIT_MASK & blue;
}
/**
* Gets if the color of this armor piece have been changed.
*
* @return true if the color has been changed, false otherwise
*/
public boolean isModified() {
return modified;
}
/** {@inheritDoc} */
@Override
public boolean hasNbt() {
return modified;
}
/** {@inheritDoc} */
@Override
public boolean isSimilar(@NotNull ItemMeta itemMeta) {
if (!(itemMeta instanceof LeatherArmorMeta)) return false;
final LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) itemMeta;
return leatherArmorMeta.isModified() == isModified()
&& leatherArmorMeta.getRed() == getRed()
&& leatherArmorMeta.getGreen() == getGreen()
&& leatherArmorMeta.getBlue() == getBlue();
}
/** {@inheritDoc} */
@Override
public void read(@NotNull NBTCompound compound) {
if (compound.containsKey("display")) {
final NBTCompound nbtCompound = compound.getCompound("display");
if (nbtCompound.containsKey("color")) {
final int color = nbtCompound.getInt("color");
// Sets the color of the leather armor piece
// This also fixes that the armor pieces do not decolorize again when you are in creative
// mode.
this.setColor(
(byte) ((color >> 16) & BIT_MASK),
(byte) ((color >> 8) & BIT_MASK),
(byte) ((color) & BIT_MASK));
}
/**
* Sets the color of the leather armor piece.
*
* @param color the color of the leather armor
*/
public void setColor(ChatColor color) {
// TODO using "CHAT color" is pretty weird, maybe that the class should be renamed to "Color"
this.red = color.getRed();
this.green = color.getGreen();
this.blue = color.getBlue();
this.modified = true;
}
}
/** {@inheritDoc} */
@Override
public void write(@NotNull NBTCompound compound) {
if (modified) {
NBTCompound displayCompound;
if (!compound.containsKey("display")) {
displayCompound = new NBTCompound();
} else {
displayCompound = compound.getCompound("display");
}
final int color = this.getRed() << 16 | this.getGreen() << 8 | this.getBlue();
displayCompound.setInt("color", color);
// Adds the color compound to the display compound
compound.set("display", displayCompound);
/**
* Changes the color of the leather armor piece.
*
* @param red The red color of the leather armor piece.
* @param green The green color of the leather armor piece.
* @param blue The blue color of the leather armor piece.
*/
public void setColor(byte red, byte green, byte blue) {
this.red = red;
this.green = green;
this.blue = blue;
this.modified = true;
}
}
/** {@inheritDoc} */
@NotNull
@Override
public ItemMeta copy() {
LeatherArmorMeta leatherArmorMeta = new LeatherArmorMeta();
leatherArmorMeta.modified = this.isModified();
leatherArmorMeta.red = (byte) this.getRed();
leatherArmorMeta.green = (byte) this.getGreen();
leatherArmorMeta.blue = (byte) this.getBlue();
/**
* Resets the color to the default leather one.
*/
public void reset() {
this.red = 0;
this.green = 0;
this.blue = 0;
this.modified = false;
}
return leatherArmorMeta;
}
/**
* Gets the red component.
*
* @return the red component
*/
public int getRed() {
return BIT_MASK & red;
}
/**
* Gets the green component.
*
* @return the green component
*/
public int getGreen() {
return BIT_MASK & green;
}
/**
* Gets the blue component.
*
* @return the blue component
*/
public int getBlue() {
return BIT_MASK & blue;
}
/**
* Gets if the color of this armor piece have been changed.
*
* @return true if the color has been changed, false otherwise
*/
public boolean isModified() {
return modified;
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasNbt() {
return modified;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSimilar(@NotNull ItemMeta itemMeta) {
if (!(itemMeta instanceof LeatherArmorMeta)) return false;
final LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) itemMeta;
return leatherArmorMeta.isModified() == isModified()
&& leatherArmorMeta.getRed() == getRed()
&& leatherArmorMeta.getGreen() == getGreen()
&& leatherArmorMeta.getBlue() == getBlue();
}
/**
* {@inheritDoc}
*/
@Override
public void read(@NotNull NBTCompound compound) {
if (compound.containsKey("display")) {
final NBTCompound nbtCompound = compound.getCompound("display");
if (nbtCompound.containsKey("color")) {
final int color = nbtCompound.getInt("color");
// Sets the color of the leather armor piece
// This also fixes that the armor pieces do not decolorize again when you are in creative
// mode.
this.setColor(
(byte) ((color >> 16) & BIT_MASK),
(byte) ((color >> 8) & BIT_MASK),
(byte) ((color) & BIT_MASK));
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void write(@NotNull NBTCompound compound) {
if (modified) {
NBTCompound displayCompound;
if (!compound.containsKey("display")) {
displayCompound = new NBTCompound();
} else {
displayCompound = compound.getCompound("display");
}
final int color = this.getRed() << 16 | this.getGreen() << 8 | this.getBlue();
displayCompound.setInt("color", color);
// Adds the color compound to the display compound
compound.set("display", displayCompound);
}
}
/**
* {@inheritDoc}
*/
@NotNull
@Override
public ItemMeta clone() {
LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) super.clone();
leatherArmorMeta.modified = this.isModified();
leatherArmorMeta.red = (byte) this.getRed();
leatherArmorMeta.green = (byte) this.getGreen();
leatherArmorMeta.blue = (byte) this.getBlue();
return leatherArmorMeta;
}
}

View File

@ -9,7 +9,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.ArrayList;
import java.util.List;
public class MapMeta implements ItemMeta {
public class MapMeta extends ItemMeta {
private int mapId;
private int mapScaleDirection = 1;
@ -132,17 +132,17 @@ public class MapMeta implements ItemMeta {
final byte type = decorationCompound.getAsByte("type");
byte x = 0;
if(decorationCompound.containsKey("x")) {
if (decorationCompound.containsKey("x")) {
x = decorationCompound.getAsByte("x");
}
byte z = 0;
if(decorationCompound.containsKey("z")) {
if (decorationCompound.containsKey("z")) {
z = decorationCompound.getAsByte("z");
}
double rotation = 0.0;
if(decorationCompound.containsKey("rot")) {
if (decorationCompound.containsKey("rot")) {
rotation = decorationCompound.getAsDouble("rot");
}
@ -194,8 +194,8 @@ public class MapMeta implements ItemMeta {
@NotNull
@Override
public ItemMeta copy() {
MapMeta mapMeta = new MapMeta();
public ItemMeta clone() {
MapMeta mapMeta = (MapMeta) super.clone();
mapMeta.setMapId(mapId);
mapMeta.setMapScaleDirection(mapScaleDirection);
mapMeta.decorations.addAll(decorations);

View File

@ -4,7 +4,7 @@ import net.minestom.server.entity.PlayerSkin;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
public class PlayerHeadMeta implements ItemMeta {
public class PlayerHeadMeta extends ItemMeta {
private String playerName;
private PlayerSkin playerSkin;
@ -34,7 +34,7 @@ public class PlayerHeadMeta implements ItemMeta {
@NotNull
@Override
public ItemMeta copy() {
public ItemMeta clone() {
return null;
}
}

View File

@ -19,7 +19,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
* {@link net.minestom.server.item.Material#SPLASH_POTION},
* {@link net.minestom.server.item.Material#TIPPED_ARROW}.
*/
public class PotionMeta implements ItemMeta {
public class PotionMeta extends ItemMeta {
private PotionType potionType;
private final List<CustomPotionEffect> customPotionEffects = new CopyOnWriteArrayList<>();
@ -152,8 +152,8 @@ public class PotionMeta implements ItemMeta {
@NotNull
@Override
public ItemMeta copy() {
PotionMeta potionMeta = new PotionMeta();
public ItemMeta clone() {
PotionMeta potionMeta = (PotionMeta) super.clone();
potionMeta.potionType = potionType;
potionMeta.customPotionEffects.addAll(customPotionEffects);

View File

@ -5,7 +5,7 @@ import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
// TODO for which item
public class SpawnEggMeta implements ItemMeta {
public class SpawnEggMeta extends ItemMeta {
private EntityType entityType;
@ -42,8 +42,8 @@ public class SpawnEggMeta implements ItemMeta {
@NotNull
@Override
public ItemMeta copy() {
SpawnEggMeta spawnEggMeta = new SpawnEggMeta();
public ItemMeta clone() {
SpawnEggMeta spawnEggMeta = (SpawnEggMeta) super.clone();
spawnEggMeta.entityType = entityType;
return spawnEggMeta;
}

View File

@ -8,7 +8,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.ArrayList;
public class WritableBookMeta implements ItemMeta {
public class WritableBookMeta extends ItemMeta {
private ArrayList<String> pages = new ArrayList<>();
@ -68,8 +68,8 @@ public class WritableBookMeta implements ItemMeta {
@NotNull
@Override
public ItemMeta copy() {
WritableBookMeta writableBookMeta = new WritableBookMeta();
public ItemMeta clone() {
WritableBookMeta writableBookMeta = (WritableBookMeta) super.clone();
writableBookMeta.pages.addAll(pages);
return writableBookMeta;

View File

@ -11,7 +11,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.ArrayList;
public class WrittenBookMeta implements ItemMeta {
public class WrittenBookMeta extends ItemMeta {
private boolean resolved;
private WrittenBookGeneration generation;
@ -180,8 +180,8 @@ public class WrittenBookMeta implements ItemMeta {
@NotNull
@Override
public ItemMeta copy() {
WrittenBookMeta writtenBookMeta = new WrittenBookMeta();
public ItemMeta clone() {
WrittenBookMeta writtenBookMeta = (WrittenBookMeta) super.clone();
writtenBookMeta.resolved = resolved;
writtenBookMeta.generation = generation;
writtenBookMeta.author = author;

View File

@ -1,5 +1,6 @@
package net.minestom.server.utils;
import net.minestom.server.utils.clone.PublicCloneable;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
@ -9,7 +10,7 @@ import java.util.Objects;
/**
* Represents the position of a block, so with integers instead of floating numbers.
*/
public class BlockPosition {
public class BlockPosition implements PublicCloneable<BlockPosition> {
private int x, y, z;
@ -199,24 +200,36 @@ public class BlockPosition {
/**
* Gets the square distance to another block position.
*
* @param blockPosition the block position to check the distance
* @return the distance between 'this' and {@code blockPosition}
*/
public int getDistanceSquared(@NotNull BlockPosition blockPosition) {
return MathUtils.square(getX() - blockPosition.getX()) +
MathUtils.square(getY() - blockPosition.getY()) +
MathUtils.square(getZ() - blockPosition.getZ());
}
*
* @param blockPosition the block position to check the distance
* @return the distance between 'this' and {@code blockPosition}
*/
public int getDistanceSquared(@NotNull BlockPosition blockPosition) {
return MathUtils.square(getX() - blockPosition.getX()) +
MathUtils.square(getY() - blockPosition.getY()) +
MathUtils.square(getZ() - blockPosition.getZ());
}
/**
* Copies this block position.
*
* @return the cloned block position
* @deprecated use {@link #clone()}
*/
@Deprecated
@NotNull
public BlockPosition copy() {
return new BlockPosition(x, y, z);
return clone();
}
@Override
public BlockPosition clone() {
try {
return (BlockPosition) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
/**
@ -229,7 +242,7 @@ public class BlockPosition {
this.y = blockPosition.getY();
this.z = blockPosition.getZ();
}
/**
* Converts this block position to a {@link Position}.
*

View File

@ -1,6 +1,7 @@
package net.minestom.server.utils;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.clone.PublicCloneable;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
@ -9,7 +10,7 @@ import java.util.Objects;
* Represents a position.
* The instance is not contained.
*/
public class Position implements Cloneable {
public class Position implements PublicCloneable<Position> {
private float x, y, z;
private float yaw, pitch;

View File

@ -0,0 +1,10 @@
package net.minestom.server.utils.clone;
/**
* Convenient interface to expose {@link Object#clone()} publicly with a generic.
*
* @param <T> the type to clone
*/
public interface PublicCloneable<T> extends Cloneable {
T clone();
}