Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.20 KB | None | 0 0
  1. package net.minecraft.block;
  2.  
  3. import com.google.common.cache.LoadingCache;
  4. import java.util.Random;
  5. import javax.annotation.Nullable;
  6. import net.minecraft.block.material.Material;
  7. import net.minecraft.block.properties.IProperty;
  8. import net.minecraft.block.properties.PropertyEnum;
  9. import net.minecraft.block.state.BlockStateContainer;
  10. import net.minecraft.block.state.BlockWorldState;
  11. import net.minecraft.block.state.IBlockState;
  12. import net.minecraft.block.state.pattern.BlockPattern;
  13. import net.minecraft.entity.Entity;
  14. import net.minecraft.entity.EntityList;
  15. import net.minecraft.entity.monster.EntityPigZombie;
  16. import net.minecraft.init.Blocks;
  17. import net.minecraft.init.SoundEvents;
  18. import net.minecraft.item.ItemMonsterPlacer;
  19. import net.minecraft.item.ItemStack;
  20. import net.minecraft.util.BlockRenderLayer;
  21. import net.minecraft.util.EnumFacing;
  22. import net.minecraft.util.EnumParticleTypes;
  23. import net.minecraft.util.Rotation;
  24. import net.minecraft.util.SoundCategory;
  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. import net.minecraftforge.fml.relauncher.Side;
  30. import net.minecraftforge.fml.relauncher.SideOnly;
  31.  
  32. public class BlockPortal extends BlockBreakable
  33. {
  34. public static final PropertyEnum<EnumFacing.Axis> AXIS = PropertyEnum.<EnumFacing.Axis>create("axis", EnumFacing.Axis.class, new EnumFacing.Axis[] {EnumFacing.Axis.X, EnumFacing.Axis.Z});
  35. protected static final AxisAlignedBB X_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.375D, 1.0D, 1.0D, 0.625D);
  36. protected static final AxisAlignedBB Z_AABB = new AxisAlignedBB(0.375D, 0.0D, 0.0D, 0.625D, 1.0D, 1.0D);
  37. protected static final AxisAlignedBB Y_AABB = new AxisAlignedBB(0.375D, 0.0D, 0.375D, 0.625D, 1.0D, 0.625D);
  38.  
  39. public BlockPortal()
  40. {
  41. super(Material.PORTAL, false);
  42. this.setDefaultState(this.blockState.getBaseState().withProperty(AXIS, EnumFacing.Axis.X));
  43. this.setTickRandomly(true);
  44. }
  45.  
  46. public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
  47. {
  48. switch ((EnumFacing.Axis)state.getValue(AXIS))
  49. {
  50. case X:
  51. return X_AABB;
  52. case Y:
  53. default:
  54. return Y_AABB;
  55. case Z:
  56. return Z_AABB;
  57. }
  58. }
  59.  
  60. public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
  61. {
  62. super.updateTick(worldIn, pos, state, rand);
  63.  
  64. if (worldIn.provider.isSurfaceWorld() && worldIn.getGameRules().getBoolean("doMobSpawning") && rand.nextInt(2000) < worldIn.getDifficulty().getDifficultyId())
  65. {
  66. int i = pos.getY();
  67. BlockPos blockpos;
  68.  
  69. for (blockpos = pos; !worldIn.getBlockState(blockpos).isFullyOpaque() && blockpos.getY() > 0; blockpos = blockpos.down())
  70. {
  71. ;
  72. }
  73.  
  74. if (i > 0 && !worldIn.getBlockState(blockpos.up()).isNormalCube())
  75. {
  76. Entity entity = ItemMonsterPlacer.spawnCreature(worldIn, EntityList.getEntityStringFromClass(EntityPigZombie.class), (double)blockpos.getX() + 0.5D, (double)blockpos.getY() + 1.1D, (double)blockpos.getZ() + 0.5D);
  77.  
  78. if (entity != null)
  79. {
  80. entity.timeUntilPortal = entity.getPortalCooldown();
  81. }
  82. }
  83. }
  84. }
  85.  
  86. @Nullable
  87. public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, World worldIn, BlockPos pos)
  88. {
  89. return NULL_AABB;
  90. }
  91.  
  92. public static int getMetaForAxis(EnumFacing.Axis axis)
  93. {
  94. return axis == EnumFacing.Axis.X ? 1 : (axis == EnumFacing.Axis.Z ? 2 : 0);
  95. }
  96.  
  97. public boolean isFullCube(IBlockState state)
  98. {
  99. return false;
  100. }
  101.  
  102. public boolean trySpawnPortal(World worldIn, BlockPos pos)
  103. {
  104. BlockPortal.Size blockportal$size = new BlockPortal.Size(worldIn, pos, EnumFacing.Axis.X);
  105.  
  106. if (blockportal$size.isValid() && blockportal$size.portalBlockCount == 0)
  107. {
  108. blockportal$size.placePortalBlocks();
  109. return true;
  110. }
  111. else
  112. {
  113. BlockPortal.Size blockportal$size1 = new BlockPortal.Size(worldIn, pos, EnumFacing.Axis.Z);
  114.  
  115. if (blockportal$size1.isValid() && blockportal$size1.portalBlockCount == 0)
  116. {
  117. blockportal$size1.placePortalBlocks();
  118. return true;
  119. }
  120. else
  121. {
  122. return false;
  123. }
  124. }
  125. }
  126.  
  127. /**
  128. * Called when a neighboring block was changed and marks that this state should perform any checks during a neighbor
  129. * change. Cases may include when redstone power is updated, cactus blocks popping off due to a neighboring solid
  130. * block, etc.
  131. */
  132. public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn)
  133. {
  134. EnumFacing.Axis enumfacing$axis = (EnumFacing.Axis)state.getValue(AXIS);
  135.  
  136. if (enumfacing$axis == EnumFacing.Axis.X)
  137. {
  138. BlockPortal.Size blockportal$size = new BlockPortal.Size(worldIn, pos, EnumFacing.Axis.X);
  139.  
  140. if (!blockportal$size.isValid() || blockportal$size.portalBlockCount < blockportal$size.width * blockportal$size.height)
  141. {
  142. worldIn.setBlockState(pos, Blocks.AIR.getDefaultState());
  143. }
  144. }
  145. else if (enumfacing$axis == EnumFacing.Axis.Z)
  146. {
  147. BlockPortal.Size blockportal$size1 = new BlockPortal.Size(worldIn, pos, EnumFacing.Axis.Z);
  148.  
  149. if (!blockportal$size1.isValid() || blockportal$size1.portalBlockCount < blockportal$size1.width * blockportal$size1.height)
  150. {
  151. worldIn.setBlockState(pos, Blocks.AIR.getDefaultState());
  152. }
  153. }
  154. }
  155.  
  156. @SideOnly(Side.CLIENT)
  157. public boolean shouldSideBeRendered(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side)
  158. {
  159. pos = pos.offset(side);
  160. EnumFacing.Axis enumfacing$axis = null;
  161.  
  162. if (blockState.getBlock() == this)
  163. {
  164. enumfacing$axis = (EnumFacing.Axis)blockState.getValue(AXIS);
  165.  
  166. if (enumfacing$axis == null)
  167. {
  168. return false;
  169. }
  170.  
  171. if (enumfacing$axis == EnumFacing.Axis.Z && side != EnumFacing.EAST && side != EnumFacing.WEST)
  172. {
  173. return false;
  174. }
  175.  
  176. if (enumfacing$axis == EnumFacing.Axis.X && side != EnumFacing.SOUTH && side != EnumFacing.NORTH)
  177. {
  178. return false;
  179. }
  180. }
  181.  
  182. boolean flag = blockAccess.getBlockState(pos.west()).getBlock() == this && blockAccess.getBlockState(pos.west(2)).getBlock() != this;
  183. boolean flag1 = blockAccess.getBlockState(pos.east()).getBlock() == this && blockAccess.getBlockState(pos.east(2)).getBlock() != this;
  184. boolean flag2 = blockAccess.getBlockState(pos.north()).getBlock() == this && blockAccess.getBlockState(pos.north(2)).getBlock() != this;
  185. boolean flag3 = blockAccess.getBlockState(pos.south()).getBlock() == this && blockAccess.getBlockState(pos.south(2)).getBlock() != this;
  186. boolean flag4 = flag || flag1 || enumfacing$axis == EnumFacing.Axis.X;
  187. boolean flag5 = flag2 || flag3 || enumfacing$axis == EnumFacing.Axis.Z;
  188. return flag4 && side == EnumFacing.WEST ? true : (flag4 && side == EnumFacing.EAST ? true : (flag5 && side == EnumFacing.NORTH ? true : flag5 && side == EnumFacing.SOUTH));
  189. }
  190.  
  191. /**
  192. * Returns the quantity of items to drop on block destruction.
  193. */
  194. public int quantityDropped(Random random)
  195. {
  196. return 0;
  197. }
  198.  
  199. /**
  200. * Called When an Entity Collided with the Block
  201. */
  202. public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
  203. {
  204. if (!entityIn.isRiding() && !entityIn.isBeingRidden() && entityIn.isNonBoss())
  205. {
  206. entityIn.setPortal(pos);
  207. }
  208. }
  209.  
  210. @Nullable
  211. public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state)
  212. {
  213. return null;
  214. }
  215.  
  216. /**
  217. * Convert the given metadata into a BlockState for this Block
  218. */
  219. public IBlockState getStateFromMeta(int meta)
  220. {
  221. return this.getDefaultState().withProperty(AXIS, (meta & 3) == 2 ? EnumFacing.Axis.Z : EnumFacing.Axis.X);
  222. }
  223.  
  224. @SideOnly(Side.CLIENT)
  225. public BlockRenderLayer getBlockLayer()
  226. {
  227. return BlockRenderLayer.TRANSLUCENT;
  228. }
  229.  
  230. @SideOnly(Side.CLIENT)
  231. public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
  232. {
  233. if (rand.nextInt(100) == 0)
  234. {
  235. worldIn.playSound((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, SoundEvents.BLOCK_PORTAL_AMBIENT, SoundCategory.BLOCKS, 0.5F, rand.nextFloat() * 0.4F + 0.8F, false);
  236. }
  237.  
  238. for (int i = 0; i < 4; ++i)
  239. {
  240. double d0 = (double)((float)pos.getX() + rand.nextFloat());
  241. double d1 = (double)((float)pos.getY() + rand.nextFloat());
  242. double d2 = (double)((float)pos.getZ() + rand.nextFloat());
  243. double d3 = ((double)rand.nextFloat() - 0.5D) * 0.5D;
  244. double d4 = ((double)rand.nextFloat() - 0.5D) * 0.5D;
  245. double d5 = ((double)rand.nextFloat() - 0.5D) * 0.5D;
  246. int j = rand.nextInt(2) * 2 - 1;
  247.  
  248. if (worldIn.getBlockState(pos.west()).getBlock() != this && worldIn.getBlockState(pos.east()).getBlock() != this)
  249. {
  250. d0 = (double)pos.getX() + 0.5D + 0.25D * (double)j;
  251. d3 = (double)(rand.nextFloat() * 2.0F * (float)j);
  252. }
  253. else
  254. {
  255. d2 = (double)pos.getZ() + 0.5D + 0.25D * (double)j;
  256. d5 = (double)(rand.nextFloat() * 2.0F * (float)j);
  257. }
  258.  
  259. worldIn.spawnParticle(EnumParticleTypes.PORTAL, d0, d1, d2, d3, d4, d5, new int[0]);
  260. }
  261. }
  262.  
  263. /**
  264. * Convert the BlockState into the correct metadata value
  265. */
  266. public int getMetaFromState(IBlockState state)
  267. {
  268. return getMetaForAxis((EnumFacing.Axis)state.getValue(AXIS));
  269. }
  270.  
  271. /**
  272. * Returns the blockstate with the given rotation from the passed blockstate. If inapplicable, returns the passed
  273. * blockstate.
  274. */
  275. public IBlockState withRotation(IBlockState state, Rotation rot)
  276. {
  277. switch (rot)
  278. {
  279. case COUNTERCLOCKWISE_90:
  280. case CLOCKWISE_90:
  281.  
  282. switch ((EnumFacing.Axis)state.getValue(AXIS))
  283. {
  284. case X:
  285. return state.withProperty(AXIS, EnumFacing.Axis.Z);
  286. case Z:
  287. return state.withProperty(AXIS, EnumFacing.Axis.X);
  288. default:
  289. return state;
  290. }
  291.  
  292. default:
  293. return state;
  294. }
  295. }
  296.  
  297. protected BlockStateContainer createBlockState()
  298. {
  299. return new BlockStateContainer(this, new IProperty[] {AXIS});
  300. }
  301.  
  302. public BlockPattern.PatternHelper createPatternHelper(World worldIn, BlockPos p_181089_2_)
  303. {
  304. EnumFacing.Axis enumfacing$axis = EnumFacing.Axis.Z;
  305. BlockPortal.Size blockportal$size = new BlockPortal.Size(worldIn, p_181089_2_, EnumFacing.Axis.X);
  306. LoadingCache<BlockPos, BlockWorldState> loadingcache = BlockPattern.createLoadingCache(worldIn, true);
  307.  
  308. if (!blockportal$size.isValid())
  309. {
  310. enumfacing$axis = EnumFacing.Axis.X;
  311. blockportal$size = new BlockPortal.Size(worldIn, p_181089_2_, EnumFacing.Axis.Z);
  312. }
  313.  
  314. if (!blockportal$size.isValid())
  315. {
  316. return new BlockPattern.PatternHelper(p_181089_2_, EnumFacing.NORTH, EnumFacing.UP, loadingcache, 1, 1, 1);
  317. }
  318. else
  319. {
  320. int[] aint = new int[EnumFacing.AxisDirection.values().length];
  321. EnumFacing enumfacing = blockportal$size.rightDir.rotateYCCW();
  322. BlockPos blockpos = blockportal$size.bottomLeft.up(blockportal$size.getHeight() - 1);
  323.  
  324. for (EnumFacing.AxisDirection enumfacing$axisdirection : EnumFacing.AxisDirection.values())
  325. {
  326. BlockPattern.PatternHelper blockpattern$patternhelper = new BlockPattern.PatternHelper(enumfacing.getAxisDirection() == enumfacing$axisdirection ? blockpos : blockpos.offset(blockportal$size.rightDir, blockportal$size.getWidth() - 1), EnumFacing.getFacingFromAxis(enumfacing$axisdirection, enumfacing$axis), EnumFacing.UP, loadingcache, blockportal$size.getWidth(), blockportal$size.getHeight(), 1);
  327.  
  328. for (int i = 0; i < blockportal$size.getWidth(); ++i)
  329. {
  330. for (int j = 0; j < blockportal$size.getHeight(); ++j)
  331. {
  332. BlockWorldState blockworldstate = blockpattern$patternhelper.translateOffset(i, j, 1);
  333.  
  334. if (blockworldstate.getBlockState() != null && blockworldstate.getBlockState().getMaterial() != Material.AIR)
  335. {
  336. ++aint[enumfacing$axisdirection.ordinal()];
  337. }
  338. }
  339. }
  340. }
  341.  
  342. EnumFacing.AxisDirection enumfacing$axisdirection1 = EnumFacing.AxisDirection.POSITIVE;
  343.  
  344. for (EnumFacing.AxisDirection enumfacing$axisdirection2 : EnumFacing.AxisDirection.values())
  345. {
  346. if (aint[enumfacing$axisdirection2.ordinal()] < aint[enumfacing$axisdirection1.ordinal()])
  347. {
  348. enumfacing$axisdirection1 = enumfacing$axisdirection2;
  349. }
  350. }
  351.  
  352. return new BlockPattern.PatternHelper(enumfacing.getAxisDirection() == enumfacing$axisdirection1 ? blockpos : blockpos.offset(blockportal$size.rightDir, blockportal$size.getWidth() - 1), EnumFacing.getFacingFromAxis(enumfacing$axisdirection1, enumfacing$axis), EnumFacing.UP, loadingcache, blockportal$size.getWidth(), blockportal$size.getHeight(), 1);
  353. }
  354. }
  355.  
  356. public static class Size
  357. {
  358. private final World world;
  359. private final EnumFacing.Axis axis;
  360. private final EnumFacing rightDir;
  361. private final EnumFacing leftDir;
  362. private int portalBlockCount;
  363. private BlockPos bottomLeft;
  364. private int height;
  365. private int width;
  366.  
  367. public Size(World worldIn, BlockPos p_i45694_2_, EnumFacing.Axis p_i45694_3_)
  368. {
  369. this.world = worldIn;
  370. this.axis = p_i45694_3_;
  371.  
  372. if (p_i45694_3_ == EnumFacing.Axis.X)
  373. {
  374. this.leftDir = EnumFacing.EAST;
  375. this.rightDir = EnumFacing.WEST;
  376. }
  377. else
  378. {
  379. this.leftDir = EnumFacing.NORTH;
  380. this.rightDir = EnumFacing.SOUTH;
  381. }
  382.  
  383. for (BlockPos blockpos = p_i45694_2_; p_i45694_2_.getY() > blockpos.getY() - 21 && p_i45694_2_.getY() > 0 && this.isEmptyBlock(worldIn.getBlockState(p_i45694_2_.down()).getBlock()); p_i45694_2_ = p_i45694_2_.down())
  384. {
  385. ;
  386. }
  387.  
  388. int i = this.getDistanceUntilEdge(p_i45694_2_, this.leftDir) - 1;
  389.  
  390. if (i >= 0)
  391. {
  392. this.bottomLeft = p_i45694_2_.offset(this.leftDir, i);
  393. this.width = this.getDistanceUntilEdge(this.bottomLeft, this.rightDir);
  394.  
  395. if (this.width < 2 || this.width > 21)
  396. {
  397. this.bottomLeft = null;
  398. this.width = 0;
  399. }
  400. }
  401.  
  402. if (this.bottomLeft != null)
  403. {
  404. this.height = this.calculatePortalHeight();
  405. }
  406. }
  407.  
  408. protected int getDistanceUntilEdge(BlockPos p_180120_1_, EnumFacing p_180120_2_)
  409. {
  410. int i;
  411.  
  412. for (i = 0; i < 22; ++i)
  413. {
  414. BlockPos blockpos = p_180120_1_.offset(p_180120_2_, i);
  415.  
  416. if (!this.isEmptyBlock(this.world.getBlockState(blockpos).getBlock()) || this.world.getBlockState(blockpos.down()).getBlock() != Blocks.OBSIDIAN)
  417. {
  418. break;
  419. }
  420. }
  421.  
  422. Block block = this.world.getBlockState(p_180120_1_.offset(p_180120_2_, i)).getBlock();
  423. return block == Blocks.OBSIDIAN ? i : 0;
  424. }
  425.  
  426. public int getHeight()
  427. {
  428. return this.height;
  429. }
  430.  
  431. public int getWidth()
  432. {
  433. return this.width;
  434. }
  435.  
  436. protected int calculatePortalHeight()
  437. {
  438. label24:
  439.  
  440. for (this.height = 0; this.height < 21; ++this.height)
  441. {
  442. for (int i = 0; i < this.width; ++i)
  443. {
  444. BlockPos blockpos = this.bottomLeft.offset(this.rightDir, i).up(this.height);
  445. Block block = this.world.getBlockState(blockpos).getBlock();
  446.  
  447. if (!this.isEmptyBlock(block))
  448. {
  449. break label24;
  450. }
  451.  
  452. if (block == Blocks.PORTAL)
  453. {
  454. ++this.portalBlockCount;
  455. }
  456.  
  457. if (i == 0)
  458. {
  459. block = this.world.getBlockState(blockpos.offset(this.leftDir)).getBlock();
  460.  
  461. if (block != Blocks.OBSIDIAN)
  462. {
  463. break label24;
  464. }
  465. }
  466. else if (i == this.width - 1)
  467. {
  468. block = this.world.getBlockState(blockpos.offset(this.rightDir)).getBlock();
  469.  
  470. if (block != Blocks.OBSIDIAN)
  471. {
  472. break label24;
  473. }
  474. }
  475. }
  476. }
  477.  
  478. for (int j = 0; j < this.width; ++j)
  479. {
  480. if (this.world.getBlockState(this.bottomLeft.offset(this.rightDir, j).up(this.height)).getBlock() != Blocks.OBSIDIAN)
  481. {
  482. this.height = 0;
  483. break;
  484. }
  485. }
  486.  
  487. if (this.height <= 21 && this.height >= 3)
  488. {
  489. return this.height;
  490. }
  491. else
  492. {
  493. this.bottomLeft = null;
  494. this.width = 0;
  495. this.height = 0;
  496. return 0;
  497. }
  498. }
  499.  
  500. protected boolean isEmptyBlock(Block blockIn)
  501. {
  502. return blockIn.blockMaterial == Material.AIR || blockIn == Blocks.FIRE || blockIn == Blocks.PORTAL;
  503. }
  504.  
  505. public boolean isValid()
  506. {
  507. return this.bottomLeft != null && this.width >= 2 && this.width <= 21 && this.height >= 3 && this.height <= 21;
  508. }
  509.  
  510. public void placePortalBlocks()
  511. {
  512. for (int i = 0; i < this.width; ++i)
  513. {
  514. BlockPos blockpos = this.bottomLeft.offset(this.rightDir, i);
  515.  
  516. for (int j = 0; j < this.height; ++j)
  517. {
  518. this.world.setBlockState(blockpos.up(j), Blocks.PORTAL.getDefaultState().withProperty(BlockPortal.AXIS, this.axis), 2);
  519. }
  520. }
  521. }
  522. }
  523. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement