Eragonn14900

Untitled

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