Advertisement
Guest User

Foundry

a guest
Mar 26th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.95 KB | None | 0 0
  1. package org.tetawex.block;
  2.  
  3. import java.util.Random;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockContainer;
  7. import net.minecraft.block.Block.SoundType;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.client.renderer.texture.IIconRegister;
  10. import net.minecraft.creativetab.CreativeTabs;
  11. import net.minecraft.entity.EntityLivingBase;
  12. import net.minecraft.entity.item.EntityItem;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.item.Item;
  15. import net.minecraft.item.ItemStack;
  16. import net.minecraft.nbt.NBTTagCompound;
  17. import net.minecraft.tileentity.TileEntity;
  18. import net.minecraft.util.IIcon;
  19. import net.minecraft.util.MathHelper;
  20. import net.minecraft.world.World;
  21.  
  22. import org.tetawex.lib.RefStrings;
  23. import org.tetawex.Main.MainRegistry;
  24. import org.tetawex.tileEntity.TileEntityFoundry;
  25. import org.tetawex.tileEntity.TileEntityRefinery;
  26.  
  27. import cpw.mods.fml.relauncher.Side;
  28. import cpw.mods.fml.relauncher.SideOnly;
  29.  
  30. public class Foundry extends BlockContainer {
  31.  
  32.     @SideOnly(Side.CLIENT)
  33.     private IIcon top;
  34.     @SideOnly(Side.CLIENT)
  35.     private IIcon front;
  36.  
  37.     private static boolean isBurning;
  38.     private final boolean isBurning2;
  39.     private final Random random = new Random();
  40.  
  41.     protected Foundry(boolean isActive,String name, float hardness,float resistance,Material m, SoundType soundType,CreativeTabs creativeTab, String harvestTool, int harvestLevel)
  42.     {
  43.         super(Material.rock);
  44.         setBlockName(name);
  45.         setCreativeTab(creativeTab);
  46.         setHardness(0.7F);
  47.         setResistance(10F);
  48.         setStepSound(soundType);
  49.         setBlockTextureName(RefStrings.MODID+":"+name);
  50.         setHarvestLevel(harvestTool, harvestLevel);
  51.         isBurning2 = isActive;
  52.     }
  53.  
  54.     @SideOnly(Side.CLIENT)
  55.     public void registerBlockIcons(IIconRegister iconregister) {
  56.         front = iconregister.registerIcon(isBurning2 ? RefStrings.MODID + ":block_foundry_front_active" : RefStrings.MODID + ":block_foundry_front_inactive");
  57.         top = iconregister.registerIcon(isBurning2 ? RefStrings.MODID + ":block_refinery_top_active" : RefStrings.MODID + ":block_refinery_top_inactive");
  58.         blockIcon = iconregister.registerIcon(RefStrings.MODID + ":block_refinery_side");
  59.     }
  60.  
  61.     public IIcon getIcon(int side, int meta) {
  62.         if (side == 1) {
  63.             return top;
  64.         } else if (side == meta) {
  65.             return front;
  66.         } else {
  67.             return this.blockIcon;
  68.         }
  69.     }
  70.  
  71.     public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9) {
  72.         player.openGui(MainRegistry.modInstance, 2, world, x, y, z);
  73.         return true;
  74.     }
  75.  
  76.     public Item getItemDropped(int par1, Random random, int par3) {
  77.         return Item.getItemFromBlock(AllBlocks.block_foundry);
  78.     }
  79.  
  80.     public Item getItem(World world, int par2, int par3, int par4) {
  81.         return Item.getItemFromBlock(AllBlocks.block_foundry);
  82.     }
  83.    
  84.     /**
  85.      * Returns a new instance of a block's tile entity class. Called on placing the block.
  86.      */
  87.     public TileEntity createNewTileEntity(World world, int par2)
  88.     {
  89.         return new TileEntityFoundry();
  90.     }
  91.  
  92.    
  93.     @SideOnly(Side.CLIENT)
  94.     public void onBlockAdded(World world, int x, int y, int z) {
  95.         super.onBlockAdded(world, x, y, z);
  96.         direction(world, x, y, z);
  97.     }
  98.  
  99.     private void direction(World world, int x, int y, int z) {
  100.         if (!world.isRemote) {
  101.             Block direction = world.getBlock(x, y, z - 1);
  102.             Block direction1 = world.getBlock(x, y, z + 1);
  103.             Block direction2 = world.getBlock(x - 1, y, z);
  104.             Block direction3 = world.getBlock(x + 1, y, z);
  105.             byte byte0 = 3;
  106.  
  107.             if (direction.func_149730_j() && !direction.func_149730_j())
  108.             {
  109.                 byte0 = 3;
  110.             }
  111.  
  112.             if (direction1.func_149730_j() && !direction1.func_149730_j())
  113.             {
  114.                 byte0 = 2;
  115.             }
  116.  
  117.             if (direction2.func_149730_j() && !direction2.func_149730_j())
  118.             {
  119.                 byte0 = 5;
  120.             }
  121.  
  122.             if (direction3.func_149730_j() && !direction3.func_149730_j())
  123.             {
  124.                 byte0 = 4;
  125.             }
  126.  
  127.             world.setBlockMetadataWithNotify(x, y, z, byte0, 2);
  128.         }
  129.     }
  130.  
  131.     public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack itemstack) {
  132.         int direction = MathHelper.floor_double((double) (entity.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
  133.  
  134.         if (direction == 0) {
  135.             world.setBlockMetadataWithNotify(x, y, z, 2, 2);
  136.         }
  137.  
  138.         if (direction == 1) {
  139.             world.setBlockMetadataWithNotify(x, y, z, 5, 2);
  140.         }
  141.  
  142.         if (direction == 2) {
  143.             world.setBlockMetadataWithNotify(x, y, z, 3, 2);
  144.         }
  145.  
  146.         if (direction == 3) {
  147.             world.setBlockMetadataWithNotify(x, y, z, 4, 2);
  148.         }
  149.  
  150.         if (itemstack.hasDisplayName()) {
  151.             ((TileEntityFoundry) world.getTileEntity(x, y, z)).setName(itemstack.getDisplayName());
  152.         }
  153.     }
  154.  
  155.     public static void updateBlockState(boolean burning, World world, int x, int y, int z) {
  156.         int direction = world.getBlockMetadata(x, y, z);
  157.         TileEntity tileentity = world.getTileEntity(x, y, z);
  158.         isBurning = true;
  159.  
  160.         if (burning) {
  161.             world.setBlock(x, y, z, AllBlocks.block_foundry_active);
  162.         } else {
  163.             world.setBlock(x, y, z, AllBlocks.block_foundry);
  164.         }
  165.  
  166.         isBurning = false;
  167.         world.setBlockMetadataWithNotify(x, y, z, direction, 2);
  168.  
  169.         if (tileentity != null) {
  170.             tileentity.validate();
  171.             world.setTileEntity(x, y, z, tileentity);
  172.         }
  173.     }
  174.  
  175.     public void breakBlock(World world, int x, int y, int z, Block block, int meta) {
  176.         if (!isBurning) {
  177.             TileEntityFoundry tileentitytutfurnace = (TileEntityFoundry) world.getTileEntity(x, y, z);
  178.  
  179.             if (tileentitytutfurnace != null) {
  180.                 for (int i = 0; i < tileentitytutfurnace.getSizeInventory(); ++i) {
  181.                     ItemStack itemstack = tileentitytutfurnace.getStackInSlot(i);
  182.  
  183.                     if (itemstack != null) {
  184.                         float f = this.random.nextFloat() * 0.6F + 0.1F;
  185.                         float f1 = this.random.nextFloat() * 0.6F + 0.1F;
  186.                         float f2 = this.random.nextFloat() * 0.6F + 0.1F;
  187.  
  188.                         while (itemstack.stackSize > 0) {
  189.                             int j = this.random.nextInt(21) + 10;
  190.  
  191.                             if (j > itemstack.stackSize) {
  192.                                 j = itemstack.stackSize;
  193.                             }
  194.  
  195.                             itemstack.stackSize -= j;
  196.                             EntityItem entityitem = new EntityItem(world, (double) ((float) x + f), (double) ((float) y + f1), (double) ((float) z + f2), new ItemStack(itemstack.getItem(), j, itemstack.getItemDamage()));
  197.  
  198.                             if (itemstack.hasTagCompound()) {
  199.                                 entityitem.getEntityItem().setTagCompound(((NBTTagCompound) itemstack.getTagCompound().copy()));
  200.                             }
  201.  
  202.                             float f3 = 0.025F;
  203.                             entityitem.motionX = (double) ((float) this.random.nextGaussian() * f3);
  204.                             entityitem.motionY = (double) ((float) this.random.nextGaussian() * f3 + 0.1F);
  205.                             entityitem.motionZ = (double) ((float) this.random.nextGaussian() * f3);
  206.                             world.spawnEntityInWorld(entityitem);
  207.                         }
  208.                     }
  209.                 }
  210.                 world.func_147453_f(x, y, z, block);
  211.             }
  212.         }
  213.         super.breakBlock(world, x, y, z, block, meta);
  214.     }
  215.  
  216.     @SideOnly(Side.CLIENT)
  217.     public void randomDisplayTick(World world, int x, int y, int z, Random random)
  218.     {
  219.         super.randomDisplayTick(world, x, y, z, random);
  220.     }
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement