2020-07-06 23:43:56 +02:00
|
|
|
package net.minestom.server.utils;
|
|
|
|
|
|
|
|
import net.minestom.server.attribute.Attribute;
|
|
|
|
import net.minestom.server.attribute.AttributeOperation;
|
2020-07-06 23:55:43 +02:00
|
|
|
import net.minestom.server.chat.ChatParser;
|
2020-07-06 23:43:56 +02:00
|
|
|
import net.minestom.server.chat.ColoredText;
|
|
|
|
import net.minestom.server.inventory.Inventory;
|
|
|
|
import net.minestom.server.item.Enchantment;
|
|
|
|
import net.minestom.server.item.ItemStack;
|
|
|
|
import net.minestom.server.item.Material;
|
2020-07-09 15:51:39 +02:00
|
|
|
import net.minestom.server.item.NBTConsumer;
|
2020-07-06 23:43:56 +02:00
|
|
|
import net.minestom.server.item.attribute.AttributeSlot;
|
|
|
|
import net.minestom.server.item.attribute.ItemAttribute;
|
2020-07-23 05:36:15 +02:00
|
|
|
import net.minestom.server.item.metadata.ItemMeta;
|
2020-07-06 23:43:56 +02:00
|
|
|
import net.minestom.server.registry.Registries;
|
2020-08-19 20:34:21 +02:00
|
|
|
import net.minestom.server.utils.binary.BinaryReader;
|
|
|
|
import net.minestom.server.utils.binary.BinaryWriter;
|
2020-07-06 23:43:56 +02:00
|
|
|
import org.jglrxavpok.hephaistos.nbt.*;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2020-07-23 05:48:47 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
2020-07-06 23:43:56 +02:00
|
|
|
|
|
|
|
// for lack of a better name
|
2020-08-07 08:10:10 +02:00
|
|
|
public final class NBTUtils {
|
2020-07-06 23:43:56 +02:00
|
|
|
|
2020-07-13 14:36:39 +02:00
|
|
|
private final static Logger LOGGER = LoggerFactory.getLogger(NBTUtils.class);
|
2020-07-06 23:43:56 +02:00
|
|
|
|
2020-08-07 08:10:10 +02:00
|
|
|
private NBTUtils() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-06 23:43:56 +02:00
|
|
|
/**
|
|
|
|
* Loads all the items from the 'items' list into the given inventory
|
2020-07-09 15:51:39 +02:00
|
|
|
*
|
2020-08-07 09:14:50 +02:00
|
|
|
* @param items the items to save
|
|
|
|
* @param destination the inventory destination
|
2020-07-06 23:43:56 +02:00
|
|
|
*/
|
|
|
|
public static void loadAllItems(NBTList<NBTCompound> items, Inventory destination) {
|
|
|
|
destination.clear();
|
2020-07-09 15:51:39 +02:00
|
|
|
for (NBTCompound tag : items) {
|
2020-07-06 23:43:56 +02:00
|
|
|
Material item = Registries.getMaterial(tag.getString("id"));
|
2020-07-09 15:51:39 +02:00
|
|
|
if (item == Material.AIR) {
|
2020-07-06 23:43:56 +02:00
|
|
|
item = Material.STONE;
|
|
|
|
}
|
|
|
|
ItemStack stack = new ItemStack(item, tag.getByte("Count"));
|
2020-07-09 15:51:39 +02:00
|
|
|
if (tag.containsKey("tag")) {
|
2020-07-06 23:50:32 +02:00
|
|
|
loadDataIntoItem(stack, tag.getCompound("tag"));
|
|
|
|
}
|
2020-07-06 23:43:56 +02:00
|
|
|
destination.setItemStack(tag.getByte("Slot"), stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void saveAllItems(NBTList<NBTCompound> list, Inventory inventory) {
|
|
|
|
for (int i = 0; i < inventory.getSize(); i++) {
|
2020-07-23 07:36:49 +02:00
|
|
|
final ItemStack stack = inventory.getItemStack(i);
|
2020-07-06 23:43:56 +02:00
|
|
|
NBTCompound nbt = new NBTCompound();
|
|
|
|
|
2020-07-06 23:50:32 +02:00
|
|
|
NBTCompound tag = new NBTCompound();
|
|
|
|
saveDataIntoNBT(stack, tag);
|
|
|
|
|
|
|
|
nbt.set("tag", tag);
|
2020-07-06 23:43:56 +02:00
|
|
|
nbt.setByte("Slot", (byte) i);
|
|
|
|
nbt.setByte("Count", stack.getAmount());
|
|
|
|
nbt.setString("id", stack.getMaterial().getName());
|
|
|
|
|
|
|
|
list.add(nbt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 00:21:03 +02:00
|
|
|
public static void writeEnchant(NBTCompound nbt, String listName, Map<Enchantment, Short> enchantmentMap) {
|
2020-07-06 23:43:56 +02:00
|
|
|
NBTList<NBTCompound> enchantList = new NBTList<>(NBTTypes.TAG_Compound);
|
|
|
|
for (Map.Entry<Enchantment, Short> entry : enchantmentMap.entrySet()) {
|
2020-07-23 07:36:49 +02:00
|
|
|
final Enchantment enchantment = entry.getKey();
|
|
|
|
final short level = entry.getValue();
|
2020-07-06 23:43:56 +02:00
|
|
|
|
|
|
|
enchantList.add(new NBTCompound()
|
|
|
|
.setShort("lvl", level)
|
|
|
|
.setString("id", "minecraft:" + enchantment.name().toLowerCase())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
nbt.set(listName, enchantList);
|
|
|
|
}
|
|
|
|
|
2020-08-19 20:34:21 +02:00
|
|
|
public static ItemStack readItemStack(BinaryReader reader) {
|
2020-07-23 05:36:15 +02:00
|
|
|
final boolean present = reader.readBoolean();
|
2020-07-06 23:43:56 +02:00
|
|
|
|
|
|
|
if (!present) {
|
|
|
|
return ItemStack.getAirItem();
|
|
|
|
}
|
|
|
|
|
2020-07-23 05:36:15 +02:00
|
|
|
final int id = reader.readVarInt();
|
2020-07-06 23:43:56 +02:00
|
|
|
if (id == -1) {
|
|
|
|
// Drop mode
|
|
|
|
return ItemStack.getAirItem();
|
|
|
|
}
|
|
|
|
|
2020-07-23 05:36:15 +02:00
|
|
|
final Material material = Material.fromId((short) id);
|
|
|
|
final byte count = reader.readByte();
|
|
|
|
ItemStack item = new ItemStack(material, count);
|
2020-07-06 23:43:56 +02:00
|
|
|
|
|
|
|
try {
|
2020-07-23 05:36:15 +02:00
|
|
|
final NBT itemNBT = reader.readTag();
|
2020-07-09 15:51:39 +02:00
|
|
|
if (itemNBT instanceof NBTCompound) { // can also be a TAG_End if no data
|
2020-07-06 23:43:56 +02:00
|
|
|
NBTCompound nbt = (NBTCompound) itemNBT;
|
|
|
|
loadDataIntoItem(item, nbt);
|
|
|
|
}
|
|
|
|
} catch (IOException | NBTException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2020-07-13 14:36:39 +02:00
|
|
|
public static void loadDataIntoItem(ItemStack item, NBTCompound nbt) {
|
2020-07-09 15:51:39 +02:00
|
|
|
if (nbt.containsKey("Damage")) item.setDamage(nbt.getInt("Damage"));
|
|
|
|
if (nbt.containsKey("Unbreakable")) item.setUnbreakable(nbt.getInt("Unbreakable") == 1);
|
|
|
|
if (nbt.containsKey("HideFlags")) item.setHideFlag(nbt.getInt("HideFlags"));
|
|
|
|
if (nbt.containsKey("display")) {
|
2020-07-06 23:43:56 +02:00
|
|
|
NBTCompound display = nbt.getCompound("display");
|
2020-07-09 15:51:39 +02:00
|
|
|
if (display.containsKey("Name")) item.setDisplayName(ChatParser.toColoredText(display.getString("Name")));
|
|
|
|
if (display.containsKey("Lore")) {
|
2020-07-06 23:43:56 +02:00
|
|
|
NBTList<NBTString> loreList = display.getList("Lore");
|
|
|
|
ArrayList<ColoredText> lore = new ArrayList<>();
|
2020-07-09 15:51:39 +02:00
|
|
|
for (NBTString s : loreList) {
|
2020-07-06 23:55:43 +02:00
|
|
|
lore.add(ChatParser.toColoredText(s.getValue()));
|
2020-07-06 23:43:56 +02:00
|
|
|
}
|
|
|
|
item.setLore(lore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 15:51:39 +02:00
|
|
|
if (nbt.containsKey("Enchantments")) {
|
2020-07-06 23:43:56 +02:00
|
|
|
loadEnchantments(nbt.getList("Enchantments"), item::setEnchantment);
|
|
|
|
}
|
2020-08-01 00:21:03 +02:00
|
|
|
|
2020-07-09 15:51:39 +02:00
|
|
|
if (nbt.containsKey("AttributeModifiers")) {
|
2020-07-06 23:43:56 +02:00
|
|
|
NBTList<NBTCompound> attributes = nbt.getList("AttributeModifiers");
|
|
|
|
for (NBTCompound attributeNBT : attributes) {
|
2020-10-04 03:04:51 +02:00
|
|
|
final UUID uuid;
|
|
|
|
{
|
|
|
|
final int[] uuidArray = attributeNBT.getIntArray("UUID");
|
|
|
|
uuid = Utils.intArrayToUuid(uuidArray);
|
|
|
|
}
|
2020-08-01 11:00:31 +02:00
|
|
|
final double value = attributeNBT.getDouble("Amount");
|
|
|
|
final String slot = attributeNBT.getString("Slot");
|
|
|
|
final String attributeName = attributeNBT.getString("AttributeName");
|
|
|
|
final int operation = attributeNBT.getInt("Operation");
|
|
|
|
final String name = attributeNBT.getString("Name");
|
2020-07-06 23:43:56 +02:00
|
|
|
|
|
|
|
final Attribute attribute = Attribute.fromKey(attributeName);
|
|
|
|
// Wrong attribute name, stop here
|
|
|
|
if (attribute == null)
|
|
|
|
break;
|
2020-09-29 21:45:16 +02:00
|
|
|
final AttributeOperation attributeOperation = AttributeOperation.fromId(operation);
|
2020-07-06 23:43:56 +02:00
|
|
|
// Wrong attribute operation, stop here
|
2020-09-29 21:45:16 +02:00
|
|
|
if (attributeOperation == null) {
|
2020-07-06 23:43:56 +02:00
|
|
|
break;
|
2020-09-29 21:45:16 +02:00
|
|
|
}
|
2020-07-06 23:43:56 +02:00
|
|
|
final AttributeSlot attributeSlot = AttributeSlot.valueOf(slot.toUpperCase());
|
|
|
|
// Wrong attribute slot, stop here
|
2020-09-29 21:45:16 +02:00
|
|
|
if (attributeSlot == null) {
|
2020-07-06 23:43:56 +02:00
|
|
|
break;
|
2020-09-29 21:45:16 +02:00
|
|
|
}
|
2020-07-06 23:43:56 +02:00
|
|
|
|
|
|
|
// Add attribute
|
|
|
|
final ItemAttribute itemAttribute =
|
|
|
|
new ItemAttribute(uuid, name, attribute, attributeOperation, value, attributeSlot);
|
|
|
|
item.addAttribute(itemAttribute);
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 05:36:15 +02:00
|
|
|
|
|
|
|
// Meta specific field
|
|
|
|
final ItemMeta itemMeta = item.getItemMeta();
|
|
|
|
if (itemMeta == null)
|
|
|
|
return;
|
2020-07-23 05:48:47 +02:00
|
|
|
itemMeta.read(nbt);
|
2020-07-06 23:43:56 +02:00
|
|
|
}
|
|
|
|
|
2020-08-01 00:21:03 +02:00
|
|
|
public static void loadEnchantments(NBTList<NBTCompound> enchantments, EnchantmentSetter setter) {
|
2020-07-09 15:51:39 +02:00
|
|
|
for (NBTCompound enchantment : enchantments) {
|
2020-07-23 07:36:49 +02:00
|
|
|
final short level = enchantment.getShort("lvl");
|
|
|
|
final String id = enchantment.getString("id");
|
|
|
|
final Enchantment enchant = Registries.getEnchantment(id);
|
2020-07-09 15:51:39 +02:00
|
|
|
if (enchant != null) {
|
2020-07-06 23:43:56 +02:00
|
|
|
setter.applyEnchantment(enchant, level);
|
|
|
|
} else {
|
2020-07-09 15:51:39 +02:00
|
|
|
LOGGER.warn("Unknown enchantment type: " + id);
|
2020-07-06 23:43:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 20:34:21 +02:00
|
|
|
public static void writeItemStack(BinaryWriter packet, ItemStack itemStack) {
|
2020-07-06 23:43:56 +02:00
|
|
|
if (itemStack == null || itemStack.isAir()) {
|
|
|
|
packet.writeBoolean(false);
|
|
|
|
} else {
|
|
|
|
packet.writeBoolean(true);
|
2020-07-23 05:36:15 +02:00
|
|
|
packet.writeVarInt(itemStack.getMaterial().getId());
|
2020-07-06 23:43:56 +02:00
|
|
|
packet.writeByte(itemStack.getAmount());
|
|
|
|
|
|
|
|
if (!itemStack.hasNbtTag()) {
|
|
|
|
packet.writeByte((byte) NBTTypes.TAG_End); // No nbt
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NBTCompound itemNBT = new NBTCompound();
|
2020-07-09 15:51:39 +02:00
|
|
|
|
|
|
|
// Vanilla compound
|
2020-07-06 23:43:56 +02:00
|
|
|
saveDataIntoNBT(itemStack, itemNBT);
|
2020-07-09 15:51:39 +02:00
|
|
|
|
|
|
|
// Custom item nbt
|
2020-07-23 07:36:49 +02:00
|
|
|
final NBTConsumer nbtConsumer = itemStack.getNBTConsumer();
|
2020-07-09 15:51:39 +02:00
|
|
|
if (nbtConsumer != null) {
|
|
|
|
nbtConsumer.accept(itemNBT);
|
|
|
|
}
|
|
|
|
|
2020-07-06 23:43:56 +02:00
|
|
|
// End custom model data
|
|
|
|
packet.writeNBT("", itemNBT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 14:36:39 +02:00
|
|
|
public static void saveDataIntoNBT(ItemStack itemStack, NBTCompound itemNBT) {
|
2020-07-06 23:43:56 +02:00
|
|
|
// Unbreakable
|
|
|
|
if (itemStack.isUnbreakable()) {
|
|
|
|
itemNBT.setInt("Unbreakable", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start damage
|
|
|
|
{
|
2020-07-23 05:36:15 +02:00
|
|
|
final int damage = itemStack.getDamage();
|
|
|
|
if (damage > 0) {
|
|
|
|
itemNBT.setInt("Damage", damage);
|
|
|
|
}
|
2020-07-06 23:43:56 +02:00
|
|
|
}
|
|
|
|
// End damage
|
|
|
|
|
|
|
|
// Display
|
2020-07-23 05:36:15 +02:00
|
|
|
final boolean hasDisplayName = itemStack.hasDisplayName();
|
|
|
|
final boolean hasLore = itemStack.hasLore();
|
2020-07-06 23:43:56 +02:00
|
|
|
|
|
|
|
if (hasDisplayName || hasLore) {
|
|
|
|
NBTCompound displayNBT = new NBTCompound();
|
|
|
|
if (hasDisplayName) {
|
|
|
|
final String name = itemStack.getDisplayName().toString();
|
|
|
|
displayNBT.setString("Name", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasLore) {
|
|
|
|
final ArrayList<ColoredText> lore = itemStack.getLore();
|
|
|
|
|
|
|
|
final NBTList<NBTString> loreNBT = new NBTList<>(NBTTypes.TAG_String);
|
|
|
|
for (ColoredText line : lore) {
|
|
|
|
loreNBT.add(new NBTString(line.toString()));
|
|
|
|
}
|
|
|
|
displayNBT.set("Lore", loreNBT);
|
|
|
|
}
|
|
|
|
|
|
|
|
itemNBT.set("display", displayNBT);
|
|
|
|
}
|
|
|
|
// End display
|
|
|
|
|
|
|
|
// Start enchantment
|
|
|
|
{
|
2020-07-23 05:36:15 +02:00
|
|
|
final Map<Enchantment, Short> enchantmentMap = itemStack.getEnchantmentMap();
|
2020-07-06 23:43:56 +02:00
|
|
|
if (!enchantmentMap.isEmpty()) {
|
|
|
|
writeEnchant(itemNBT, "Enchantments", enchantmentMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// End enchantment
|
|
|
|
|
|
|
|
// Start attribute
|
|
|
|
{
|
2020-07-23 05:36:15 +02:00
|
|
|
final List<ItemAttribute> itemAttributes = itemStack.getAttributes();
|
2020-07-06 23:43:56 +02:00
|
|
|
if (!itemAttributes.isEmpty()) {
|
|
|
|
NBTList<NBTCompound> attributesNBT = new NBTList<>(NBTTypes.TAG_Compound);
|
|
|
|
|
|
|
|
for (ItemAttribute itemAttribute : itemAttributes) {
|
2020-07-23 05:36:15 +02:00
|
|
|
final UUID uuid = itemAttribute.getUuid();
|
2020-07-06 23:43:56 +02:00
|
|
|
attributesNBT.add(
|
|
|
|
new NBTCompound()
|
2020-10-04 03:04:51 +02:00
|
|
|
.setIntArray("UUID", Utils.uuidToIntArray(uuid))
|
2020-07-06 23:43:56 +02:00
|
|
|
.setDouble("Amount", itemAttribute.getValue())
|
|
|
|
.setString("Slot", itemAttribute.getSlot().name().toLowerCase())
|
2020-10-04 03:04:51 +02:00
|
|
|
.setString("AttributeName", itemAttribute.getAttribute().getKey())
|
2020-07-06 23:43:56 +02:00
|
|
|
.setInt("Operation", itemAttribute.getOperation().getId())
|
|
|
|
.setString("Name", itemAttribute.getInternalName())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
itemNBT.set("AttributeModifiers", attributesNBT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// End attribute
|
|
|
|
|
|
|
|
// Start hide flags
|
|
|
|
{
|
2020-07-23 05:36:15 +02:00
|
|
|
final int hideFlag = itemStack.getHideFlag();
|
2020-07-06 23:43:56 +02:00
|
|
|
if (hideFlag != 0) {
|
|
|
|
itemNBT.setInt("HideFlags", hideFlag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// End hide flags
|
|
|
|
|
|
|
|
// Start custom model data
|
|
|
|
{
|
2020-07-23 05:36:15 +02:00
|
|
|
final int customModelData = itemStack.getCustomModelData();
|
2020-07-06 23:43:56 +02:00
|
|
|
if (customModelData != 0) {
|
|
|
|
itemNBT.setInt("CustomModelData", customModelData);
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 05:48:47 +02:00
|
|
|
// End custom model data
|
2020-07-23 05:36:15 +02:00
|
|
|
|
|
|
|
// Start custom meta
|
|
|
|
final ItemMeta itemMeta = itemStack.getItemMeta();
|
|
|
|
if (itemMeta != null) {
|
2020-07-23 05:48:47 +02:00
|
|
|
itemMeta.write(itemNBT);
|
2020-07-23 05:36:15 +02:00
|
|
|
}
|
2020-07-23 05:48:47 +02:00
|
|
|
// End custom meta
|
2020-07-06 23:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@FunctionalInterface
|
2020-08-01 00:21:03 +02:00
|
|
|
public interface EnchantmentSetter {
|
2020-07-06 23:43:56 +02:00
|
|
|
void applyEnchantment(Enchantment name, short level);
|
|
|
|
}
|
|
|
|
}
|