Giving a player 1 item instead of everything in the list

Discussion in 'Plugin Development' started by BrushPainter, Oct 12, 2014.

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

    BrushPainter

    Code:java
    1. ItemStack[] possibleItems = {sfsword, new ItemStack(Material.GOLDEN_APPLE, 3, (short) 1), new ItemStack(Material.EXP_BOTTLE, 160)};
    2.  
    3. player.getInventory().addItem(possibleItems);


    Here is my current code, this gives all 3 items, I just want it to pick one random item instead of all three but I can't figure it out.
     
  2. Offline

    pookeythekid

    BrushPainter Do you know of the Random class?
    Code:java
    1. Random random = new Random();
    2. int itemNum = random.nextInt(possibleItems.length) + 1;
    3. player.getInventory().addItem(possibleItems[itemNum]);

    ;)

    Edit: By the way, what kind of plugin is this being used for? Looks interesting.
     
  3. Offline

    SmooshCakez

    If you add 1 to the random, it'll never pick the first item. You should generate a random with a max of possibleItems.length + 1, then it'll give you a number between 0 and the length of that array, which will have a change of picking any random item out of the array, not just 1-max.
     
  4. Offline

    pookeythekid

    SmooshCakez Oh... I never really bothered to look into exactly how to use a Random. I just saw it that way on a tutorial and went with it.
     
Thread Status:
Not open for further replies.

Share This Page