Simple question about scanning blocks that I can't think of

Discussion in 'Plugin Development' started by skipperguy12, Mar 2, 2013.

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

    skipperguy12

    Alright, many of you know already that i'm horrible at thinking of ways to do things, and now is one of those moments.

    What I have: a lava block, and obsidian around it.

    What I need to do: Turn all the obsidian around the lava to another block

    I have the lava's x, y, and z as doubles, but now how do I scan the blocks around? It would be preferred if there is a simple int variable to control how far it scans (Kind of like scanning a sphere inside of a cuboid, if that makes any sense.)
     
  2. Offline

    caseif

    Two for loops, one for the x, and one for the z. Start at the lava's coords minus the radius, and end at the lava's coords plus the radius.
     
  3. Offline

    tom1000o

    BUT, if its a 3x3 cube, then put a 3rd for loop around those 2 existing loops for the y value.
     
  4. Offline

    caseif

    Ehh... you'd need to write up some relatively complex algorithms for that (relative to just using loops).
     
  5. Offline

    frozenpoptartmc

    Code:java
    1.  
    2. if(block.getRelative(BlockFace.NORTH).getType() == Material.OBSIDIAN){
    3. }
    4. if(block.getRelative(BlockFace.EAST).getType() == Material.OBSIDIAN){
    5. }
    6. if(block.getRelative(BlockFace.SOUTH).getType() == Material.OBSIDIAN){
    7. }
    8. if(block.getRelative(BlockFace.WEST).getType() == Material.OBSIDIAN){
    9. }
    10.  


    or something similar, can't try it. :p
     
  6. Offline

    Netizen

    What you might be able to do is use an array of offsets to check each direction around the location using getRelative().

    This is what I use to find adjacent blocks:
    Code:
    /*
      * If 0,0 is the origin of the block, search each of these coords for what we are
      * looking for.
      */
    int[][] offset = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 }, {1, 1}, {-1, -1}, {-1, 1}, {1, -1} };
    for (int i = 0; i < offset.length; i++) {
        Block next  = loc.getRelative(offset[i][0], 0, offset[i][1]);
        //test block for w/e we want...
    }
    
    If we want to do a radius, add another loop and a multiplier like this:
    Code:
    /*
      * If 0,0 is the origin of the block, search each of these coords for what we are
      * looking for.
      */
    int[][] offset = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 }, {1, 1}, {-1, -1}, {-1, 1}, {1, -1} };
    for (int r = 1; r < radius; r++) {
        for (int i = 0; i < offset.length; i++) {
            Block next  = loc.getRelative(offset[i][0]*r, 0, offset[i][1]*r);
            //test block for w/e we want...
     
        }
    }
    
    Haven't really tested it, but I don't see why we couldn't extend this to 3D like this:
    Code:
    /*
      * If 0,0 is the origin of the block, search each of these coords for what we are
      * looking for.
      */
    int[][] offset = { { -1,0, 0 }, { 1,0, 0 }, { 0, 0, -1 }, { 0, 0, 1 }, {1,0, 1}, {-1, 0, -1}, {-1, 0, 1}, {1, 0,-1},
                                  { -1,1, 0 }, { 1,1, 0 }, { 0, 1, -1 }, { 0, 1, 1 }, {1,1, 1}, {-1, 1, -1}, {-1, 1, 1}, {1, 1,-1},
                                  { -1,-1, 0 }, { 1,-1, 0 }, { 0, -1, -1 }, { 0, -1, 1 }, {1,-1, 1}, {-1, -1, -1}, {-1, -1, 1}, {1, -1,-1}};
    for (int r = 1; r < radius; r++) {
        for (int i = 0; i < offset.length; i++) {
            Block next  = loc.getRelative(offset[i][0]*r, offset[i][1], offset[i][2]*r);
            //test block for w/e we want...
     
        }
    }
    
     
  7. Offline

    skipperguy12

    Figured it out myself.

    frozenpoptartmc
    I did this before I read AngryNerd post.

    Wound up with 48,357 lines of code. I even hit the Java method byte limit!
    Never use this...You need to:
    Check N, S, E, W, NE, SE, SW, NW, NU, ND, SU, SD, EU, ED, NEU, NED, SEU, SED, SWU, SWD, NWU, NWU

    And do that for layer 1, then layer two you need to copy and paste that, and I had 4 layers...

    Better solution than all of you up there:
    Code:
    int radius = 7;
                                final Block block =blah.getBlock(); //placed block
                                for (int x = -(radius); x <= radius; x ++){
                                  for (int y = -(radius); y <= radius; y ++) {
                                    for (int z = -(radius); z <= radius; z ++) {
                                  //Stuff
                                    }
                                  }
                                }
     
  8. Offline

    caseif

    GEE. ZUS.
     
  9. Offline

    Tirelessly

    Recursion!

    Code:java
    1.  
    2. void changeObsid(Block block) {
    3. if (!block.getType().equals(Material.OBSIDIAN)) {
    4. return;
    5. }
    6. block.setType(Material.LAVA);
    7. for (BlockFace bf : BlockFace.values()) {
    8. changeObsid(block.getRelative(bf));
    9. }
    10.  
    11. }


    skipperguy12 I highly recommend this instead of 48,000 lines of copy/pasted code..
     
Thread Status:
Not open for further replies.

Share This Page