Solved Iterate through each block in a region

Discussion in 'Plugin Development' started by teozfrank, Jun 2, 2014.

Thread Status:
Not open for further replies.
  1. Hi guys,

    Say if I had two locations.

    How do I interate through the both locations in a region to check to see is each block the same?

    I want to select a region and check to see within that region selected are there only wall signs.


    Frank.
     
  2. Offline

    XLordalX

    Loop through two locs:

    Code:java
    1.  
    2. Location loc1 = new Location();
    3. Location loc2 = new Location();
    4. int x = loc1.getBlockX();
    5. int y = loc1.getBlockY();
    6. int z = loc1.getBlockZ();
    7. int maxX = loc2.getBlockX();
    8. int maxZ = loc2.getBlockZ();
    9.  
    10. for(int x1 = x; x1 < maxX; x1++)
    11. {
    12. for(int z1 = z; z1 < maxZ; z1++)
    13. {
    14. Location loc = new Location(x1, y, z1)
    15. //Do what you want
    16. }
    17. }
     
  3. Offline

    mythbusterma

    Did you completely forget the Y-axis?
     
    NathanWolf likes this.
  4. yep that would explain why it doesnt work lol

    does not work.
     
  5. Offline

    mythbusterma

    You have to use your own Locations, not new Location(). In this case they are any two corners of the region. Then you have to find the minimum and maximum vectors from those, then iterate over all three axis like so:

    Code:java
    1.  
    2. Location point1 = <yourfirstlocation>;
    3. Location point2 = <yoursecondlocation>;
    4. Vector max = Vector.getMaximum(point1.toVector, point2.toVector());
    5. Vector min = Vector.getMinimum(point1.toVector(), point2.toVector());
    6. for (int i = min.getBlockX(); i <= max.getBlockX();i++) {
    7. for (int j = min.getBlockY(); j <= max.getBlockY(); j++) {
    8. for (int k = min.getBlockZ(); k <= max.getBlockZ();k++) {
    9. Block block = Bukkit.getServer().getWorld(<worldname>).getBlockAt(i,j,k);
    10. // do what you want because a pirate is free
    11. }
    12. }
    13. }
     
    NathanWolf likes this.
  6. Offline

    XLordalX

    mythbusterma Yes I forgot the Y-axis because I copied this code from my project I am working on which doesn't need the Y-axis. Also I don't see why new Location() wouldn't work?
     
  7. Offline

    mythbusterma

    Because new Location() is a blank location not guarenteed to be in any particular place, you have to make it the specific points your looking for (that was more of a pointer to him, as it does work just fine for example code).
     
  8. Thank you! here is my solution for what I want guys:

    Code:java
    1. public boolean isWallSign(Block block) {
    2.  
    3. if(block.getType().equals(Material.WALL_SIGN)) {
    4. return true;
    5. }
    6. return false;
    7. }
    8.  
    9. public boolean isRegionAllWallSigns(Location pos1, Location pos2) {
    10. String world = pos1.getWorld().getName();
    11. int totalBlockCount = 0;
    12. int totalSignCount = 0;
    13.  
    14. Vector max = Vector.getMaximum(pos1.toVector(), pos2.toVector());
    15. Vector min = Vector.getMinimum(pos1.toVector(), pos2.toVector());
    16. for (int i = min.getBlockX(); i <= max.getBlockX();i++) {
    17. for (int j = min.getBlockY(); j <= max.getBlockY(); j++) {
    18. for (int k = min.getBlockZ(); k <= max.getBlockZ();k++) {
    19. Block block = Bukkit.getServer().getWorld(world).getBlockAt(i,j,k);
    20. if(isWallSign(block)) {
    21. totalSignCount++;
    22. }
    23. totalBlockCount++;
    24. }
    25. }
    26. }
    27.  
    28. if(totalBlockCount == totalSignCount) {
    29. return true;
    30. }
    31.  
    32. return false;
    33. }
     
    mythbusterma likes this.
Thread Status:
Not open for further replies.

Share This Page