Solved How to get value of an Attribute modifier of an Item in player inventory [1.13.2]?

Discussion in 'Spigot Plugin Development' started by a_l_e_x_0_6, Apr 16, 2019.

Thread Status:
Not open for further replies.
  1. Hello, i need help, i'm making a plugin that allow EPF to go above the current minecraft cap(EPF = Enchantement Protection Factor) it works fine currently but i still need to retrieve the value of GENERIC_ARMOR and GENERIC_ARMOR_TOUGHNESS attributes to calculate the natural protection of the armor,

    however, i do not know how to achieve that, what i want to do is to read the value of these two attributes, but i do not know how to read them, i can remove them/change them using the itemMeta API but i couldn't find how to read them.

    so far i've tried to do that
    Code (Text):
            ItemStack[] armor = ply.getInventory().getArmorContents();//ply is the player
                for(ItemStack piece : armor)
                    ItemMeta meta = piece.getItemMeta();
                    meta.getAttributeModifiers(Attribute.GENERIC_ARMOR);//i'm stuck here, i do not know what to do after that

    if you could help me, i would really appreciate it. thanks in advance.
  2. First step is to read the javadoc and see what your function returns..
    Code (Text):
    In this case it returns a collection of AttributeModifiers, but here you are just discarding it. I would start by iterating the collection, like this:
    Code (Text):
    for (AttributeModifier am : meta.getAttributeModifiers(Attribute.GENERIC_ARMOR))
        // Query each AttributeModifier and get the info you wanted
    If you are unsure how to read the information contained in a AttributeModifier, the javadoc is the best place to start.

  3. Thanks for your reply, but i'm still having issue, i somehow cannot iterates the collection.
    there are no errors in the IDE, but once i try it in minecraft the plugin throw an exception every times it gets triggered.

    i tried:
    Code (Java):
    ItemMeta meta = piece.getItemMeta();
                    for (AttributeModifier am : meta.getAttributeModifiers(Attribute.GENERIC_ARMOR))
                        //totalArmorProt += am.getAmount();

    Code (Java):
    Collection<AttributeModifier> meta = piece.getItemMeta().getAttributeModifiers(Attribute.GENERIC_ARMOR);
                    for (AttributeModifier am : meta)
                        //totalArmorProt += am.getAmount();
    buth they both do not work.
  4. When you say they don't work, do you mean it throws an error, or doesn't output anything to System.out? If there's no output, then the returned collection is empty. Items aren't guaranteed to have the attribute you request, and if they don't then getAttributeModifiers(attribute) will return an empty Collection. Based on what you're saying I'm almost positive that's the issue, but if you want to know for sure you could always do this:
    Code (Text):
    Collection<AttributeModifier> meta = piece.getItemMeta().getAttributeModifiers(Attribute.GENERIC_ARMOR);
        System.out.println("Collection was empty!")
    If you want to know every Attribute that your item has, you can do this:
    Code (Text):
    for (Attribute attribute : piece.getItemMeta().getAttributeModifiers().keys())
    If nothing is printed here, your item has no Attribute data associated with it.
  5. it throws an error.
    the 2 codes you provided throw errors too.

    i tried for just the chestplate to see what would it prints.
    Code (Text):
    Collection<AttributeModifier> meta = ply.getInventory().getChestplate().getItemMeta().getAttributeModifiers(Attribute.GENERIC_ARMOR);
    this print null which is absolutely false as i was wearing a diamond chestplate with the GENERIC_ARMOR attribute.

    Code (Text):
    ItemMeta meta = ply.getInventory().getChestplate().getItemMeta();
    this print UNSPECIFIC_META:{meta-type=UNSPECIFIC, Damage=0} while wearing a diamond chestplate

    i don't know what's wrong because i think that i'm doing a correct usage of the api here.
  6. I'll check this when I get home from work. Could you post the stack trace for the error it threw?
  7. sure thing but the error doesn't tell much.
    also i don't know how to see the whole error, the end is cropped "... 36 more".
    Code (Text):
    Could not pass event EntityDamageByEntityEvent to ProtectionEPFlimitRemover v1.0
    org.bukkit.event.EventException: null
        at$1.execute( ~[spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.craftbukkit.v1_13_R2.event.CraftEventFactory.callEvent( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.craftbukkit.v1_13_R2.event.CraftEventFactory.callEntityDamageEvent( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.craftbukkit.v1_13_R2.event.CraftEventFactory.handleEntityDamageEvent( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.craftbukkit.v1_13_R2.event.CraftEventFactory.handleLivingEntityDamageEvent( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityLiving.damageEntity0( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityHuman.damageEntity0( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityLiving.damageEntity( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityHuman.damageEntity( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityPlayer.damageEntity( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityInsentient.B( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityZombie.B( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.PathfinderGoalMeleeAttack.a(SourceFile:124) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.PathfinderGoalMeleeAttack.e(SourceFile:116) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.PathfinderGoalZombieAttack.e(SourceFile:28) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.PathfinderGoalSelector.doTick(SourceFile:129) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityInsentient.doTick( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityLiving.movementTick( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityInsentient.movementTick( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityMonster.movementTick(SourceFile:35) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityZombie.movementTick( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityLiving.tick( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityInsentient.tick( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityMonster.tick(SourceFile:40) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityZombie.tick( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.World.entityJoinedWorld( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.World.g( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.World.tickEntities( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.WorldServer.tickEntities( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.MinecraftServer.b( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.DedicatedServer.b( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.MinecraftServer.a( [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
        at com.alex06.ProtFixPlugin.MyListener.protcalc( ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_161]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_161]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_161]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_161]
        at$1.execute( ~[spigot1132.jar:git-Spigot-f09662d-7c395d4]
        ... 36 more
    my plugin triggers when an entity hit another entity.
    here a zombie hit me.
  8. This error tells you everything you need to know..
    Code (Text):
     Caused by: java.lang.NullPointerException
        at com.alex06.ProtFixPlugin.MyListener.protcalc( ~[?:?]
    There's a null pointer exception on line 39 of your MyListener class. Could you post this code, highlighting line 39?

  9. EDIT: i found the issue.
    somehow if the attribute modifier is equal to the default value on the item it won't detect it.
    (ie: armor and armor toughness on armor)
    so instead i will just check if the attributes exist (for armor with custom values) and if they don't i will get the values by checking the armor type (diamond/gold/..)
    thanks for the help @CraigParton i wouldn't have found without you.

    #9 a_l_e_x_0_6, Apr 19, 2019
    Last edited: Apr 19, 2019
    • Like Like x 1
Thread Status:
Not open for further replies.