Auto-stocking chests with random items from a list

Discussion in 'Plugin Development' started by Teddinator, Nov 3, 2013.

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

    Teddinator

    What the title said. Here is my code, but it doesn't seem to work:

    Code:java
    1. @EventHandler
    2. public void fillChests(PlayerJoinEvent event) {
    3. World world = Bukkit.getWorld("world");
    4. for(Chunk c : world.getLoadedChunks()){
    5. for(BlockState b : c.getTileEntities()){
    6. if(b instanceof Chest){
    7. Inventory inv = ((Chest) b).getBlockInventory();
    8. inv.clear();
    9. for (int i = 0; i <= 36; i++) {
    10. Random r = new Random();
    11. if (i == r.nextInt(36)) {
    12. inv.addItem(new ItemStack(Material.WOOL, 1), new ItemStack(Material.BOWL, 1), new ItemStack(Material.BED, 1), new ItemStack(Material.ARROW, 1), new ItemStack(Material.CAKE, 1), new ItemStack(Material.CARROT, 1));
    13. }
    14. }
    15. }
    16. }
    17. }
    18. }
     
  2. Offline

    qhenckel

    ok do you really want to clear all the loaded chests in a world? is that what you really want to happen?
    secondly you're random thingy will be weird it will randomly give the chest up to 36 of everything.
    cheers, Q
     
    Hellborn5456 likes this.
  3. Offline

    Milkywayz

    Add the items you want into a ItemStack array,
    Code:
    ItemStack[] array_instance = new ItemStack[]{new ItemStack(...), ...}
    Code:
    addItem(array_instance[random.nextInt(array_instance.length)])
    // Warning, Pseudocode
    Put that inside the for loop for how many items to add to the inventory.
    Of course you should be able to add an empty itemstack (Material.AIR) to the array for the chance of no item for that specific loop cycle.

    Also in your title you imply you're selecting random items from a list, however you didn't use a list in your code. If you want to have the ability to dynamically add and remove items from the possible items, use a list.
     
    Hellborn5456 likes this.
Thread Status:
Not open for further replies.

Share This Page