Advertisement
Cmonster910

TNT block

Jun 23rd, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. package com.Cmonster.OreMod.blocks;
  2.  
  3. import com.Cmonster.OreMod.Reference;
  4. import com.Cmonster.OreMod.creativetabs.TabElements;
  5. import com.Cmonster.OreMod.entities.EntityJohn_tnt;
  6.  
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.SoundType;
  9. import net.minecraft.block.material.Material;
  10. import net.minecraft.block.properties.IProperty;
  11. import net.minecraft.block.properties.PropertyBool;
  12. import net.minecraft.block.state.BlockStateContainer;
  13. import net.minecraft.block.state.IBlockState;
  14. import net.minecraft.creativetab.CreativeTabs;
  15. import net.minecraft.entity.Entity;
  16. import net.minecraft.entity.EntityLivingBase;
  17. import net.minecraft.entity.item.EntityTNTPrimed;
  18. import net.minecraft.entity.player.EntityPlayer;
  19. import net.minecraft.entity.projectile.EntityArrow;
  20. import net.minecraft.init.Blocks;
  21. import net.minecraft.init.Items;
  22. import net.minecraft.init.SoundEvents;
  23. import net.minecraft.item.ItemStack;
  24. import net.minecraft.util.EnumFacing;
  25. import net.minecraft.util.EnumHand;
  26. import net.minecraft.util.SoundCategory;
  27. import net.minecraft.util.math.BlockPos;
  28. import net.minecraft.world.Explosion;
  29. import net.minecraft.world.World;
  30.  
  31. public class BlockJohnny_tnt extends Block
  32. {
  33.  
  34. public static final PropertyBool EXPLODE = PropertyBool.create("explode");
  35.  
  36. public BlockJohnny_tnt()
  37. {
  38. super(Material.TNT);
  39. this.setUnlocalizedName(Reference.OreModBlocks.JOHNNY_TNT.getUnlocalizedName());
  40. this.setRegistryName(Reference.OreModBlocks.JOHNNY_TNT.getRegistryName());
  41. this.setDefaultState(this.blockState.getBaseState().withProperty(EXPLODE, Boolean.valueOf(false)));
  42. this.setSoundType(SoundType.PLANT);
  43. this.setCreativeTab(TabElements.tab_elements);
  44. }
  45.  
  46. /**
  47. * Called after the block is set in the Chunk data, but before the Tile Entity is set
  48. */
  49. public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
  50. {
  51. super.onBlockAdded(worldIn, pos, state);
  52.  
  53. if (worldIn.isBlockPowered(pos))
  54. {
  55. this.onBlockDestroyedByPlayer(worldIn, pos, state.withProperty(EXPLODE, Boolean.valueOf(true)));
  56. worldIn.setBlockToAir(pos);
  57. }
  58. }
  59.  
  60. /**
  61. * Called when a neighboring block was changed and marks that this state should perform any checks during a neighbor
  62. * change. Cases may include when redstone power is updated, cactus blocks popping off due to a neighboring solid
  63. * block, etc.
  64. */
  65. public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos)
  66. {
  67. if (worldIn.isBlockPowered(pos))
  68. {
  69. this.onBlockDestroyedByPlayer(worldIn, pos, state.withProperty(EXPLODE, Boolean.valueOf(true)));
  70. worldIn.setBlockToAir(pos);
  71. }
  72. }
  73.  
  74. /**
  75. * Called when this Block is destroyed by an Explosion
  76. */
  77. public void onBlockDestroyedByExplosion(World worldIn, BlockPos pos, Explosion explosionIn)
  78. {
  79. if (!worldIn.isRemote)
  80. {
  81. EntityJohn_tnt entitytntprimed = new EntityJohn_tnt(worldIn, (double)((float)pos.getX() + 0.5F), (double)pos.getY(), (double)((float)pos.getZ() + 0.5F), explosionIn.getExplosivePlacedBy());
  82. entitytntprimed.setFuse((short)(worldIn.rand.nextInt(entitytntprimed.getFuse() / 4) + entitytntprimed.getFuse() / 8));
  83. worldIn.spawnEntity(entitytntprimed);
  84. }
  85. }
  86.  
  87. /**
  88. * Called when a player destroys this Block
  89. */
  90. public void onBlockDestroyedByPlayer(World worldIn, BlockPos pos, IBlockState state)
  91. {
  92. this.explode(worldIn, pos, state, (EntityLivingBase)null);
  93. }
  94.  
  95. public void explode(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase igniter)
  96. {
  97. if (!worldIn.isRemote)
  98. {
  99. if (((Boolean)state.getValue(EXPLODE)).booleanValue())
  100. {
  101. EntityJohn_tnt entityjohn_tnt = new EntityJohn_tnt(worldIn, (double)((float)pos.getX() + 0.5F), (double)pos.getY(), (double)((float)pos.getZ() + 0.5F), igniter);
  102. worldIn.spawnEntity(entityjohn_tnt);
  103. worldIn.playSound((EntityPlayer)null, entityjohn_tnt.posX, entityjohn_tnt.posY, entityjohn_tnt.posZ, SoundEvents.ENTITY_TNT_PRIMED, SoundCategory.BLOCKS, 1.0F, 1.0F);
  104. }
  105. }
  106. }
  107.  
  108. /**
  109. * Called when the block is right clicked by a player.
  110. */
  111. public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
  112. {
  113. ItemStack itemstack = playerIn.getHeldItem(hand);
  114.  
  115. if (!itemstack.isEmpty() && (itemstack.getItem() == Items.FLINT_AND_STEEL || itemstack.getItem() == Items.FIRE_CHARGE))
  116. {
  117. this.explode(worldIn, pos, state.withProperty(EXPLODE, Boolean.valueOf(true)), playerIn);
  118. worldIn.setBlockState(pos, Blocks.AIR.getDefaultState(), 11);
  119.  
  120. if (itemstack.getItem() == Items.FLINT_AND_STEEL)
  121. {
  122. itemstack.damageItem(1, playerIn);
  123. }
  124. else if (!playerIn.capabilities.isCreativeMode)
  125. {
  126. itemstack.shrink(1);
  127. }
  128.  
  129. return true;
  130. }
  131. else
  132. {
  133. return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
  134. }
  135. }
  136.  
  137. /**
  138. * Called When an Entity Collided with the Block
  139. */
  140. public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
  141. {
  142. if (!worldIn.isRemote && entityIn instanceof EntityArrow)
  143. {
  144. EntityArrow entityarrow = (EntityArrow)entityIn;
  145.  
  146. if (entityarrow.isBurning())
  147. {
  148. this.explode(worldIn, pos, worldIn.getBlockState(pos).withProperty(EXPLODE, Boolean.valueOf(true)), entityarrow.shootingEntity instanceof EntityLivingBase ? (EntityLivingBase)entityarrow.shootingEntity : null);
  149. worldIn.setBlockToAir(pos);
  150. }
  151. }
  152. }
  153.  
  154. /**
  155. * Return whether this block can drop from an explosion.
  156. */
  157. public boolean canDropFromExplosion(Explosion explosionIn)
  158. {
  159. return false;
  160. }
  161.  
  162. /**
  163. * Convert the BlockState into the correct metadata value
  164. */
  165. public int getMetaFromState(IBlockState state)
  166. {
  167. return ((Boolean)state.getValue(EXPLODE)).booleanValue() ? 1 : 0;
  168. }
  169.  
  170. protected BlockStateContainer createBlockState()
  171. {
  172. return new BlockStateContainer(this, new IProperty[] {EXPLODE});
  173. }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement