Need Help Iterating Through Blocks, Please.

Discussion in 'Plugin Development' started by aciid, Jul 11, 2012.

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

    aciid

    Hi, currently I'm trying to iterate between each block in a region and if it is wool, replace it with grass. The plugin uses worlguard and worldedit so I thought I would be fine with the following code but it doesn't work exactly. As you see below that's what happens when I try iterate through all blocks in the region (the red square) and turn each to obsidian as a test.
    [​IMG]

    Code: (I was testing it by trying to change all blocks within the region to obsidian)

    Code:
    try {
        Vector nwB = new BlockVector(nw.getBlockX(),nw.getBlockY(),nw.getBlockZ());
        Vector neB= new BlockVector(ne.getBlockX(),ne.getBlockY(),ne.getBlockZ());
        Region temp = new CuboidRegion(nwB,neB);
     
        Iterator<BlockVector> rs = temp.iterator();
        while (rs.hasNext()) {
            player.sendMessage("rs: "+rs.next().toString());
     
            Location loc = new Location(player.getWorld(),rs.next().getX(),rs.next().getY(),rs.next().getZ());
            Block block = player.getWorld().getBlockAt(loc);
            block.setType(Material.OBSIDIAN);
            /**
            if (block.getType() == Material.WOOL){
                block.setType(Material.GRASS);
            }*/
        }
    } catch (Exception e) { e.printStackTrace(); }
    }
    
    Someone please help point me in the right direction!
     
  2. Offline

    Father Of Time

    You can't keep calling "rs.next()" within the while loop, each time you do it grabs the "next" object in the list, not the same one. Instead use it once at the beginning of the iteration and assign it to a local variable, then use that variable over and over instead of calling .next().

    Code:
    BlockVector blockvec = rs.next();
    blockvec.
    
    Hope this helps, good luck!
     
    aciid likes this.
  3. Offline

    aciid

    Makes sense haha thank you, father!

    Edit: Worked!
     
  4. Offline

    Father Of Time

    Fantastic, I am happy to hear you were able to resolve your issue.

    Good luck with the remainder of your project. :D
     
Thread Status:
Not open for further replies.

Share This Page