Testing if player has an item

Discussion in 'Plugin Development' started by MrLizardDogMan, Mar 22, 2014.

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

    MrLizardDogMan

    i want to test if a player has a specific item, and how many of that item, so that if they have over 4 sticks, it will give them 2 logs, and it will take the 4 sticks, but if they have more than 4, those will stay
     
  2. Offline

    MRPS

    Code:
    if(Arrays.asList(p.getInventory().getContents()).contains(item)){
      // do some stuff if they got the items required
    }
     
  3. Offline

    MrLizardDogMan

    Yeah i've found that, but how do i test to see if they have a specific amount of that item
     
  4. Offline

    tommyhoogstra

    MrLizardDogMan

    Look into ItemStack to check if a specific stack of items is greater than 4 etc
     
  5. Offline

    MRPS

    tommyhoogstra MrLizardDogMan
    Code:
    if(Arrays.asList(p.getInventory().getContents()).contains(item)){
      Material targetMat = Material.STICK; // or whatever you are trying to count the amount of
      int amount = 0;
      for(ItemStack is: p.getInventory().getContents()){
        if(is.getType().equals(targetMat)) amount+=is.getAmount();
      }
     
    // amount is how much of the item there is TOTAL (all stacks combined)
    // .getAmount() would only return the amount in one stack
    }
     
Thread Status:
Not open for further replies.

Share This Page