Search in sources :

Example 16 with NBTTagList

use of net.minecraft.server.v1_16_R3.NBTTagList in project WildChests by BG-Software-LLC.

the class NMSAdapter_v1_16_R3 method deserialize.

private static InventoryHolder deserialize(NBTTagCompound tagCompound) {
    InventoryHolder inventory = new InventoryHolder(tagCompound.getInt("Size"), "Chest");
    NBTTagList itemsList = tagCompound.getList("Items", 10);
    for (int i = 0; i < itemsList.size(); i++) {
        NBTTagCompound nbtTagCompound = itemsList.getCompound(i);
        inventory.setItem(nbtTagCompound.getByte("Slot"), CraftItemStack.asBukkitCopy(ItemStack.a(nbtTagCompound)));
    }
    return inventory;
}
Also used : NBTTagList(net.minecraft.server.v1_16_R3.NBTTagList) NBTTagCompound(net.minecraft.server.v1_16_R3.NBTTagCompound) InventoryHolder(com.bgsoftware.wildchests.objects.inventory.InventoryHolder)

Example 17 with NBTTagList

use of net.minecraft.server.v1_16_R3.NBTTagList in project WildChests by BG-Software-LLC.

the class NMSAdapter_v1_8_R3 method deserialize.

private InventoryHolder deserialize(NBTTagCompound tagCompound) {
    InventoryHolder inventory = new InventoryHolder(tagCompound.getInt("Size"), "Chest");
    NBTTagList itemsList = tagCompound.getList("Items", 10);
    for (int i = 0; i < itemsList.size(); i++) {
        NBTTagCompound nbtTagCompound = itemsList.get(i);
        inventory.setItem(nbtTagCompound.getByte("Slot"), CraftItemStack.asBukkitCopy(ItemStack.createStack(nbtTagCompound)));
    }
    return inventory;
}
Also used : NBTTagList(net.minecraft.server.v1_8_R3.NBTTagList) NBTTagCompound(net.minecraft.server.v1_8_R3.NBTTagCompound) InventoryHolder(com.bgsoftware.wildchests.objects.inventory.InventoryHolder)

Example 18 with NBTTagList

use of net.minecraft.server.v1_16_R3.NBTTagList in project WildChests by BG-Software-LLC.

the class NMSAdapter_v1_12_R1 method serialize.

private void serialize(Inventory inventory, NBTTagCompound tagCompound) {
    NBTTagList itemsList = new NBTTagList();
    org.bukkit.inventory.ItemStack[] items = inventory.getContents();
    for (int i = 0; i < items.length; ++i) {
        if (items[i] != null) {
            NBTTagCompound nbtTagCompound = new NBTTagCompound();
            nbtTagCompound.setByte("Slot", (byte) i);
            CraftItemStack.asNMSCopy(items[i]).save(nbtTagCompound);
            itemsList.add(nbtTagCompound);
        }
    }
    tagCompound.setInt("Size", inventory.getSize());
    tagCompound.set("Items", itemsList);
}
Also used : NBTTagList(net.minecraft.server.v1_12_R1.NBTTagList) NBTTagCompound(net.minecraft.server.v1_12_R1.NBTTagCompound) CraftItemStack(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack) ItemStack(net.minecraft.server.v1_12_R1.ItemStack)

Example 19 with NBTTagList

use of net.minecraft.server.v1_16_R3.NBTTagList in project MechanicsMain by WeaponMechanics.

the class NBT_1_10_R1 method setAttribute.

@Nonnull
@Override
public void setAttribute(@Nonnull ItemStack bukkitItem, @Nonnull AttributeType attribute, @Nullable AttributeSlot slot, double value) {
    net.minecraft.server.v1_10_R1.ItemStack nmsItem = getNMSStack(bukkitItem);
    if (nmsItem.getTag() == null) {
        nmsItem.setTag(new NBTTagCompound());
    }
    NBTTagCompound compound = nmsItem.getTag();
    if (compound.hasKey("AttributeModifiers")) {
        NBTTagList list = (NBTTagList) compound.get("AttributeModifiers");
        // NBT lists don't have an indexOf method, so we need to loop
        // through each attribute, and determine if it is one we want to
        // modify. We want to modify an attribute if the attribute was
        // set using MechanicsCore, and it's attribute type matches the
        // parameter attribute type.
        boolean isModifiedAttribute = false;
        for (int i = 0; i < list.size(); i++) {
            // 10 is the id for nbt lists
            if (list.get(i).getTypeId() != 10) {
                continue;
            }
            NBTTagCompound nbt = list.get(i);
            String name = nbt.getString("Name");
            String attributeName = nbt.getString("AttributeName");
            // There is no offhand, or slot argument in 1_8_8.
            if (!"MechanicsCoreAttribute".equals(name) || !attribute.getMinecraftName().equals(attributeName)) {
                continue;
            }
            // Since this attribute already exists, we only need to modify
            // the existing value. No need to set the name/uuid
            nbt.setDouble("Amount", value);
            isModifiedAttribute = true;
            break;
        }
        if (!isModifiedAttribute) {
            NBTTagCompound nbt = new NBTTagCompound();
            nbt.setString("AttributeName", attribute.getMinecraftName());
            nbt.setString("Name", "MechanicsCoreAttribute");
            nbt.setDouble("Amount", value);
            // 0 == add
            nbt.setInt("Operation", 0);
            nbt.setLong("UUIDLeast", attribute.getUUID().getLeastSignificantBits());
            nbt.setLong("UUIDMost", attribute.getUUID().getMostSignificantBits());
            if (slot != null) {
                nbt.setString("Slot", slot.getSlotName());
            }
        }
    }
    bukkitItem.setItemMeta(CraftItemStack.getItemMeta(nmsItem));
}
Also used : NBTTagList(net.minecraft.server.v1_10_R1.NBTTagList) NBTTagCompound(net.minecraft.server.v1_10_R1.NBTTagCompound) Nonnull(javax.annotation.Nonnull)

Example 20 with NBTTagList

use of net.minecraft.server.v1_16_R3.NBTTagList in project MechanicsMain by WeaponMechanics.

the class NBT_1_12_R1 method setAttribute.

@Nonnull
@Override
public void setAttribute(@Nonnull ItemStack bukkitItem, @Nonnull AttributeType attribute, @Nullable AttributeSlot slot, double value) {
    net.minecraft.server.v1_12_R1.ItemStack nmsItem = getNMSStack(bukkitItem);
    if (nmsItem.getTag() == null) {
        nmsItem.setTag(new NBTTagCompound());
    }
    NBTTagCompound compound = nmsItem.getTag();
    if (compound.hasKey("AttributeModifiers")) {
        NBTTagList list = (NBTTagList) compound.get("AttributeModifiers");
        // NBT lists don't have an indexOf method, so we need to loop
        // through each attribute, and determine if it is one we want to
        // modify. We want to modify an attribute if the attribute was
        // set using MechanicsCore, and it's attribute type matches the
        // parameter attribute type.
        boolean isModifiedAttribute = false;
        for (int i = 0; i < list.size(); i++) {
            // 10 is the id for nbt lists
            if (list.get(i).getTypeId() != 10) {
                continue;
            }
            NBTTagCompound nbt = list.get(i);
            String name = nbt.getString("Name");
            String attributeName = nbt.getString("AttributeName");
            // There is no offhand, or slot argument in 1_8_8.
            if (!"MechanicsCoreAttribute".equals(name) || !attribute.getMinecraftName().equals(attributeName)) {
                continue;
            }
            // Since this attribute already exists, we only need to modify
            // the existing value. No need to set the name/uuid
            nbt.setDouble("Amount", value);
            isModifiedAttribute = true;
            break;
        }
        if (!isModifiedAttribute) {
            NBTTagCompound nbt = new NBTTagCompound();
            nbt.setString("AttributeName", attribute.getMinecraftName());
            nbt.setString("Name", "MechanicsCoreAttribute");
            nbt.setDouble("Amount", value);
            // 0 == add
            nbt.setInt("Operation", 0);
            nbt.setLong("UUIDLeast", attribute.getUUID().getLeastSignificantBits());
            nbt.setLong("UUIDMost", attribute.getUUID().getMostSignificantBits());
            if (slot != null) {
                nbt.setString("Slot", slot.getSlotName());
            }
        }
    }
    bukkitItem.setItemMeta(CraftItemStack.getItemMeta(nmsItem));
}
Also used : NBTTagList(net.minecraft.server.v1_12_R1.NBTTagList) NBTTagCompound(net.minecraft.server.v1_12_R1.NBTTagCompound) Nonnull(javax.annotation.Nonnull)

Aggregations

ItemStack (org.bukkit.inventory.ItemStack)24 ArrayList (java.util.ArrayList)22 ItemMeta (org.bukkit.inventory.meta.ItemMeta)22 CompoundTag (com.wasteofplastic.org.jnbt.CompoundTag)20 ListTag (com.wasteofplastic.org.jnbt.ListTag)20 StringTag (com.wasteofplastic.org.jnbt.StringTag)20 Tag (com.wasteofplastic.org.jnbt.Tag)20 Map (java.util.Map)20 NBTTagCompound (net.minecraft.server.v1_12_R1.NBTTagCompound)17 NBTTagList (net.minecraft.server.v1_12_R1.NBTTagList)17 TagCompound (de.keyle.knbt.TagCompound)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 CraftItemStack (org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack)8 NBTTagString (net.minecraft.server.v1_12_R1.NBTTagString)6 NBTTagCompound (net.minecraft.server.v1_8_R3.NBTTagCompound)6 NBTTagList (net.minecraft.server.v1_8_R3.NBTTagList)6 NBTTagCompound (net.minecraft.server.v1_16_R3.NBTTagCompound)5 NBTTagList (net.minecraft.server.v1_16_R3.NBTTagList)5 NBTTagCompound (net.minecraft.server.v1_9_R2.NBTTagCompound)5 NBTTagList (net.minecraft.server.v1_9_R2.NBTTagList)5