Override base item attribute value

Discussion in 'Spigot Plugin Development' started by MrDienns, Apr 28, 2017.

Thread Status:
Not open for further replies.
  1. I've recently been able to change the attribute values of an item.

    I have a JSON file that contains a bit of information about an item. These values from the JSON file are used to build a custom item, which includes: A display name, lores and attributes. Of the attributes is attack speed. The current problem is:

    I have set an attack speed of 0.5 in my JSON file, and it uses a diamond sword as material with a default of 1.6 attack speed. Problem is, it's adding 0.5 attack speed instead of setting it to 0.5. How can one achieve this? I've looked into operations (with three options), but that doesn't seem to help greatly...
     
  2. Why not ADD .4 and MULTIPLY with .25?

    EDIT: from the attributes page:
    Code (Text):
    /summon Zombie ~ ~ ~ {Attributes:[{Name:"generic.followRange",Base:100.0}]}
     
  3. The command is slightly more usefull. How does one do this in code though?

    I'm using this API
    (GitHub has a new version I'm using, check here)
     
  4. I guess it doesn't have support for lists. You'd need to try a different api.
     
  5. It does. That's why I added a link to github and the specific branch. I've been talking to the developer. He added the list functionality. We're currently testing it. If it didn't support lists I wouldn't be able to change attribute values to begin with :p
     
  6. Oops ;p. Had just woken up and didn't notice it.
    I guess something like this could work:
    Code (Java):
    NBTListCompound attr = nbti.getList("Attributes", NBTType.NBTTagList).addCompound();
    attr.setString("Name", "generic.attackSpeed");
    attr.setDouble("Base", 0.5);
     
  7. I can't test it at the moment so I'll test it out when I get home. Thanks for the help so far :D
     
  8. Coder of the lib here o/.
    Putting an Attribute on an Item with the Operation 0 overrides the normal items attributes (For diamond swords that would be 1.6 attack speed(in whatever metric, because it's not the Attributes one) and 7 damage) and uses the default hand one (4.0 attack speed and 1 damage). Now you can just add or remove attack speed or damage:
    Damage:
    The base damage is 1 heart(or 2 half hearts). Using -0.5 as Attribute you bring down the damage of the item to 0.5 hearts. Using 0 sets the items damage to 1 heart. Everything >0 changes the damage to 1+value (value of 4 sets the items dmg to 5heaths).
    Code(Diamond sword that does only 0.1 hearths damage):
    Code (Text):
            ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
            NBTItem nbti = new NBTItem(sword);
            NBTList attribute = nbti.getList("AttributeModifiers", NBTType.NBTTagCompound);
            NBTListCompound mod1 = attribute.addCompound();
            mod1.setDouble("Amount", -0.9);
            mod1.setString("AttributeName", "generic.attackDamage");
            mod1.setString("Name", "generic.attackDamage");
            mod1.setInteger("Operation", 0);
            mod1.setInteger("UUIDLeast", 59764);
            mod1.setInteger("UUIDMost", 31483);
            sword = nbti.getItem();
    AttackSpeed:
    The base attack speed is 4. The higher the attack speed the faster the cooldown is gone. Setting the speed to 99999999 completely removes the cooldown, while setting it to -3.9 makes it take ages to recharge.
    Code(Diamond sword with an insanely long cooldown):
    Code (Text):
            ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
            NBTItem nbti = new NBTItem(sword);
            NBTList attribute = nbti.getList("AttributeModifiers", NBTType.NBTTagCompound);
            NBTListCompound mod1 = attribute.addCompound();
            mod1.setDouble("Amount", -3.9);
            mod1.setString("AttributeName", "generic.attackSpeed");
            mod1.setString("Name", "generic.attackSpeed");
            mod1.setInteger("Operation", 0);
            mod1.setInteger("UUIDLeast", 59764);
            mod1.setInteger("UUIDMost", 31483);
            sword = nbti.getItem();
    Code(Sword without cooldown):
    Code (Text):
            ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
            NBTItem nbti = new NBTItem(sword);
            NBTList attribute = nbti.getList("AttributeModifiers", NBTType.NBTTagCompound);
            NBTListCompound mod1 = attribute.addCompound();
            mod1.setDouble("Amount", 99999999);
            mod1.setString("AttributeName", "generic.attackSpeed");
            mod1.setString("Name", "generic.attackSpeed");
            mod1.setInteger("Operation", 0);
            mod1.setInteger("UUIDLeast", 59764);
            mod1.setInteger("UUIDMost", 31483);
            sword = nbti.getItem();
    I hope this explains how they work :D.
    tr7zw
     
    • Like Like x 1
    • Useful Useful x 1
  9. Sweet, I now got it working by doing the following:

    Code (Text):
    mod1.setDouble("Amount", -4.0 + this.getAttackSpeed());
    Simply removing the base value of 4 and then adding my own custom value fixes the issue of attack speed. I'm yet to test damage and other values
     
    • Useful Useful x 1
Thread Status:
Not open for further replies.