001 package net.minecraft.src;
002
003 import java.util.List;
004 import java.util.Random;
005
006 public class BlockDetectorRail extends BlockRail
007 {
008 public BlockDetectorRail(int par1, int par2)
009 {
010 super(par1, par2, true);
011 this.setTickRandomly(true);
012 }
013
014 /**
015 * How many world ticks before ticking
016 */
017 public int tickRate()
018 {
019 return 20;
020 }
021
022 /**
023 * Can this block provide power. Only wire currently seems to have this change based on its state.
024 */
025 public boolean canProvidePower()
026 {
027 return true;
028 }
029
030 /**
031 * Triggered whenever an entity collides with this block (enters into the block). Args: world, x, y, z, entity
032 */
033 public void onEntityCollidedWithBlock(World par1World, int par2, int par3, int par4, Entity par5Entity)
034 {
035 if (!par1World.isRemote)
036 {
037 int var6 = par1World.getBlockMetadata(par2, par3, par4);
038
039 if ((var6 & 8) == 0)
040 {
041 this.setStateIfMinecartInteractsWithRail(par1World, par2, par3, par4, var6);
042 }
043 }
044 }
045
046 /**
047 * Ticks the block if it's been scheduled
048 */
049 public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
050 {
051 if (!par1World.isRemote)
052 {
053 int var6 = par1World.getBlockMetadata(par2, par3, par4);
054
055 if ((var6 & 8) != 0)
056 {
057 this.setStateIfMinecartInteractsWithRail(par1World, par2, par3, par4, var6);
058 }
059 }
060 }
061
062 /**
063 * Is this block powering the block on the specified side
064 */
065 public boolean isPoweringTo(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
066 {
067 return (par1IBlockAccess.getBlockMetadata(par2, par3, par4) & 8) != 0;
068 }
069
070 /**
071 * Is this block indirectly powering the block on the specified side
072 */
073 public boolean isIndirectlyPoweringTo(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
074 {
075 return (par1IBlockAccess.getBlockMetadata(par2, par3, par4) & 8) == 0 ? false : par5 == 1;
076 }
077
078 /**
079 * Update the detector rail power state if a minecart enter, stays or leave the block.
080 */
081 private void setStateIfMinecartInteractsWithRail(World par1World, int par2, int par3, int par4, int par5)
082 {
083 boolean var6 = (par5 & 8) != 0;
084 boolean var7 = false;
085 float var8 = 0.125F;
086 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)));
087
088 if (!var9.isEmpty())
089 {
090 var7 = true;
091 }
092
093 if (var7 && !var6)
094 {
095 par1World.setBlockMetadataWithNotify(par2, par3, par4, par5 | 8);
096 par1World.notifyBlocksOfNeighborChange(par2, par3, par4, this.blockID);
097 par1World.notifyBlocksOfNeighborChange(par2, par3 - 1, par4, this.blockID);
098 par1World.markBlocksDirty(par2, par3, par4, par2, par3, par4);
099 }
100
101 if (!var7 && var6)
102 {
103 par1World.setBlockMetadataWithNotify(par2, par3, par4, par5 & 7);
104 par1World.notifyBlocksOfNeighborChange(par2, par3, par4, this.blockID);
105 par1World.notifyBlocksOfNeighborChange(par2, par3 - 1, par4, this.blockID);
106 par1World.markBlocksDirty(par2, par3, par4, par2, par3, par4);
107 }
108
109 if (var7)
110 {
111 par1World.scheduleBlockUpdate(par2, par3, par4, this.blockID, this.tickRate());
112 }
113 }
114 }