Get Enchantment + Level ?!

Discussion in 'Plugin Development' started by recon88, May 29, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    recon88

    How to get the Enchantment + Level of an Item in the inventory (also armor slots)?
    I messed around with "getEnchantments().containsKey(Enchantment.SILK_TOUCH" (which should work) and "getEnchantmentLevel()" (don't know how this one works).

    Base code:
    Code:
          public static void Task() {
              for (Player player : Bukkit.getOnlinePlayers()) {
                  ItemStack helm = player.getInventory().getHelmet();
                  ItemStack chest = player.getInventory().getChestplate();
                  ItemStack pants = player.getInventory().getLeggings();
                  ItemStack boots = player.getInventory().getBoots();
                  {
                  if (helm/chest/pants/boots enchantment+level equals XY)
                  {
                    //  do stuff here
                  }
              }
          }
          }
     
  2. Offline

    r0306

    recon88
    Code:
    HashMap<Enchantment, Integer> enchant = player.getInventory().getEnchantments();
    And there you have a hashmap of enchantments and integers which represent their levels. :)
     
  3. Offline

    recon88

    "The method getEnchantments() is undefined for the type PlayerInventory"
     
  4. Offline

    r0306

    recon88
    You need to define an itemstack inside their inventory.

    Code:
    HashMap<Enchantment, Integer> helmet = helm.getEnchantments();
    HashMap<Enchantment, Integer> chestplate = chest.getEnchantments();
    HashMap<Enchantment, Integer> pant = pants.getEnchantments();
    HashMap<Enchantment, Integer> boot = boots.getEnchantments();
     
  5. Offline

    recon88

    I don't get it :/
     
  6. Offline

    r0306

    recon88
    Ok. The variable I'm using are the ItemStacks you defined in your first post.

    Code:
                  ItemStack helm = player.getInventory().getHelmet();
                  ItemStack chest = player.getInventory().getChestplate();
                  ItemStack pants = player.getInventory().getLeggings();
                  ItemStack boots = player.getInventory().getBoots();
    
    Then, I just assigned a hashmap to each of them to store their enchantments and their respective levels. That was what you were asking right? If you only need the levels for the enchantments, then use a loop to add up the integers in all 4 hashmaps and store them in an Integer variable for processing. :)
     
  7. Offline

    recon88

    I'm confused (or just a bit tired) :eek:
    I want to check the players inventory (also the the armor slots).
    If it contains (let's say) an Helmet with the Enchantment Durability & Level 3, I want to do something.
     
  8. Offline

    r0306

    recon88
    Okay. In that case, you would get the helmet slot and check for its level. Here's a basic example of what you would do:
    Code:
    HashMap<Enchantment, Integer> helmet = player.getInventory().getHelmet().getEnchantments();
    if (helmet.containsKey(Enchantment.DURABILITY) {
    int level = helmet.get(Enchantment.DURABILITY);
    }
     
  9. Offline

    recon88

    What I'm doing seems to be wrong:

    Code:
          public void Task() {
              for (Player player : Bukkit.getOnlinePlayers()) {
    (Line 39 here)              Map<Enchantment, Integer> Enchants = player.getInventory().getHelmet().getEnchantments();
                      if (Enchants.containsKey(Enchantment.DURABILITY)) {
                          Integer level = Enchants.get(Enchantment.DURABILITY);
                              if (level == 1) {
                                  //    DO STUFF HERE
                              }
                          }
                      }
              }
    Code:
    2012-05-30 12:13:48 [WARNING] Task of 'EnchantTest' generated an exception
    java.lang.NullPointerException
        at com.recon.EnchantWhipe.EnchantTest.Task(EnchantTest.java:39)
        at com.recon.EnchantWhipe.EnchantTest$1.run(EnchantTest.java:24)
        at org.bukkit.craftbukkit.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:126)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:533)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
     
  10. That error is not pointing to any part of the code you posted. It relates to the "ClearArmor" method which is, as far as I can see, not called inside the Task() method at all.
     
  11. Offline

    recon88

    I just posted the error of the old method name. Renamed it before I pasted it into this thread.
    Fixed the code above.

    Still can't find the right solution.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  12. getHelmet() returns null if the player doesn't have a helmet.

    And next time tell us which is the line in the error.
     
  13. Offline

    recon88

    Already edited it.
     
  14. Yes, and I've already told you the answer it seems :p ... so check getHelmet() against null before using any methods from it !
     
  15. Offline

    recon88

    I've already added it after you said that and it works :p
    Thanks
     
Thread Status:
Not open for further replies.

Share This Page