Creating random itemstacks.

Discussion in 'Plugin Development' started by Minibros, Apr 23, 2016.

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

    Minibros

    Hello,
    I'm trying to create random itemstacks with Material.Something

    ArrayList:
    Code:
    static ArrayList<ItemStack> goodies = new ArrayList<ItemStack>();
    
    Random selection system:
    Code:
                  for (int i = (3 + 1); i > 0; i--) {
                        Random random = new Random();
                        int index = random.nextInt(goodies.size());
                        ItemStack dropItem = goodies.get(index);
                       
                        event.getPlayer().getInventory().addItem(dropItem);
                    }
    
    Unfinished method that i need:
    Code:
            for (int i = (100 + 1); i > 0; i--) {
                // Create random itemstacks.
            }
    
     
  2. Offline

    mine-care

    First of all, what is the problem?
    And then, some notes:
    Is static nessesary here?

    No need to create 3+1 Random objects, create one outside the loop or even as a class variable and use it instead.

    I thought 100+1 = 101 :O hehe :p <Laugh now, it was supose to be a joke>
     
  3. Code:
    Material[] m = Material.values();
    int mlength = m.length;
    Random random = new Random();
    int index = random.nextInt(mlength);
    Material randMaterial = m[index];
    ItemStack item = new ItemStack(randMaterial);
    event.getPlayer().getInventory().addItem(item);
    
    Here's something you can use. (untested).
     
  4. Offline

    mine-care

    @ZanctarianDevelopment As i said in another thread as well, don't spoonfeed.
    Read the post from @Zombie_Striker found here for a brief explanation of the reasons and feel free to argue or ask any questions you have there. This code as it is there can be abused to an inefficient solution.
     
  5. Offline

    Badwolf330

    @Minibros
    They all are the same here.... :(
     
  6. Offline

    mcdorli

    Use item id's with java's built in random class, you can use new ItemStacks with the original ID's for different items.

    Why do I even bother helping?
     
  7. Offline

    mine-care

    Ok as long as you are not abusing it and you remain within the boundaries of Object Oriented Programming.

    Thanks ill remember that. I would apriciate some sort of politeness, and i remind you part of the rules you read and accepted when you registered to this comunity:
    (Mentioned under rule #2)

    But anyways, we have a reason why we don't spoonfeed and that is because experience shows it does more harm than good. You can read more here and feel free to ask any questions you have and argue with us about it there.
     
Thread Status:
Not open for further replies.

Share This Page