Advertisement
Mujun_Kross

PhotonicGenerator.java

Nov 25th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.36 KB | None | 0 0
  1. package mod_Tutorial.commom;
  2.  
  3. import java.util.Random;
  4.  
  5. import net.minecraft.src.Block;
  6. import net.minecraft.src.IBlockAccess;
  7. import net.minecraft.src.Material;
  8. import net.minecraft.src.World;
  9.  
  10. public class PhotonicGenerator extends GenericBlock
  11. {
  12.    
  13.     public boolean solar;
  14.    
  15.     public PhotonicGenerator(int id, int texture, Material material)
  16.     {
  17.         super(id, texture, material);
  18.  
  19.         solar = false;
  20.        
  21.         setHardness(1F);
  22.         setTickRandomly(true);
  23.         setStepSound(Block.soundMetalFootstep);
  24.         setBlockName("Photonic Generator");
  25.        
  26.     }
  27.  
  28.     /**
  29.      * Grabs the current texture file used for this block
  30.      */
  31.     public String getTextureFile()
  32.     {
  33.         return mod_Tutorial.CUSTOM_TEXTURES + "photonic_generator.png";
  34.     }
  35.  
  36.     /**
  37.      * Returns the block texture based on the side being looked at.  Args: side
  38.      */
  39.     public int getBlockTextureFromSide(int i)
  40.     {
  41.         /*
  42.          * 0 = bottom
  43.          * 1 = top
  44.          * 2 = east
  45.          * 3 = west
  46.          * 4 = north
  47.          * 5 = south
  48.          */
  49.         switch(i)
  50.         {
  51.         case 0:
  52.             return 0;
  53.         case 1:
  54.             return 1;
  55.         case 2:
  56.             return 2;
  57.         case 3:
  58.             return 3;
  59.         case 4:
  60.             return 4;
  61.         case 5:
  62.             return 5;
  63.         default:
  64.             return 0;
  65.         }
  66.     }
  67.    
  68.     public int tickRate()
  69.     {
  70.         //how often does the system check for update
  71.         return 120;
  72.     }
  73.    
  74.     //this is required for the block when it is added to the world
  75.     //i j k are x y z coordinates
  76.     public void onBlockAdded(World world, int i, int j, int k)
  77.     {
  78.         if(world.getBlockMetadata(i, j, k) == 0)
  79.         {
  80.             super.onBlockAdded(world, i, j, k);
  81.         }
  82.        
  83.         //check to see if its getting light, this is custom for our block
  84.         if (solar)
  85.         {
  86.             notifyNeighbors(world, i, j, k);
  87.         }
  88.        
  89.         //this sets up the block to be updated in the world based on the tick rate
  90.         world.scheduleBlockUpdate(i, j, k, blockID, tickRate());
  91.     }
  92.    
  93.     /**
  94.      * Can this block provide power. Only wire currently seems to have this change based on its state.
  95.      */
  96.     public boolean canProvidePower()
  97.     {
  98.         return solar;
  99.     }
  100.  
  101.     /**
  102.      * Is this block indirectly powering the block on the specified side
  103.      */
  104.     public boolean isIndirectlyPoweringTo(World world, int i, int j, int k, int l)
  105.     {
  106.         //return  isPoweringTo(world, i, j, k, l);
  107.         return l == 0 ? this.isPoweringTo(world, i, j, k, l) : false;
  108.     }
  109.    
  110.     /**
  111.      * Returns true or false based on whether the block the torch is attached to is providing indirect power.
  112.      */
  113.     private boolean isIndirectlyPowered(World par1World, int par2, int par3, int par4)
  114.     {
  115.         int var5 = par1World.getBlockMetadata(par2, par3, par4);
  116.         return var5 == 5 && par1World.isBlockIndirectlyProvidingPowerTo(par2, par3 - 1, par4, 0) ? true : (var5 == 3 && par1World.isBlockIndirectlyProvidingPowerTo(par2, par3, par4 - 1, 2) ? true : (var5 == 4 && par1World.isBlockIndirectlyProvidingPowerTo(par2, par3, par4 + 1, 3) ? true : (var5 == 1 && par1World.isBlockIndirectlyProvidingPowerTo(par2 - 1, par3, par4, 4) ? true : var5 == 2 && par1World.isBlockIndirectlyProvidingPowerTo(par2 + 1, par3, par4, 5))));
  117.     }
  118.    
  119.     /**
  120.      * Is this block powering the block on the specified side
  121.      */
  122.     public boolean isPoweringTo(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
  123.     {
  124.         if (!solar)
  125.         {
  126.             return false;
  127.         }
  128.         else
  129.         {
  130.             int var6 = par1IBlockAccess.getBlockMetadata(par2, par3, par4);
  131.             return var6 == 5 && par5 == 1 ? false : (var6 == 3 && par5 == 3 ? false : (var6 == 4 && par5 == 2 ? false : (var6 == 1 && par5 == 5 ? false : var6 != 2 || par5 != 4)));
  132.         }
  133.     }
  134.  
  135.     /**
  136.      * Called right before the block is destroyed by a player.  Args: world, x, y, z, metaData
  137.      */
  138.     public void onBlockDestroyedByPlayer(World world, int i, int j, int k, int l)
  139.     {
  140.         notifyNeighbors(world, i, j, k);
  141.     }
  142.  
  143.     //i j k are equal to x y z
  144.     private void notifyNeighbors(World world, int i, int j, int k)
  145.     {
  146.         world.notifyBlocksOfNeighborChange(i, j + 1, k, this.blockID); //above
  147.         world.notifyBlocksOfNeighborChange(i, j - 1, k, this.blockID); //below
  148.         world.notifyBlocksOfNeighborChange(i - 1, j, k, this.blockID); //
  149.         world.notifyBlocksOfNeighborChange(i + 1, j, k, this.blockID); //
  150.         world.notifyBlocksOfNeighborChange(i, j, k - 1, this.blockID); //
  151.         world.notifyBlocksOfNeighborChange(i, j, k + 1, this.blockID); //
  152.     }
  153.    
  154.     /**
  155.      * Ticks the block if it's been scheduled
  156.      */
  157.     public void updateTick(World world, int i, int j, int k, Random par5Random)
  158.     {
  159.         //check to see if the sun is up
  160.         solar = super.isSolar(world, i, j, k, this.blockID);
  161.         //tell the neighbors
  162.         notifyNeighbors(world, i, j, k);
  163.         //schedule another update
  164.         world.scheduleBlockUpdate(i, j, k, this.blockID, tickRate());
  165.     }
  166.    
  167.     /**
  168.      * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
  169.      * their own) Args: x, y, z, neighbor blockID
  170.      */
  171.     public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
  172.     {
  173.         super.onNeighborBlockChange(par1World, par2, par3, par4, par5);
  174.         par1World.scheduleBlockUpdate(par2, par3, par4, this.blockID, this.tickRate());
  175.     }
  176.  
  177.     /**
  178.      * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
  179.      * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
  180.      */
  181.     public boolean isOpaqueCube()
  182.     {
  183.         return true;
  184.     }
  185.  
  186.     /**
  187.      * Determine if this block can make a redstone connection on the side provided,
  188.      * Useful to control which sides are inputs and outputs for redstone wires.
  189.      *
  190.      * Side:
  191.      *  -1: UP
  192.      *   0: NORTH
  193.      *   1: EAST
  194.      *   2: SOUTH
  195.      *   3: WEST
  196.      *
  197.      * @param world The current world
  198.      * @param x X Position
  199.      * @param y Y Position
  200.      * @param z Z Position
  201.      * @param side The side that is trying to make the connection
  202.      * @return True to make the connection
  203.      */
  204.     public boolean canConnectRedstone(IBlockAccess world, int x, int y, int z, int side)
  205.     {
  206.         return Block.blocksList[blockID].canProvidePower() && side != -1;
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement