item meta cleanup

This commit is contained in:
Felix Cravic 2020-07-23 05:48:47 +02:00
parent f8453b4906
commit 01cb95c9b2
4 changed files with 91 additions and 38 deletions

View File

@ -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
* <p>
* 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
* <p>
* 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
* <p>
* Used by {@link ItemStack#clone()}
*
* @return the cloned item meta
*/
ItemMeta clone();
}

View File

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

View File

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

View File

@ -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<NBTCompound> 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<PotionType> 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