Getting all blocks of same type that are touching

Discussion in 'Plugin Development' started by Valrix, Mar 6, 2011.

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

    Valrix

    I've been trying to think of a way to get all blocks that have the same type and are touching regardless of how oddly shaped or big it is as long as it's not so big that it's in an unloaded chunk. Anyone have ideas of how to do this?
     
  2. Offline

    Afforess

    Recursion sounds like the easiest way.
     
  3. Offline

    Valrix

    Any code you can post to help figure it out?
     
  4. Offline

    Afforess

    Small, Compact, and Recursive. Not recommended for large groups of blocks.

    Code:
    public void checkBlocks(int typeId, Block block) {
    	if (block.getTypeId() == typeId) {
    		ListOfBlocks.add(block);
    		int range = 1;
    		for (int dx = -(range); dx <= range; dx++){
    			for (int dy = -(range); dy <= range; dy++){
    				for (int dz = -(range); dz <= range; dz++){
    					checkBlocks(typeId, block.getRelative(dx, dy, dz));
    				}
    			}
    		}
    	}
    }
    
     
  5. Offline

    Yogoda

    I think you forgot to add the check if the block have already been visited, otherwise the algorithm will never end.
     
  6. Offline

    nickguletskii

    I would also start it in a different thread. Someone would be "smart" enough to go through rock in the ground.
     
Thread Status:
Not open for further replies.

Share This Page