Inventory

Discussion in 'Spigot Plugin Development' started by ula492, May 15, 2016.

  1. How do I get a specific item from a player with a custom name on the item?
    Ive Tried this.
    Code (Text):
            if (p.getInventory().getType().equals("§aVoting Key")) {
     
     
  2. An InventoryType will never equal a String. Loop through the inventory and find the item with the same name instead.
     
  3. This worked, only problem I dont want the "getItem(0)"

    Code (Text):
            if (p.getInventory().getItem(0).getItemMeta().getDisplayName().contains("§aVoting Key")) {
     
     
  4. Loop through the inventory. Inventory#getContents returns an ItemStack array.
     
  5. Can u show an example? sorry for the bother :p.
     
  6. I tried
    Code (Text):
            if (!p.getInventory().getContents().equals("§aVoting Key")) {
     
     
    • Optimistic Optimistic x 1
  7. Use a for loop to iterate through the inventory, and get the contents using Inventory#getContents().
     
  8. Example please?
     
    • Funny Funny x 1
  9. for (ItemStack itemstack : inv.getContents()){

    }
    OR
    if (player.getItemInHand().getItemMeta().getDisplayName().equals("")){
    }
     
    • Agree Agree x 1
  10. I assume you're using this to open a crate. Do not loop through the inventory and instead just have it check the item held in their hand. It saves resources and would stop you from getting roasted by most java devs.
     
  11. The way I'm using my crates I'll have to do it via looping. Since players are not right clicking a chest too open.
     
  12. @ula492 I recommend learning some java. If you don't want to waste a lot of time on it there are some tutorials that teach java in a rapid speed.

    Because you didn't know it, you had to be spoonfed by @BakedPotato_HD, and now it seems you skipped their post
     
    • Agree Agree x 1
  13. You will need to use #getContents, which will return an ItemStack array.
    Code (Text):

    for (ItemStack ... : ...getContents())
     

    After, you want to grab the ItemMeta -with #getItemMeta- from each individual ItemStack from the array returned, and check for a specific ItemMeta condition. I would suggest checking if the item has a display name first, before checking a custom pre-defined display name. This will save you from some null exceptions.
    Code (Text):

    if (....getItemMeta().hasDisplayName())
     

    Then, you can simply check the ItemStack's display name for any given string value, and proceed with further condition checks.
    Code (Text):

    if (....getItemMeta().getDisplayName().equals(...))
     

    And of course, do not forget to implement additional condition checks for statements that are applicable to return null values.
     
    #13 ChefJava, May 15, 2016
    Last edited: May 15, 2016
  14. I didn't want to spoonfeed, but if nobody gives an example it's really hard to learn. The chances are high that @ula492 now understands a for loop. If you don't like my reply, you should also think that tutorials are spoonfeeding, but you recommended tutorials so....
    I am just trying to help and the best way to help is a little bit of spoonfeeding so the person doesn't need to keep asking.
     
    • Agree Agree x 1
  15. @BakedPotato_HD actually I wasn't saying (at least not trying to do so) I didn't like it. I said because OP had next to 0 knowledge of java they had to be spoonfed for a simple for-loop.
     
  16. Sorry, I misunderstood you. After all we both are just trying to help :)
     
  17. Thanks your a peach <3, But it trows me a nullException
     
    #17 ula492, May 15, 2016
    Last edited: May 15, 2016
  18. Show your code, the stacktrace and the line where the NullPointerException occurs.
     
  19. Code (Text):

    for(ItemStack item : player.getInventory().getContents()){
    if(item.getItemMeta().getDisplayName().equals("§aVoting Key")){// the if line is null exception
    //code here
    }
    }
     
    • Your item meta could be null.
    • Your display name could be null.
    On a side note, instead of checking if the command is directly equal to the name with the exact chat colors, you can change it to a name that doesn't rely on colors (if you ever decide to change the color/name format).

    String stripColor(String input)
    Strips the given message of all color code

    So basically you could do something like:

    Code (Text):
    if(item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.stripColor("Voting Key")) {
    //code here
    }