Remove blocks in 20 x 10 x 20 radius

Discussion in 'Plugin Development' started by CraftCreeper6, Sep 29, 2014.

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

    CraftCreeper6

    Hi! So I am starting to focus on Block Manipulation as I know hardly anything about it.
    Here is my code:
    Code:java
    1. public void removeBlocks(Player player) {
    2. int startx = player.getLocation().getBlockX();
    3. int starty = player.getLocation().getBlockY();
    4. int startz = player.getLocation().getBlockZ();
    5. int endx = startx + 20;
    6. int endy = starty + 10;
    7. int endz = startz + 20;
    8.  
    9. for (int x = startx; x < endx; x++) {
    10. for (int y = starty; y < endy; y++) {
    11. for (int z = startz; z < endz; z++) {
    12. Block block = player.getWorld().getBlockAt(x, y, z); //do what you want with the block
    13. block.setType(Material.AIR);
    14. }
    15. }
    16. }
    17. }


    I want this to remove blocks in a 20 x 10 x 20 radius. But the snippet of code above does not work.
     
  2. Offline

    AoH_Ruthless

    CraftCreeper6
    Basically your end and start locations are 2 corners, so what you are making is a rectangular prism.
    You have to have your center be a location. Store the x, y, and z of the center in their own variables. Increment x from storedx - 20 to storedx + 20, nested y loop doing same thing, nested z loop doing same thing. Then get that location and set it to air.
     
  3. Offline

    CraftCreeper6

    AoH_Ruthless
    Nevermind, fixed it with: How would I stop this happening though?: http://prntscr.com/4rjjd3
    Code:java
    1. public static void removeBlocks(Player player) {
    2. double startx = player.getLocation().getX();
    3. double starty = player.getLocation().getY();
    4. double startz = player.getLocation().getZ();
    5. double endx = startx + 20;
    6. double endy = starty + 10;
    7. double endz = startz + 20;
    8.  
    9. for (double x = startx; x < endx; x++) {
    10. for (double y = starty; y < endy; y++) {
    11. for (double z = startz; z < endz; z++) {
    12. Location loc = new Location(player.getWorld(), x, y, z);
    13. Block block = player.getWorld().getBlockAt(loc); //do what you want with the block
    14. block.setType(Material.AIR);
    15. }
    16. }
    17. }
    18. }
     
Thread Status:
Not open for further replies.

Share This Page