Get the whole stream of water or lava

Discussion in 'Plugin Development' started by blackwolf12333, Nov 9, 2012.

Thread Status:
Not open for further replies.
  1. Hi
    I want to get the whole water stream, thus, I want to have a list of all the blocks of water that are connected to each other starting with a source block.
    I tried some stuff with recursion, but I am not really good with that:(
    I hoped that someone could help me with this.
    andf54 I know you know some stuff about recursion, maybe you can help me?

    Thanks in advance, blackwolf12333
    (If you guys need the stuff i already tried, just say so.)
     
  2. Offline

    andf54

    Create a method
    Code:
    public void collectBlocks(Block anchor, HashSet<Block> collected){
     
       if(!blockIsWater) return; // Ignore non water blocks.
     
       if(collected.contains(anchor)) return; // Important! Prevents loop of death.
       collected.add(anchor); // Add this block (anchor) to collected list.
     
       // Move to adjacent block and repeat the procedure by using this same method (recursion):
       collectBlocks(anchor.getRelative(BlockFace.NORTH_EAST), collected);
       collectBlocks(anchor.getRelative(BlockFace.NORTH), collected);
       etc;
     
    }
    
    Recursion is a method that calls itself.
    It basically starts with one block and adds it to the collection. Then the method fires itself on adjacent blocks which get added to the collection. For adjacent blocks it again fires itself on adjacent of adjacent blocks. It basically creates a landslide. If not stopped it will loop until the end of time.
     
  3. Yeah, i knew the theory bit, but can that last part of your code be done in a for loop looping through all the values of BlockFace?
     
  4. Offline

    andf54

    No, because BlockFace has non adjacent values. For example: BlockFace.NORTH_NORTH_EAST might give you two water streams that are not connected.

    You only need 10 BlockFace values. It shouldn’t be a problem.
     
  5. Hmm ok, well thanks alot :D
     
Thread Status:
Not open for further replies.

Share This Page