Circles and Structures

Discussion in 'Plugin Development' started by Ahniolator, Oct 9, 2011.

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

    Ahniolator

    I use the following code to get all of the blocks in a square, and change them. I was wondering what I would do if I wanted to do this in a circle.
    Code:java
    1. stringRadius = args[1];
    2. radius = Integer.valueOf(stringRadius);
    3. float xy = radius / 2;
    4. xv = Math.round(xy);
    5. zv = Math.round(xy);
    6. World world = player.getWorld();
    7. for (int x = -xv; x < radius - xv; x++) {
    8. for (int z = -zv; z < radius - zv; z++) {
    9. Block b = world.getHighestBlockAt(player.getLocation().getBlockX() + x, player.getLocation().getBlockZ() + z);
    10. while (true) {
    11. if (b.getLocation().getY() <= 2) {
    12. break; //Just in case it reaches the bottom of the world (inf. loop)
    13. }
    14. Material type = b.getType();
    15. if (type == Material.LEAVES) {
    16. b.setType(Material.AIR);
    17. } else if (type == Material.GRASS) {
    18. b.setType(Material.DIRT);
    19. } else if (type == Material.AIR) {
    20. //to prevent it exiting the loop in gaps
    21. } else if (type == Material.TORCH) {
    22. b.setType(Material.AIR);
    23. ItemStack item = new ItemStack(Material.TORCH, 1);
    24. world.dropItem(b.getLocation(), item);
    25. } else if (type == Material.BROWN_MUSHROOM) {
    26. b.setType(Material.AIR);
    27. ItemStack item = new ItemStack(Material.BROWN_MUSHROOM, 1);
    28. world.dropItem(b.getLocation(), item);
    29. } else if (type == Material.RED_MUSHROOM) {
    30. b.setType(Material.AIR);
    31. ItemStack item = new ItemStack(Material.RED_MUSHROOM, 1);
    32. world.dropItem(b.getLocation(), item);
    33. } else if (type == Material.YELLOW_FLOWER) {
    34. b.setType(Material.AIR);
    35. ItemStack item = new ItemStack(Material.YELLOW_FLOWER, 1);
    36. world.dropItem(b.getLocation(), item);
    37. } else if (type == Material.RED_ROSE) {
    38. b.setType(Material.AIR);
    39. ItemStack item = new ItemStack(Material.RED_ROSE, 1);
    40. world.dropItem(b.getLocation(), item);
    41. } else if (type == Material.LONG_GRASS) {
    42. b.setType(Material.AIR);
    43. } else if (type == Material.LOG) {
    44. } else if (type == Material.AIR) {
    45. } else if (type == Material.VINE) {
    46. b.setType(Material.AIR);
    47. } else if (type == Material.STONE) {
    48. } else if (type == Material.DIRT) {
    49. } else if (type == Material.HUGE_MUSHROOM_1) {
    50. } else if (type == Material.HUGE_MUSHROOM_2) {
    51. } else {
    52. break;
    53. }
    54. b = b.getRelative(BlockFace.DOWN);
    55. }
    56. }
    57. }


    Also, if I wanted to insert a structure into the world that overwrites terrain, how would I save the overwritten terrain, protect all of the blocks in the structure from being broken/having other blocks placed on them, and whenever said structure is removed from the world to replace the overwritten terrain?
     
  2. Offline

    nickrak

    just add this between lines 8-9 above:

    Code:java
    1. final Location p = player.getLocation();
    2. final Location t = p.add(x, 0, z);
    3. if (p.distance(t) > radius)
    4. {
    5. continue;
    6. }
     
Thread Status:
Not open for further replies.

Share This Page