001    package net.minecraft.block;
002    
003    import java.util.List;
004    import java.util.Random;
005    import net.minecraft.entity.Entity;
006    import net.minecraft.entity.item.EntityMinecart;
007    import net.minecraft.util.AxisAlignedBB;
008    import net.minecraft.world.IBlockAccess;
009    import net.minecraft.world.World;
010    
011    public class BlockDetectorRail extends BlockRail
012    {
013        public BlockDetectorRail(int par1, int par2)
014        {
015            super(par1, par2, true);
016            this.setTickRandomly(true);
017        }
018    
019        /**
020         * How many world ticks before ticking
021         */
022        public int tickRate()
023        {
024            return 20;
025        }
026    
027        /**
028         * Can this block provide power. Only wire currently seems to have this change based on its state.
029         */
030        public boolean canProvidePower()
031        {
032            return true;
033        }
034    
035        /**
036         * Triggered whenever an entity collides with this block (enters into the block). Args: world, x, y, z, entity
037         */
038        public void onEntityCollidedWithBlock(World par1World, int par2, int par3, int par4, Entity par5Entity)
039        {
040            if (!par1World.isRemote)
041            {
042                int var6 = par1World.getBlockMetadata(par2, par3, par4);
043    
044                if ((var6 & 8) == 0)
045                {
046                    this.setStateIfMinecartInteractsWithRail(par1World, par2, par3, par4, var6);
047                }
048            }
049        }
050    
051        /**
052         * Ticks the block if it's been scheduled
053         */
054        public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
055        {
056            if (!par1World.isRemote)
057            {
058                int var6 = par1World.getBlockMetadata(par2, par3, par4);
059    
060                if ((var6 & 8) != 0)
061                {
062                    this.setStateIfMinecartInteractsWithRail(par1World, par2, par3, par4, var6);
063                }
064            }
065        }
066    
067        /**
068         * Returns true if the block is emitting indirect/weak redstone power on the specified side. If isBlockNormalCube
069         * returns true, standard redstone propagation rules will apply instead and this will not be called. Args: World, X,
070         * Y, Z, side. Note that the side is reversed - eg it is 1 (up) when checking the bottom of the block.
071         */
072        public boolean isProvidingWeakPower(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
073        {
074            return (par1IBlockAccess.getBlockMetadata(par2, par3, par4) & 8) != 0;
075        }
076    
077        /**
078         * Returns true if the block is emitting direct/strong redstone power on the specified side. Args: World, X, Y, Z,
079         * side. Note that the side is reversed - eg it is 1 (up) when checking the bottom of the block.
080         */
081        public boolean isProvidingStrongPower(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
082        {
083            return (par1IBlockAccess.getBlockMetadata(par2, par3, par4) & 8) == 0 ? false : par5 == 1;
084        }
085    
086        /**
087         * Update the detector rail power state if a minecart enter, stays or leave the block.
088         */
089        private void setStateIfMinecartInteractsWithRail(World par1World, int par2, int par3, int par4, int par5)
090        {
091            boolean var6 = (par5 & 8) != 0;
092            boolean var7 = false;
093            float var8 = 0.125F;
094            List var9 = par1World.getEntitiesWithinAABB(EntityMinecart.class, AxisAlignedBB.getAABBPool().addOrModifyAABBInPool((double)((float)par2 + var8), (double)par3, (double)((float)par4 + var8), (double)((float)(par2 + 1) - var8), (double)((float)(par3 + 1) - var8), (double)((float)(par4 + 1) - var8)));
095    
096            if (!var9.isEmpty())
097            {
098                var7 = true;
099            }
100    
101            if (var7 && !var6)
102            {
103                par1World.setBlockMetadataWithNotify(par2, par3, par4, par5 | 8);
104                par1World.notifyBlocksOfNeighborChange(par2, par3, par4, this.blockID);
105                par1World.notifyBlocksOfNeighborChange(par2, par3 - 1, par4, this.blockID);
106                par1World.markBlockRangeForRenderUpdate(par2, par3, par4, par2, par3, par4);
107            }
108    
109            if (!var7 && var6)
110            {
111                par1World.setBlockMetadataWithNotify(par2, par3, par4, par5 & 7);
112                par1World.notifyBlocksOfNeighborChange(par2, par3, par4, this.blockID);
113                par1World.notifyBlocksOfNeighborChange(par2, par3 - 1, par4, this.blockID);
114                par1World.markBlockRangeForRenderUpdate(par2, par3, par4, par2, par3, par4);
115            }
116    
117            if (var7)
118            {
119                par1World.scheduleBlockUpdate(par2, par3, par4, this.blockID, this.tickRate());
120            }
121        }
122    }