Checking the player has a renamed item

Discussion in 'Plugin Development' started by Skionz, Jul 29, 2014.

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

    Skionz

    Does anyone know how to check if the players inventory contains a item with a renamed displayname? I already tried
    Code:
    ItemStack item = new ItemStack(Material.FLINT, 1);
    ItemMeta itemsMeta = item.getItemMeta();
    itemsMeta.setDisplayName("item");
    item.setItemMeta(itemsMeta);
     
    if(player.getInventory.contains(item){
        //stuff
    }
     
  2. Offline

    rfsantos1996

    iterate through all items than check if item has DisplayName and if it does, check if it is the same
     
  3. Offline

    DevRosemberg

    Skionz Should be something like this:

    Code:java
    1. for(ItemStack stack : event.getPlayer().getInventory().getContents()) {
    2. if (stack.getItemMeta() != null) {
    3. if (stack.getItemMeta().getDisplayName() != null) {
    4.  
    5. }
    6. }
    7. }


    Though you could use Iterator<ItemStack> but its the same.
     
  4. Offline

    Stealth2800

    DevRosemberg You can replace 'stack.getItemMeta() != null' with 'stack.hasItemMeta()'
     
  5. Offline

    DevRosemberg

    Stealth2800 Ah, never noticed that method. Thanks.
     
  6. Offline

    Jalau

  7. Offline

    fireblast709

    DevRosemberg you can also iterate over the Inventory object. This will both contain the contents and the armor
     
Thread Status:
Not open for further replies.

Share This Page