Guest User

Untitled

a guest
Jan 22nd, 2018
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package forgingaura.forgeyourworld.ffactory.block;
  2.  
  3. import forgingaura.forgeyourworld.ffactory.init.FactoryBlock;
  4. import forgingaura.forgeyourworld.ffactory.tileentity.TileEntitySteampunkFurnace;
  5. import net.minecraft.block.BlockContainer;
  6. import net.minecraft.block.BlockHorizontal;
  7. import net.minecraft.block.material.Material;
  8. import net.minecraft.block.properties.IProperty;
  9. import net.minecraft.block.properties.PropertyDirection;
  10. import net.minecraft.block.state.BlockStateContainer;
  11. import net.minecraft.block.state.IBlockState;
  12. import net.minecraft.entity.EntityLivingBase;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.init.SoundEvents;
  15. import net.minecraft.inventory.Container;
  16. import net.minecraft.inventory.InventoryHelper;
  17. import net.minecraft.item.Item;
  18. import net.minecraft.item.ItemStack;
  19. import net.minecraft.stats.StatList;
  20. import net.minecraft.tileentity.TileEntity;
  21. import net.minecraft.util.*;
  22. import net.minecraft.util.math.BlockPos;
  23. import net.minecraft.world.World;
  24. import net.minecraftforge.fml.relauncher.Side;
  25. import net.minecraftforge.fml.relauncher.SideOnly;
  26.  
  27. import java.util.Random;
  28.  
  29. public class SteampunkFurnace extends BlockContainer {
  30. public static final PropertyDirection FACING = BlockHorizontal.FACING;
  31. private final boolean isBurning;
  32. private static boolean keepInventory;
  33.  
  34. public SteampunkFurnace(boolean isBurning){
  35. super(Material.ROCK);
  36. this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
  37. this.isBurning = isBurning;
  38. this.setHarvestLevel("pickaxe", 1);
  39. }
  40.  
  41. /**
  42. * Get the Item that this Block should drop when harvested.
  43. */
  44. public Item getItemDropped(IBlockState state, Random rand, int fortune)
  45. {
  46. return Item.getItemFromBlock(FactoryBlock.steampunkfurnace);
  47. }
  48.  
  49. /**
  50. * Called after the block is set in the Chunk data, but before the Tile Entity is set
  51. */
  52. public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
  53. {
  54. this.setDefaultFacing(worldIn, pos, state);
  55. }
  56.  
  57. private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
  58. {
  59. if (!worldIn.isRemote)
  60. {
  61. IBlockState iblockstate = worldIn.getBlockState(pos.north());
  62. IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
  63. IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
  64. IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
  65. EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
  66.  
  67. if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock())
  68. {
  69. enumfacing = EnumFacing.SOUTH;
  70. }
  71. else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock())
  72. {
  73. enumfacing = EnumFacing.NORTH;
  74. }
  75. else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock())
  76. {
  77. enumfacing = EnumFacing.EAST;
  78. }
  79. else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock())
  80. {
  81. enumfacing = EnumFacing.WEST;
  82. }
  83.  
  84. worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
  85. }
  86. }
  87.  
  88. @SideOnly(Side.CLIENT)
  89. @SuppressWarnings("incomplete-switch")
  90. public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
  91. {
  92. if (this.isBurning)
  93. {
  94. EnumFacing enumfacing = (EnumFacing)stateIn.getValue(FACING);
  95. double d0 = (double)pos.getX() + 0.5D;
  96. double d1 = (double)pos.getY() + rand.nextDouble() * 6.0D / 16.0D;
  97. double d2 = (double)pos.getZ() + 0.5D;
  98. double d3 = 0.52D;
  99. double d4 = rand.nextDouble() * 0.6D - 0.3D;
  100.  
  101. if (rand.nextDouble() < 0.1D)
  102. {
  103. worldIn.playSound((double)pos.getX() + 0.5D, (double)pos.getY(), (double)pos.getZ() + 0.5D, SoundEvents.BLOCK_FURNACE_FIRE_CRACKLE, SoundCategory.BLOCKS, 1.0F, 1.0F, false);
  104. }
  105.  
  106. switch (enumfacing)
  107. {
  108. case WEST:
  109. worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - 0.52D, d1, d2 + d4, 0.0D, 0.0D, 0.0D);
  110. worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 - 0.52D, d1, d2 + d4, 0.0D, 0.0D, 0.0D);
  111. break;
  112. case EAST:
  113. worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + 0.52D, d1, d2 + d4, 0.0D, 0.0D, 0.0D);
  114. worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + 0.52D, d1, d2 + d4, 0.0D, 0.0D, 0.0D);
  115. break;
  116. case NORTH:
  117. worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - 0.52D, 0.0D, 0.0D, 0.0D);
  118. worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4, d1, d2 - 0.52D, 0.0D, 0.0D, 0.0D);
  119. break;
  120. case SOUTH:
  121. worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + 0.52D, 0.0D, 0.0D, 0.0D);
  122. worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4, d1, d2 + 0.52D, 0.0D, 0.0D, 0.0D);
  123. }
  124. }
  125. }
  126.  
  127. /**
  128. * Called when the block is right clicked by a player.
  129. */
  130. public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
  131. {
  132. if (worldIn.isRemote)
  133. {
  134. return true;
  135. }
  136. else
  137. {
  138. TileEntity tileentity = worldIn.getTileEntity(pos);
  139.  
  140. if (tileentity instanceof TileEntitySteampunkFurnace)
  141. {
  142. playerIn.displayGUIChest((TileEntitySteampunkFurnace)tileentity);
  143. playerIn.addStat(StatList.FURNACE_INTERACTION);
  144. }
  145.  
  146. return true;
  147. }
  148. }
  149.  
  150. public static void setState(boolean active, World worldIn, BlockPos pos)
  151. {
  152. IBlockState iblockstate = worldIn.getBlockState(pos);
  153. TileEntity tileentity = worldIn.getTileEntity(pos);
  154. keepInventory = true;
  155.  
  156. if (active)
  157. {
  158. worldIn.setBlockState(pos, FactoryBlock.litsteampunkfurnace.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
  159. worldIn.setBlockState(pos, FactoryBlock.litsteampunkfurnace.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
  160. }
  161. else
  162. {
  163. worldIn.setBlockState(pos, FactoryBlock.steampunkfurnace.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
  164. worldIn.setBlockState(pos, FactoryBlock.steampunkfurnace.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
  165. }
  166.  
  167. keepInventory = false;
  168.  
  169. if (tileentity != null)
  170. {
  171. tileentity.validate();
  172. worldIn.setTileEntity(pos, tileentity);
  173. }
  174. }
  175.  
  176. /**
  177. * Returns a new instance of a block's tile entity class. Called on placing the block.
  178. */
  179. public TileEntity createNewTileEntity(World worldIn, int meta)
  180. {
  181. return new TileEntitySteampunkFurnace();
  182. }
  183.  
  184. /**
  185. * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
  186. * IBlockstate
  187. */
  188. public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  189. {
  190. return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
  191. }
  192.  
  193. /**
  194. * Called by ItemBlocks after a block is set in the world, to allow post-place logic
  195. */
  196. public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
  197. {
  198. worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()), 2);
  199.  
  200. if (stack.hasDisplayName())
  201. {
  202. TileEntity tileentity = worldIn.getTileEntity(pos);
  203.  
  204. if (tileentity instanceof TileEntitySteampunkFurnace)
  205. {
  206. ((TileEntitySteampunkFurnace)tileentity).setCustomInventoryName(stack.getDisplayName());
  207. }
  208. }
  209. }
  210.  
  211. /**
  212. * Called serverside after this block is replaced with another in Chunk, but before the Tile Entity is updated
  213. */
  214. public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
  215. {
  216. if (!keepInventory)
  217. {
  218. TileEntity tileentity = worldIn.getTileEntity(pos);
  219.  
  220. if (tileentity instanceof TileEntitySteampunkFurnace)
  221. {
  222. InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntitySteampunkFurnace)tileentity);
  223. worldIn.updateComparatorOutputLevel(pos, this);
  224. }
  225. }
  226.  
  227. super.breakBlock(worldIn, pos, state);
  228. }
  229.  
  230. public boolean hasComparatorInputOverride(IBlockState state)
  231. {
  232. return true;
  233. }
  234.  
  235. public int getComparatorInputOverride(IBlockState blockState, World worldIn, BlockPos pos)
  236. {
  237. return Container.calcRedstone(worldIn.getTileEntity(pos));
  238. }
  239.  
  240. public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state)
  241. {
  242. return new ItemStack(FactoryBlock.steampunkfurnace);
  243. }
  244.  
  245. /**
  246. * The type of render function called. MODEL for mixed tesr and static model, MODELBLOCK_ANIMATED for TESR-only,
  247. * LIQUID for vanilla liquids, INVISIBLE to skip all rendering
  248. */
  249. public EnumBlockRenderType getRenderType(IBlockState state)
  250. {
  251. return EnumBlockRenderType.MODEL;
  252. }
  253.  
  254. /**
  255. * Convert the given metadata into a BlockState for this Block
  256. */
  257. public IBlockState getStateFromMeta(int meta)
  258. {
  259. EnumFacing enumfacing = EnumFacing.getFront(meta);
  260.  
  261. if (enumfacing.getAxis() == EnumFacing.Axis.Y)
  262. {
  263. enumfacing = EnumFacing.NORTH;
  264. }
  265.  
  266. return this.getDefaultState().withProperty(FACING, enumfacing);
  267. }
  268.  
  269. /**
  270. * Convert the BlockState into the correct metadata value
  271. */
  272. public int getMetaFromState(IBlockState state)
  273. {
  274. return ((EnumFacing)state.getValue(FACING)).getIndex();
  275. }
  276.  
  277. /**
  278. * Returns the blockstate with the given rotation from the passed blockstate. If inapplicable, returns the passed
  279. * blockstate.
  280. */
  281. public IBlockState withRotation(IBlockState state, Rotation rot)
  282. {
  283. return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
  284. }
  285.  
  286. /**
  287. * Returns the blockstate with the given mirror of the passed blockstate. If inapplicable, returns the passed
  288. * blockstate.
  289. */
  290. public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
  291. {
  292. return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING)));
  293. }
  294.  
  295. protected BlockStateContainer createBlockState()
  296. {
  297. return new BlockStateContainer(this, new IProperty[] {FACING});
  298. }
  299. }
RAW Paste Data