Solved Need help adding multiple attribute modifiers

Discussion in 'Plugin Development' started by awesomebutter234, Dec 21, 2017.

Thread Status:
Not open for further replies.
  1. Some weird things is happening with my code, to me it looks like it's somehow managing to retroactively change the values in both please and modifiers. This is making it so that the only attribute that gets put on the item is the armor attribute, and that one is only put once instead of 3 times.
    Code:
    public static ItemStack createItem(Material m,String name,String lore,String[] a,boolean unbreakable,String[] enchantments)
        {
            //Naming
          
            ItemStack p = new ItemStack(m);
          
            ItemMeta meta = p.getItemMeta();
            meta.setDisplayName(name);
            meta.setLore(Arrays.asList(lore));
            p.setItemMeta(meta);
          
            //Attributes
            net.minecraft.server.v1_12_R1.ItemStack nmscopy = CraftItemStack.asNMSCopy(p);
          
            //Check if the thing has a tag, if s get it, if it doesn't, create a new one
            NBTTagCompound compound = (nmscopy.hasTag()) ? nmscopy.getTag() : new NBTTagCompound();
            NBTTagCompound attributes = new NBTTagCompound();
            NBTTagList base = compound.getList("AttributeModifiers", 10);
            NBTTagList modifiers = new NBTTagList();
            HashMap<Integer,NBTTagCompound> please = new HashMap<Integer,NBTTagCompound>();
            //Check if they even used attribute modifiers//
            if(a.length >0)
            {
            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],"[","]"));
                String slot = StringUtils.substringBetween(a[i],"{","}");
                //check to see if it's a legal slot
          
              
                attributes.set("AttributeName", new NBTTagString("generic." + attributename));
                attributes.set("Name", new NBTTagString("generic." + attributename));
                attributes.set("Amount", new NBTTagDouble(amount));
                attributes.set("Operation", new NBTTagInt(operation));
                attributes.set("UUIDLeast", new NBTTagInt(ThreadLocalRandom.current().nextInt(1, 10000 + 1)));
                attributes.set("UUIDMost", new NBTTagInt(ThreadLocalRandom.current().nextInt(1, 10000 + 1)));
              
              
              
                if(legalslots.contains(slot))
                {
                attributes.set("Slot", new NBTTagString(slot));
                }
                else
                {
                attributes.set("Slot", new NBTTagString("Unknown"));
              
                Bukkit.getLogger().info(slot + "is illegal");
                Bukkit.getLogger().info("Legal slot options:");
                for(int run = 0 ; run<legalslots.size();run++)
                {
                  
                    Bukkit.getLogger().info(legalslots.get(run));
                  
                }
              
                }
              
              
              
                please.put(i, attributes);
                modifiers.add(attributes);
                modifiers.add(base);
                Bukkit.broadcastMessage(ChatColor.RED + attributes.toString());
                for(int loop=0;loop<please.size();loop++)
                {
                    Bukkit.broadcastMessage(ChatColor.AQUA + "How many times looped:"+ String.valueOf(loop  +1));
                    Bukkit.broadcastMessage(please.get(loop).toString());
                }
              
    
                }
      
            Bukkit.broadcastMessage("How many attributes: " +String.valueOf(a.length));
            Bukkit.broadcastMessage("How many actually work: "+String.valueOf(please.size()));
            for(int pe = 0;pe<please.size();pe++)
            {
                Bukkit.broadcastMessage(please.get(pe).toString());  
                //Bukkit.broadcastMessage(modifiers.get(pe).toString());
            }
                  
            }  
            if(modifiers.isEmpty() == false)
                {
              
                if(unbreakable == true)
                {
                    Bukkit.broadcastMessage("Unbreakable with attributes");
                    compound.set("Unbreakable", new NBTTagByte((byte) 1));
                }
              
                    Bukkit.broadcastMessage("Atttributes added");
                    compound.set("AttributeModifiers", modifiers);
                    nmscopy.setTag(compound);
                    p = CraftItemStack.asBukkitCopy(nmscopy);
                }
    When I run it I get this feedback:
    [18:20:07 INFO]: {UUIDMost:9200,UUIDLeast:3619,Amount:20.0d,Slot:"mainhand",Attr
    ibuteName:"generic.attackDamage",Operation:0,Name:"generic.attackDamage"}

    [18:20:07 INFO]: How many times looped:1
    [18:20:07 INFO]: {UUIDMost:9200,UUIDLeast:3619,Amount:20.0d,Slot:"mainhand",Attr
    ibuteName:"generic.attackDamage",Operation:0,Name:"generic.attackDamage"}
    [18:20:07 INFO]: {UUIDMost:7058,UUIDLeast:1571,Amount:30.0d,Slot:"offhand",Attri
    buteName:"generic.attackSpeed",Operation:1,Name:"generic.attackSpeed"}

    [18:20:07 INFO]: How many times looped:1
    [18:20:07 INFO]: {UUIDMost:7058,UUIDLeast:1571,Amount:30.0d,Slot:"offhand",Attri
    buteName:"generic.attackSpeed",Operation:1,Name:"generic.attackSpeed"}
    [18:20:07 INFO]: How many times looped:2
    [18:20:07 INFO]: {UUIDMost:7058,UUIDLeast:1571,Amount:30.0d,Slot:"offhand",Attri
    buteName:"generic.attackSpeed",Operation:1,Name:"generic.attackSpeed"}
    [18:20:07 INFO]: {UUIDMost:873,UUIDLeast:8343,Amount:5.0d,Slot:"chest",Attribute
    Name:"generic.armor",Operation:2,Name:"generic.armor"}

    [18:20:07 INFO]: How many times looped:1
    [18:20:07 INFO]: {UUIDMost:873,UUIDLeast:8343,Amount:5.0d,Slot:"chest",Attribute
    Name:"generic.armor",Operation:2,Name:"generic.armor"}
    [18:20:07 INFO]: How many times looped:2
    [18:20:07 INFO]: {UUIDMost:873,UUIDLeast:8343,Amount:5.0d,Slot:"chest",Attribute
    Name:"generic.armor",Operation:2,Name:"generic.armor"}
    [18:20:07 INFO]: How many times looped:3
    [18:20:07 INFO]: {UUIDMost:873,UUIDLeast:8343,Amount:5.0d,Slot:"chest",Attribute
    Name:"generic.armor",Operation:2,Name:"generic.armor"}
    [18:20:07 INFO]: How many attributes: 3
    [18:20:07 INFO]: How many actually work: 3
    [18:20:07 INFO]: {UUIDMost:873,UUIDLeast:8343,Amount:5.0d,Slot:"chest",Attribute
    Name:"generic.armor",Operation:2,Name:"generic.armor"}
    [18:20:07 INFO]: {UUIDMost:873,UUIDLeast:8343,Amount:5.0d,Slot:"chest",Attribute
    Name:"generic.armor",Operation:2,Name:"generic.armor"}
    [18:20:08 INFO]: {UUIDMost:873,UUIDLeast:8343,Amount:5.0d,Slot:"chest",Attribute
    Name:"generic.armor",Operation:2,Name:"generic.armor"}

    [18:20:08 INFO]: Unbreakable with attributes
    [18:20:08 INFO]: Atttributes added

    The purple is what's actually inside of the list
    The red is the attribute that is supposed to be added
    The blue is how many for loop checks for the things inside of please



    This is the command that calls this
    Code:
    if(c.getName().equalsIgnoreCase("Item"))
            {
                Player p = (Player) s;
                String[] enchantments = new String[] {"(DAMAGE_ALL) [100]","(FIRE_ASPECT) [100]"};
                String[] attributes = new String[]{"(attackDamage) <0> [20] {mainhand}","(attackSpeed) <1> [30] {offhand}","(armor) <2> [5] {chest}"};
                ItemStack give = ItemGenerator.createItem(Material.DIAMOND_AXE, "Potato", "Hello\n Goodbye", attributes, true, enchantments);
                p.getInventory().addItem(give);
                return true;
            }
    The reason that I made attributes and enchantments an String[] is because I needed to add multiple conditions and values.

    The other things[unbreakable,enchantments and name] work fine.
     
Thread Status:
Not open for further replies.

Share This Page