Advertisement
Creepinson

d

Jun 22nd, 2017
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.68 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 (this.isOn && !worldIn.isBlockPowered(pos)) {
  48.  
  49. worldIn.setBlockState(pos, state.withProperty(LIT, Boolean.valueOf(false)), 2);
  50. this.isOn = false;
  51. } else if (!this.isOn && worldIn.isBlockPowered(pos)) {
  52. worldIn.setBlockState(pos, state.withProperty(LIT, Boolean.valueOf(true)), 2);
  53. this.isOn = true;
  54.  
  55. }
  56. }
  57. }
  58.  
  59. public Bulb(Material mat, String name, CreativeTabs tab, float hardness, float resistance, int harvest,
  60. String tool) {
  61. super(mat);
  62. setUnlocalizedName(name);
  63. setRegistryName(name);
  64. setCreativeTab(tab);
  65. setHardness(hardness);
  66. setResistance(resistance);
  67. setHarvestLevel(tool, harvest);
  68. this.setDefaultState(
  69. this.blockState.getBaseState().withProperty(FACING, EnumFacing.DOWN).withProperty(LIT, Boolean.valueOf(false)));
  70. this.isOn = false;
  71. }
  72.  
  73. private int ticks;
  74.  
  75. public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn) {
  76. if (!worldIn.isRemote) {
  77. if (this.isOn && !worldIn.isBlockPowered(pos)) {
  78. worldIn.setBlockState(pos, state.withProperty(LIT, Boolean.valueOf(false)), 2);
  79.  
  80.  
  81. } else if (!this.isOn && worldIn.isBlockPowered(pos)) {
  82. worldIn.setBlockState(pos, state.withProperty(LIT, Boolean.valueOf(true)), 2);
  83. this.isOn = true;
  84.  
  85. }
  86. }
  87. }
  88.  
  89. @Override
  90. public int getLightValue(IBlockState state, IBlockAccess world, BlockPos pos) {
  91.  
  92. if (state.getValue(LIT).booleanValue())
  93. {
  94. return 15;
  95.  
  96. }
  97. else {
  98.  
  99. return 0;
  100.  
  101. }
  102.  
  103. }
  104.  
  105. @Override
  106. public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand) {
  107. if (!worldIn.isRemote) {
  108.  
  109.  
  110.  
  111. }
  112. if (ticks == 60) {
  113.  
  114. ticks = 0;
  115.  
  116. }
  117.  
  118. ticks++;
  119.  
  120. super.updateTick(worldIn, pos, state, rand);
  121. }
  122.  
  123. @Override
  124. public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player) {
  125.  
  126. worldIn.removeTileEntity(pos);
  127.  
  128. this.dropBlockAsItem(worldIn, pos, state, 0);
  129.  
  130. super.onBlockHarvested(worldIn, pos, state, player);
  131. }
  132.  
  133. public boolean isOpaqueCube(IBlockState state) {
  134. return false;
  135. }
  136.  
  137. public boolean isFullCube(IBlockState state) {
  138. return false;
  139. }
  140.  
  141. public IBlockState getStateFromMeta(int meta) {
  142. return this.getDefaultState().withProperty(FACING, EnumFacing.getFront(meta & 7)).withProperty(LIT, Boolean.valueOf((meta & 1) > 0));
  143. }
  144.  
  145. public int getMetaFromState(IBlockState state) {
  146. int i = 0;
  147. i = i | ((EnumFacing)state.getValue(FACING)).getIndex();
  148.  
  149. if (((Boolean)state.getValue(LIT)).booleanValue())
  150. {
  151. i |= 8;
  152. }
  153.  
  154. return i;
  155. }
  156.  
  157. public IBlockState withRotation(IBlockState state, Rotation rot) {
  158. return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING)));
  159. }
  160.  
  161. public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
  162. return state.withRotation(mirrorIn.toRotation((EnumFacing) state.getValue(FACING)));
  163. }
  164.  
  165. protected BlockStateContainer createBlockState() {
  166. return new BlockStateContainer(this, new IProperty[]{FACING,LIT});
  167. }
  168.  
  169. @Override
  170. public boolean canPlaceTorchOnTop(IBlockState state, IBlockAccess world, BlockPos pos) {
  171. return false;
  172. }
  173.  
  174. @Override
  175. public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY,
  176. float hitZ, int meta, EntityLivingBase placer, ItemStack stack) {
  177.  
  178. if (facing == EnumFacing.NORTH || facing == EnumFacing.SOUTH || facing == EnumFacing.WEST
  179. || facing == EnumFacing.EAST) {
  180. this.dropBlockAsItem(world, pos, this.getDefaultState(), 0);
  181. return Blocks.AIR.getDefaultState();
  182. } else {
  183.  
  184. return this.getDefaultState().withProperty(FACING, facing.getOpposite());
  185.  
  186. }
  187.  
  188. }
  189.  
  190. @Override
  191. public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ,
  192. int meta, EntityLivingBase placer) {
  193. if (facing == EnumFacing.NORTH || facing == EnumFacing.SOUTH || facing == EnumFacing.WEST
  194. || facing == EnumFacing.EAST) {
  195. this.dropBlockAsItem(worldIn, pos, this.getDefaultState(), 0);
  196. return Blocks.AIR.getDefaultState();
  197. } else {
  198.  
  199. return this.getDefaultState().withProperty(FACING, facing.getOpposite()).withProperty(LIT, Boolean.valueOf(false));
  200.  
  201. }
  202.  
  203. }
  204.  
  205. @Override
  206. public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer,
  207. ItemStack stack) {
  208.  
  209. if (this.isNextToLeaves(pos, world, false)) {
  210. this.dropBlockAsItem(world, pos, state, 0);
  211. world.setBlockState(pos, Blocks.AIR.getDefaultState());
  212. return;
  213.  
  214. }
  215. // if (this.isNextToBulb(pos, world, false)) {
  216. // this.dropBlockAsItem(world, pos, state, 0);
  217. // world.setBlockState(pos, Blocks.AIR.getDefaultState());
  218. // return;
  219. //
  220. // }
  221. super.onBlockPlacedBy(world, pos, state, placer, stack);
  222. }
  223.  
  224. // @Override
  225. // public IBlockState getStateForPlacement(World world, BlockPos pos,
  226. // EnumFacing facing, float hitX, float hitY,
  227. // float hitZ, int meta, EntityLivingBase placer, ItemStack stack) {
  228. // if(facing.getAxis().isVertical()){
  229. // return this.getDefaultState().withProperty(FACING, facing.getOpposite());
  230. // }
  231. // else return this.getDefaultState().withProperty(FACING, EnumFacing.DOWN);
  232. // }
  233.  
  234. @Override
  235. public TileEntity createNewTileEntity(World worldIn, int meta) {
  236.  
  237. return new TEBulb();
  238. }
  239.  
  240. public boolean isNextToLeaves(BlockPos pos, World world, boolean checkNextBlock) {
  241. IBlockState state;
  242. // check the block above
  243. state = world.getBlockState(pos.up());
  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.  
  252. // check the block below
  253. state = world.getBlockState(pos.down());
  254. if (state.getBlock().equals(Blocks.LEAVES)) {
  255. return true;
  256. } else {
  257. if (checkNextBlock)
  258. if (isNextToLeaves(pos.up(), world, false))
  259. return false;
  260. }
  261. // check the nothen block
  262. state = world.getBlockState(pos.north());
  263. if (state.getBlock().equals(Blocks.LEAVES)) {
  264. return true;
  265. } else {
  266. if (checkNextBlock)
  267. if (isNextToLeaves(pos.up(), world, false))
  268. return false;
  269. }
  270. // check the southen block
  271. state = world.getBlockState(pos.south());
  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 eastern block
  280. state = world.getBlockState(pos.east());
  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 western block
  289. state = world.getBlockState(pos.west());
  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. return false;
  298. }
  299.  
  300. public boolean isNextToBulb(BlockPos pos, World world, boolean checkNextBlock) {
  301. IBlockState state;
  302. // check the block above
  303. state = world.getBlockState(pos.up());
  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.  
  312. // check the block below
  313. state = world.getBlockState(pos.down());
  314. if (state.getBlock().equals(BlockHandler.bulb)) {
  315. return true;
  316. } else {
  317. if (checkNextBlock)
  318. if (isNextToLeaves(pos.up(), world, false))
  319. return false;
  320. }
  321. // check the nothen block
  322. state = world.getBlockState(pos.north());
  323. if (state.getBlock().equals(BlockHandler.bulb)) {
  324. return true;
  325. } else {
  326. if (checkNextBlock)
  327. if (isNextToLeaves(pos.up(), world, false))
  328. return false;
  329. }
  330. // check the southen block
  331. state = world.getBlockState(pos.south());
  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. // check the eastern block
  340. state = world.getBlockState(pos.east());
  341. if (state.getBlock().equals(BlockHandler.bulb)) {
  342. return true;
  343. } else {
  344. if (checkNextBlock)
  345. if (isNextToLeaves(pos.up(), world, false))
  346. return false;
  347. }
  348. // Check the western block
  349. state = world.getBlockState(pos.west());
  350. if (state.getBlock().equals(BlockHandler.bulb)) {
  351. return true;
  352. } else {
  353. if (checkNextBlock)
  354. if (isNextToLeaves(pos.up(), world, false))
  355. return false;
  356. }
  357. return false;
  358. }
  359.  
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement