Advertisement
Creepinson

gg

Jun 23rd, 2017
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.34 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. worldIn.scheduleUpdate(pos, this, 2);
  128.  
  129. this.dropBlockAsItem(worldIn, pos, state, 0);
  130.  
  131. super.onBlockHarvested(worldIn, pos, state, player);
  132. }
  133.  
  134. public boolean isOpaqueCube(IBlockState state) {
  135. return false;
  136. }
  137.  
  138. public boolean isFullCube(IBlockState state) {
  139. return false;
  140. }
  141.  
  142. public IBlockState getStateFromMeta(int meta) {
  143. return this.getDefaultState().withProperty(FACING, EnumFacing.getFront(meta & 7)).withProperty(LIT, Boolean.valueOf((meta & 1) > 0));
  144. }
  145.  
  146. public int getMetaFromState(IBlockState state) {
  147. int i = 0;
  148. i = i | ((EnumFacing)state.getValue(FACING)).getIndex();
  149.  
  150. if (((Boolean)state.getValue(LIT)).booleanValue())
  151. {
  152. i |= 8;
  153. }
  154.  
  155. return i;
  156. }
  157.  
  158. public IBlockState withRotation(IBlockState state, Rotation rot) {
  159. return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING)));
  160. }
  161.  
  162. public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
  163. return state.withRotation(mirrorIn.toRotation((EnumFacing) state.getValue(FACING)));
  164. }
  165.  
  166. protected BlockStateContainer createBlockState() {
  167. return new BlockStateContainer(this, new IProperty[]{FACING,LIT});
  168. }
  169.  
  170. @Override
  171. public boolean canPlaceTorchOnTop(IBlockState state, IBlockAccess world, BlockPos pos) {
  172. return false;
  173. }
  174.  
  175. @Override
  176. public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY,
  177. float hitZ, int meta, EntityLivingBase placer, ItemStack stack) {
  178.  
  179. if (facing == EnumFacing.NORTH || facing == EnumFacing.SOUTH || facing == EnumFacing.WEST
  180. || facing == EnumFacing.EAST) {
  181. this.dropBlockAsItem(world, pos, this.getDefaultState(), 0);
  182. return Blocks.AIR.getDefaultState();
  183. } else {
  184.  
  185. return this.getDefaultState().withProperty(FACING, facing.getOpposite());
  186.  
  187. }
  188.  
  189. }
  190.  
  191. @Override
  192. public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ,
  193. int meta, EntityLivingBase placer) {
  194. if (facing == EnumFacing.NORTH || facing == EnumFacing.SOUTH || facing == EnumFacing.WEST
  195. || facing == EnumFacing.EAST) {
  196. this.dropBlockAsItem(worldIn, pos, this.getDefaultState(), 0);
  197. return Blocks.AIR.getDefaultState();
  198. } else {
  199.  
  200. return this.getDefaultState().withProperty(FACING, facing.getOpposite()).withProperty(LIT, Boolean.valueOf(false));
  201.  
  202. }
  203.  
  204. }
  205.  
  206. @Override
  207. public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer,
  208. ItemStack stack) {
  209.  
  210. if (this.isNextToLeaves(pos, world, false)) {
  211. this.dropBlockAsItem(world, pos, state, 0);
  212. world.setBlockState(pos, Blocks.AIR.getDefaultState());
  213. return;
  214. }
  215. IBlockState statey;
  216. // check the block above
  217.  
  218. statey = world.getBlockState(pos.up());
  219. if (statey.getBlock().equals(BlockHandler.bulb)) {
  220. this.dropBlockAsItem(world, pos, state, 0);
  221. world.setBlockState(pos, Blocks.AIR.getDefaultState());
  222. return;
  223.  
  224. }
  225. // check the block above
  226. IBlockState statey2;
  227. statey2 = world.getBlockState(pos.up());
  228. if (statey2.getBlock().equals(BlockHandler.bulb)) {
  229. this.dropBlockAsItem(world, pos, state, 0);
  230. world.setBlockState(pos, Blocks.AIR.getDefaultState());
  231. return;
  232.  
  233. }
  234.  
  235. // if (this.isNextToBulb(pos, world, false)) {
  236. // this.dropBlockAsItem(world, pos, state, 0);
  237. // world.setBlockState(pos, Blocks.AIR.getDefaultState());
  238. // return;
  239. //
  240. // }
  241. super.onBlockPlacedBy(world, pos, state, placer, stack);
  242. }
  243.  
  244. // @Override
  245. // public IBlockState getStateForPlacement(World world, BlockPos pos,
  246. // EnumFacing facing, float hitX, float hitY,
  247. // float hitZ, int meta, EntityLivingBase placer, ItemStack stack) {
  248. // if(facing.getAxis().isVertical()){
  249. // return this.getDefaultState().withProperty(FACING, facing.getOpposite());
  250. // }
  251. // else return this.getDefaultState().withProperty(FACING, EnumFacing.DOWN);
  252. // }
  253.  
  254. @Override
  255. public TileEntity createNewTileEntity(World worldIn, int meta) {
  256.  
  257. return new TEBulb();
  258. }
  259.  
  260. public boolean isNextToLeaves(BlockPos pos, World world, boolean checkNextBlock) {
  261. IBlockState state;
  262. // check the block above
  263. state = world.getBlockState(pos.up());
  264. if (state.getBlock().equals(Blocks.LEAVES)) {
  265. return true;
  266. } else {
  267. if (checkNextBlock)
  268. if (isNextToLeaves(pos.up(), world, false))
  269. return false;
  270. }
  271.  
  272. // check the block below
  273. state = world.getBlockState(pos.down());
  274. if (state.getBlock().equals(Blocks.LEAVES)) {
  275. return true;
  276. } else {
  277. if (checkNextBlock)
  278. if (isNextToLeaves(pos.up(), world, false))
  279. return false;
  280. }
  281. // check the nothen block
  282. state = world.getBlockState(pos.north());
  283. if (state.getBlock().equals(Blocks.LEAVES)) {
  284. return true;
  285. } else {
  286. if (checkNextBlock)
  287. if (isNextToLeaves(pos.up(), world, false))
  288. return false;
  289. }
  290. // check the southen block
  291. state = world.getBlockState(pos.south());
  292. if (state.getBlock().equals(Blocks.LEAVES)) {
  293. return true;
  294. } else {
  295. if (checkNextBlock)
  296. if (isNextToLeaves(pos.up(), world, false))
  297. return false;
  298. }
  299. // check the eastern block
  300. state = world.getBlockState(pos.east());
  301. if (state.getBlock().equals(Blocks.LEAVES)) {
  302. return true;
  303. } else {
  304. if (checkNextBlock)
  305. if (isNextToLeaves(pos.up(), world, false))
  306. return false;
  307. }
  308. // Check the western block
  309. state = world.getBlockState(pos.west());
  310. if (state.getBlock().equals(Blocks.LEAVES)) {
  311. return true;
  312. } else {
  313. if (checkNextBlock)
  314. if (isNextToLeaves(pos.up(), world, false))
  315. return false;
  316. }
  317. return false;
  318. }
  319.  
  320. public boolean isNextToBulb(BlockPos pos, World world, boolean checkNextBlock) {
  321. IBlockState state;
  322. // check the block above
  323. state = world.getBlockState(pos.up());
  324. if (state.getBlock().equals(BlockHandler.bulb)) {
  325. return true;
  326. } else {
  327. if (checkNextBlock)
  328. if (isNextToLeaves(pos.up(), world, false))
  329. return false;
  330. }
  331.  
  332. // check the block below
  333. state = world.getBlockState(pos.down());
  334. if (state.getBlock().equals(BlockHandler.bulb)) {
  335. return true;
  336. } else {
  337. if (checkNextBlock)
  338. if (isNextToLeaves(pos.up(), world, false))
  339. return false;
  340. }
  341. return false;
  342. }
  343.  
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement