Solved If player has item with X amount?

Discussion in 'Plugin Development' started by isleepzzz, May 15, 2013.

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

    isleepzzz

    Ok so I am just trying simple things for my plugin.
    What i want:
    A boolean called "hasIngots" and then so i can use it like
    if (hasIngots(10)) {
    then do something;
    }


    But I want the "hasIngots" boolean to have one argument which will be how much iron ingots it needs to return true. else return false.

    Thank you!:D
     
  2. Offline

    felixfritz

    If you just want to make sure that the player has a certain amount of ingots, you can create the boolean method eg. like this:
    PHP:
    public static boolean hasIngots(int amount) {
        if(
    amount == 10)
            return 
    true;
        return 
    false;
    }
    or if you want to make the code really "smooth"
    PHP:
    public static boolean hasIngots(int amount){
        return 
    amount == 10;
    }
    Does this answer your question?
     
  3. Offline

    Polaris29

    You can try this, but you'll also need to have an argument for the player.
    Code:
    public static boolean hasIngots(Player p, int minAmount) {
        return hasItem(p, Material.IRON_INGOT, minAmount);
    }
     
    public static boolean hasItem(Player p, Material material, int minAmount) {
        return hasItem(p, material, minAmount, (byte) 0);
    }
     
    public static boolean hasItem(Player p, Material material, int minAmount, byte data) {
        int totalAmount = 0;
        for (ItemStack i : p.getInventory().getContents()) {
            totalAmount += (i.getData().getData() == data && i.getType() == material) ? i.getAmount() : 0;
        }
        return totalAmount >= minAmount; //Change >= to == if you want exact amount instead
    }
    
     
  4. Offline

    ZeusAllMighty11 Retired Staff

    Code:
    public boolean hasIngots(int amount, Material mat,  Player player){
        return player.getInventory().containsAtLeast(amount, mat);
    }
     
  5. Offline

    isleepzzz

    How do I make an ItemStack?
    Because I'm getting an error on the red:
    if (p.getInventory().containsAtLeast(Material.IRON_INGOT, 20)) {
    SOMETHING;
    }

    How do I fix this?

    FIXED!:D Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page