package eggdropsoap.spreadinglilypads;

import java.util.Random;

import net.minecraft.block.BlockLilyPad;
import net.minecraft.world.World;

public class BlockSpreadingLilyPad extends BlockLilyPad
{

	protected BlockSpreadingLilyPad(int id)
	{
		super(id);
	}
	
	// based on reading the code in net.minecraft.block.BlockMushroom
    public void updateTick(World world, int blockX, int blockY, int blockZ, Random rng)
    {
    	System.out.printf("Received update tick at %d %d %d\n", blockX, blockY, blockZ);
    	if (true)//rng.nextInt(25) == 0)
    	{
//    		System.out.println("Entering spreading attempt");
    		int checkDistance = 1;
    		int lilyCap = 5;
    		int i;
    		int j;
    		
    		// First count the number of Lily Pads in range and return if too many
    		for (i = blockX - checkDistance; i <= blockX + checkDistance; ++i)
    		{
        		for (j = blockZ - checkDistance; j <= blockZ + checkDistance; ++j)
        		{
        			if (world.getBlockId(i, blockY, j) == this.blockID)
        			{
        				--lilyCap;
        				if (lilyCap <= 0)
        				{
        					System.out.println("Too many lilies in range");
        					return;
        				}
        			}
        		}
    			
    		}
    		
    		// pick a spot to try to spread to
    		int newX = blockX + rng.nextInt(3) - 1;	// pick one of [-1, 0, 1] 
    		int newZ = blockZ + rng.nextInt(3) - 1;	// ditto
    		
//    		System.out.printf("Trying to spread to %d %d %d\n", newX, blockY, newZ);
    		
    		for (int attempts = 0; attempts < 4; ++attempts)
    		{
    			if (world.isAirBlock(newX, blockY, newZ) && this.canBlockStay(world, newX, blockY, newZ))
    			{
//    				System.out.println("This location is a valid target");

    				blockX = newX;
    				blockZ = newZ;
    			}
    			
        		// pick a spot to try to spread to
        		newX = blockX + rng.nextInt(3) - 1;	// pick one of [-1, 0, 1] 
        		newZ = blockZ + rng.nextInt(3) - 1;	// ditto

//        		System.out.printf("Trying to spread to %d %d %d\n", newX, blockY, newZ);
    			
    			if (world.isAirBlock(newX, blockY, newZ) && this.canBlockStay(world, newX, blockY, newZ))
        		{
    				System.out.println("Spreading!");
    				
    				// set the block location to a new Lily Pad with no metadata and a client update
        			world.setBlock(blockX, blockY, blockZ, this.blockID, 0, 2);
        		}
    		}
    			
    	}
    }
	
	

}

@@ -1235,608 +1235,220 @@
   %09%09
-// pick a spot to try to spread to%0A    %09%09int newX = blockX + rng.nextInt(3) - 1;%09// pick one of %5B-1, 0, 1%5D %0A    %09%09int newZ = blockZ + rng.nextInt(3) - 1;%09// ditto%0A    %09%09%0A//    %09%09System.out.printf(%22Trying to spread to %25d %25d %25d%5Cn%22, newX, blockY, newZ);%0A    %09%09%0A    %09%09for (int attempts = 0; attempts %3C 4; ++attempts)%0A    %09%09%7B%0A    %09%09%09if (world.isAirBlock(newX, blockY, newZ) && this.canBlockStay(world, newX, blockY, newZ))%0A    %09%09%09%7B%0A//    %09%09%09%09System.out.println(%22This location is a valid target%22);%0A%0A    %09%09%09%09blockX = newX;%0A    %09%09%09%09blockZ = newZ;%0A    %09%09%09%7D
+int newX; %0A    %09%09int newZ;%0A    %09%09%0A//    %09%09System.out.printf(%22Trying to spread to %25d %25d %25d%5Cn%22, newX, blockY, newZ);%0A    %09%09%0A    %09%09for (int attempts = 0; attempts %3C 4; ++attempts)%09// try four times to spread
 %0A    %09%09
-%09
-%0A        %09%09// pick a spot to try to spread to
+%7B
 %0A   
@@ -1573,18 +1573,16 @@
  ditto%0A%0A
-//
         
@@ -1587,96 +1587,8 @@
   %09%09
-System.out.printf(%22Trying to spread to %25d %25d %25d%5Cn%22, newX, blockY, newZ);%0A    %09%09%09%0A    %09%09%09
 if (
@@ -1673,31 +1673,149 @@
  newZ))%0A    
-    %09%09%7B
+%09%09%09%7B%0A//    %09%09%09%09System.out.println(%22This location is a valid target%22);%0A%0A    %09%09%09%09blockX = newX;%0A    %09%09%09%09blockZ = newZ;%0A    %09%09%09%09
 %0A    %09%09%09%09Sys
@@ -2014,16 +2014,17 @@
  0, 2);%0A
+%0A
         
@@ -2029,18 +2029,39 @@
   %09%09
-%7D%0A    %09%09%7D%0A
+%09break;%0A    %09%09%09%7D    %09%09%09%0A    %09%09%7D
     

Simplify spreading logic