Solved Item Amount 0

Discussion in 'Plugin Development' started by boomboompower, Sep 20, 2015.

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

    boomboompower

    When I use this code, the player sometimes receives an amount of 0 items
    Code:
    public void regularPodzolDrops(Player p) {
                Random adrops = new Random();
                Random sdrops = new Random();
                ItemStack arrow = new ItemStack(Material.ARROW, 1);
                ItemStack string = new ItemStack(Material.STRING, 1);
                int achance = adrops.nextInt(3);
                int schance = sdrops.nextInt(3);
                string.setAmount(schance);
                arrow.setAmount(achance);
                p.getInventory().addItem(new ItemStack[] { arrow });
                p.getInventory().addItem(new ItemStack[] { string });
            }
    Here is the proof:

    is there any way to stop this
     
  2. Offline

    teej107

    @boomboompower I always thought that setting an item to show 0 would be harder than just setting the amount, but it may be your random generating a 0. Btw, you don't need to an array to add items to the inventory. You can actually call it once and put as many itemstack arguments as you like.

    EDIT: You also don't need those 2 random objects.
     
  3. Offline

    SyTeck

    Random generates a number from 0 to the number you specified, so if you want it to be 1 to the number you want just add 1. :)
     
  4. Offline

    boomboompower

    I have 2 ransoms so you don't get the same amount of both items, to make it look more natural.
    So you mean just get the random number and add 1?
     
  5. Offline

    teej107

    @boomboompower You don't need 2 different instances of Random. You only need 1. Truth be told, you don't even need to create a Random object. Use ThreadLocalRandom
     
  6. Offline

    SyTeck

    @boomboompower, you can use the same Random twice by calling nextInt() on the same Random object.

    Like this:
    Code:
    Random random = new Random();
    int first = random.nextInt(2) + 1;
    int second = random.nextInt(2) + 1;
    This will create 2 different random generated numbers.
     
  7. Offline

    RoboticPlayer

    I've never actually used random, how do they work?
     
  8. Offline

    mythbusterma

    @henderry2019

    You use an instance of "Random" and request numbers from it. It will be (fairly) random.
     
  9. Offline

    RoboticPlayer

    @mythbusterma So in @SyTeck 's example:
    Code:
    Random random = new Random()
    int first = random.nextInt(2)+1
    Would that create a random value from 0-2, then add one to the value? Or does it work differently than that.
     
  10. Offline

    mythbusterma

  11. Offline

    boomboompower

    Thx for the help :)
    Marking this thread as solved
     
Thread Status:
Not open for further replies.

Share This Page