How to get 1 ItemStack from a list of them? (at random)

Discussion in 'Plugin Development' started by Lorey879, Oct 14, 2014.

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

    Lorey879

    hi guys , I was wondering
    how can i make an list from item stack and when player write an command (/gift) he get an random item stack

    what the method should i use ? and how i use it ? :)




    Thanks ~<3
     
  2. Offline

    Gater12

    Lorey879
    Have an Array of ItemStacks you want to be included as a random ItemStack. Use Java's Random object to generate a number between 0 and the Array's max size. Then choose the ItemStack object based on the randomly created number.
     
  3. Offline

    TheCodingCat

    I would recommend saving the MATERIAL instead (e.g STONE, DIRT) and saving them to a config because array lists get cleared on reload and on shutdown.
     
  4. Offline

    Lorey879

    Can You Show me how .. Please :)
     
  5. Offline

    TheCodingCat

  6. Offline

    ItsOneAndTwo

    Lorey879
    I'm gonna spoonfeed you just because I feel like it. But bare in mind that you will have to change some stuff.
    Code:java
    1. public void giveGift(Player p) {
    2. ArrayList<ItemStack> gifts = new ArrayList<ItemStack>();
    3.  
    4. gifts.add(new ItemStack(Material.DIAMOND, 1));
    5. gifts.add(new ItemStack(Material.GOLD_BLOCK, 1));
    6. gifts.add(new ItemStack(Material.DIRT, 1));
    7. gifts.add(new ItemStack(Material.DIAMOND_BLOCK, 1));
    8. gifts.add(new ItemStack(Material.EMERALD, 1));
    9.  
    10. Random random = new Random();
    11.  
    12. int randomItem = random.nextInt(gifts.size());
    13.  
    14. ItemStack gift = gifts.get(randomItem);
    15.  
    16. p.getInventory().addItem(gift);
    17. }
     
    ChipDev and VG.Developments like this.
Thread Status:
Not open for further replies.

Share This Page