Advertisement
Creepinson

block

Jun 22nd, 2017
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.39 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.tileentity.TEBulb;
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.BlockContainer;
  9. import net.minecraft.block.material.Material;
  10. import net.minecraft.block.properties.PropertyDirection;
  11. import net.minecraft.block.state.BlockStateContainer;
  12. import net.minecraft.block.state.IBlockState;
  13. import net.minecraft.creativetab.CreativeTabs;
  14. import net.minecraft.entity.EntityLivingBase;
  15. import net.minecraft.entity.player.EntityPlayer;
  16. import net.minecraft.init.Blocks;
  17. import net.minecraft.item.ItemStack;
  18. import net.minecraft.tileentity.TileEntity;
  19. import net.minecraft.util.EnumBlockRenderType;
  20. import net.minecraft.util.EnumFacing;
  21. import net.minecraft.util.Mirror;
  22. import net.minecraft.util.Rotation;
  23. import net.minecraft.util.math.AxisAlignedBB;
  24. import net.minecraft.util.math.BlockPos;
  25. import net.minecraft.world.IBlockAccess;
  26. import net.minecraft.world.World;
  27.  
  28. public class Bulb extends BlockContainer {
  29. protected static final AxisAlignedBB DEFAULT_AABB = new AxisAlignedBB(0.25D, 0.0D, 0.25D, 0.75D, 0.5D, 0.75D);
  30. protected static final AxisAlignedBB NORTH_AABB = new AxisAlignedBB(0.25D, 0.25D, 0.5D, 0.75D, 0.75D, 1.0D);
  31. protected static final AxisAlignedBB SOUTH_AABB = new AxisAlignedBB(0.25D, 0.25D, 0.0D, 0.75D, 0.75D, 0.5D);
  32. protected static final AxisAlignedBB WEST_AABB = new AxisAlignedBB(0.5D, 0.25D, 0.25D, 1.0D, 0.75D, 0.75D);
  33. protected static final AxisAlignedBB EAST_AABB = new AxisAlignedBB(0.0D, 0.25D, 0.25D, 0.5D, 0.75D, 0.75D);
  34. public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.VERTICAL);
  35.  
  36. private boolean isOn;
  37. public EnumBlockRenderType getRenderType(IBlockState state)
  38. {
  39. return EnumBlockRenderType.MODEL;
  40. }
  41.  
  42.  
  43. public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
  44. {
  45. if (!worldIn.isRemote)
  46. {
  47. if (this.isOn && !worldIn.isBlockPowered(pos))
  48. {
  49. this.isOn = false;
  50. worldIn.scheduleUpdate(pos, this, 4);
  51.  
  52. }
  53. else if (!this.isOn && worldIn.isBlockPowered(pos))
  54. {
  55. this.isOn = true;
  56. worldIn.scheduleUpdate(pos, this, 4);
  57. }
  58. }
  59. }
  60.  
  61.  
  62. public Bulb(Material mat, String name, CreativeTabs tab, float hardness, float resistance, int harvest,
  63. String tool) {
  64. super(mat);
  65. setUnlocalizedName(name);
  66. setRegistryName(name);
  67. setCreativeTab(tab);
  68. setHardness(hardness);
  69. setResistance(resistance);
  70. setHarvestLevel(tool, harvest);
  71. this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.DOWN));
  72. this.isOn = false;
  73.  
  74. }
  75.  
  76. private int ticks;
  77.  
  78. public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn)
  79. {
  80. if (!worldIn.isRemote)
  81. {
  82. if (this.isOn && !worldIn.isBlockPowered(pos))
  83. {
  84. this.isOn = false;
  85. worldIn.scheduleUpdate(pos, this, 4);
  86. }
  87. else if (!this.isOn && worldIn.isBlockPowered(pos))
  88. {
  89. this.isOn = true;
  90. worldIn.scheduleUpdate(pos, this, 4);
  91. }
  92. }
  93. }
  94. @Override
  95. public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand) {
  96. if (!worldIn.isRemote)
  97. {
  98. if (isOn)
  99. {
  100. this.setLightLevel(1.0F);
  101. }
  102. if (this.isOn && !worldIn.isBlockPowered(pos))
  103. {
  104. this.isOn = false;
  105. worldIn.scheduleUpdate(pos, this, 4);
  106. }
  107. }
  108. if (ticks == 60) {
  109.  
  110. ticks = 0;
  111.  
  112. }
  113.  
  114. ticks++;
  115.  
  116. super.updateTick(worldIn, pos, state, rand);
  117. }
  118.  
  119. @Override
  120. public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player) {
  121.  
  122. worldIn.removeTileEntity(pos);
  123.  
  124. this.dropBlockAsItem(worldIn, pos, state, 0);
  125.  
  126. super.onBlockHarvested(worldIn, pos, state, player);
  127. }
  128.  
  129. public boolean isOpaqueCube(IBlockState state) {
  130. return false;
  131. }
  132.  
  133. public boolean isFullCube(IBlockState state) {
  134. return false;
  135. }
  136.  
  137. public IBlockState getStateFromMeta(int meta) {
  138. return this.getDefaultState().withProperty(FACING, EnumFacing.getFront(meta & 7));
  139. }
  140.  
  141. public int getMetaFromState(IBlockState state) {
  142. int i = 0;
  143. i = i | ((EnumFacing) state.getValue(FACING)).getIndex();
  144.  
  145. return i;
  146. }
  147.  
  148. public IBlockState withRotation(IBlockState state, Rotation rot) {
  149. return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING)));
  150. }
  151.  
  152. public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
  153. return state.withRotation(mirrorIn.toRotation((EnumFacing) state.getValue(FACING)));
  154. }
  155.  
  156. protected BlockStateContainer createBlockState() {
  157. return new BlockStateContainer(this, FACING);
  158. }
  159.  
  160. @Override
  161. public boolean canPlaceTorchOnTop(IBlockState state, IBlockAccess world, BlockPos pos) {
  162. return false;
  163. }
  164.  
  165. @Override
  166. public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY,
  167. float hitZ, int meta, EntityLivingBase placer, ItemStack stack) {
  168.  
  169. if(facing == EnumFacing.NORTH || facing == EnumFacing.SOUTH || facing == EnumFacing.WEST || facing == EnumFacing.EAST){
  170. this.dropBlockAsItem(world, pos, this.getDefaultState(), 0);
  171. return Blocks.AIR.getDefaultState();
  172. }
  173. else{
  174.  
  175. return this.getDefaultState().withProperty(FACING, facing.getOpposite());
  176.  
  177. }
  178.  
  179. }
  180. @Override
  181. public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ,
  182. int meta, EntityLivingBase placer) {
  183. if(facing == EnumFacing.NORTH || facing == EnumFacing.SOUTH || facing == EnumFacing.WEST || facing == EnumFacing.EAST){
  184. this.dropBlockAsItem(worldIn, pos, this.getDefaultState(), 0);
  185. return Blocks.AIR.getDefaultState();
  186. }
  187. else{
  188.  
  189. return this.getDefaultState().withProperty(FACING, facing.getOpposite());
  190.  
  191. }
  192.  
  193. }
  194.  
  195. @Override
  196. public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer,
  197. ItemStack stack) {
  198.  
  199.  
  200. if (this.isNextToLeaves(pos, world, false)) {
  201. this.dropBlockAsItem(world, pos, state, 0);
  202. world.setBlockState(pos, Blocks.AIR.getDefaultState());
  203. return;
  204.  
  205. }
  206. // if (this.isNextToBulb(pos, world, false)) {
  207. // this.dropBlockAsItem(world, pos, state, 0);
  208. // world.setBlockState(pos, Blocks.AIR.getDefaultState());
  209. // return;
  210. //
  211. // }
  212. super.onBlockPlacedBy(world, pos, state, placer, stack);
  213. }
  214.  
  215. // @Override
  216. // public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY,
  217. // float hitZ, int meta, EntityLivingBase placer, ItemStack stack) {
  218. //if(facing.getAxis().isVertical()){
  219. // return this.getDefaultState().withProperty(FACING, facing.getOpposite());
  220. // }
  221. //else return this.getDefaultState().withProperty(FACING, EnumFacing.DOWN);
  222. // }
  223.  
  224. @Override
  225. public TileEntity createNewTileEntity(World worldIn, int meta) {
  226.  
  227. return new TEBulb();
  228. }
  229.  
  230. public boolean isNextToLeaves(BlockPos pos, World world, boolean checkNextBlock) {
  231. IBlockState state;
  232. // check the block above
  233. state = world.getBlockState(pos.up());
  234. if (state.getBlock().equals(Blocks.LEAVES)) {
  235. return true;
  236. } else {
  237. if (checkNextBlock)
  238. if (isNextToLeaves(pos.up(), world, false))
  239. return false;
  240. }
  241.  
  242. // check the block below
  243. state = world.getBlockState(pos.down());
  244. if (state.getBlock().equals(Blocks.LEAVES)) {
  245. return true;
  246. } else {
  247. if (checkNextBlock)
  248. if (isNextToLeaves(pos.up(), world, false))
  249. return false;
  250. }
  251. // check the nothen block
  252. state = world.getBlockState(pos.north());
  253. if (state.getBlock().equals(Blocks.LEAVES)) {
  254. return true;
  255. } else {
  256. if (checkNextBlock)
  257. if (isNextToLeaves(pos.up(), world, false))
  258. return false;
  259. }
  260. // check the southen block
  261. state = world.getBlockState(pos.south());
  262. if (state.getBlock().equals(Blocks.LEAVES)) {
  263. return true;
  264. } else {
  265. if (checkNextBlock)
  266. if (isNextToLeaves(pos.up(), world, false))
  267. return false;
  268. }
  269. // check the eastern block
  270. state = world.getBlockState(pos.east());
  271. if (state.getBlock().equals(Blocks.LEAVES)) {
  272. return true;
  273. } else {
  274. if (checkNextBlock)
  275. if (isNextToLeaves(pos.up(), world, false))
  276. return false;
  277. }
  278. // Check the western block
  279. state = world.getBlockState(pos.west());
  280. if (state.getBlock().equals(Blocks.LEAVES)) {
  281. return true;
  282. } else {
  283. if (checkNextBlock)
  284. if (isNextToLeaves(pos.up(), world, false))
  285. return false;
  286. }
  287. return false;
  288. }
  289.  
  290. public boolean isNextToBulb(BlockPos pos, World world, boolean checkNextBlock) {
  291. IBlockState state;
  292. // check the block above
  293. state = world.getBlockState(pos.up());
  294. if (state.getBlock().equals(BlockHandler.bulb)) {
  295. return true;
  296. } else {
  297. if (checkNextBlock)
  298. if (isNextToLeaves(pos.up(), world, false))
  299. return false;
  300. }
  301.  
  302. // check the block below
  303. state = world.getBlockState(pos.down());
  304. if (state.getBlock().equals(BlockHandler.bulb)) {
  305. return true;
  306. } else {
  307. if (checkNextBlock)
  308. if (isNextToLeaves(pos.up(), world, false))
  309. return false;
  310. }
  311. // check the nothen block
  312. state = world.getBlockState(pos.north());
  313. if (state.getBlock().equals(BlockHandler.bulb)) {
  314. return true;
  315. } else {
  316. if (checkNextBlock)
  317. if (isNextToLeaves(pos.up(), world, false))
  318. return false;
  319. }
  320. // check the southen block
  321. state = world.getBlockState(pos.south());
  322. if (state.getBlock().equals(BlockHandler.bulb)) {
  323. return true;
  324. } else {
  325. if (checkNextBlock)
  326. if (isNextToLeaves(pos.up(), world, false))
  327. return false;
  328. }
  329. // check the eastern block
  330. state = world.getBlockState(pos.east());
  331. if (state.getBlock().equals(BlockHandler.bulb)) {
  332. return true;
  333. } else {
  334. if (checkNextBlock)
  335. if (isNextToLeaves(pos.up(), world, false))
  336. return false;
  337. }
  338. // Check the western block
  339. state = world.getBlockState(pos.west());
  340. if (state.getBlock().equals(BlockHandler.bulb)) {
  341. return true;
  342. } else {
  343. if (checkNextBlock)
  344. if (isNextToLeaves(pos.up(), world, false))
  345. return false;
  346. }
  347. return false;
  348. }
  349.  
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement