Outputting multiple stacks of 1 item from an array?

Discussion in 'Plugin Development' started by marshmallowz, Dec 8, 2013.

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

    marshmallowz

    Hello I need some help with getting multiple stacks for wood planks from a random list
    Here is my code so far:
    Code:
    ItemStack[] supplies = {giveWood(3, player)};           
     
    for(int i = 0; i<1; i++){
        int index = random.nextInt(supplies.length);
        player.getInventory().addItem(supplies[index]);
    }
     
        private ItemStack giveWood(int howMuch, Player player){
            switch(howMuch){
            case 3:
                ItemStack[] wood = {new ItemStack(Material.WOOD, 64, (short)1),
                        new ItemStack(Material.WOOD, 64, (short)1),
                        new ItemStack(Material.WOOD, 64, (short)1)};
                player.getInventory().addItem(wood);
            }
            return null;
        }
     
  2. Offline

    Njol

    What do you actually want to achieve with your code?
     
  3. Offline

    marshmallowz

    Njol
    Okay lets say the array was bigger like
    Code:
    ItemStack[] supplies = {new ItemStack(Material.WOOD, 64), giveWood(3, player), new ItemStack(Material.APPLE, 64), new ItemStack(Material.DIAMOND_PICKAXE};
      
    With the for loop I want it to randomly select an item stack from the array but I want it so that instead of just selecting 1 stack of wood at a time I want it to give me 3 at once. Does that make it any clearer?

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    xTrollxDudex

    marshmallowz
    Nice try with the giveWood method. null != ItemStack
     
  5. Offline

    marshmallowz

  6. Offline

    samosaara

    First, look at Java syntax. This forum is not to answer java questions. But, anyway:
    Code:
    return wood;
     
  7. Offline

    marshmallowz

    That doesnt work because my method is an ItemStack not an ItemStack Array
     
  8. Offline

    JRL1004

    marshmallowz do you want to give three of the whole stack or just three of the item?
    (E.G. let's say it chooses a stack of 32 wood planks, do you want it to give the player 3 wood planks or 3 * 32 (96 planks))
     
  9. Offline

    samosaara

    marshmallowz What? If you mean that your method return a ItemStack instead of an item stack array , make it returns a array. It's possible. Or:
    return wood[index];
    Keep in mind that your method is cancelled immediately at a return statement.
     
  10. Offline

    marshmallowz

    JRL1004
    I want it to give the player 3 stacks of wood planks.
     
  11. Offline

    Njol

    So basically you want something like this?
    Code:java
    1. ItemStack[] items = {some random items};
    2. Random r = new Random();
    3.  
    4. /**
    5.  * @return a random item stack out of #items with the specified amount
    6.  */
    7. ItemStack getRandom(int amount) {
    8. ItemStack i = items[r.nextInt(items.length)]; // get a random item stack
    9. i = i.clone(); // clone it - we don't want to modify the items in #items
    10. i.setAmount(amount); // set the amount
    11. return i;
    12. }
     
Thread Status:
Not open for further replies.

Share This Page