Advertisement
Creepinson

bulb

Jun 19th, 2017
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. package me.creepinson.block;
  2.  
  3. import java.util.Random;
  4.  
  5. import me.creepinson.handler.BlockHandler;
  6. import me.creepinson.lib.util.Utils;
  7. import me.creepinson.tileentity.TEBulb;
  8. import me.creepinson.util.helper.WorldHelper;
  9. import net.minecraft.block.BlockDirectional;
  10. import net.minecraft.block.ITileEntityProvider;
  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.creativetab.CreativeTabs;
  17. import net.minecraft.entity.EntityLivingBase;
  18. import net.minecraft.entity.player.EntityPlayer;
  19. import net.minecraft.init.Blocks;
  20. import net.minecraft.item.ItemStack;
  21. import net.minecraft.tileentity.TileEntity;
  22. import net.minecraft.util.EnumFacing;
  23. import net.minecraft.util.EnumHand;
  24. import net.minecraft.util.Mirror;
  25. import net.minecraft.util.Rotation;
  26. import net.minecraft.util.math.AxisAlignedBB;
  27. import net.minecraft.util.math.BlockPos;
  28. import net.minecraft.world.IBlockAccess;
  29. import net.minecraft.world.World;
  30.  
  31. public class Bulb extends ModBlocks implements ITileEntityProvider {
  32. protected static final AxisAlignedBB DEFAULT_AABB = new AxisAlignedBB(0.25D, 0.0D, 0.25D, 0.75D, 0.5D, 0.75D);
  33. protected static final AxisAlignedBB NORTH_AABB = new AxisAlignedBB(0.25D, 0.25D, 0.5D, 0.75D, 0.75D, 1.0D);
  34. protected static final AxisAlignedBB SOUTH_AABB = new AxisAlignedBB(0.25D, 0.25D, 0.0D, 0.75D, 0.75D, 0.5D);
  35. protected static final AxisAlignedBB WEST_AABB = new AxisAlignedBB(0.5D, 0.25D, 0.25D, 1.0D, 0.75D, 0.75D);
  36. protected static final AxisAlignedBB EAST_AABB = new AxisAlignedBB(0.0D, 0.25D, 0.25D, 0.5D, 0.75D, 0.75D);
  37. public static final PropertyDirection FACING = BlockDirectional.FACING;
  38. private boolean powered;
  39.  
  40. public void setPowered(boolean newPower) {
  41.  
  42. powered = newPower;
  43.  
  44. }
  45.  
  46. public boolean isPowered() {
  47. return powered;
  48. }
  49.  
  50. public Bulb(Material mat, String name, CreativeTabs tab, float hardness, float resistance, int harvest,
  51. String tool) {
  52. super(mat, name, tab, hardness, resistance, harvest, tool);
  53. this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
  54. this.setPowered(false);
  55. }
  56.  
  57. private int ticks;
  58.  
  59. @Override
  60. public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand,
  61. ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
  62. if (world.isRemote)
  63. return false;
  64.  
  65. if (this.isPowered()) {
  66. Utils.getLogger().info("Powering off");
  67. setLightLevel(0.0F);
  68. this.setPowered(false);
  69. world.notifyLightSet(pos);
  70. return true;
  71. }
  72.  
  73. if (!this.isPowered()) {
  74. Utils.getLogger().info("Powering on");
  75. setLightLevel(1.0F);
  76. world.notifyLightSet(pos);
  77. this.setPowered(true);
  78.  
  79. return true;
  80. }
  81.  
  82. return true;
  83. }
  84.  
  85. @Override
  86. public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand) {
  87.  
  88. if (ticks == 60) {
  89.  
  90. ticks = 0;
  91.  
  92. }
  93.  
  94. ticks++;
  95.  
  96. super.updateTick(worldIn, pos, state, rand);
  97. }
  98.  
  99. @Override
  100. public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player) {
  101.  
  102. worldIn.removeTileEntity(pos);
  103.  
  104. this.dropBlockAsItem(worldIn, pos, state, 0);
  105.  
  106. super.onBlockHarvested(worldIn, pos, state, player);
  107. }
  108.  
  109. public boolean isOpaqueCube(IBlockState state) {
  110. return false;
  111. }
  112.  
  113. public boolean isFullCube(IBlockState state) {
  114. return false;
  115. }
  116.  
  117. public IBlockState getStateFromMeta(int meta) {
  118. return this.getDefaultState().withProperty(FACING, EnumFacing.getFront(meta & 7));
  119. }
  120.  
  121. public int getMetaFromState(IBlockState state) {
  122. int i = 0;
  123. i = i | ((EnumFacing) state.getValue(FACING)).getIndex();
  124.  
  125. return i;
  126. }
  127.  
  128. public IBlockState withRotation(IBlockState state, Rotation rot) {
  129. return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING)));
  130. }
  131.  
  132. public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
  133. return state.withRotation(mirrorIn.toRotation((EnumFacing) state.getValue(FACING)));
  134. }
  135.  
  136. protected BlockStateContainer createBlockState() {
  137. return new BlockStateContainer(this, new IProperty[] { FACING });
  138. }
  139.  
  140. @Override
  141. public boolean canPlaceTorchOnTop(IBlockState state, IBlockAccess world, BlockPos pos) {
  142. return false;
  143. }
  144.  
  145. @Override
  146. public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer,
  147. ItemStack stack) {
  148. world.setBlockState(pos, state.withProperty(FACING, WorldHelper.getFacingFromEntity(pos, placer)), 2);
  149.  
  150. if (this.isNextToLeaves(pos, world, false)) {
  151. this.dropBlockAsItem(world, pos, state, 0);
  152. world.setBlockState(pos, Blocks.AIR.getDefaultState());
  153. return;
  154.  
  155. }
  156. if (this.isNextToBulb(pos, world, false)) {
  157. this.dropBlockAsItem(world, pos, state, 0);
  158. world.setBlockState(pos, Blocks.AIR.getDefaultState());
  159. return;
  160.  
  161. }
  162.  
  163. super.onBlockPlacedBy(world, pos, state, placer, stack);
  164. }
  165.  
  166. @Override
  167. public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY,
  168. float hitZ, int meta, EntityLivingBase placer, ItemStack stack) {
  169.  
  170. return this.blockState.getBaseState().withProperty(FACING, facing.getOpposite());
  171. }
  172.  
  173. @Override
  174. public TileEntity createNewTileEntity(World worldIn, int meta) {
  175.  
  176. return new TEBulb();
  177. }
  178.  
  179. public boolean isNextToLeaves(BlockPos pos, World world, boolean checkNextBlock) {
  180. IBlockState state;
  181. // check the block above
  182. state = world.getBlockState(pos.up());
  183. if (state.getBlock().equals(Blocks.LEAVES)) {
  184. return true;
  185. } else {
  186. if (checkNextBlock)
  187. if (isNextToLeaves(pos.up(), world, false))
  188. return false;
  189. }
  190.  
  191. // check the block below
  192. state = world.getBlockState(pos.down());
  193. if (state.getBlock().equals(Blocks.LEAVES)) {
  194. return true;
  195. } else {
  196. if (checkNextBlock)
  197. if (isNextToLeaves(pos.up(), world, false))
  198. return false;
  199. }
  200. // check the nothen block
  201. state = world.getBlockState(pos.north());
  202. if (state.getBlock().equals(Blocks.LEAVES)) {
  203. return true;
  204. } else {
  205. if (checkNextBlock)
  206. if (isNextToLeaves(pos.up(), world, false))
  207. return false;
  208. }
  209. // check the southen block
  210. state = world.getBlockState(pos.south());
  211. if (state.getBlock().equals(Blocks.LEAVES)) {
  212. return true;
  213. } else {
  214. if (checkNextBlock)
  215. if (isNextToLeaves(pos.up(), world, false))
  216. return false;
  217. }
  218. // check the eastern block
  219. state = world.getBlockState(pos.east());
  220. if (state.getBlock().equals(Blocks.LEAVES)) {
  221. return true;
  222. } else {
  223. if (checkNextBlock)
  224. if (isNextToLeaves(pos.up(), world, false))
  225. return false;
  226. }
  227. // Check the western block
  228. state = world.getBlockState(pos.west());
  229. if (state.getBlock().equals(Blocks.LEAVES)) {
  230. return true;
  231. } else {
  232. if (checkNextBlock)
  233. if (isNextToLeaves(pos.up(), world, false))
  234. return false;
  235. }
  236. return false;
  237. }
  238.  
  239. public boolean isNextToBulb(BlockPos pos, World world, boolean checkNextBlock) {
  240. IBlockState state;
  241. // check the block above
  242. state = world.getBlockState(pos.up());
  243. if (state.getBlock().equals(BlockHandler.bulb)) {
  244. return true;
  245. } else {
  246. if (checkNextBlock)
  247. if (isNextToLeaves(pos.up(), world, false))
  248. return false;
  249. }
  250.  
  251. // check the block below
  252. state = world.getBlockState(pos.down());
  253. if (state.getBlock().equals(BlockHandler.bulb)) {
  254. return true;
  255. } else {
  256. if (checkNextBlock)
  257. if (isNextToLeaves(pos.up(), world, false))
  258. return false;
  259. }
  260. // check the nothen block
  261. state = world.getBlockState(pos.north());
  262. if (state.getBlock().equals(BlockHandler.bulb)) {
  263. return true;
  264. } else {
  265. if (checkNextBlock)
  266. if (isNextToLeaves(pos.up(), world, false))
  267. return false;
  268. }
  269. // check the southen block
  270. state = world.getBlockState(pos.south());
  271. if (state.getBlock().equals(BlockHandler.bulb)) {
  272. return true;
  273. } else {
  274. if (checkNextBlock)
  275. if (isNextToLeaves(pos.up(), world, false))
  276. return false;
  277. }
  278. // check the eastern block
  279. state = world.getBlockState(pos.east());
  280. if (state.getBlock().equals(BlockHandler.bulb)) {
  281. return true;
  282. } else {
  283. if (checkNextBlock)
  284. if (isNextToLeaves(pos.up(), world, false))
  285. return false;
  286. }
  287. // Check the western block
  288. state = world.getBlockState(pos.west());
  289. if (state.getBlock().equals(BlockHandler.bulb)) {
  290. return true;
  291. } else {
  292. if (checkNextBlock)
  293. if (isNextToLeaves(pos.up(), world, false))
  294. return false;
  295. }
  296. return false;
  297. }
  298.  
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement