Solved Generating a cube with two corners.

Discussion in 'Plugin Development' started by bkleinman1, Aug 28, 2014.

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

    bkleinman1

    Right, so I am trying to make (Just out of my own enjoyment) something similar to the world edit selection, where someone can select two corners, and every block inside those corners will be turned into a different block.

    So far, I have this, but it does not work. This code below seems to only change a few blocks. ('pos1' being one corner, and 'pos2' being the second)
    Code:java
    1. public void setRegion(Location pos1, Location pos2, Player player) {
    2.  
    3. double minX;
    4. double maxX;
    5. double minY;
    6. double maxY;
    7. double minZ;
    8. double maxZ;
    9.  
    10. if (pos1.getX() > pos2.getX()) { minX = pos2.getX(); maxX = pos1.getX(); } else { minX = pos1.getX(); maxX = pos2.getX(); }
    11. if (pos1.getY() > pos2.getY()) { minY = pos2.getY(); maxY = pos1.getY(); } else { minY= pos1.getY(); maxY = pos2.getY(); }
    12. if (pos1.getZ() > pos2.getZ()) { minZ = pos2.getZ(); maxZ = pos1.getZ(); } else { minZ = pos1.getZ(); maxZ = pos2.getZ(); }
    13.  
    14. for (; minX < maxX; minX++) {
    15. for (; minY < maxY; minY++) {
    16. for (; minZ < maxZ; minZ++) {
    17. Location loc = new Location(player.getWorld(), minX, minY, minZ);
    18. loc.getBlock().setType(Material.DIAMOND_BLOCK);
    19. }
    20. }
    21. }
    22.  
    23. }


    I am not sure if this is the right approach to it, and if it is, I may have made a silly logic error, but if anyone could help me out, it would be much appreciated.
     
  2. Offline

    Flamedek

    bkleinman1
    Your general approach is good. But your loops are just wrong (does that even compile? )
    If you look at the Location you are creating, you are using the same values every time, so it will never get them all.

    You will need to create 3 new int values representing x, y and z and not change the min values.
    Luckly inside the for loop you can easly create those values that will range from the min to the max value.
    If you look at it it should make a lot of sence, it would look like this:
    Code:java
    1. for(int x = minX; x <= maxX; x++) {
    2. for(int y = minY; y <= maxY; y++) {
    3. for(int z = minZ; z <= maxZ; z++) {
    4. Location loc = new Location(world, x, y, z);
    5. // Do stuff
    6.  
    7. }
    8. }
    9. }
     
  3. Offline

    bkleinman1

    Ah, I see where I went wrong. Thank you for the help. :)
     
  4. Offline

    FerusGrim

    bkleinman1
    If this works, don't forget to mark it as solved! :D
     
Thread Status:
Not open for further replies.

Share This Page