Advertisement
Creepinson

d

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