BlockLantern.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package me.km.blocks;
  2. import java.util.Random;
  3. import me.km.KajetansMod;
  4. import net.minecraft.block.Block;
  5. import net.minecraft.block.BlockDirectional;
  6. import net.minecraft.block.SoundType;
  7. import net.minecraft.block.material.Material;
  8. import net.minecraft.block.properties.IProperty;
  9. import net.minecraft.block.state.BlockFaceShape;
  10. import net.minecraft.block.state.BlockStateContainer;
  11. import net.minecraft.block.state.IBlockState;
  12. import net.minecraft.creativetab.CreativeTabs;
  13. import net.minecraft.entity.EntityLivingBase;
  14. import net.minecraft.item.Item;
  15. import net.minecraft.util.BlockRenderLayer;
  16. import net.minecraft.util.EnumFacing;
  17. import net.minecraft.util.EnumParticleTypes;
  18. import net.minecraft.util.Mirror;
  19. import net.minecraft.util.Rotation;
  20. import net.minecraft.util.math.AxisAlignedBB;
  21. import net.minecraft.util.math.BlockPos;
  22. import net.minecraft.world.IBlockAccess;
  23. import net.minecraft.world.World;
  24. import net.minecraftforge.fml.relauncher.Side;
  25. import net.minecraftforge.fml.relauncher.SideOnly;
  26. public class BlockLantern extends BlockDirectional implements IBlockBase
  27. {
  28. protected static final AxisAlignedBB HANGING_AABB = new AxisAlignedBB(0.1875d, 0.125d, 0.1875d, 0.8125d, 1, 0.875d);
  29. protected static final AxisAlignedBB STANDING_AABB = new AxisAlignedBB(0.1875d, 0, 0.1875d, 0.8125d, 0.75d, 0.875d);
  30. protected static final AxisAlignedBB TORCH_NORTH_AABB = new AxisAlignedBB(0.1875d, 0, 0.375d, 0.8125d, 1, 1);
  31. protected static final AxisAlignedBB TORCH_SOUTH_AABB = new AxisAlignedBB(0.1875d, 0, 0, 0.8125d, 1, 0.625d);
  32. protected static final AxisAlignedBB TORCH_WEST_AABB = new AxisAlignedBB(0.375d, 0, 0.1875d, 1, 1, 0.8125d);
  33. protected static final AxisAlignedBB TORCH_EAST_AABB = new AxisAlignedBB(0, 0, 0.1875d, 0.625d, 1, 0.8125d);
  34. protected String name;
  35. public BlockLantern(String name, String local)
  36. {
  37. super(Material.IRON);
  38. this.name = name;
  39. super.setHardness(0.0F);
  40. super.setLightLevel(0.9375F);
  41. super.setSoundType(SoundType.METAL);
  42. this.setRegistryName(name);
  43. super.setUnlocalizedName(local);
  44. super.setCreativeTab(CreativeTabs.DECORATIONS);
  45. this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.UP));
  46. super.setTickRandomly(true);
  47. }
  48. @Override
  49. public void registerItemModel(Item itemBlock)
  50. {
  51. KajetansMod.proxy.registerItemRenderer(itemBlock, 0, name);
  52. }
  53. @Override
  54. public BlockLantern setSoundType(SoundType sound)
  55. {
  56. super.setSoundType(sound);
  57. return this;
  58. }
  59. @Override
  60. public AxisAlignedBB getCollisionBoundingBox(IBlockState state, IBlockAccess w, BlockPos pos)
  61. {
  62. return NULL_AABB;
  63. }
  64. @Override
  65. public boolean isOpaqueCube(IBlockState state)
  66. {
  67. return false;
  68. }
  69. @Override
  70. public boolean isFullCube(IBlockState state)
  71. {
  72. return false;
  73. }
  74. @Override
  75. public boolean canPlaceBlockOnSide(World w, BlockPos pos, EnumFacing side)
  76. {
  77. return canPlaceBlock(w, pos, side);
  78. }
  79. @Override
  80. public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
  81. {
  82. for(EnumFacing enumfacing : EnumFacing.values())
  83. {
  84. if(canPlaceBlock(worldIn, pos, enumfacing))
  85. {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. protected static boolean canPlaceBlock(World w, BlockPos pos, EnumFacing direction)
  92. {
  93. BlockPos blockpos = pos.offset(direction.getOpposite());
  94. IBlockState state = w.getBlockState(blockpos);
  95. Block block = state.getBlock();
  96. if(direction == EnumFacing.UP)
  97. {
  98. return block.canPlaceTorchOnTop(state, w, pos);
  99. }
  100. else
  101. {
  102. return !isExceptBlockForAttachWithPiston(block) && state.getBlockFaceShape(w, blockpos, direction) == BlockFaceShape.SOLID;
  103. }
  104. }
  105. @Override
  106. public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  107. {
  108. return canPlaceBlock(worldIn, pos, facing) ? this.getDefaultState().withProperty(FACING, facing) : this.getDefaultState().withProperty(FACING, EnumFacing.DOWN);
  109. }
  110. @Override
  111. public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos)
  112. {
  113. if(this.checkForDrop(worldIn, pos, state) && !canPlaceBlock(worldIn, pos, state.getValue(FACING)))
  114. {
  115. this.dropBlockAsItem(worldIn, pos, state, 0);
  116. worldIn.setBlockToAir(pos);
  117. }
  118. }
  119. private boolean checkForDrop(World worldIn, BlockPos pos, IBlockState state)
  120. {
  121. if(this.canPlaceBlockAt(worldIn, pos))
  122. {
  123. return true;
  124. }
  125. else
  126. {
  127. this.dropBlockAsItem(worldIn, pos, state, 0);
  128. worldIn.setBlockToAir(pos);
  129. return false;
  130. }
  131. }
  132. @Override
  133. public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
  134. {
  135. EnumFacing enumfacing = (EnumFacing) state.getValue(FACING);
  136. switch(enumfacing)
  137. {
  138. case EAST:
  139. return TORCH_EAST_AABB;
  140. case WEST:
  141. return TORCH_WEST_AABB;
  142. case SOUTH:
  143. return TORCH_SOUTH_AABB;
  144. case NORTH:
  145. default:
  146. return TORCH_NORTH_AABB;
  147. case UP:
  148. return STANDING_AABB;
  149. case DOWN:
  150. return HANGING_AABB;
  151. }
  152. }
  153. @Override
  154. public IBlockState getStateFromMeta(int meta)
  155. {
  156. EnumFacing enumfacing;
  157. switch(meta & 7)
  158. {
  159. case 0:
  160. enumfacing = EnumFacing.DOWN;
  161. break;
  162. case 1:
  163. enumfacing = EnumFacing.EAST;
  164. break;
  165. case 2:
  166. enumfacing = EnumFacing.WEST;
  167. break;
  168. case 3:
  169. enumfacing = EnumFacing.SOUTH;
  170. break;
  171. case 4:
  172. enumfacing = EnumFacing.NORTH;
  173. break;
  174. case 5:
  175. default:
  176. enumfacing = EnumFacing.UP;
  177. }
  178. return this.getDefaultState().withProperty(FACING, enumfacing);
  179. }
  180. @Override
  181. public int getMetaFromState(IBlockState state)
  182. {
  183. switch(state.getValue(FACING))
  184. {
  185. case EAST: return 1;
  186. case WEST: return 2;
  187. case SOUTH: return 3;
  188. case NORTH: return 4;
  189. case UP:
  190. default: return 5;
  191. case DOWN: return 0;
  192. }
  193. }
  194. @Override
  195. public IBlockState withRotation(IBlockState state, Rotation rot)
  196. {
  197. return state.withProperty(FACING, rot.rotate(state.getValue(FACING)));
  198. }
  199. @Override
  200. public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
  201. {
  202. return state.withRotation(mirrorIn.toRotation(state.getValue(FACING)));
  203. }
  204. @Override
  205. protected BlockStateContainer createBlockState()
  206. {
  207. return new BlockStateContainer(this, new IProperty[] {FACING});
  208. }
  209. @Override
  210. public BlockFaceShape getBlockFaceShape(IBlockAccess p_193383_1_, IBlockState p_193383_2_, BlockPos p_193383_3_, EnumFacing p_193383_4_)
  211. {
  212. return BlockFaceShape.UNDEFINED;
  213. }
  214. @SideOnly(Side.CLIENT)
  215. @Override
  216. public void randomDisplayTick(IBlockState state, World w, BlockPos pos, Random rand)
  217. {
  218. EnumFacing face = state.getValue(FACING);
  219. double x = pos.getX() + 0.5D;
  220. double y = pos.getY() + 0.7D;
  221. double z = pos.getZ() + 0.5D;
  222. switch(face)
  223. {
  224. case EAST:
  225. case WEST:
  226. case SOUTH:
  227. case NORTH:
  228. EnumFacing oface = face.getOpposite();
  229. x += 0.1875d * oface.getFrontOffsetX();
  230. z += 0.1875d * oface.getFrontOffsetZ();
  231. break;
  232. case DOWN:
  233. break;
  234. case UP:
  235. y -= 0.125d;
  236. break;
  237. }
  238. w.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, x, y , z, 0, 0, 0);
  239. w.spawnParticle(EnumParticleTypes.FLAME, x, y, z, 0, 0, 0);
  240. }
  241. @SideOnly(Side.CLIENT)
  242. @Override
  243. public BlockRenderLayer getBlockLayer()
  244. {
  245. return BlockRenderLayer.TRANSLUCENT;
  246. }
  247. }