Advertisement
Creepinson

d

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