Random blocks in squared area

Discussion in 'Spigot Plugin Development' started by EtharraNetwork, Jun 18, 2016.

  1. Hi there, I'm trying to solve an issue that I am facing. I'm trying to make the code below so that it spawns blocks in the squared are which I've already completed although I'm wanting to make it so that within that squared area, there are more than one different types of material (Stone, Iron Ore). How would I do this?

    Code (Text):
    max.getWorld().getBlockAt(x, y, z).setType();
     
  2. Make a list of materials you want to possibly show up, then generate a random number between 0 and the size of the list minus 1 and use list.get(randomnum) to get a random material from the list

    You can use ThreadLocalRandom.current().nextInt(<max>) to generate a random int between 0 and the max.
     
  3. How would I make it so that within that list the materials will spawn in different positions within that area?
     
  4. Generate random x, y, and z numbers using the same method I described above. Or loop through them with 3x nested for loop.
     
  5. This what you mean?
    Code (Text):
            Material[] mat = {Material.EMERALD_BLOCK, Material.DIAMOND_BLOCK, Material.GOLD_BLOCK};
            Random ran = new Random();
            int choice = ran.nextInt(mat.length);
            max.getWorld().getBlockAt(x, y, z).setType(Material.valueOf("" + mat[choice]));
     
    • Make an array with all the materials you want
    • Loop through all blocks within 2 points
    • Pick a random int, max value at your array size - 1
    • Get a material from the array using the random int
    • Set the block type
     
  6. Yes. But put ran.nextInt(mat.length - 1), otherwise it might generate a 3 sometimes which would throw an ArrayIndexOutOfBoundsException.
    Also you should define the material array somewhere statically rather than defining it every time this code runs.
     
    • Like Like x 1
  7. How could I select randoms positions in the x, y, z and set them as a random block from the array?

    Edit: Just need more of a brief explanation as that's what I'm mainly struggling to accomplish.
     
  8. Generate a random int for each x, y, and z and do getBlockAt(randomX, randomY, randomZ)
     
  9. If I set a random int for each of them coordinated, it won't spawn the blocks in that area that we set the positions for nor will it also spawn different types of blocks from the array.
     
  10. A better example of what I'm trying to do is have a prison mine.