Chest set random items?

Discussion in 'Spigot Plugin Development' started by Frealy1995, Jul 2, 2016.

  1. Hello, I search to find a Chest set items. And I have a problems, how to random (inventory) set item?

    Code (Text):
            for (Chunk chunk : Bukkit.getServer().getWorld("world").getLoadedChunks()) {
                for (BlockState entities : chunk.getTileEntities()) {
                    if (entities instanceof Chest) {
                        Inventory inv = ((Chest) entities).getInventory();
                        inv.setContents(/*ItemStack[] of items here*/);
                    }
                }
            }
    Code thread: https://www.spigotmc.org/threads/fi...t-in-items-from-a-config.111479/#post-1198029
     
  2. FormallyMyles

    Supporter

    Have an array of Materials,

    Material[] material = new Material[]{Material.DIRT, Material.STONE};

    Then use java random to make new items and add it to the inventory.

    Random random = new Random();

    for(int i = 0;i<10;i++) {
    inv.add(new ItemStack(material[random.nextInt(material.length)]));
    }

    You can find similar stuff here, https://bukkit.org/threads/random-items-in-chests.119418/
     
    • Agree Agree x 1
  3. I don't undersantd?

    My Code:

    Code (Text):

                    for (Chunk chunk : Bukkit.getServer().getWorld("world").getLoadedChunks()) {
                       for (BlockState blockstate : chunk.getTileEntities()) {
                           if (blockstate instanceof Chest) {
                               Inventory inv = ((Chest) blockstate).getInventory();
                               ItemStack[] a = {new ItemStack(Material.GOLDEN_APPLE, 1), new ItemStack(Material.IRON_CHESTPLATE, 1), new ItemStack(Material.BOW, 1)};
                               ItemStack[] b = {new ItemStack(Material.BREAD, 2), new ItemStack(Material.DIAMOND_SWORD, 1), new ItemStack(Material.IRON_LEGGINGS, 1)};
                               ItemStack[] c = {new ItemStack(Material.POTION, 1, (byte) 8290), new ItemStack(Material.POTION, 1, (byte) 16385), new ItemStack(Material.IRON_HELMET)};
                               ItemStack[] d = {new ItemStack(Material.GOLDEN_APPLE, 2), new ItemStack(Material.IRON_BOOTS, 1), new ItemStack(Material.CHAINMAIL_LEGGINGS, 1)};
                               Random random = new Random();
                              int value = random.nextInt(4);
                              for(int i = 0; i < 10; i++){
                                 
                              }
                           }
                       }
                   }
     
     
  4. FormallyMyles

    Supporter

    So in your loop you want to pick random items, inv.add(d[random.nextInt(d.length)]); would add a new item to their inventory from the d array, (random.nextInt selects a random number up to the input which the input is the length of d)
     
    • Agree Agree x 1
  5. :D I don't understand
     
  6. FormallyMyles

    Supporter

    So you want to in your loop add random items.

    You need to first pick a random item from one of your lists of items, so you need to know what number to pick.

    random.nextInt() picks a number, you need to tell it how many items in the list, random.nextInt(a.length), tells it to pick a number between 0 and the maximum for the list.

    So,

    ItemStack toGive = a[random.nextInt(a.length)];

    Would select an item from your a list, using random. You then need to add it to the inventory using inv.addItem(toGive);
     
  7. length not to size(); changed and not working

    [​IMG]
     
  8. FormallyMyles

    Supporter

    You just changed from arrays to lists. If you want to use a list use list.get(random.nextInt...);
     
    • Like Like x 1
  9. Thanks