001    package net.minecraft.src;
002    
003    import java.util.Random;
004    
005    public class BlockSand extends Block
006    {
007        /** Do blocks fall instantly to where they stop or do they fall over time */
008        public static boolean fallInstantly = false;
009    
010        public BlockSand(int par1, int par2)
011        {
012            super(par1, par2, Material.sand);
013            this.setCreativeTab(CreativeTabs.tabBlock);
014        }
015    
016        public BlockSand(int par1, int par2, Material par3Material)
017        {
018            super(par1, par2, par3Material);
019        }
020    
021        /**
022         * Called whenever the block is added into the world. Args: world, x, y, z
023         */
024        public void onBlockAdded(World par1World, int par2, int par3, int par4)
025        {
026            par1World.scheduleBlockUpdate(par2, par3, par4, this.blockID, this.tickRate());
027        }
028    
029        /**
030         * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
031         * their own) Args: x, y, z, neighbor blockID
032         */
033        public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
034        {
035            par1World.scheduleBlockUpdate(par2, par3, par4, this.blockID, this.tickRate());
036        }
037    
038        /**
039         * Ticks the block if it's been scheduled
040         */
041        public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
042        {
043            if (!par1World.isRemote)
044            {
045                this.tryToFall(par1World, par2, par3, par4);
046            }
047        }
048    
049        /**
050         * If there is space to fall below will start this block falling
051         */
052        private void tryToFall(World par1World, int par2, int par3, int par4)
053        {
054            if (canFallBelow(par1World, par2, par3 - 1, par4) && par3 >= 0)
055            {
056                byte var8 = 32;
057    
058                if (!fallInstantly && par1World.checkChunksExist(par2 - var8, par3 - var8, par4 - var8, par2 + var8, par3 + var8, par4 + var8))
059                {
060                    if (!par1World.isRemote)
061                    {
062                        EntityFallingSand var9 = new EntityFallingSand(par1World, (double)((float)par2 + 0.5F), (double)((float)par3 + 0.5F), (double)((float)par4 + 0.5F), this.blockID, par1World.getBlockMetadata(par2, par3, par4));
063                        this.onStartFalling(var9);
064                        par1World.spawnEntityInWorld(var9);
065                    }
066                }
067                else
068                {
069                    par1World.setBlockWithNotify(par2, par3, par4, 0);
070    
071                    while (canFallBelow(par1World, par2, par3 - 1, par4) && par3 > 0)
072                    {
073                        --par3;
074                    }
075    
076                    if (par3 > 0)
077                    {
078                        par1World.setBlockWithNotify(par2, par3, par4, this.blockID);
079                    }
080                }
081            }
082        }
083    
084        /**
085         * Called when the falling block entity for this block is created
086         */
087        protected void onStartFalling(EntityFallingSand par1EntityFallingSand) {}
088    
089        /**
090         * How many world ticks before ticking
091         */
092        public int tickRate()
093        {
094            return 5;
095        }
096    
097        /**
098         * Checks to see if the sand can fall into the block below it
099         */
100        public static boolean canFallBelow(World par0World, int par1, int par2, int par3)
101        {
102            int var4 = par0World.getBlockId(par1, par2, par3);
103    
104            if (var4 == 0)
105            {
106                return true;
107            }
108            else if (var4 == Block.fire.blockID)
109            {
110                return true;
111            }
112            else
113            {
114                Material var5 = Block.blocksList[var4].blockMaterial;
115                return var5 == Material.water ? true : var5 == Material.lava;
116            }
117        }
118    
119        /**
120         * Called when the falling block entity for this block hits the ground and turns back into a block
121         */
122        public void onFinishFalling(World par1World, int par2, int par3, int par4, int par5) {}
123    }