Solved Adding multiple NBT Tags to item

Discussion in 'Plugin Development' started by plisov, Oct 20, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    plisov

    I'm trying to add multiple NBT Tags to an item however each time I try to add another tag to an item that already has an nbt tag, the old one gets replaced with the new one instead of getting added onto it. Here is what I have currently.
    Code:
    package me.plisov.opprison;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemHeldEvent;
    import org.bukkit.inventory.ItemStack;
    
    import me.plisov.opprison.labels.ItemLores;
    import net.md_5.bungee.api.ChatColor;
    import net.minecraft.server.v1_12_R1.NBTTagCompound;
    import net.minecraft.server.v1_12_R1.NBTTagDouble;
    import net.minecraft.server.v1_12_R1.NBTTagInt;
    import net.minecraft.server.v1_12_R1.NBTTagList;
    import net.minecraft.server.v1_12_R1.NBTTagString;
    
    public class NBTFactory implements Listener {
    
        @EventHandler
        public void lightWeight(PlayerItemHeldEvent event) {
    
            Bukkit.broadcastMessage("11111");
    
            if (event.getPlayer() instanceof Player) {
    
                Player player = (Player) event.getPlayer();
    
                int itemId = event.getNewSlot();
                ItemStack item = player.getInventory().getItem(itemId);
    
                if (item.getItemMeta().getLore().contains(ItemLores.lightweight1)
                        && !item.getType().equals(Material.BOOK)) {
    
                    net.minecraft.server.v1_12_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
                    NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound();
                    NBTTagList modifiers = new NBTTagList();
                    NBTTagCompound nCompound = new NBTTagCompound();
    
                    player.sendMessage("1");
                    nCompound.set("AttributeName", new NBTTagString("generic.movementSpeed"));
                    nCompound.set("Name", new NBTTagString("generic.movementSpeed"));
    
                    player.sendMessage(ChatColor.RED + "2");
                    nCompound.set("Amount", new NBTTagDouble(0.02));
    
                    nCompound.set("Operation", new NBTTagInt(0));
                    nCompound.set("UUIDLeast", new NBTTagInt(894654));
    
                    if (item.getType().name().endsWith("HELMET")) {
                        nCompound.set("Slot", new NBTTagString("head"));
                        nCompound.set("UUIDMost", new NBTTagInt(2872));
    
                    } else if (item.getType().name().endsWith("CHESTPLATE")) {
                        nCompound.set("Slot", new NBTTagString("chest"));
                        nCompound.set("UUIDMost", new NBTTagInt(2873));
    
                    } else if (item.getType().name().endsWith("LEGGINGS")) {
                        nCompound.set("Slot", new NBTTagString("legs"));
                        nCompound.set("UUIDMost", new NBTTagInt(2874));
    
                    } else if (item.getType().name().endsWith("BOOTS")) {
                        nCompound.set("Slot", new NBTTagString("feet"));
                        nCompound.set("UUIDMost", new NBTTagInt(2875));
    
                    }
    
                    modifiers.add(nCompound);
                    compound.set("AttributeModifiers", modifiers);
                    nmsStack.setTag(compound);
                    item = CraftItemStack.asBukkitCopy(nmsStack);
    
                    player.getInventory().setItem(itemId, item);
    
                }
    
                if (item.getItemMeta().getLore().contains(ItemLores.reinforced1) && !item.getType().equals(Material.BOOK)) {
    
                    net.minecraft.server.v1_12_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
                    NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound();
                    NBTTagList modifiers = new NBTTagList();
                    NBTTagCompound nCompound = new NBTTagCompound();
    
                    player.sendMessage("1");
    
                    nCompound.set("AttributeName", new NBTTagString("generic.maxHealth"));
                    nCompound.set("Name", new NBTTagString("generic.maxHealth"));
    
                    player.sendMessage(ChatColor.RED + "2");
    
                    nCompound.set("Amount", new NBTTagDouble(2));
    
                    nCompound.set("Operation", new NBTTagInt(0));
                    nCompound.set("UUIDLeast", new NBTTagInt(894654));
    
                    if (item.getType().name().endsWith("HELMET")) {
                        nCompound.set("Slot", new NBTTagString("head"));
                        nCompound.set("UUIDMost", new NBTTagInt(3872));
    
                    } else if (item.getType().name().endsWith("CHESTPLATE")) {
                        nCompound.set("Slot", new NBTTagString("chest"));
                        nCompound.set("UUIDMost", new NBTTagInt(3873));
    
                    } else if (item.getType().name().endsWith("LEGGINGS")) {
                        nCompound.set("Slot", new NBTTagString("legs"));
                        nCompound.set("UUIDMost", new NBTTagInt(3874));
    
                    } else if (item.getType().name().endsWith("BOOTS")) {
                        nCompound.set("Slot", new NBTTagString("feet"));
                        nCompound.set("UUIDMost", new NBTTagInt(3875));
                    }
    
                    player.sendMessage("3");
    
                    modifiers.add(nCompound);
                    compound.set("AttributeModifiers", modifiers);
                    nmsStack.setTag(compound);
                    item = CraftItemStack.asBukkitCopy(nmsStack);
    
                    player.getInventory().setItem(itemId, item);
                }
            }
        }
    }
    
    Anyone have any idea what I'm doing wrong?
     
  2. Offline

    Zombie_Striker

    @plisov
    Are they specific modifiers that get overriden, or is it just any modifiers? Can you loop through all the modifiers for the nCompound tag? Does it show all of the values that you set?
     
  3. Offline

    plisov

    For example, when I add speed to the item, it adds speed but when I added extra hearts to the same item, the more hearts overrides the speed instead of adding it onto the nbt tags that are already there. As if it just clears the ones already on there and creates a new set.
     
  4. Offline

    Zombie_Striker

    @plisov
    Does bukkit say that the other values are overriding the existing values? Can you print out all of the values for the tag after you set it?
     
  5. Offline

    plisov

    I printed out the tags on the item. This is what it shows
    https://gyazo.com/eb1a45f32a120d6f2b182985edb727da
    Code:
    package me.plisov.opprison;
    
    import java.lang.reflect.Method;
    
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemHeldEvent;
    import org.bukkit.inventory.ItemStack;
    
    import me.plisov.opprison.api.CraftiReflection;
    import me.plisov.opprison.labels.ItemLores;
    import net.md_5.bungee.api.ChatColor;
    import net.minecraft.server.v1_12_R1.NBTTagCompound;
    import net.minecraft.server.v1_12_R1.NBTTagDouble;
    import net.minecraft.server.v1_12_R1.NBTTagInt;
    import net.minecraft.server.v1_12_R1.NBTTagList;
    import net.minecraft.server.v1_12_R1.NBTTagString;
    
    public class NBTFactory implements Listener {
    
        @EventHandler
        public void itemHeld(PlayerItemHeldEvent event) {
    
            if (event.getPlayer() instanceof Player) {
    
                Player player = (Player) event.getPlayer();
    
                int itemId = event.getNewSlot();
                ItemStack item = player.getInventory().getItem(itemId);
    
                net.minecraft.server.v1_12_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
                NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound();
                NBTTagList modifiers = new NBTTagList();
                NBTTagCompound nCompound = new NBTTagCompound();
    
                if (item != null) {
                    if (item.hasItemMeta()) {
    
                        lightweight(player, item, nCompound, compound, modifiers, nmsStack, itemId);
    
                        reinforced(player, item, nCompound, compound, modifiers, nmsStack, itemId);
                    }
                }
            }
        }
    
        private void lightweight(Player player, ItemStack item, NBTTagCompound nCompound, NBTTagCompound compound,
                NBTTagList modifiers, net.minecraft.server.v1_12_R1.ItemStack nmsStack, int itemId) {
            if (item.getItemMeta().getLore().contains(ItemLores.lightweight1) && !item.getType().equals(Material.BOOK)) {
    
                player.sendMessage("1");
                nCompound.set("AttributeName", new NBTTagString("generic.movementSpeed"));
                nCompound.set("Name", new NBTTagString("generic.movementSpeed"));
    
                player.sendMessage(ChatColor.RED + "2");
                nCompound.set("Amount", new NBTTagDouble(0.02));
    
                nCompound.set("Operation", new NBTTagInt(0));
                nCompound.set("UUIDLeast", new NBTTagInt(894654));
    
                if (item.getType().name().endsWith("HELMET")) {
                    nCompound.set("Slot", new NBTTagString("head"));
                    nCompound.set("UUIDMost", new NBTTagInt(2872));
    
                } else if (item.getType().name().endsWith("CHESTPLATE")) {
                    nCompound.set("Slot", new NBTTagString("chest"));
                    nCompound.set("UUIDMost", new NBTTagInt(2873));
    
                } else if (item.getType().name().endsWith("LEGGINGS")) {
                    nCompound.set("Slot", new NBTTagString("legs"));
                    nCompound.set("UUIDMost", new NBTTagInt(2874));
    
                } else if (item.getType().name().endsWith("BOOTS")) {
                    nCompound.set("Slot", new NBTTagString("feet"));
                    nCompound.set("UUIDMost", new NBTTagInt(2875));
    
                }
    
                modifiers.add(nCompound);
                compound.set("AttributeModifiers", modifiers);
                nmsStack.setTag(compound);
                item = CraftItemStack.asBukkitCopy(nmsStack);
    
                player.getInventory().setItem(itemId, item);
             
                player.sendMessage(nmsStack.getTag() + "");
            }
        }
    
        private void reinforced(Player player, ItemStack item, NBTTagCompound nCompound, NBTTagCompound compound,
                NBTTagList modifiers, net.minecraft.server.v1_12_R1.ItemStack nmsStack, int itemId) {
            if (item.getItemMeta().getLore().contains(ItemLores.reinforced1) && !item.getType().equals(Material.BOOK)) {
    
                player.sendMessage("1");
    
                nCompound.set("AttributeName", new NBTTagString("generic.maxHealth"));
                nCompound.set("Name", new NBTTagString("generic.maxHealth"));
    
                player.sendMessage(ChatColor.RED + "2");
    
                nCompound.set("Amount", new NBTTagDouble(2));
    
                nCompound.set("Operation", new NBTTagInt(0));
                nCompound.set("UUIDLeast", new NBTTagInt(894654));
    
                if (item.getType().name().endsWith("HELMET")) {
                    nCompound.set("Slot", new NBTTagString("head"));
                    nCompound.set("UUIDMost", new NBTTagInt(3872));
    
                } else if (item.getType().name().endsWith("CHESTPLATE")) {
                    nCompound.set("Slot", new NBTTagString("chest"));
                    nCompound.set("UUIDMost", new NBTTagInt(3873));
    
                } else if (item.getType().name().endsWith("LEGGINGS")) {
                    nCompound.set("Slot", new NBTTagString("legs"));
                    nCompound.set("UUIDMost", new NBTTagInt(3874));
    
                } else if (item.getType().name().endsWith("BOOTS")) {
                    nCompound.set("Slot", new NBTTagString("feet"));
                    nCompound.set("UUIDMost", new NBTTagInt(3875));
                }
    
                player.sendMessage("3");
    
                modifiers.add(nCompound);
                compound.set("AttributeModifiers", modifiers);
                nmsStack.setTag(compound);
                item = CraftItemStack.asBukkitCopy(nmsStack);
    
                player.getInventory().setItem(itemId, item);
    
            } else if (item.getItemMeta().getLore().contains(ItemLores.reinforced2)
                    && !item.getType().equals(Material.BOOK)) {
    
                player.sendMessage("1");
    
                nCompound.set("AttributeName", new NBTTagString("generic.maxHealth"));
                nCompound.set("Name", new NBTTagString("generic.maxHealth"));
    
                player.sendMessage(ChatColor.RED + "2");
    
                nCompound.set("Amount", new NBTTagDouble(4));
    
                nCompound.set("Operation", new NBTTagInt(0));
                nCompound.set("UUIDLeast", new NBTTagInt(894654));
    
                if (item.getType().name().endsWith("HELMET")) {
                    nCompound.set("Slot", new NBTTagString("head"));
                    nCompound.set("Slot", new NBTTagString("offhand"));
                    nCompound.set("UUIDMost", new NBTTagInt(3872));
    
                } else if (item.getType().name().endsWith("CHESTPLATE")) {
                    nCompound.set("Slot", new NBTTagString("chest"));
                    nCompound.set("UUIDMost", new NBTTagInt(3873));
    
                } else if (item.getType().name().endsWith("LEGGINGS")) {
                    nCompound.set("Slot", new NBTTagString("legs"));
                    nCompound.set("UUIDMost", new NBTTagInt(3874));
    
                } else if (item.getType().name().endsWith("BOOTS")) {
                    nCompound.set("Slot", new NBTTagString("feet"));
                    nCompound.set("UUIDMost", new NBTTagInt(3875));
                }
    
                modifiers.add(nCompound);
                compound.set("AttributeModifiers", modifiers);
                nmsStack.setTag(compound);
                item = CraftItemStack.asBukkitCopy(nmsStack);
    
                player.getInventory().setItem(itemId, item);
    
            } else if (item.getItemMeta().getLore().contains(ItemLores.reinforced3)
                    && !item.getType().equals(Material.BOOK)) {
    
                player.sendMessage("1");
    
                nCompound.set("AttributeName", new NBTTagString("generic.maxHealth"));
                nCompound.set("Name", new NBTTagString("generic.maxHealth"));
    
                player.sendMessage(ChatColor.RED + "2");
    
                nCompound.set("Amount", new NBTTagDouble(6));
    
                nCompound.set("Operation", new NBTTagInt(0));
                nCompound.set("UUIDLeast", new NBTTagInt(894654));
    
                if (item.getType().name().endsWith("HELMET")) {
                    nCompound.set("Slot", new NBTTagString("head"));
                    nCompound.set("UUIDMost", new NBTTagInt(3872));
    
                } else if (item.getType().name().endsWith("CHESTPLATE")) {
                    nCompound.set("Slot", new NBTTagString("chest"));
                    nCompound.set("UUIDMost", new NBTTagInt(3873));
    
                } else if (item.getType().name().endsWith("LEGGINGS")) {
                    nCompound.set("Slot", new NBTTagString("legs"));
                    nCompound.set("UUIDMost", new NBTTagInt(3874));
    
                } else if (item.getType().name().endsWith("BOOTS")) {
                    nCompound.set("Slot", new NBTTagString("feet"));
                    nCompound.set("UUIDMost", new NBTTagInt(3875));
                }
    
                player.sendMessage("3");
    
                modifiers.add(nCompound);
                compound.set("AttributeModifiers", modifiers);
                nmsStack.setTag(compound);
                item = CraftItemStack.asBukkitCopy(nmsStack);
    
                player.getInventory().setItem(itemId, item);
    
            } else if (item.getItemMeta().getLore().contains(ItemLores.reinforced4)
                    && !item.getType().equals(Material.BOOK)) {
    
                player.sendMessage("1");
    
                nCompound.set("AttributeName", new NBTTagString("generic.maxHealth"));
                nCompound.set("Name", new NBTTagString("generic.maxHealth"));
    
                player.sendMessage(ChatColor.RED + "2");
    
                nCompound.set("Amount", new NBTTagDouble(8));
    
                nCompound.set("Operation", new NBTTagInt(0));
                nCompound.set("UUIDLeast", new NBTTagInt(894654));
    
                if (item.getType().name().endsWith("HELMET")) {
                    nCompound.set("Slot", new NBTTagString("head"));
                    nCompound.set("UUIDMost", new NBTTagInt(3872));
    
                } else if (item.getType().name().endsWith("CHESTPLATE")) {
                    nCompound.set("Slot", new NBTTagString("chest"));
                    nCompound.set("UUIDMost", new NBTTagInt(3873));
    
                } else if (item.getType().name().endsWith("LEGGINGS")) {
                    nCompound.set("Slot", new NBTTagString("legs"));
                    nCompound.set("UUIDMost", new NBTTagInt(3874));
    
                } else if (item.getType().name().endsWith("BOOTS")) {
                    nCompound.set("Slot", new NBTTagString("feet"));
                    nCompound.set("UUIDMost", new NBTTagInt(3875));
                }
    
                player.sendMessage("3");
    
             
                modifiers.add(nCompound);
                compound.set("AttributeModifiers", modifiers);
                nmsStack.setTag(compound);
                item = CraftItemStack.asBukkitCopy(nmsStack);
    
                player.getInventory().setItem(itemId, item);
             
                player.sendMessage(nmsStack.getTag() + "");
    
            }
        }
    
        private static ItemStack addAttribute(ItemStack item, AttributeSlot slot, Integer valueInt, Double valueDouble,
                Float valueFloat, String attribute) {
    
            try {
    
                // Class<?> nbsStack = CraftiReflection.getNMSClass("ItemStack");
                Class<?> nmsItemStack = CraftiReflection.getNMSClassCraftbukkit("inventory.CraftItemStack");
                Method asNMS = nmsItemStack.getDeclaredMethod("asNMSCopy", item.getClass());
                Object nbsnmsStack = asNMS.invoke(nmsItemStack, item);
    
                Method hasTagMethod = nbsnmsStack.getClass().getMethod("hasTag");
                boolean hasTag = (boolean) hasTagMethod.invoke(nbsnmsStack);
    
                Class<?> nbtCompound = CraftiReflection.getNMSClass("NBTTagCompound");
                Object objectCompound;
    
                if (hasTag) {
    
                    Method getTagMethod = nbsnmsStack.getClass().getMethod("getTag");
                    objectCompound = getTagMethod.invoke(nbsnmsStack);
    
                } else
                    objectCompound = nbtCompound.newInstance();
    
                Class<?> nbtModifiers = CraftiReflection.getNMSClass("NBTTagList");
                Object objectModifiers = nbtModifiers.newInstance();
    
                Object objectDamageCompound = nbtCompound.newInstance();
    
                {
    
                    Method setAt = nbtCompound.getDeclaredMethod("set", String.class,
                            CraftiReflection.getNMSClass("NBTBase"));
    
                    Class<?> nbtString = CraftiReflection.getNMSClass("NBTTagString");
                    Class<?> nbtInt = CraftiReflection.getNMSClass("NBTTagInt");
                    Class<?> nbtFloat = CraftiReflection.getNMSClass("NBTTagFloat");
                    Class<?> nbtDouble = CraftiReflection.getNMSClass("NBTTagDouble");
    
                    Object objectAtName = nbtString.getConstructor(String.class).newInstance(attribute);
                    Object objectName = nbtString.getConstructor(String.class).newInstance(attribute);
    
                    Object objectAmount;
                    if (valueInt != null)
                        objectAmount = nbtInt.getConstructor(int.class).newInstance(valueInt.intValue());
                    else if (valueDouble != null)
                        objectAmount = nbtDouble.getConstructor(double.class).newInstance(valueDouble.doubleValue());
                    else
                        objectAmount = nbtFloat.getConstructor(float.class).newInstance(valueFloat.floatValue());
    
                    Object objectOperation = nbtInt.getConstructor(int.class).newInstance(0);
                    Object objectIDLeast = nbtInt.getConstructor(int.class).newInstance(894654);
                    Object objectIDMost = nbtInt.getConstructor(int.class).newInstance(2872);
                    Object objectSlot = nbtString.getConstructor(String.class).newInstance(slot.getNMS());
    
                    setAt.invoke(objectDamageCompound, "AttributeName", objectAtName);
                    setAt.invoke(objectDamageCompound, "Name", objectName);
                    setAt.invoke(objectDamageCompound, "Amount", objectAmount);
                    setAt.invoke(objectDamageCompound, "Operation", objectOperation);
                    setAt.invoke(objectDamageCompound, "UUIDLeast", objectIDLeast);
                    setAt.invoke(objectDamageCompound, "UUIDMost", objectIDMost);
    
                    setAt.invoke(objectDamageCompound, "Slot", objectSlot);
    
                }
    
                Method add = nbtModifiers.getMethod("add", CraftiReflection.getNMSClass("NBTBase"));
                add.invoke(objectModifiers, objectDamageCompound);
    
                Method setAt = nbtCompound.getDeclaredMethod("set", String.class, CraftiReflection.getNMSClass("NBTBase"));
                setAt.invoke(objectCompound, "AttributeModifiers", objectModifiers);
    
                Method setTag = nbsnmsStack.getClass().getDeclaredMethod("setTag",
                        CraftiReflection.getNMSClass("NBTTagCompound"));
                setTag.invoke(nbsnmsStack, objectCompound);
    
                Method asBukkit = nmsItemStack.getDeclaredMethod("asBukkitCopy", CraftiReflection.getNMSClass("ItemStack"));
                item = (ItemStack) asBukkit.invoke(nmsItemStack, nbsnmsStack);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return item;
    
        }
     
        public enum AttributeSlot {
           
              HAND_IN("mainhand"),
              HAND_OFF("offhand"),
              FEET("feet"),
              LEGS("legs"),
              CHEST("chest"),
              HEAD("head");
           
              private final String NMS;
              AttributeSlot(String nms) { NMS = nms; }
           
              public String getNMS() { return NMS; }
           
             }
    }
    
    It said it adds movement speed however on the item itself it shows that it adds 2 extra hearts.

    It should be showing on the item that its adding 2 hearts and the speed. However it shows that it adds the hearts on the item and when I print out the tags it shows the speed. When I try the item, only the extra hearts work.
     
Thread Status:
Not open for further replies.

Share This Page