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.

  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..
    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/meta/ItemMeta.html
    Code (Text):
    meta.getAttributeModifiers(Attribute.GENERIC_ARMOR)
    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.
    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/AttributeModifier.html
     

  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))
                    {
                        System.out.print(am);
                        //totalArmorProt += am.getAmount();
                    }

    and
    Code (Java):
    Collection<AttributeModifier> meta = piece.getItemMeta().getAttributeModifiers(Attribute.GENERIC_ARMOR);
                    for (AttributeModifier am : meta)
                    {
                        System.out.print(am);
                        //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);
    if(meta.isEmpty())
        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())
        System.out.println(attribute.name());
    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);
                System.out.println(meta);
    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();
                System.out.println(meta);
    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 org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:66) ~[spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:507) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:492) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.craftbukkit.v1_13_R2.event.CraftEventFactory.callEvent(CraftEventFactory.java:102) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.craftbukkit.v1_13_R2.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:762) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.craftbukkit.v1_13_R2.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:661) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at org.bukkit.craftbukkit.v1_13_R2.event.CraftEventFactory.handleLivingEntityDamageEvent(CraftEventFactory.java:794) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityLiving.damageEntity0(EntityLiving.java:1445) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityHuman.damageEntity0(EntityHuman.java:879) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityLiving.damageEntity(EntityLiving.java:967) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityHuman.damageEntity(EntityHuman.java:792) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityPlayer.damageEntity(EntityPlayer.java:625) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityInsentient.B(EntityInsentient.java:1221) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityZombie.B(EntityZombie.java:291) [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(EntityInsentient.java:659) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityLiving.movementTick(EntityLiving.java:2254) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityInsentient.movementTick(EntityInsentient.java:510) [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(EntityZombie.java:202) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityLiving.tick(EntityLiving.java:2098) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.EntityInsentient.tick(EntityInsentient.java:248) [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(EntityZombie.java:176) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.World.entityJoinedWorld(World.java:1331) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.World.g(World.java:1298) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.World.tickEntities(World.java:1132) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.WorldServer.tickEntities(WorldServer.java:583) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.MinecraftServer.b(MinecraftServer.java:968) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.DedicatedServer.b(DedicatedServer.java:417) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:831) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:729) [spigot1132.jar:git-Spigot-f09662d-7c395d4]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
        at com.alex06.ProtFixPlugin.MyListener.protcalc(MyListener.java:39) ~[?:?]
        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 org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:312) ~[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(MyListener.java:39) ~[?:?]
    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.

    SOLVED
     
    #9 a_l_e_x_0_6, Apr 19, 2019 at 11:58 AM
    Last edited: Apr 19, 2019 at 12:26 PM

Share This Page