Remove items with custom display name?

Discussion in 'Plugin Development' started by Connor2weirdness, Mar 27, 2014.

Thread Status:
Not open for further replies.
  1. Title :3
    Thanks.
     
  2. Offline

    xXSniperzzXx_SD

    Connor2weirdness
    Explain :3
    Thanks.

    Just a heads up, never make a thread, and just say "Look at Title" because then all replies you get will be "Explain", "Don't do that", or they'll tag a staff, and your thread will be removed.


    Loop through inventory
    if itemstack has item meta
    if itemmeta's display name is "Name"
    set Type to Air
     
  3. How am I supposed to explain that? I want to remove an item from an inventory with a custom display name. Explained.

    I came up with this code but it throws NullPointerException on the .getType check.
    Code:
    for(ItemStack i : player.getInventory().getContents()){
                  if(i.getType() == Material.STONE_BUTTON){
                        player.getInventory().removeItem(new ItemStack(Material.STONE_BUTTON));
                        player.updateInventory();
                    }
                }
    
     
  4. Offline

    NonameSL

    Connor2weirdness, what you need to do is this:

    Code:
    ItemStack item = new ItemStack(Material.STONE_BUTTON);
    ItemMeta im = item.getItemMeta();
    im.setDisplayName("DisplayName");
    item.setItemMeta(im);
    
    and then make a for loop to get all the contents, then use the isSimilar method which checks if an itemstack is equal to another itemstack but ignores how much there is of that item - if the itemstack item is similar to the itemstack that you just got from the for loop, use the removeItem method to remove the item from the for loop.
     
  5. Thanks.
     
  6. Offline

    xXSniperzzXx_SD

  7. That is what creates the NullPointerException?
     
  8. Offline

    xXSniperzzXx_SD

Thread Status:
Not open for further replies.

Share This Page