Advertisement
TeNNoX

Minecraft block detector

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