Selecting an area

Discussion in 'Plugin Development' started by instipod, Aug 9, 2011.

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

    instipod

    How would I have a user select two points and get the blocks inside that selection (cuboid)?
     
  2. Offline

    Vynlar

    Use loops to loop through all the blocks in the selection. Always store 2 positions, opposite corners, and the world the selections are in.
     
  3. Offline

    DrBowe

    @Vynlar
    I just tried to sit down and sketch up a rough function to do this, and I've run into a small problem: How to identify which point to start at.

    If you have 2 points...lets say -1, -1 and 1, 1
    It's easy to tell which direction to loop in.

    However, when given -1, 2 and 1, 1
    It becomes more difficult.

    I can't seem to write the loops without several if statements (to decide which point ot start at)
    I'm assuming that I'm missing an important concept here, because I shouldnt have 4 slightly different blocks of code just to handle each 'point possibility'
     
  4. Offline

    Shamebot

    Just use Math.min and Math.max, to loop from the point with the to smallest componets to the point with the to biggest.
    -1,2 and 1,1 is the same cuboid as -1,1 and 1,2
     
  5. Offline

    instipod

  6. Offline

    Shamebot

    Code:java
    1. private void loopThrough(Location loc1, Location loc2)
    2. {
    3. int minx = Math.min(loc1.getBlockX(), loc2.getBlockX()),
    4. miny = Math.min(loc1.getBlockY(), loc2.getBlockY()),
    5. minz = Math.min(loc1.getBlockZ(), loc2.getBlockZ()),
    6. maxx = Math.max(loc1.getBlockX(), loc2.getBlockX()),
    7. maxy = Math.max(loc1.getBlockY(), loc2.getBlockY()),
    8. maxz = Math.max(loc1.getBlockZ(), loc2.getBlockZ());
    9. for(int x = minx; x<=maxx;x++)
    10. {
    11. for(int y = miny; y<=maxy;y++)
    12. {
    13. for(int z = minz; z<=maxz;z++)
    14. {
    15. //do stuff
    16. }
    17. }
    18. }
    19. }
     
    Maulss, Tim Visee and dkramer like this.
  7. Offline

    DrBowe

    Disregard that, I was ninja'd by a much better code block :)
     
  8. Offline

    Shamebot

    I wish Location had a getMinimum(Location loc1, Location loc2) like Vector.
     
  9. Offline

    instipod

    @Shamebot,
    The code you gave I cannot get to work. Also, I want it to look for blocks all the way from bedrocks to sky limit not the heights between the two locations.
     
  10. Offline

    Shamebot

    Code:java
    1. private void loopThrough(Location loc1, Location loc2)
    2. {
    3. int minx = Math.min(loc1.getBlockX(), loc2.getBlockX()),
    4. minz = Math.min(loc1.getBlockZ(), loc2.getBlockZ()),
    5. maxx = Math.max(loc1.getBlockX(), loc2.getBlockX()),
    6. maxz = Math.max(loc1.getBlockZ(), loc2.getBlockZ());
    7. for(int x = minx; x<=maxx;x++)
    8. {
    9. for(int y = 0; y<128;y++)
    10. {
    11. for(int z = minz; z<=maxz;z++)
    12. {
    13. //do stuff
    14. }
    15. }
    16. }
    17. }
     
  11. Offline

    instipod

    Never mind, I got it to work. Thanks!
     
  12. Offline

    toothplck1

    What exactly do you need Every single block for. Like what are you going to do to them?
     
Thread Status:
Not open for further replies.

Share This Page