Solved Random items in chest | HELP

Discussion in 'Plugin Development' started by Xyplo, Jul 6, 2014.

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

    Xyplo

    I've managed to place random items in chests across my world, but they are set in a line (one after another). Where I want them to be randomly placed in slots in the chest. How can I modify the following code to do so?

    Code:java
    1. //Some code before this
    2.  
    3. if(b instanceof Chest)
    4. {
    5. Chest chest = (Chest) b;
    6. Inventory chestInventory = chest.getInventory();
    7.  
    8. int o = 0;
    9. Random randomvariable = new Random();
    10. for(int i = o; i<randomvariable.nextInt(24); i++){
    11. chestInventory.addItem(new ItemStack(mats.get(randomvariable.nextInt(mats.size())),
    12. randomvariable.nextInt(1) + 1));
    13. }
    14.  
    15.  
    16. }
    17.  
    18. //END


    -Thanks, Xyplo
     
  2. Offline

    endoml

    I would use setItem instead of addItem and use a new random variable for the slot.
     
    unrealdesign likes this.
  3. Offline

    RingOfStorms

    You can do what endoml said with setItem(int slotIndex, ItemStack item) and for the slotIndex you could either do just a random number between 0-chest size (which could lead to accidentally replacing an item you already set), or you could make a list of integers that contains 0-chest size and shuffle it. Using that list you can use the next number in the list and remove it to prevent accidentally replacing an item.
     
  4. Offline

    dsouzamatt

    Xyplo You could do something like this to randomly choose the slot:
    Code:java
    1. chestInventory.setItem(rand.nextInt(27), theItem);

    where theItem is an ItemStack of the material that your wish to put in a random slot.

    Edit: And rand is just a new random
     
  5. Offline

    Xyplo

    I forgot about this thread, this is pretty much what I did anyway XD
     
Thread Status:
Not open for further replies.

Share This Page