Solved Random item in inventory

Discussion in 'Plugin Development' started by The Fancy Whale, Apr 24, 2014.

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

    The Fancy Whale

    I am trying to make a plugin where it picks a random item from someone's inventory and gives that item to someone else. I am not sure how to do this though.
    Trying to find a way to use the loop:
    Code:java
    1. for (ItemStack i : victim.getInventory().getContents()){
    2. //get random item here
    3. }

    I am not sure if this is the correct way to approach this, and if it is I still am not sure.
    Any help is greatly appreciated. Thanks!
     
  2. Offline

    Garris0n

  3. Offline

    rfsantos1996

    Code:java
    1. ItemStack is = victim.getInventory().getContents()[new Random().nextInt(victim.getInventory().getContents().size()-1)];
    2. victim.getInventory().remove(is);
    3. playerToGiveItem.getInventory().addItem(is);
     
    The Fancy Whale likes this.
  4. Offline

    The Fancy Whale

    Garris0n I have used randoms before, however I wasn't sure if how to apply to this scenario...

    rfsantos1996 Thanks for the help I will try that out!
     
  5. Offline

    Garris0n

    Well it looks like somebody else already spoonfed you, but you would generate a random number under the size of the inventory and get that item.
     
  6. Offline

    Seadragon91

    Okay 1 small problem, if the iventory is not completely full the inventory will then contains null values too

    Edit: And why size() -1? The random number is not between 0 <= random number, its between 0 < random number. Correct would be size() without -1.
     
  7. Offline

    The Fancy Whale

    Seadragon91 .getContents() returns null values as well?
     
  8. Offline

    Seadragon91

    The ItemStack array can contains null values, if the inventory is not full.
     
  9. Offline

    rfsantos1996


    Oh, forgot that netInt don't give <= value but give < value, I use other code, and I don't know if it returns null values, if it returns what Garris0n is what I sent @TheFhancyWhale.
     
  10. Offline

    The Fancy Whale

    Seadragon91 rfsantos1996 Only fix I can think of would be a repeating task where I do that method, then check if the itemstack is null, and if it is null, let the task repeat, however if the itemstack is not null, cancel the task and add the items.
     
  11. Offline

    Seadragon91

    A idea would that.
    - A method that gets as parameter the itemstack array
    - creates a array list of type itemstack
    - for loop adding all not null items to the array list
    - return a random item from the array list

    Code:
    public ItemStack getRandomItem(ItemStack[] content) {
        Random r = new Random();
        ArrayList<ItemStack> items = new ArrayList<ItemStack>();
     
        for (ItemStack item : content) {
            if (item != null)
                items.add(item);
        }
     
        return items.get(r.nextInt(items.size()));
    }
     
    The Fancy Whale and rfsantos1996 like this.
  12. Offline

    The Fancy Whale

    Seadragon91 That is probably much more efficient than doing a repeating task! Thanks for the idea!
     
  13. Offline

    Seadragon91


    Yes and other small problem, sorry. It can still return null. If the player has no item in his inventory, then it will returns a null.
     
  14. Offline

    The Fancy Whale

    Yeah I noticed that, so I added a check to see if their inventory was null
    Code:java
    1. if (victim.getInventory().getContents() == null){
     
  15. Offline

    Seadragon91


    That won't work, because it returns always itemstack array with a length of 36.
     
  16. Offline

    The Fancy Whale

    Seadragon91
    Code:
    public boolean isEmpty(Player player){
    for(ItemStack item : player.getInventory().getContents())
    {
        if(item != null)
          return false;
    }
    return true;
    }
    Would that work?
     
  17. Offline

    Seadragon91

    Yes, that will work :). Good luck then.
     
    The Fancy Whale likes this.
  18. Offline

    The Fancy Whale

    Thanks for the help!
     
Thread Status:
Not open for further replies.

Share This Page