package net.minecraft.src; import java.util.Random; import net.minecraft.client.Minecraft; public class BlockDetector extends Block { public BlockDetector(int i, int j) { super(i, j, Material.rock); setBlockName("DetectorBlock"); setRequiresSelfNotify(); boolean[] nn = requiresSelfNotify; System.out.println("req: "+nn); } public void onBlockAdded(World world, int i, int j, int k) { if(!world.multiplayerWorld) { updateDetectorState(world, i, j, k); } } public boolean OnTickInGame(float f, Minecraft mc){ Random r = new Random(); updateTick(mc.theWorld, 0, 0, 0, r); return true; } public void onBlockPlacedBy(World world, int i, int j, int k, EntityLiving entityliving) { int l = determineOrientation(world, i, j, k, (EntityPlayer)entityliving); world.setBlockMetadataWithNotify(i, j, k, l); if(!world.multiplayerWorld) { updateDetectorState(world, i, j, k); } } public boolean isPoweringTo(IBlockAccess iblockaccess, int i, int j, int k, int l) { boolean powered = (iblockaccess.getBlockMetadata(i, j, k) & 8) != 0; System.out.println("pow: " + powered); return true; } public int getBlockTextureFromSide(int i, int j) { return i != 1 ? i != 0 ? i != 3 ? blockIndexInTexture : blockIndexInTexture + 1 : blockIndexInTexture + 17 : blockIndexInTexture + 17; } public int getBlockTexture(IBlockAccess iblockaccess, int i, int j, int k, int l) { if(l == 1) { return blockIndexInTexture + 17; } if(l == 0) { return blockIndexInTexture + 17; } else { int i1 = iblockaccess.getBlockMetadata(i, j, k); if (i1 >= 8) { i1 = i1 - 8; } return l == i1 ? blockIndexInTexture + 1 : blockIndexInTexture; } } public void updateDetectorState(World world, int i, int j, int k) { boolean powered = false; int meta = world.getBlockMetadata(i, j, k); int rotation = getOrientation(meta); if (0 != world.getBlockId(i, j, k-1) && rotation == 2) { powered = true; } else if (0 != world.getBlockId(i, j, k+1) && rotation == 3) { powered = true; } else if (0 != world.getBlockId(i-1, j, k) && rotation == 4) { powered = true; } else if (0 != world.getBlockId(i+1, j, k) && rotation == 5) { powered = true; } else if (powered == true){ powered = false; } if (powered) { world.setBlockMetadata(i, j, k, rotation | 8); } else { world.setBlockMetadata(i, j, k, rotation); } int nmeta = world.getBlockMetadata(i, j, k); if (nmeta != meta) { int block = world.getBlockId(i+1, j, k); System.out.println("x" + block + "xx" + Block.oreRedstoneGlowing.blockID + "xxx" + powered); if(world.getBlockId(i+1, j, k) == Block.oreRedstoneGlowing.blockID && powered == false) { world.setBlockWithNotify(i+1, j, k, Block.oreRedstone.blockID); System.out.println("ore off"); } if(world.getBlockId(i+1, j, k) == Block.oreRedstone.blockID && powered == true) { world.setBlockWithNotify(i+1, j, k, Block.oreRedstoneGlowing.blockID); System.out.println("ore on"); } world.markBlocksDirty(i, j, k, i, j, k); world.notifyBlocksOfNeighborChange(i, j, k, blockID); world.notifyBlocksOfNeighborChange(i+1, j, k, blockID); world.notifyBlocksOfNeighborChange(i, j, k+1, blockID); world.notifyBlocksOfNeighborChange(i, j, k-1, blockID); world.notifyBlocksOfNeighborChange(i-1, j, k, blockID); world.notifyBlocksOfNeighborChange(i, j - 1, k, blockID); System.out.println("meta: " + meta + " nmeta: " + nmeta + " pow: " + powered); } } public void onNeighborBlockChange(World world, int i, int j, int k, int l) { if(!world.multiplayerWorld) { updateDetectorState(world, i, j, k); } } /*private void setDetectorDefaultDirection(World world, int i, int j, int k) { if(!world.multiplayerWorld) { int l = world.getBlockId(i, j, k - 1); int i1 = world.getBlockId(i, j, k + 1); int j1 = world.getBlockId(i - 1, j, k); int k1 = world.getBlockId(i + 1, j, k); byte byte0 = 3; if(Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[i1]) { byte0 = 3; } if(Block.opaqueCubeLookup[i1] && !Block.opaqueCubeLookup[l]) { byte0 = 2; } if(Block.opaqueCubeLookup[j1] && !Block.opaqueCubeLookup[k1]) { byte0 = 5; } if(Block.opaqueCubeLookup[k1] && !Block.opaqueCubeLookup[j1]) { byte0 = 4; } this.rotation = byte0; world.setBlockMetadataWithNotify(i, j, k, byte0); } }*/ public void updateTick(World world, int i, int j, int k, Random random) { updateDetectorState(world, i, j, k); } private static int determineOrientation(World world, int i, int j, int k, EntityPlayer entityplayer) { if(MathHelper.abs((float)entityplayer.posX - (float)i) < 2.0F && MathHelper.abs((float)entityplayer.posZ - (float)k) < 2.0F) { double d = (entityplayer.posY + 1.8200000000000001D) - (double)entityplayer.yOffset; if(d - (double)j > 2D) { return 1; } if((double)j - d > 0.0D) { return 0; } } int l = MathHelper.floor_double((double)((entityplayer.rotationYaw * 4F) / 360F) + 0.5D) & 3; if(l == 0) { return 2; } if(l == 1) { return 5; } if(l == 2) { return 3; } return l != 3 ? 0 : 4; } public int tickRate() { return 20; } public boolean isIndirectlyPoweringTo(World world, int i, int j, int k, int l) { boolean powered = (world.getBlockMetadata(i, j, k) & 8) != 0; System.out.println("pow ind: " + powered); if((world.getBlockMetadata(i, j, k) & 8) == 0) { return false; } else { return true; } } public boolean canProvidePower() { return true; } public static int getOrientation(int i) { return i & 7; } public int idDropped(int i, Random random, int j) { return 209; } public int quantityDropped(Random random) { return 1; } }