Detecting all Wood and Leave Blocks in Tree

Discussion in 'Plugin Development' started by SoThatsIt, Dec 15, 2013.

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

    SoThatsIt

    Hey, for a plugin i am making i need to detect all the wood and leave blocks in a tree and then save their locations, what is an efficient way i could do this?
     
  2. Offline

    NathanWolf

    You could make an empty HashSet of locations and pass it to a recursive function. That function would start at a block and check if it's wood or leaves. If it is, store the location and then look at the neighboring blocks. If the block/location are not in your set, recurse. You may also want to track recursion depth and make sure it doesn't go crazy, I could see a jungle or canopy forest being a mess!

    I hope that makes sense.
     
  3. Offline

    SoThatsIt

    yeah, that does make sense, ill give that a try

    ok, i got it to work, here is how i did it, note you can change the material it changes the blocks to, to anything but wood and leaves, if you want to keep the wood and leaves, after its found all of the blocks just iterate through all the treeblocks and use TreeBlock.restore()

    Code:
    public class Tree
    {
       
        private static final int MAX_TIMEOUT = 500;
        private int timeout = 0;
        private List< TreeBlock > blocks;
       
        public Tree( Location loc )
        {
            blocks = new ArrayList< TreeBlock >();
            findBlocks(loc);
        }
       
        public void findBlocks(Location loc)
        {
            timeout++;
           
            loc.getBlock().setType(Material.DIAMOND_BLOCK);
            blocks.add(new TreeBlock(loc.getBlock()));
           
            if ( timeout < MAX_TIMEOUT )
            {
                List< Block > blocks = new ArrayList< Block >();
                for ( Direction dir : Direction.values() )
                {
                    Block b = dir.getRelative(loc).getBlock();
                    if ( b.getType() == Material.LEAVES || b.getType() == Material.LOG )
                    {
                        blocks.add(b);
                    }
                }
               
                for ( Block b : blocks )
                {
                    findBlocks(b.getLocation());
                }
            }
        }
       
        class TreeBlock
        {
            private Location location;
            private Material type;
            private byte data;
           
            public TreeBlock( Block b )
            {
                this.location = b.getLocation();
                this.type = b.getType();
                this.data = b.getData();
            }
           
            public void restore()
            {
                Block b = location.getBlock();
                b.setType(type);
                b.setData(data);
            }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
    NathanWolf likes this.
Thread Status:
Not open for further replies.

Share This Page