Solved "Search" for an item in your inventory

Discussion in 'Plugin Development' started by CheesyFreezy, Dec 31, 2014.

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

    CheesyFreezy

    Heey heey developers,
    I'm working on a plugin where you can save your inventory. When it's saved the arrows in your inventory will be set to 32 and the fire charges to 3.

    I tried doing this, but it failed:
    Code:
    for(ItemStack is1 : player.getInventory().getContents()) {
       if(is1.getType() == Material.ARROW) {
          is1.setAmount(32);
          }
    
    for(ItemStack is2 : player.getInventory().getContents()) {
       if(is2.getType() == Material.FIREBALL) {
          is2.setAmount(3);
          }
       }
     
    Last edited: Dec 31, 2014
  2. Offline

    uksspy

    @CheesyFreezy
    Make sure to check that the ItemStack isn't null before getting the type.
     
  3. @CheesyFreezy
    Code:
    for (int n = 0; n < player.getInventory().getContents().length; n++) {
        if (i != null) {
            if (i.getType().equals(Material.ARROW) {
                i.setAmount(32);
            }
            if (i.getType().equals(Material.FIREBALL) {
                i.setAmount(3);
            }
            getConfig().set("Items." + n, i);
        } else {
            getConfig().set("Items." + n, null);
        }
    }
    saveConfig();
     
  4. Offline

    CheesyFreezy

    Thank you @uksspy, that worked!
     
    uksspy likes this.
Thread Status:
Not open for further replies.

Share This Page