Advertisement
TeNNoX

Detectorblock 2

Dec 6th, 2011
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.04 KB | None | 0 0
  1. package net.minecraft.src;
  2.  
  3. import java.util.Random;
  4.  
  5. import net.minecraft.client.Minecraft;
  6.  
  7. public class BlockDetector extends Block
  8. {
  9.     public BlockDetector(int i, int j) {
  10.         super(i, j, Material.rock);
  11.         setBlockName("DetectorBlock");
  12.         setRequiresSelfNotify();
  13.         boolean[] nn = requiresSelfNotify;
  14.         System.out.println("req: "+nn);
  15.     }
  16.        
  17.     public void onBlockAdded(World world, int i, int j, int k)
  18.     {
  19.         if(!world.multiplayerWorld)
  20.         {
  21.             updateDetectorState(world, i, j, k);
  22.         }
  23.     }
  24.    
  25.     public boolean OnTickInGame(float f, Minecraft mc){
  26.           Random r = new Random();
  27.           updateTick(mc.theWorld, 0, 0, 0, r);
  28.           return true;
  29.     }
  30.    
  31.     public void onBlockPlacedBy(World world, int i, int j, int k, EntityLiving entityliving)
  32.     {
  33.         int l = determineOrientation(world, i, j, k, (EntityPlayer)entityliving);
  34.         world.setBlockMetadataWithNotify(i, j, k, l);
  35.         if(!world.multiplayerWorld)
  36.         {
  37.             updateDetectorState(world, i, j, k);
  38.         }
  39.     }
  40.    
  41.    
  42.     public boolean isPoweringTo(IBlockAccess iblockaccess, int i, int j, int k, int l)
  43.     {
  44.         boolean powered = (iblockaccess.getBlockMetadata(i, j, k) & 8) != 0;
  45.         System.out.println("pow: " + powered);
  46.         return true;
  47.     }
  48.    
  49.     public int getBlockTextureFromSide(int i, int j)
  50.     {
  51.         return i != 1 ? i != 0 ? i != 3 ? blockIndexInTexture : blockIndexInTexture + 1 : blockIndexInTexture + 17 : blockIndexInTexture + 17;
  52.     }
  53.    
  54.     public int getBlockTexture(IBlockAccess iblockaccess, int i, int j, int k, int l)
  55.     {
  56.         if(l == 1)
  57.         {
  58.             return blockIndexInTexture + 17;
  59.         }
  60.         if(l == 0)
  61.         {
  62.             return blockIndexInTexture + 17;
  63.         } else
  64.         {
  65.             int i1 = iblockaccess.getBlockMetadata(i, j, k);
  66.             if (i1 >= 8) {
  67.                 i1 = i1 - 8;
  68.             }
  69.             return l == i1 ? blockIndexInTexture + 1 : blockIndexInTexture;
  70.         }
  71.     }
  72.    
  73.  
  74.     public void updateDetectorState(World world, int i, int j, int k) {
  75.         boolean powered = false;
  76.         int meta = world.getBlockMetadata(i, j, k);
  77.         int rotation = getOrientation(meta);
  78.        
  79.         if (0 != world.getBlockId(i, j, k-1) && rotation == 2) {
  80.             powered = true;
  81.         }
  82.         else if (0 != world.getBlockId(i, j, k+1) && rotation == 3) {
  83.             powered = true;
  84.         }
  85.         else if (0 != world.getBlockId(i-1, j, k) && rotation == 4) {
  86.             powered = true;
  87.         }
  88.         else if (0 != world.getBlockId(i+1, j, k) && rotation == 5) {
  89.             powered = true;
  90.         }
  91.         else if (powered == true){
  92.             powered = false;
  93.         }
  94.        
  95.         if (powered) {
  96.             world.setBlockMetadata(i, j, k, rotation | 8);
  97.         } else {
  98.             world.setBlockMetadata(i, j, k, rotation);
  99.         }
  100.        
  101.         int nmeta = world.getBlockMetadata(i, j, k);
  102.         if (nmeta != meta) {
  103.             int block = world.getBlockId(i+1, j, k);
  104.             System.out.println("x" + block + "xx" + Block.oreRedstoneGlowing.blockID + "xxx" + powered);
  105.             if(world.getBlockId(i+1, j, k) == Block.oreRedstoneGlowing.blockID && powered == false)
  106.             {
  107.                 world.setBlockWithNotify(i+1, j, k, Block.oreRedstone.blockID);
  108.                 System.out.println("ore off");
  109.             }
  110.            
  111.             if(world.getBlockId(i+1, j, k) == Block.oreRedstone.blockID && powered == true)
  112.             {
  113.                 world.setBlockWithNotify(i+1, j, k, Block.oreRedstoneGlowing.blockID);
  114.                 System.out.println("ore on");
  115.             }
  116.                 world.markBlocksDirty(i, j, k, i, j, k);
  117.             world.notifyBlocksOfNeighborChange(i, j, k, blockID);
  118.             world.notifyBlocksOfNeighborChange(i+1, j, k, blockID);
  119.             world.notifyBlocksOfNeighborChange(i, j, k+1, blockID);
  120.             world.notifyBlocksOfNeighborChange(i, j, k-1, blockID);
  121.             world.notifyBlocksOfNeighborChange(i-1, j, k, blockID);
  122.             world.notifyBlocksOfNeighborChange(i, j - 1, k, blockID);
  123.             System.out.println("meta: " + meta + " nmeta: " + nmeta + " pow: " + powered);
  124.         }
  125.     }
  126.    
  127.     public void onNeighborBlockChange(World world, int i, int j, int k, int l)
  128.     {
  129.         if(!world.multiplayerWorld)
  130.         {
  131.             updateDetectorState(world, i, j, k);
  132.         }
  133.     }
  134.    
  135.     /*private void setDetectorDefaultDirection(World world, int i, int j, int k)
  136.     {
  137.         if(!world.multiplayerWorld)
  138.         {
  139.             int l = world.getBlockId(i, j, k - 1);
  140.             int i1 = world.getBlockId(i, j, k + 1);
  141.             int j1 = world.getBlockId(i - 1, j, k);
  142.             int k1 = world.getBlockId(i + 1, j, k);
  143.             byte byte0 = 3;
  144.             if(Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[i1])
  145.             {
  146.                 byte0 = 3;
  147.             }
  148.             if(Block.opaqueCubeLookup[i1] && !Block.opaqueCubeLookup[l])
  149.             {
  150.                 byte0 = 2;
  151.             }
  152.             if(Block.opaqueCubeLookup[j1] && !Block.opaqueCubeLookup[k1])
  153.             {
  154.                 byte0 = 5;
  155.             }
  156.             if(Block.opaqueCubeLookup[k1] && !Block.opaqueCubeLookup[j1])
  157.             {
  158.                 byte0 = 4;
  159.             }
  160.             this.rotation = byte0;
  161.             world.setBlockMetadataWithNotify(i, j, k, byte0);
  162.         }
  163.     }*/
  164.    
  165.     public void updateTick(World world, int i, int j, int k, Random random) {
  166.         updateDetectorState(world, i, j, k);
  167.     }
  168.    
  169.     private static int determineOrientation(World world, int i, int j, int k, EntityPlayer entityplayer)
  170.     {
  171.         if(MathHelper.abs((float)entityplayer.posX - (float)i) < 2.0F && MathHelper.abs((float)entityplayer.posZ - (float)k) < 2.0F)
  172.         {
  173.             double d = (entityplayer.posY + 1.8200000000000001D) - (double)entityplayer.yOffset;
  174.             if(d - (double)j > 2D)
  175.             {
  176.                 return 1;
  177.             }
  178.             if((double)j - d > 0.0D)
  179.             {
  180.                 return 0;
  181.             }
  182.         }
  183.         int l = MathHelper.floor_double((double)((entityplayer.rotationYaw * 4F) / 360F) + 0.5D) & 3;
  184.         if(l == 0)
  185.         {
  186.             return 2;
  187.         }
  188.         if(l == 1)
  189.         {
  190.             return 5;
  191.         }
  192.         if(l == 2)
  193.         {
  194.             return 3;
  195.         }
  196.         return l != 3 ? 0 : 4;
  197.     }
  198.    
  199.     public int tickRate()
  200.     {
  201.         return 20;
  202.     }
  203.    
  204.     public boolean isIndirectlyPoweringTo(World world, int i, int j, int k, int l)
  205.     {
  206.         boolean powered = (world.getBlockMetadata(i, j, k) & 8) != 0;
  207.         System.out.println("pow ind: " + powered);
  208.         if((world.getBlockMetadata(i, j, k) & 8) == 0)
  209.         {
  210.             return false;
  211.         } else
  212.         {
  213.             return true;
  214.         }
  215.     }
  216.    
  217.     public boolean canProvidePower()
  218.     {
  219.         return true;
  220.     }
  221.    
  222.     public static int getOrientation(int i)
  223.     {
  224.         return i & 7;
  225.     }
  226.    
  227.     public int idDropped(int i, Random random, int j)
  228.     {
  229.         return 209;
  230.     }
  231.        
  232.     public int quantityDropped(Random random)
  233.     {
  234.         return 1;
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement