Guest User

Block Class

a guest
Mar 17th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.56 KB | None | 0 0
  1. package Com.BRHSM.Mod.Blocks;
  2.  
  3. import java.util.Random;
  4.  
  5. import Com.BRHSM.Mod.Registry.MainRegistry;
  6. import Com.BRHSM.Mod.TileEntities.TileEntityGemSmelter;
  7. import cpw.mods.fml.relauncher.Side;
  8. import cpw.mods.fml.relauncher.SideOnly;
  9. import net.minecraft.block.Block;
  10. import net.minecraft.block.BlockFurnace;
  11. import net.minecraft.block.material.Material;
  12. import net.minecraft.client.renderer.texture.IIconRegister;
  13. import net.minecraft.creativetab.CreativeTabs;
  14. import net.minecraft.entity.EntityLivingBase;
  15. import net.minecraft.entity.item.EntityItem;
  16. import net.minecraft.entity.player.EntityPlayer;
  17. import net.minecraft.item.Item;
  18. import net.minecraft.item.ItemStack;
  19. import net.minecraft.nbt.NBTTagCompound;
  20. import net.minecraft.tileentity.TileEntity;
  21. import net.minecraft.util.IIcon;
  22. import net.minecraft.util.MathHelper;
  23. import net.minecraft.world.World;
  24.  
  25. public class GemSmelter extends Block{
  26.  
  27.     @SideOnly(Side.CLIENT)
  28.     private IIcon top;
  29.     @SideOnly(Side.CLIENT)
  30.     private IIcon front;
  31.     @SideOnly(Side.CLIENT)
  32.     private IIcon bottom;
  33.  
  34.     private static boolean IsWorking;
  35.     private final boolean IsWorking2;
  36.     private final Random random = new Random();
  37.    
  38.     public GemSmelter( int ID, Material mat, boolean activity){
  39.         super(mat);
  40.         this.setCreativeTab(CreativeTabs.tabMaterials);
  41.         IsWorking2 = activity;
  42.     }
  43.    
  44.     @SideOnly(Side.CLIENT)
  45.     public void registerBlockIcons(IIconRegister reg){
  46.         this.blockIcon = reg.registerIcon(MainRegistry.MODID + ":GemSmelter_Side");
  47.         this.top = reg.registerIcon(this.IsWorking2 ? MainRegistry.MODID + ":GemSmelter_Top_On" :  MainRegistry.MODID + ":GemSmelter_Top_Off");
  48.         this.bottom = reg.registerIcon(MainRegistry.MODID + ":GemSmelter_Bottom");
  49.         this.front = reg.registerIcon(MainRegistry.MODID + ":GemSmelter_Front");
  50.     }
  51.    
  52.     public IIcon getIcon(int side, int meta){
  53.         if(side == 1){
  54.             return top;
  55.         }else if(side == 3){
  56.             return front;
  57.         }else if(side == 0){
  58.             return bottom;
  59.         }else{
  60.             return this.blockIcon;
  61.         }
  62.        
  63.     }
  64.    
  65.     public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9){
  66.         player.openGui(MainRegistry.MODID, 0, world, x, y, z);
  67.         return true;
  68.     }
  69.    
  70.     public Item GetItemDropped(int par1, Random random, int par3){
  71.         return Item.getItemFromBlock(MainRegistry.GemSmelter);
  72.     }
  73.    
  74.     public Item GetItem(World world, int par2, int par3, int par4){
  75.         return Item.getItemFromBlock(MainRegistry.GemSmelter);
  76.     }
  77.    
  78.     @SideOnly(Side.CLIENT)
  79.     public void onBlockAdded(World world, int x, int y, int z){
  80.         super.onBlockAdded(world,x,y,z);
  81.         this.direction(world,x,y,z);
  82.     }
  83.  
  84.     private void direction(World world, int x, int y, int z) {
  85.         if(world.isRemote){
  86.             Block direction  = world.getBlock(x, y, z-1);
  87.             Block direction1 = world.getBlock(x, y, z+1);
  88.             Block direction2 = world.getBlock(x-1, y, z);
  89.             Block direction3 = world.getBlock(x+1, y, z);
  90.             byte byte0 = 3;
  91.            
  92.             if(direction.func_149730_j() && direction.func_149730_j()){
  93.                 byte0 = 3;
  94.             }
  95.             if(direction1.func_149730_j() && direction1.func_149730_j()){
  96.                 byte0 = 2;
  97.             }
  98.             if(direction2.func_149730_j() && direction2.func_149730_j()){
  99.                 byte0 = 5;
  100.             }
  101.             if(direction3.func_149730_j() && direction3.func_149730_j()){
  102.                 byte0 = 4;
  103.             }
  104.            
  105.             world.setBlockMetadataWithNotify(x,y,z,byte0,2);
  106.            
  107.         }
  108.     }
  109.    
  110.     public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack itemstack){
  111.         int direction = MathHelper.floor_double((double)(entity.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
  112.         if(direction == 0){
  113.             world.setBlockMetadataWithNotify(x,y,z,2,2);
  114.         }
  115.         if(direction == 1){
  116.             world.setBlockMetadataWithNotify(x,y,z,5,2);
  117.         }
  118.         if(direction == 2){
  119.             world.setBlockMetadataWithNotify(x,y,z,3,2);
  120.         }
  121.         if(direction == 3){
  122.             world.setBlockMetadataWithNotify(x,y,z,4,2);
  123.         }
  124.        
  125.         if(itemstack.hasDisplayName()){
  126.             ((TileEntityGemSmelter) world.getTileEntity(x, y, z)).furnaceName(itemstack.getDisplayName());
  127.         }
  128.        
  129.     }
  130.    
  131.     public static void updateBlockState(boolean IsActive, World world, int x, int y, int z){
  132.         int direction = world.getBlockMetadata(x, y, z);
  133.         TileEntity tileentity = world.getTileEntity(x, y, z);
  134.         IsWorking = true;
  135.                
  136.         if(IsActive){
  137.             world.setBlock(x,y,z,MainRegistry.GemSmelterActive);
  138.         }else{
  139.             world.setBlock(x,y,z,MainRegistry.GemSmelter);
  140.         }
  141.        
  142.         IsWorking = false;
  143.         world.setBlockMetadataWithNotify(x, y, z, direction, 2);
  144.        
  145.        
  146.         if(tileentity != null){
  147.             tileentity.validate();
  148.             world.setTileEntity(x,y,z,tileentity);
  149.         }
  150.     }
  151.    
  152.     public void breakBlock(World world, int x, int y, int z, Block block, int meta){
  153.         if (!IsWorking){
  154.             TileEntityGemSmelter tileentitygemsmelter = (TileEntityGemSmelter) world.getTileEntity(x, y, z);
  155.            
  156.             if(tileentitygemsmelter != null){
  157.                 for(int i=0;i<tileentitygemsmelter.getSizeInventory();++i){
  158.                     ItemStack itemstack = tileentitygemsmelter.getStackInSlot(i);
  159.                    
  160.                     if(itemstack != null){
  161.                         float f=this.random.nextFloat()*0.6F+0.1F;
  162.                         float f1=this.random.nextFloat()*0.6F+0.1F;
  163.                         float f2=this.random.nextFloat()*0.6F+0.1F;
  164.                        
  165.                         while(itemstack.stackSize > 0){
  166.                             int j = this.random.nextInt(21)+10;
  167.                             if(j>itemstack.stackSize){
  168.                                 j = itemstack.stackSize;
  169.                             }
  170.                             itemstack.stackSize -= j;
  171.                             EntityItem entityitem = new EntityItem(world, (double) ((float) x+f), (double) ((float) y+f1), (double) ((float)z+f2), new ItemStack(itemstack.getItem(),j,itemstack.getItemDamage()));
  172.                            
  173.                             if(itemstack.hasTagCompound()){
  174.                                 entityitem.getEntityItem().setTagCompound(((NBTTagCompound) itemstack.getTagCompound().copy()));
  175.                             }
  176.                             float f3 = 0.025F;
  177.                             entityitem.motionX = (double) ((float)this.random.nextGaussian() * f3);
  178.                             entityitem.motionY = (double) ((float)this.random.nextGaussian() * f3 + 0.1F);
  179.                             entityitem.motionZ = (double) ((float)this.random.nextGaussian() * f3);
  180.                             world.spawnEntityInWorld(entityitem);
  181.                         }
  182.                     }
  183.                 }
  184.                 world.func_147453_f(x,y,z,block);
  185.             }
  186.         }
  187.         super.breakBlock(world,x,y,z,block,meta);
  188.     }
  189.    
  190.     @SideOnly(Side.CLIENT)
  191.     public void randomDisplayTick(World world, int x, int y, int z ,Random random){
  192.         if(this.IsWorking2){
  193.             int direction = world.getBlockMetadata(x, y, z);
  194.             float xx = (float) x + 0.5F, yy = (float) y + random.nextFloat() * 6.0F /16.0F ,zz = (float) z + 0.5F, xx2 = random.nextFloat() * 0.3F, zz2 = 0.5F ;
  195.                    
  196.             if(direction == 1){
  197.                 world.spawnParticle("smoke", (double) (xx-zz2), (double) yy, (double) zz + xx2, 0.0F, 0.0F, 0.0F);
  198.             }
  199.         }
  200.        
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment