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 * Returns true if the block is emitting indirect/weak redstone power on the specified side. If isBlockNormalCube 064 * returns true, standard redstone propagation rules will apply instead and this will not be called. Args: World, X, 065 * Y, Z, side 066 */ 067 public boolean isProvidingWeakPower(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5) 068 { 069 return (par1IBlockAccess.getBlockMetadata(par2, par3, par4) & 8) != 0; 070 } 071 072 /** 073 * Returns true if the block is emitting direct/strong redstone power on the specified side. Args: World, X, Y, Z, 074 * side 075 */ 076 public boolean isProvidingStrongPower(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5) 077 { 078 return (par1IBlockAccess.getBlockMetadata(par2, par3, par4) & 8) == 0 ? false : par5 == 1; 079 } 080 081 /** 082 * Update the detector rail power state if a minecart enter, stays or leave the block. 083 */ 084 private void setStateIfMinecartInteractsWithRail(World par1World, int par2, int par3, int par4, int par5) 085 { 086 boolean var6 = (par5 & 8) != 0; 087 boolean var7 = false; 088 float var8 = 0.125F; 089 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))); 090 091 if (!var9.isEmpty()) 092 { 093 var7 = true; 094 } 095 096 if (var7 && !var6) 097 { 098 par1World.setBlockMetadataWithNotify(par2, par3, par4, par5 | 8); 099 par1World.notifyBlocksOfNeighborChange(par2, par3, par4, this.blockID); 100 par1World.notifyBlocksOfNeighborChange(par2, par3 - 1, par4, this.blockID); 101 par1World.markBlockRangeForRenderUpdate(par2, par3, par4, par2, par3, par4); 102 } 103 104 if (!var7 && var6) 105 { 106 par1World.setBlockMetadataWithNotify(par2, par3, par4, par5 & 7); 107 par1World.notifyBlocksOfNeighborChange(par2, par3, par4, this.blockID); 108 par1World.notifyBlocksOfNeighborChange(par2, par3 - 1, par4, this.blockID); 109 par1World.markBlockRangeForRenderUpdate(par2, par3, par4, par2, par3, par4); 110 } 111 112 if (var7) 113 { 114 par1World.scheduleBlockUpdate(par2, par3, par4, this.blockID, this.tickRate()); 115 } 116 } 117 }