Solved 1.12.2 to 1.13.1 NBT Tag Trouble

Discussion in 'Plugin Development' started by awesomebutter234, Nov 15, 2018.

Thread Status:
Not open for further replies.
  1. In the 1.12.2 version of my plugin I used
    Code:
    ItemStack p = new ItemStack(m);
    
        net.minecraft.server.v1_12_R1.ItemStack nmscopy = CraftItemStack.asNMSCopy(p);
        NBTTagCompound compound = (nmscopy.hasTag()) ? nmscopy.getTag() : new NBTTagCompound();
        NBTTagList modifiers = new NBTTagList();
    
        if(a != null)
        {
        NBTTagCompound[] ip = new NBTTagCompound[a.length];
        for(int start = 0;start<ip.length;start++)
        {
            ip[start] = new NBTTagCompound();
        }
    
    
        for(int i=0;i<a.length;i++)
        {
            String attributename = StringUtils.substringBetween(a[i],"(",")");
            int operation = Integer.valueOf(StringUtils.substringBetween(a[i],"<",">"));
            double amount = Double.valueOf(StringUtils.substringBetween(a[i],"[","]"));
            amount = Math.round(amount*1000);
            amount = amount/1000;
        
            if(attributename.equals("attackSpeed") && operation == 0 && a[i].contains("display"))
            {
                //if you put in 1.6 attack speed, make it into 1.6 attack speed
                double tas = 4-amount;
                amount = tas*-1;
                lorespeed += amount;
            }
        
            if(attributename.equals("attackDamage") && operation == 0 )
            {
                if(a[i].contains("show") || a[i].contains("display"))
                {
                loredamage += amount;
                }
                else
                {}
            }
        
            String slot = StringUtils.substringBetween(a[i],"{","}");
        
        
            ip[i].set("AttributeName", new NBTTagString("generic." + attributename));
            ip[i].set("Name", new NBTTagString("generic." + attributename));
            ip[i].set("Amount", new NBTTagDouble(amount));
            ip[i].set("Operation", new NBTTagInt(operation));
            ip[i].set("UUIDLeast", new NBTTagInt(ThreadLocalRandom.current().nextInt(1,10000)));
            ip[i].set("UUIDMost", new NBTTagInt(ThreadLocalRandom.current().nextInt(1,10000)));
            ip[i].set("Slot", new NBTTagString(slot));
            ip[i].set("Identifier", new NBTTagInt(ThreadLocalRandom.current().nextInt(1,100000)));
            if(a[i].toLowerCase().contains("show") || a[i].toLowerCase().contains("display"))
            {
                if(a[i].toLowerCase().contains("display"))
                {
                    ip[i].set("DisplayLevel", new NBTTagString("Display"));
                }
                else
                {
                    ip[i].set("DisplayLevel", new NBTTagString("Show"));
                }
            }
            else
            {
                //Add in a Unknown feature, which allows it to hide the Slot and/or the attribute and/or the slot
                ip[i].set("DisplayLevel", new NBTTagString("Hidden"));
            }
        
        
            modifiers.add(ip[i]);
            compound.set("AttributeModifiers",modifiers);
    .........
    nmscopy.setTag(compound);
    
    
        p = CraftItemStack.asBukkitCopy(nmscopy);

    and in the 1.13.1 version I used
    Code:
    ItemStack p = new ItemStack(m);
    
    
        net.minecraft.server.v1_13_R2.ItemStack nmscopy = CraftItemStack.asNMSCopy(p);
        NBTTagCompound compound = (nmscopy.hasTag()) ? nmscopy.getTag() : new NBTTagCompound();
        NBTTagList modifiers = new NBTTagList();
    
        if(a != null)
        {
        NBTTagCompound[] ip = new NBTTagCompound[a.length];
        for(int start = 0;start<ip.length;start++)
        {
            ip[start] = new NBTTagCompound();
        }
    
    
        for(int i=0;i<a.length;i++)
        {
            String attributename = StringUtils.substringBetween(a[i],"(",")");
            int operation = Integer.valueOf(StringUtils.substringBetween(a[i],"<",">"));
            double amount = Double.valueOf(StringUtils.substringBetween(a[i],"[","]"));
            amount = Math.round(amount*1000);
            amount = amount/1000;
        
            if(attributename.equals("attackSpeed") && operation == 0 && a[i].contains("display"))
            {
                //if you put in 1.6 attack speed, make it into 1.6 attack speed
                double tas = 4-amount;
                amount = tas*-1;
                lorespeed += amount;
            }
        
            if(attributename.equals("attackDamage") && operation == 0 )
            {
                if(a[i].contains("show") || a[i].contains("display"))
                {
                loredamage += amount;
                }
                else
                {}
            }
        
            String slot = StringUtils.substringBetween(a[i],"{","}");
        
        
            ip[i].set("AttributeName", new NBTTagString("generic." + attributename));
            ip[i].set("Name", new NBTTagString("generic." + attributename));
            ip[i].set("Amount", new NBTTagDouble(amount));
            ip[i].set("Operation", new NBTTagInt(operation));
            ip[i].set("UUIDLeast", new NBTTagInt(ThreadLocalRandom.current().nextInt(1,10000)));
            ip[i].set("UUIDMost", new NBTTagInt(ThreadLocalRandom.current().nextInt(1,10000)));
            ip[i].set("Slot", new NBTTagString(slot));
            ip[i].set("Identifier", new NBTTagInt(ThreadLocalRandom.current().nextInt(1,100000)));
            if(a[i].toLowerCase().contains("show") || a[i].toLowerCase().contains("display"))
            {
                if(a[i].toLowerCase().contains("display"))
                {
                    ip[i].set("DisplayLevel", new NBTTagString("Display"));
                }
                else
                {
                    ip[i].set("DisplayLevel", new NBTTagString("Show"));
                }
            }
            else
            {
                //Add in a Unknown feature, which allows it to hide the Slot and/or the attribute and/or the slot
                ip[i].set("DisplayLevel", new NBTTagString("Hidden"));
    .......
            
    //nmscopy.save(compound);
        nmscopy.setTag(compound);
        //nmscopy.getDamage();
       // Bukkit.getLogger().info(ChatColor.RED + nmscopy.toString());
        p = CraftItemStack.asBukkitCopy(nmscopy);
    
        //Turning the item into a bukkit copy removes the display and identifier
        net.minecraft.server.v1_13_R2.ItemStack thing = CraftItemStack.asNMSCopy(p);
        NBTTagList testing = (NBTTagList) thing.getOrCreateTag().get("AttributeModifiers");
        Bukkit.getLogger().info("Please God: "+testing.get(0));
    
        NBTTagList testing2 = (NBTTagList) thing.getTag().get("AttributeModifiers");
        Bukkit.getLogger().info("Please God2: "+testing2.get(0));
            }
        
        
            modifiers.add(ip[i]);
            compound.set("AttributeModifiers",(NBTTagList)modifiers);
            
    What the code is supposed to do is create an attribute that has DisplayLevel, and Identifier on it(Display level so that I know how to format the attributes in the lore to look like normal minecraft attributes, and Identifier to easily reference a specific attribute that I gave a number, like -16), but in 1.13.1 it removes both the DisplayLevel and Identifier parts of it when I turn the nms version of the itemstack back into the bukkit version of the itemstack. Also I used to be able to create attributes that were not there before(like arrowDamage) before in 1.12.2, but whenever I try to do it now in 1.13.1 it removes the attribute completely. Is there any way to do what I did in 1.12.2 similarly, or will I have to find another way?

    UPDATE: I just found out where exactly these things were getting removed, it's actually being caused by setting the item's meta data to a value.
    Code:
        //p.setItemMeta(meta);
        CraftItemStack.setItemMeta(nmscopy, meta);
        Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + nmscopy.getTag().toString());
    If either of those two aren't commented out then it will remove the other stuff.

    I solved it by just adding in another nbt tag list, instead of finding out why any time the item updates the other attribute information gets removed.
     
    Last edited: Nov 24, 2018
  2. @awesomebutter234

    You said in your comment that CraftItemStack.asBukkitCopy removes the tag data. So what if you try to bypass that part and use the NMS codes of the player (or where ever you want the itemstack to be) to give the nms stack 'directly'?
     
  3. When I bypassed the CraftItemStack.asBukkitCopy part it showed the DisplayLevel, and the Identifier. I bypassed it by doing ((CraftPlayer)Bukkit.getPlayer("awesomebutter234")).getHandle().inventory.setItem(2, nmscopy); before the CraftItemStack.asBukkitCopy part. The only issue is that I need to modify the Item afterwards in the code without it going into a player's inventory, and I'm not sure how I would do the same as I did in the setItem part.
     
  4. @awesomebutter234
    The easiest way would be to modify the item before messing with the NBT.
    But if that is not possible for some reason, you can try to edit the NMS stack. This is no funny task, but it is possible.
     
Thread Status:
Not open for further replies.

Share This Page