Advertisement
Guest User

VoidExtractorBlock

a guest
Sep 13th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. package net.prefixaut.voidmod.blocks;
  2.  
  3. import net.minecraft.block.Block;
  4. import net.minecraft.block.BlockContainer;
  5. import net.minecraft.block.material.Material;
  6. import net.minecraft.client.renderer.texture.IIconRegister;
  7. import net.minecraft.entity.EntityLivingBase;
  8. import net.minecraft.entity.item.EntityItem;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.item.Item;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.tileentity.TileEntity;
  13. import net.minecraft.util.IIcon;
  14. import net.minecraft.util.MathHelper;
  15. import net.minecraft.world.World;
  16. import net.prefixaut.voidmod.VoidMod;
  17.  
  18. import net.prefixaut.voidmod.api.VoidBlocks;
  19.  
  20. import net.prefixaut.voidmod.tileentitys.VoidExtractorTileEntity;
  21.  
  22. import java.util.Random;
  23.  
  24. import cpw.mods.fml.relauncher.Side;
  25. import cpw.mods.fml.relauncher.SideOnly;
  26.  
  27.  
  28. public class VoidExtractorBlock extends BlockContainer {
  29.  
  30.     private boolean isActive;
  31.     @SideOnly(Side.CLIENT)
  32.     private IIcon top, left, right, back, front, frontA, bottom;
  33.     private String ipath = VoidMod.MODID + ":";
  34.    
  35.     public VoidExtractorBlock(boolean isActive, String name) {
  36.         super(Material.rock);
  37.         this.isActive = isActive;
  38.         if (isActive) ipath += name.substring(0, name.length() - 6);
  39.         else ipath += name;
  40.         this.setBlockName(name);
  41.         this.setBlockTextureName(VoidMod.MODID + ":" + name);
  42. //      if (!isActive)
  43.             this.setCreativeTab(VoidMod.voidTab);
  44.         this.setHardness(4.0F);
  45.         this.setResistance(7.0F);
  46.         this.setHarvestLevel("pickaxe", 2);
  47.     }
  48.    
  49.     public void updateActivity(boolean active, World world, int x, int y, int z, VoidExtractorTileEntity entity) {
  50.         if (active == isActive) return;
  51.         isActive = active;
  52.         int dir = world.getBlockMetadata(x, y, z);
  53.         System.out.println("saved TileEnetity " + entity);
  54.         world.setBlockToAir(x, y, z);
  55.         if (isActive) world.setBlock(x, y, z, VoidBlocks.voidExtractorActive);
  56.         else world.setBlock(x, y, z, VoidBlocks.voidExtractor);
  57.         world.setBlockMetadataWithNotify(x, y, z, dir, 2);
  58.         if (entity != null) {
  59.             entity.validate();
  60.             world.setTileEntity(x, y, z, entity);
  61.             entity.validate();
  62.         } else world.setTileEntity(x, y, z, new VoidExtractorTileEntity());
  63.     }
  64.  
  65.     @SideOnly(Side.CLIENT)
  66.     @Override
  67.     public void registerBlockIcons(IIconRegister ireg) {
  68.         this.top = ireg.registerIcon(ipath + "_top");
  69.         this.left = ireg.registerIcon(ipath + "_left");
  70.         this.right = ireg.registerIcon(ipath + "_right");
  71.         this.back = ireg.registerIcon(ipath + "_back");
  72.         this.front = ireg.registerIcon(ipath + "_front");
  73.         this.frontA = ireg.registerIcon(ipath + "_frontActive");
  74.         this.bottom = ireg.registerIcon(ipath + "_bottom");
  75.     }
  76.    
  77.     @Override
  78.     public IIcon getIcon(int side, int metadata) {
  79.         switch (side) {
  80.             case 0: return this.bottom;
  81.             case 1: return this.top;
  82.             case 2: return isActive ? this.frontA : this.front;
  83.             case 3: return this.back;
  84.             case 4: return this.left;
  85.             case 5: return this.right;
  86.             default: return null;
  87.         }
  88.     }
  89.    
  90.     @Override
  91.     public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9) {
  92.         player.openGui(VoidMod.instance, 666, world, x, y, z);
  93.         return true;
  94.     }
  95.    
  96.     @Override
  97.     public Item getItemDropped(int par1, Random random, int par3) {
  98.         return Item.getItemFromBlock(VoidBlocks.voidExtractor);
  99.     }
  100.  
  101.     @Override
  102.     public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack itemstack) {
  103.         int dir = MathHelper.floor_double((double) (entity.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
  104.         switch (dir) {
  105.             case 0: world.setBlockMetadataWithNotify(x, y, z, 2, 2);
  106.                     break;
  107.             case 1: world.setBlockMetadataWithNotify(x, y, z, 5, 2);
  108.                     break;
  109.             case 2: world.setBlockMetadataWithNotify(x, y, z, 3, 2);
  110.                     break;
  111.             case 3: world.setBlockMetadataWithNotify(x, y, z, 4, 2);
  112.                     break;
  113.         }
  114.         super.onBlockPlacedBy(world, x, y, z, entity, itemstack);
  115.     }
  116.    
  117.     @Override
  118.     public void breakBlock(World world, int x, int y, int z, Block block, int meta) {
  119.         VoidExtractorTileEntity te = (VoidExtractorTileEntity) world.getTileEntity(x, y, z);
  120.         if (te != null) {
  121.             for (int i=0; i < te.getSizeInventory(); i++) {
  122.                 ItemStack itm = te.getStackInSlot(i);
  123.                 if (itm != null) {
  124.                     float fx = new Random().nextFloat() * 0.6F + 0.1F;
  125.                     float fy = new Random().nextFloat() * 0.6F + 0.1F;
  126.                     float fz = new Random().nextFloat() * 0.6F + 0.1F;
  127.                     EntityItem eitm = new EntityItem(world, (double) x + fx, (double) y + fy, (double) z + fz, itm);
  128.                 }
  129.             }
  130.         }
  131.         super.breakBlock(world, x, y, z, block, meta);
  132.     }
  133.    
  134.     @Override
  135.     public TileEntity createNewTileEntity(World world, int metadata) {
  136.         return new VoidExtractorTileEntity();
  137.     }
  138.    
  139.     @Override
  140.     public TileEntity createTileEntity(World world, int metadata) {
  141.         return new VoidExtractorTileEntity();
  142.     }
  143.    
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement