Advertisement
Guest User

Untitled

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