Guest User

Block Carpenter Bench

a guest
Aug 24th, 2014
7
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package minefantasy.mf2.block.crafting;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. import minefantasy.mf2.MineFantasyII;
  7. import minefantasy.mf2.api.crafting.BaseRecipeMF;
  8. import minefantasy.mf2.api.crafting.CarpenterRecipes;
  9. import minefantasy.mf2.api.helpers.ToolHelper;
  10. import minefantasy.mf2.block.tileentity.TileEntityCarpenter;
  11. import minefantasy.mf2.item.list.CreativeTabMF;
  12. import net.minecraft.block.Block;
  13. import net.minecraft.block.BlockContainer;
  14. import net.minecraft.block.material.Material;
  15. import net.minecraft.entity.item.EntityItem;
  16. import net.minecraft.entity.player.EntityPlayer;
  17. import net.minecraft.init.Blocks;
  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.world.World;
  23. import cpw.mods.fml.common.registry.GameRegistry;
  24. import cpw.mods.fml.relauncher.Side;
  25. import cpw.mods.fml.relauncher.SideOnly;
  26.  
  27. public class BlockCarpenterBench extends BlockContainer
  28. {
  29. private int tier = 0;
  30. private Random rand = new Random();
  31. public BlockCarpenterBench(String name)
  32. {
  33. super(Material.wood);
  34. GameRegistry.registerBlock(this, name);
  35. setBlockName(name);
  36. setBlockTextureName("minefantasy2:crafters/"+name);
  37.  
  38. if(tier == 0)
  39. {
  40. this.setBlockBounds(0F, 0F, 0F, 1F, 0.5F, 1F);
  41. }
  42. this.setLightOpacity(0);
  43. this.setCreativeTab(CreativeTabMF.tabUtil);
  44. }
  45.  
  46. @Override
  47. @SideOnly(Side.CLIENT)
  48. public IIcon getIcon(int side, int meta)
  49. {
  50. return Blocks.crafting_table.getIcon(side, meta);
  51. }
  52.  
  53. /**
  54. * Called upon block activation (right click on the block.)
  55. */
  56. @Override
  57. public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float xOffset, float yOffset, float zOffset)
  58. {
  59. TileEntityCarpenter tile = getTile(world, x, y, z);
  60. ItemStack tool = player.getHeldItem();
  61. String tooltype = ToolHelper.getCrafterTool(tool);
  62. int toolTier = ToolHelper.getCrafterTier(tool);
  63. if(!world.isRemote && tile != null)
  64. {
  65. System.out.println(tile.toString());
  66. if (tile == null || player.isSneaking()) {
  67. return false;
  68. }
  69.  
  70. player.openGui(MineFantasyII.instance, 0, world, x, y, z);
  71.  
  72.  
  73. }
  74. return true;
  75. }
  76.  
  77. private void tryAdd(TileEntityCarpenter tile, String tab, String key)
  78. {
  79. if(tile.hasProject())
  80. {
  81. return;
  82. }
  83. ArrayList<BaseRecipeMF> list = CarpenterRecipes.getRecipeList(tab, key);
  84. if(list != null && !list.isEmpty())
  85. {
  86. BaseRecipeMF recipe = list.get(rand.nextInt(list.size()));
  87. if(recipe != null)
  88. {
  89. tile.setProject(recipe);
  90. }
  91. }
  92. }
  93.  
  94. @Override
  95. public TileEntity createNewTileEntity(World world, int meta)
  96. {
  97. return new TileEntityCarpenter(tier);
  98. }
  99.  
  100. private TileEntityCarpenter getTile(World world, int x, int y, int z)
  101. {
  102. return (TileEntityCarpenter)world.getTileEntity(x, y, z);
  103. }
  104.  
  105. @Override
  106. public boolean isOpaqueCube()
  107. {
  108. return false;
  109. }
  110.  
  111. @Override
  112. public void breakBlock(World world, int x, int y, int z, Block block, int meta)
  113. {
  114. TileEntityCarpenter tile = getTile(world, x, y, z);
  115.  
  116. if (tile != null)
  117. {
  118. tile.clearProject(true);
  119.  
  120. for (int i1 = 0; i1 < tile.getSizeInventory(); ++i1)
  121. {
  122. ItemStack itemstack = tile.getStackInSlot(i1);
  123.  
  124. if (itemstack != null)
  125. {
  126. float f = this.rand .nextFloat() * 0.8F + 0.1F;
  127. float f1 = this.rand.nextFloat() * 0.8F + 0.1F;
  128. float f2 = this.rand.nextFloat() * 0.8F + 0.1F;
  129.  
  130. while (itemstack.stackSize > 0)
  131. {
  132. int j1 = this.rand.nextInt(21) + 10;
  133.  
  134. if (j1 > itemstack.stackSize)
  135. {
  136. j1 = itemstack.stackSize;
  137. }
  138.  
  139. itemstack.stackSize -= j1;
  140. EntityItem entityitem = new EntityItem(world, (double)((float)x + f), (double)((float)y + f1), (double)((float)z + f2), new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));
  141.  
  142. if (itemstack.hasTagCompound())
  143. {
  144. entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
  145. }
  146.  
  147. float f3 = 0.05F;
  148. entityitem.motionX = (double)((float)this.rand.nextGaussian() * f3);
  149. entityitem.motionY = (double)((float)this.rand.nextGaussian() * f3 + 0.2F);
  150. entityitem.motionZ = (double)((float)this.rand.nextGaussian() * f3);
  151. world.spawnEntityInWorld(entityitem);
  152. }
  153. }
  154. }
  155.  
  156. world.func_147453_f(x, y, z, block);
  157. }
  158.  
  159. super.breakBlock(world, x, y, z, block, meta);
  160. }
  161. }
RAW Paste Data