Eragonn14900

Untitled

Nov 27th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.83 KB | None | 0 0
  1. package com.reactioncraft.core.common.blocks;
  2.  
  3. import java.util.Random;
  4. import javax.annotation.Nullable;
  5.  
  6. import com.reactioncraft.integration.instances.IntegratedBlocks;
  7. import com.reactioncraft.integration.instances.IntegratedItems;
  8.  
  9. import net.minecraft.block.Block;
  10. import net.minecraft.block.BlockDoor;
  11. import net.minecraft.block.BlockHorizontal;
  12. import net.minecraft.block.material.EnumPushReaction;
  13. import net.minecraft.block.material.MapColor;
  14. import net.minecraft.block.material.Material;
  15. import net.minecraft.block.properties.IProperty;
  16. import net.minecraft.block.properties.PropertyBool;
  17. import net.minecraft.block.properties.PropertyDirection;
  18. import net.minecraft.block.properties.PropertyEnum;
  19. import net.minecraft.block.state.BlockStateContainer;
  20. import net.minecraft.block.state.IBlockState;
  21. import net.minecraft.entity.player.EntityPlayer;
  22. import net.minecraft.init.Blocks;
  23. import net.minecraft.init.Items;
  24. import net.minecraft.item.Item;
  25. import net.minecraft.item.ItemStack;
  26. import net.minecraft.util.BlockRenderLayer;
  27. import net.minecraft.util.EnumFacing;
  28. import net.minecraft.util.EnumHand;
  29. import net.minecraft.util.IStringSerializable;
  30. import net.minecraft.util.Mirror;
  31. import net.minecraft.util.Rotation;
  32. import net.minecraft.util.math.AxisAlignedBB;
  33. import net.minecraft.util.math.BlockPos;
  34. import net.minecraft.util.text.translation.I18n;
  35. import net.minecraft.world.IBlockAccess;
  36. import net.minecraft.world.World;
  37. import net.minecraftforge.fml.relauncher.Side;
  38. import net.minecraftforge.fml.relauncher.SideOnly;
  39.  
  40. public class BlockBaseDoor extends BlockDoor
  41. {
  42. public static final PropertyDirection FACING = BlockHorizontal.FACING;
  43. public static final PropertyBool OPEN = PropertyBool.create("open");
  44. public static final PropertyEnum<BlockBaseDoor.EnumHingePosition> HINGE = PropertyEnum.<BlockBaseDoor.EnumHingePosition>create("hinge", BlockBaseDoor.EnumHingePosition.class);
  45. public static final PropertyBool POWERED = PropertyBool.create("powered");
  46. public static final PropertyEnum<BlockBaseDoor.EnumDoorHalf> HALF = PropertyEnum.<BlockBaseDoor.EnumDoorHalf>create("half", BlockBaseDoor.EnumDoorHalf.class);
  47. protected static final AxisAlignedBB SOUTH_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 0.1875D);
  48. protected static final AxisAlignedBB NORTH_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.8125D, 1.0D, 1.0D, 1.0D);
  49. protected static final AxisAlignedBB WEST_AABB = new AxisAlignedBB(0.8125D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D);
  50. protected static final AxisAlignedBB EAST_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 0.1875D, 1.0D, 1.0D);
  51.  
  52. protected BlockBaseDoor(Material materialIn)
  53. {
  54. super(materialIn);
  55. this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(OPEN, Boolean.valueOf(false)).withProperty(HINGE, BlockBaseDoor.EnumHingePosition.LEFT).withProperty(POWERED, Boolean.valueOf(false)).withProperty(HALF, BlockBaseDoor.EnumDoorHalf.LOWER));
  56. }
  57.  
  58. public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
  59. {
  60. state = state.getActualState(source, pos);
  61. EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
  62. boolean flag = !((Boolean)state.getValue(OPEN)).booleanValue();
  63. boolean flag1 = state.getValue(HINGE) == BlockBaseDoor.EnumHingePosition.RIGHT;
  64.  
  65. switch (enumfacing)
  66. {
  67. case EAST:
  68. default:
  69. return flag ? EAST_AABB : (flag1 ? NORTH_AABB : SOUTH_AABB);
  70. case SOUTH:
  71. return flag ? SOUTH_AABB : (flag1 ? EAST_AABB : WEST_AABB);
  72. case WEST:
  73. return flag ? WEST_AABB : (flag1 ? SOUTH_AABB : NORTH_AABB);
  74. case NORTH:
  75. return flag ? NORTH_AABB : (flag1 ? WEST_AABB : EAST_AABB);
  76. }
  77. }
  78.  
  79. /**
  80. * Gets the localized name of this block. Used for the statistics page.
  81. */
  82. public String getLocalizedName()
  83. {
  84. return I18n.translateToLocal((this.getUnlocalizedName() + ".name").replaceAll("tile", "item"));
  85. }
  86.  
  87. /**
  88. * Used to determine ambient occlusion and culling when rebuilding chunks for render
  89. */
  90. public boolean isOpaqueCube(IBlockState state)
  91. {
  92. return false;
  93. }
  94.  
  95. public boolean isPassable(IBlockAccess worldIn, BlockPos pos)
  96. {
  97. return isOpen(combineMetadata(worldIn, pos));
  98. }
  99.  
  100. public boolean isFullCube(IBlockState state)
  101. {
  102. return false;
  103. }
  104.  
  105. private int getCloseSound()
  106. {
  107. return this.blockMaterial == Material.IRON ? 1011 : 1012;
  108. }
  109.  
  110. private int getOpenSound()
  111. {
  112. return this.blockMaterial == Material.IRON ? 1005 : 1006;
  113. }
  114.  
  115.  
  116. public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ)
  117. {
  118. if (this.blockMaterial == Material.IRON)
  119. {
  120. return false; //Allow items to interact with the door
  121. }
  122. else
  123. {
  124. BlockPos blockpos = state.getValue(HALF) == BlockBaseDoor.EnumDoorHalf.LOWER ? pos : pos.down();
  125. IBlockState iblockstate = pos.equals(blockpos) ? state : worldIn.getBlockState(blockpos);
  126.  
  127. if (iblockstate.getBlock() != this)
  128. {
  129. return false;
  130. }
  131. else
  132. {
  133. state = iblockstate.cycleProperty(OPEN);
  134. worldIn.setBlockState(blockpos, state, 10);
  135. worldIn.markBlockRangeForRenderUpdate(blockpos, pos);
  136. worldIn.playEvent(playerIn, ((Boolean)state.getValue(OPEN)).booleanValue() ? this.getOpenSound() : this.getCloseSound(), pos, 0);
  137. return true;
  138. }
  139. }
  140. }
  141.  
  142. public void toggleDoor(World worldIn, BlockPos pos, boolean open)
  143. {
  144. IBlockState iblockstate = worldIn.getBlockState(pos);
  145.  
  146. if (iblockstate.getBlock() == this)
  147. {
  148. BlockPos blockpos = iblockstate.getValue(HALF) == BlockBaseDoor.EnumDoorHalf.LOWER ? pos : pos.down();
  149. IBlockState iblockstate1 = pos == blockpos ? iblockstate : worldIn.getBlockState(blockpos);
  150.  
  151. if (iblockstate1.getBlock() == this && ((Boolean)iblockstate1.getValue(OPEN)).booleanValue() != open)
  152. {
  153. worldIn.setBlockState(blockpos, iblockstate1.withProperty(OPEN, Boolean.valueOf(open)), 10);
  154. worldIn.markBlockRangeForRenderUpdate(blockpos, pos);
  155. worldIn.playEvent((EntityPlayer)null, open ? this.getOpenSound() : this.getCloseSound(), pos, 0);
  156. }
  157. }
  158. }
  159.  
  160. /**
  161. * Called when a neighboring block was changed and marks that this state should perform any checks during a neighbor
  162. * change. Cases may include when redstone power is updated, cactus blocks popping off due to a neighboring solid
  163. * block, etc.
  164. */
  165. public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn)
  166. {
  167. if (state.getValue(HALF) == BlockBaseDoor.EnumDoorHalf.UPPER)
  168. {
  169. BlockPos blockpos = pos.down();
  170. IBlockState iblockstate = worldIn.getBlockState(blockpos);
  171.  
  172. if (iblockstate.getBlock() != this)
  173. {
  174. worldIn.setBlockToAir(pos);
  175. }
  176. else if (blockIn != this)
  177. {
  178. iblockstate.neighborChanged(worldIn, blockpos, blockIn);
  179. }
  180. }
  181. else
  182. {
  183. boolean flag1 = false;
  184. BlockPos blockpos1 = pos.up();
  185. IBlockState iblockstate1 = worldIn.getBlockState(blockpos1);
  186.  
  187. if (iblockstate1.getBlock() != this)
  188. {
  189. worldIn.setBlockToAir(pos);
  190. flag1 = true;
  191. }
  192.  
  193. if (!worldIn.getBlockState(pos.down()).isSideSolid(worldIn, pos.down(), EnumFacing.UP))
  194. {
  195. worldIn.setBlockToAir(pos);
  196. flag1 = true;
  197.  
  198. if (iblockstate1.getBlock() == this)
  199. {
  200. worldIn.setBlockToAir(blockpos1);
  201. }
  202. }
  203.  
  204. if (flag1)
  205. {
  206. if (!worldIn.isRemote)
  207. {
  208. this.dropBlockAsItem(worldIn, pos, state, 0);
  209. }
  210. }
  211. else
  212. {
  213. boolean flag = worldIn.isBlockPowered(pos) || worldIn.isBlockPowered(blockpos1);
  214.  
  215. if (blockIn != this && (flag || blockIn.getDefaultState().canProvidePower()) && flag != ((Boolean)iblockstate1.getValue(POWERED)).booleanValue())
  216. {
  217. worldIn.setBlockState(blockpos1, iblockstate1.withProperty(POWERED, Boolean.valueOf(flag)), 2);
  218.  
  219. if (flag != ((Boolean)state.getValue(OPEN)).booleanValue())
  220. {
  221. worldIn.setBlockState(pos, state.withProperty(OPEN, Boolean.valueOf(flag)), 2);
  222. worldIn.markBlockRangeForRenderUpdate(pos, pos);
  223. worldIn.playEvent((EntityPlayer)null, flag ? this.getOpenSound() : this.getCloseSound(), pos, 0);
  224. }
  225. }
  226. }
  227. }
  228. }
  229.  
  230. /**
  231. * Get the Item that this Block should drop when harvested.
  232. */
  233. @Nullable
  234. public Item getItemDropped(IBlockState state, Random rand, int fortune)
  235. {
  236. return state.getValue(HALF) == BlockBaseDoor.EnumDoorHalf.UPPER ? null : this.getItem();
  237. }
  238.  
  239. public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
  240. {
  241. return pos.getY() >= worldIn.getHeight() - 1 ? false : worldIn.getBlockState(pos.down()).isSideSolid(worldIn, pos.down(), EnumFacing.UP) && super.canPlaceBlockAt(worldIn, pos) && super.canPlaceBlockAt(worldIn, pos.up());
  242. }
  243.  
  244. public EnumPushReaction getMobilityFlag(IBlockState state)
  245. {
  246. return EnumPushReaction.DESTROY;
  247. }
  248.  
  249. public static int combineMetadata(IBlockAccess worldIn, BlockPos pos)
  250. {
  251. IBlockState iblockstate = worldIn.getBlockState(pos);
  252. int i = iblockstate.getBlock().getMetaFromState(iblockstate);
  253. boolean flag = isTop(i);
  254. IBlockState iblockstate1 = worldIn.getBlockState(pos.down());
  255. int j = iblockstate1.getBlock().getMetaFromState(iblockstate1);
  256. int k = flag ? j : i;
  257. IBlockState iblockstate2 = worldIn.getBlockState(pos.up());
  258. int l = iblockstate2.getBlock().getMetaFromState(iblockstate2);
  259. int i1 = flag ? i : l;
  260. boolean flag1 = (i1 & 1) != 0;
  261. boolean flag2 = (i1 & 2) != 0;
  262. return removeHalfBit(k) | (flag ? 8 : 0) | (flag1 ? 16 : 0) | (flag2 ? 32 : 0);
  263. }
  264.  
  265. public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state)
  266. {
  267. return new ItemStack(this.getItem());
  268. }
  269.  
  270. private Item getItem()
  271. {
  272. return this == IntegratedBlocks.IronBookcasedoorBlockBase ? IntegratedItems.IronBookcasedoor : (this == IntegratedBlocks.WoodenBookcasedoorBlockBase ? IntegratedItems.WoodenBookcasedoor : (this == IntegratedBlocks.cherrydoorBlockBase ? IntegratedItems.cherry_door : IntegratedItems.cherry_door));
  273. }
  274.  
  275. public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player)
  276. {
  277. BlockPos blockpos = pos.down();
  278. BlockPos blockpos1 = pos.up();
  279.  
  280. if (player.capabilities.isCreativeMode && state.getValue(HALF) == BlockBaseDoor.EnumDoorHalf.UPPER && worldIn.getBlockState(blockpos).getBlock() == this)
  281. {
  282. worldIn.setBlockToAir(blockpos);
  283. }
  284.  
  285. if (state.getValue(HALF) == BlockBaseDoor.EnumDoorHalf.LOWER && worldIn.getBlockState(blockpos1).getBlock() == this)
  286. {
  287. if (player.capabilities.isCreativeMode)
  288. {
  289. worldIn.setBlockToAir(pos);
  290. }
  291.  
  292. worldIn.setBlockToAir(blockpos1);
  293. }
  294. }
  295.  
  296. @SideOnly(Side.CLIENT)
  297. public BlockRenderLayer getBlockLayer()
  298. {
  299. return BlockRenderLayer.CUTOUT;
  300. }
  301.  
  302. /**
  303. * Get the actual Block state of this Block at the given position. This applies properties not visible in the
  304. * metadata, such as fence connections.
  305. */
  306. public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
  307. {
  308. if (state.getValue(HALF) == BlockBaseDoor.EnumDoorHalf.LOWER)
  309. {
  310. IBlockState iblockstate = worldIn.getBlockState(pos.up());
  311.  
  312. if (iblockstate.getBlock() == this)
  313. {
  314. state = state.withProperty(HINGE, iblockstate.getValue(HINGE)).withProperty(POWERED, iblockstate.getValue(POWERED));
  315. }
  316. }
  317. else
  318. {
  319. IBlockState iblockstate1 = worldIn.getBlockState(pos.down());
  320.  
  321. if (iblockstate1.getBlock() == this)
  322. {
  323. state = state.withProperty(FACING, iblockstate1.getValue(FACING)).withProperty(OPEN, iblockstate1.getValue(OPEN));
  324. }
  325. }
  326.  
  327. return state;
  328. }
  329.  
  330. /**
  331. * Returns the blockstate with the given rotation from the passed blockstate. If inapplicable, returns the passed
  332. * blockstate.
  333. */
  334. public IBlockState withRotation(IBlockState state, Rotation rot)
  335. {
  336. return state.getValue(HALF) != BlockBaseDoor.EnumDoorHalf.LOWER ? state : state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
  337. }
  338.  
  339. /**
  340. * Returns the blockstate with the given mirror of the passed blockstate. If inapplicable, returns the passed
  341. * blockstate.
  342. */
  343. public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
  344. {
  345. return mirrorIn == Mirror.NONE ? state : state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING))).cycleProperty(HINGE);
  346. }
  347.  
  348. /**
  349. * Convert the given metadata into a BlockState for this Block
  350. */
  351. public IBlockState getStateFromMeta(int meta)
  352. {
  353. return (meta & 8) > 0 ? this.getDefaultState().withProperty(HALF, BlockBaseDoor.EnumDoorHalf.UPPER).withProperty(HINGE, (meta & 1) > 0 ? BlockBaseDoor.EnumHingePosition.RIGHT : BlockBaseDoor.EnumHingePosition.LEFT).withProperty(POWERED, Boolean.valueOf((meta & 2) > 0)) : this.getDefaultState().withProperty(HALF, BlockBaseDoor.EnumDoorHalf.LOWER).withProperty(FACING, EnumFacing.getHorizontal(meta & 3).rotateYCCW()).withProperty(OPEN, Boolean.valueOf((meta & 4) > 0));
  354. }
  355.  
  356. /**
  357. * Convert the BlockState into the correct metadata value
  358. */
  359. public int getMetaFromState(IBlockState state)
  360. {
  361. int i = 0;
  362.  
  363. if (state.getValue(HALF) == BlockBaseDoor.EnumDoorHalf.UPPER)
  364. {
  365. i = i | 8;
  366.  
  367. if (state.getValue(HINGE) == BlockBaseDoor.EnumHingePosition.RIGHT)
  368. {
  369. i |= 1;
  370. }
  371.  
  372. if (((Boolean)state.getValue(POWERED)).booleanValue())
  373. {
  374. i |= 2;
  375. }
  376. }
  377. else
  378. {
  379. i = i | ((EnumFacing)state.getValue(FACING)).rotateY().getHorizontalIndex();
  380.  
  381. if (((Boolean)state.getValue(OPEN)).booleanValue())
  382. {
  383. i |= 4;
  384. }
  385. }
  386.  
  387. return i;
  388. }
  389.  
  390. protected static int removeHalfBit(int meta)
  391. {
  392. return meta & 7;
  393. }
  394.  
  395. public static boolean isOpen(IBlockAccess worldIn, BlockPos pos)
  396. {
  397. return isOpen(combineMetadata(worldIn, pos));
  398. }
  399.  
  400. public static EnumFacing getFacing(IBlockAccess worldIn, BlockPos pos)
  401. {
  402. return getFacing(combineMetadata(worldIn, pos));
  403. }
  404.  
  405. public static EnumFacing getFacing(int combinedMeta)
  406. {
  407. return EnumFacing.getHorizontal(combinedMeta & 3).rotateYCCW();
  408. }
  409.  
  410. protected static boolean isOpen(int combinedMeta)
  411. {
  412. return (combinedMeta & 4) != 0;
  413. }
  414.  
  415. protected static boolean isTop(int meta)
  416. {
  417. return (meta & 8) != 0;
  418. }
  419.  
  420. protected BlockStateContainer createBlockState()
  421. {
  422. return new BlockStateContainer(this, new IProperty[] {HALF, FACING, OPEN, HINGE, POWERED});
  423. }
  424.  
  425. public static enum EnumDoorHalf implements IStringSerializable
  426. {
  427. UPPER,
  428. LOWER;
  429.  
  430. public String toString()
  431. {
  432. return this.getName();
  433. }
  434.  
  435. public String getName()
  436. {
  437. return this == UPPER ? "upper" : "lower";
  438. }
  439. }
  440.  
  441. public static enum EnumHingePosition implements IStringSerializable
  442. {
  443. LEFT,
  444. RIGHT;
  445.  
  446. public String toString()
  447. {
  448. return this.getName();
  449. }
  450.  
  451. public String getName()
  452. {
  453. return this == LEFT ? "left" : "right";
  454. }
  455. }
  456. }
Advertisement
Add Comment
Please, Sign In to add comment