Guest User

woodenfurnace

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