Filling in cuboids with a certain block using the bukkit api

Discussion in 'Plugin Development' started by MinecraftManiac365, Aug 2, 2014.

Thread Status:
Not open for further replies.
  1. Have any of you found it odd that bukkit doesn't have a fill feature implemented into their API yet? I did. So instead of playing around with the (in my opinion) confusing world edit API, I decided to share a way to quickly complete this task with something in java called for loops.
    I am not the first one to make up this idea and this might not be the only thread explaining this.
    If you already know what for loops are just skip to when I am done explaining them.

    For loops tutorial:
    a for loop is a loop that keeps repeating until a certain task is done.
    Example
    Code:java
    1. for(int n=0;n=<10;n++){
    2. System.out.println(""+n);
    3. }

    The first part is the code that initializes the loop(a temporary variable created and used every time the loop is run. The second is the condition. The loop will repeat until that statement returns false. Make sure it eventually does or you will have an infinite loop on your hands and that is no good. The third part is what happens each time the loop is complete. Then in the brackets is what runs every time the the condition returns true.
    So this code would return:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    The second concept I need you to understand is nested for loops. So a for loop inside a for loop inside a for loop. This may sound very confusing at first but I will try my best to explain it.

    Lets imagine a cuboid from (0,0,0) to (5,5,5). What blocks would you need to fill in?

    Here is the answer: (0,0,1)(0,0,2)(0,0,3)(0,0,4)(0,0,5)(0,1,0)(0,1,1)...(0,5,5)(1,0,0)until(5,5,5)
    And here is how we would do it:
    Code:java
    1. for(int x=0;x<=5;x++){
    2. for(int y=0;y<=5;y++{
    3. for(int z=0; z<=5; z++){
    4. Bukkit.getWorld("world").getBlockAt(x,y,z).setType(Material.STONE);
    5. }
    6. }
    7. }

    Let me explain:
    The first loop creates integer x=0 and will keep running as long as x is less than or equal to 5. So at first x=0 right? Remember that all the values of these integers we are creating, we will need them later. Then since x=0 it is less than or equal to 5 and the code continues. In the code it starts another for loop that creates integer y=0 that will keep running as long as y is less than or equal to 5. So since y=0 it is less than or equal to five and the code continues. What is the code, well another for loop that declares integer z=0 and this for loop will keep running as long as z is less than or equal to 5; since z=0 z is less than or equal to five and the code runs, this time it is not a another for loop. By this time we have 3 integers(x, y, and z) that all equal zero. then the code says Bukkit.getWorld("world").getBlockAt(x,y,z).setType(Material.STONE); What this does is finds the 0,0,0 in the world and replaces it with stone. Next the z for loop has to finish so z goes up by one and we place a block at 0,0,1 all the way up to 5 and that loop is finished then we go back the y loop and the y value goes up 1 and starts another z loop so (0,y(1),z(1,2,3,4,5) and y finishes at 5 so (0,y(5),z(1,2,3,4,5)) and that finishes the first y loop but then the x loop has to go on so then x=1 and starts another y loop and each y loop triggers five z loops. until we have a 5x5x5 cube. If this still doesn't make any sense I will create a video on it soon but for now, I hope this helped!
     
    Totom3 likes this.
Thread Status:
Not open for further replies.

Share This Page