diff --git a/src/main/java/net/minestom/server/item/metadata/ItemMeta.java b/src/main/java/net/minestom/server/item/metadata/ItemMeta.java index 27ed14884..5fb7f1305 100644 --- a/src/main/java/net/minestom/server/item/metadata/ItemMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/ItemMeta.java @@ -1,11 +1,54 @@ package net.minestom.server.item.metadata; -public abstract class ItemMeta { +import net.minestom.server.item.ItemStack; +import org.jglrxavpok.hephaistos.nbt.NBTCompound; - public abstract boolean hasNbt(); +/** + * Represent nbt data only available for a type of item + */ +public interface ItemMeta { - public abstract boolean isSimilar(ItemMeta itemMeta); + /** + * Get 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 ItemMeta clone(); + /** + * Get if the two ItemMeta are similar + *

+ * It is used by {@link ItemStack#isSimilar(ItemStack)} + * + * @param itemMeta the second item meta to check + * @return true if the two meta are similar, false otherwise + */ + boolean isSimilar(ItemMeta itemMeta); + + /** + * Read nbt data from a compound + *

+ * WARNING: it is possible that it contains no useful data, + * it has to be checked before getting anything + * + * @param compound the compound containing the data + */ + void read(NBTCompound compound); + + /** + * Write nbt data to a compound + * + * @param compound the compound receiving the item meta data + */ + void write(NBTCompound compound); + + /** + * Clone this item meta + *

+ * Used by {@link ItemStack#clone()} + * + * @return the cloned item meta + */ + ItemMeta clone(); } 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 47b165052..5ce6298bc 100644 --- a/src/main/java/net/minestom/server/item/metadata/MapMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/MapMeta.java @@ -1,6 +1,8 @@ package net.minestom.server.item.metadata; -public class MapMeta extends ItemMeta { +import org.jglrxavpok.hephaistos.nbt.NBTCompound; + +public class MapMeta implements ItemMeta { private int mapId; @@ -22,6 +24,20 @@ public class MapMeta extends ItemMeta { return itemMeta instanceof MapMeta && ((MapMeta) itemMeta).getMapId() == mapId; } + @Override + public void read(NBTCompound compound) { + if (compound.containsKey("map")) { + this.mapId = compound.getInt("map"); + } + } + + @Override + public void write(NBTCompound compound) { + if (mapId != 0) { + compound.setInt("map", mapId); + } + } + @Override public ItemMeta clone() { MapMeta mapMeta = new MapMeta(); diff --git a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java index 20f88a2d5..87dad3954 100644 --- a/src/main/java/net/minestom/server/item/metadata/PotionMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/PotionMeta.java @@ -1,12 +1,14 @@ package net.minestom.server.item.metadata; import net.minestom.server.potion.PotionType; +import net.minestom.server.registry.Registries; +import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.util.Collections; import java.util.HashSet; import java.util.Set; -public class PotionMeta extends ItemMeta { +public class PotionMeta implements ItemMeta { private Set potionTypes = new HashSet<>(); @@ -47,6 +49,22 @@ public class PotionMeta extends ItemMeta { return itemMeta instanceof PotionMeta && ((PotionMeta) itemMeta).potionTypes.equals(potionTypes); } + @Override + public void read(NBTCompound compound) { + if (compound.containsKey("Potion")) { + addPotionType(Registries.getPotionType(compound.getString("Potion"))); + } + } + + @Override + public void write(NBTCompound compound) { + if (!potionTypes.isEmpty()) { + for (PotionType potionType : potionTypes) { + compound.setString("Potion", potionType.getNamespaceID()); + } + } + } + @Override public ItemMeta clone() { PotionMeta potionMeta = new PotionMeta(); diff --git a/src/main/java/net/minestom/server/utils/NBTUtils.java b/src/main/java/net/minestom/server/utils/NBTUtils.java index 02d7a6adf..e31e7ef83 100644 --- a/src/main/java/net/minestom/server/utils/NBTUtils.java +++ b/src/main/java/net/minestom/server/utils/NBTUtils.java @@ -12,18 +12,18 @@ import net.minestom.server.item.NBTConsumer; import net.minestom.server.item.attribute.AttributeSlot; import net.minestom.server.item.attribute.ItemAttribute; import net.minestom.server.item.metadata.ItemMeta; -import net.minestom.server.item.metadata.MapMeta; -import net.minestom.server.item.metadata.PotionMeta; import net.minestom.server.network.packet.PacketReader; import net.minestom.server.network.packet.PacketWriter; -import net.minestom.server.potion.PotionType; import net.minestom.server.registry.Registries; import org.jglrxavpok.hephaistos.nbt.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; // for lack of a better name public class NBTUtils { @@ -172,19 +172,7 @@ public class NBTUtils { final ItemMeta itemMeta = item.getItemMeta(); if (itemMeta == null) return; - final Class metaType = itemMeta.getClass(); - if (metaType == PotionMeta.class) { - final PotionMeta potionMeta = (PotionMeta) itemMeta; - if (nbt.containsKey("Potion")) { - potionMeta.addPotionType(Registries.getPotionType(nbt.getString("Potion"))); - } - } else if (metaType == MapMeta.class) { - final MapMeta mapMeta = (MapMeta) itemMeta; - if (nbt.containsKey("map")) { - mapMeta.setMapId(nbt.getInt("map")); - } - } - + itemMeta.read(nbt); } private static void loadEnchantments(NBTList enchantments, EnchantmentSetter setter) { @@ -324,26 +312,14 @@ public class NBTUtils { itemNBT.setInt("CustomModelData", customModelData); } } + // End custom model data // Start custom meta final ItemMeta itemMeta = itemStack.getItemMeta(); if (itemMeta != null) { - final Class metaType = itemMeta.getClass(); - if (metaType == PotionMeta.class) { - final Set potionTypes = ((PotionMeta) itemMeta).getPotionTypes(); - if (!potionTypes.isEmpty()) { - for (PotionType potionType : potionTypes) { - itemNBT.setString("Potion", potionType.getNamespaceID()); - } - } - } else if (metaType == MapMeta.class) { - final int mapId = ((MapMeta) itemMeta).getMapId(); - if (mapId != 0) { - itemNBT.setInt("map", mapId); - } - } - + itemMeta.write(itemNBT); } + // End custom meta } @FunctionalInterface