Guest User

Untitled

a guest
Apr 20th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.91 KB | None | 0 0
  1. package fr.elias.rumbledimension.dimension;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Random;
  7.  
  8. import fr.elias.rumbledimension.common.BlockRumblePortal;
  9. import fr.elias.rumbledimension.common.RumbleDimension;
  10. import net.minecraft.block.state.IBlockState;
  11. import net.minecraft.block.state.pattern.BlockPattern;
  12. import net.minecraft.entity.Entity;
  13. import net.minecraft.entity.player.EntityPlayerMP;
  14. import net.minecraft.init.Blocks;
  15. import net.minecraft.util.EnumFacing;
  16. import net.minecraft.util.LongHashMap;
  17. import net.minecraft.util.math.BlockPos;
  18. import net.minecraft.util.math.MathHelper;
  19. import net.minecraft.world.ChunkCoordIntPair;
  20. import net.minecraft.world.Teleporter;
  21. import net.minecraft.world.WorldServer;
  22.  
  23. public class RDTeleporter extends Teleporter {
  24.  
  25. public final WorldServer worldServerInstance;
  26. public Random rand;
  27. private final LongHashMap destinationCoordinateCache = new LongHashMap();
  28. private final List destinationCoordinateKeys = new ArrayList();
  29.  
  30. public RDTeleporter(WorldServer worldIn) {
  31. super(worldIn);
  32. worldServerInstance = worldIn;
  33. rand = new Random(worldIn.getSeed());
  34. }
  35. public void placeInPortal(Entity entityIn, float rotationYaw)
  36. {
  37. if (this.worldServerInstance.provider.getDimensionType().getId() != 1)
  38. {
  39. if (!this.placeInExistingPortal(entityIn, rotationYaw))
  40. {
  41. this.makePortal(entityIn);
  42. this.placeInExistingPortal(entityIn, rotationYaw);
  43. }
  44. }
  45. else
  46. {
  47. int i = MathHelper.floor_double(entityIn.posX);
  48. int j = MathHelper.floor_double(entityIn.posY) - 1;
  49. int k = MathHelper.floor_double(entityIn.posZ);
  50. int l = 1;
  51. int i1 = 0;
  52.  
  53. for (int j1 = -2; j1 <= 2; ++j1)
  54. {
  55. for (int k1 = -2; k1 <= 2; ++k1)
  56. {
  57. for (int l1 = -1; l1 < 3; ++l1)
  58. {
  59. int i2 = i + k1 * l + j1 * i1;
  60. int j2 = j + l1;
  61. int k2 = k + k1 * i1 - j1 * l;
  62. boolean flag = l1 < 0;
  63. this.worldServerInstance.setBlockState(new BlockPos(i2, j2, k2), flag ? RumbleDimension.rumbleBlock.getDefaultState() : Blocks.air.getDefaultState());
  64. }
  65. }
  66. }
  67.  
  68. entityIn.setLocationAndAngles((double)i, (double)j, (double)k, entityIn.rotationYaw, 0.0F);
  69. entityIn.motionX = entityIn.motionY = entityIn.motionZ = 0.0D;
  70. }
  71. }
  72. public boolean placeInExistingPortal(Entity entityIn, float rotationYaw)
  73. {
  74. int i = 128;
  75. double d0 = -1.0D;
  76. int j = MathHelper.floor_double(entityIn.posX);
  77. int k = MathHelper.floor_double(entityIn.posZ);
  78. boolean flag = true;
  79. BlockPos blockpos = BlockPos.ORIGIN;
  80. long l = ChunkCoordIntPair.chunkXZ2Int(j, k);
  81.  
  82. if (this.destinationCoordinateCache.containsItem(l))
  83. {
  84. RDTeleporter.PortalPosition teleporter$portalposition = (RDTeleporter.PortalPosition)this.destinationCoordinateCache.getValueByKey(l);
  85. d0 = 0.0D;
  86. blockpos = teleporter$portalposition;
  87. teleporter$portalposition.lastUpdateTime = this.worldServerInstance.getTotalWorldTime();
  88. flag = false;
  89. }
  90. else
  91. {
  92. BlockPos blockpos3 = new BlockPos(entityIn);
  93.  
  94. for (int i1 = -128; i1 <= 128; ++i1)
  95. {
  96. BlockPos blockpos2;
  97.  
  98. for (int j1 = -128; j1 <= 128; ++j1)
  99. {
  100. for (BlockPos blockpos1 = blockpos3.add(i1, this.worldServerInstance.getActualHeight() - 1 - blockpos3.getY(), j1); blockpos1.getY() >= 0; blockpos1 = blockpos2)
  101. {
  102. blockpos2 = blockpos1.down();
  103.  
  104. if (this.worldServerInstance.getBlockState(blockpos1).getBlock() == RumbleDimension.rPortal)
  105. {
  106. while (this.worldServerInstance.getBlockState(blockpos2 = blockpos1.down()).getBlock() == RumbleDimension.rPortal)
  107. {
  108. blockpos1 = blockpos2;
  109. }
  110.  
  111. double d1 = blockpos1.distanceSq(blockpos3);
  112.  
  113. if (d0 < 0.0D || d1 < d0)
  114. {
  115. d0 = d1;
  116. blockpos = blockpos1;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123.  
  124. if (d0 >= 0.0D)
  125. {
  126. if (flag)
  127. {
  128. this.destinationCoordinateCache.add(l, new RDTeleporter.PortalPosition(blockpos, this.worldServerInstance.getTotalWorldTime()));
  129. this.destinationCoordinateKeys.add(Long.valueOf(l));
  130. }
  131.  
  132. double d5 = (double)blockpos.getX() + 0.5D;
  133. double d6 = (double)blockpos.getY() + 0.5D;
  134. double d7 = (double)blockpos.getZ() + 0.5D;
  135. BlockPattern.PatternHelper blockpattern$patternhelper = RumbleDimension.rPortal.func_181089_f(this.worldServerInstance, blockpos);
  136. boolean flag1 = blockpattern$patternhelper.getFinger().rotateY().getAxisDirection() == EnumFacing.AxisDirection.NEGATIVE;
  137. double d2 = blockpattern$patternhelper.getFinger().getAxis() == EnumFacing.Axis.X ? (double)blockpattern$patternhelper.getPos().getZ() : (double)blockpattern$patternhelper.getPos().getX();
  138. if(entityIn.getLastPortalVec() != null)
  139. d6 = (double)(blockpattern$patternhelper.getPos().getY() + 1) - entityIn.getLastPortalVec().yCoord * (double)blockpattern$patternhelper.func_181119_e();
  140.  
  141. if (flag1)
  142. {
  143. ++d2;
  144. }
  145.  
  146. try{
  147. if (blockpattern$patternhelper.getFinger().getAxis() == EnumFacing.Axis.X)
  148. {
  149. d7 = d2 + (1.0D - entityIn.getLastPortalVec().xCoord) * (double)blockpattern$patternhelper.func_181118_d() * (double)blockpattern$patternhelper.getFinger().rotateY().getAxisDirection().getOffset();
  150. }
  151. else
  152. {
  153. d5 = d2 + (1.0D - entityIn.getLastPortalVec().xCoord) * (double)blockpattern$patternhelper.func_181118_d() * (double)blockpattern$patternhelper.getFinger().rotateY().getAxisDirection().getOffset();
  154. }
  155. } catch(NullPointerException npe)
  156. {
  157. npe.printStackTrace();
  158. }
  159.  
  160. float f = 0.0F;
  161. float f1 = 0.0F;
  162. float f2 = 0.0F;
  163. float f3 = 0.0F;
  164.  
  165.  
  166. try{
  167. if (blockpattern$patternhelper.getFinger().getOpposite() == entityIn.getTeleportDirection())
  168. {
  169. f = 1.0F;
  170. f1 = 1.0F;
  171. }
  172. else if (blockpattern$patternhelper.getFinger().getOpposite() == entityIn.getTeleportDirection().getOpposite())
  173. {
  174. f = -1.0F;
  175. f1 = -1.0F;
  176. }
  177. else if (blockpattern$patternhelper.getFinger().getOpposite() == entityIn.getTeleportDirection().rotateY())
  178. {
  179. f2 = 1.0F;
  180. f3 = -1.0F;
  181. }
  182. else
  183. {
  184. f2 = -1.0F;
  185. f3 = 1.0F;
  186. }
  187. } catch(NullPointerException npe)
  188. {
  189. npe.printStackTrace();
  190. }
  191. double d3 = entityIn.motionX;
  192. double d4 = entityIn.motionZ;
  193. entityIn.motionX = d3 * (double)f + d4 * (double)f3;
  194. entityIn.motionZ = d3 * (double)f2 + d4 * (double)f1;
  195. try{
  196. entityIn.rotationYaw = rotationYaw - (float)(entityIn.getTeleportDirection().getOpposite().getHorizontalIndex() * 90) + (float)(blockpattern$patternhelper.getFinger().getHorizontalIndex() * 90);
  197. } catch(NullPointerException npe)
  198. {
  199. npe.printStackTrace();
  200. }
  201. if (entityIn instanceof EntityPlayerMP)
  202. {
  203. ((EntityPlayerMP)entityIn).playerNetServerHandler.setPlayerLocation(d5, d6, d7, entityIn.rotationYaw, entityIn.rotationPitch);
  204. }
  205. else
  206. {
  207. entityIn.setLocationAndAngles(d5, d6, d7, entityIn.rotationYaw, entityIn.rotationPitch);
  208. }
  209.  
  210. return true;
  211. }
  212. else
  213. {
  214. return false;
  215. }
  216. }
  217. public boolean makePortal(Entity entityIn)
  218. {
  219. int i = 16;
  220. double d0 = -1.0D;
  221. int j = MathHelper.floor_double(entityIn.posX);
  222. int k = MathHelper.floor_double(entityIn.posY);
  223. int l = MathHelper.floor_double(entityIn.posZ);
  224. int i1 = j;
  225. int j1 = k;
  226. int k1 = l;
  227. int l1 = 0;
  228. int i2 = this.rand.nextInt(4);
  229. BlockPos.MutableBlockPos blockpos$mutableblockpos = new BlockPos.MutableBlockPos();
  230.  
  231. for (int j2 = j - i; j2 <= j + i; ++j2)
  232. {
  233. double d1 = (double)j2 + 0.5D - entityIn.posX;
  234.  
  235. for (int l2 = l - i; l2 <= l + i; ++l2)
  236. {
  237. double d2 = (double)l2 + 0.5D - entityIn.posZ;
  238. label142:
  239.  
  240. for (int j3 = this.worldServerInstance.getActualHeight() - 1; j3 >= 0; --j3)
  241. {
  242. if (this.worldServerInstance.isAirBlock(blockpos$mutableblockpos.set(j2, j3, l2)))
  243. {
  244. while (j3 > 0 && this.worldServerInstance.isAirBlock(blockpos$mutableblockpos.set(j2, j3 - 1, l2)))
  245. {
  246. --j3;
  247. }
  248.  
  249. for (int k3 = i2; k3 < i2 + 4; ++k3)
  250. {
  251. int l3 = k3 % 2;
  252. int i4 = 1 - l3;
  253.  
  254. if (k3 % 4 >= 2)
  255. {
  256. l3 = -l3;
  257. i4 = -i4;
  258. }
  259.  
  260. for (int j4 = 0; j4 < 3; ++j4)
  261. {
  262. for (int k4 = 0; k4 < 4; ++k4)
  263. {
  264. for (int l4 = -1; l4 < 4; ++l4)
  265. {
  266. int i5 = j2 + (k4 - 1) * l3 + j4 * i4;
  267. int j5 = j3 + l4;
  268. int k5 = l2 + (k4 - 1) * i4 - j4 * l3;
  269. blockpos$mutableblockpos.set(i5, j5, k5);
  270.  
  271. if (l4 < 0 && !this.worldServerInstance.getBlockState(blockpos$mutableblockpos).getMaterial().isSolid() || l4 >= 0 && !this.worldServerInstance.isAirBlock(blockpos$mutableblockpos))
  272. {
  273. continue label142;
  274. }
  275. }
  276. }
  277. }
  278.  
  279. double d5 = (double)j3 + 0.5D - entityIn.posY;
  280. double d7 = d1 * d1 + d5 * d5 + d2 * d2;
  281.  
  282. if (d0 < 0.0D || d7 < d0)
  283. {
  284. d0 = d7;
  285. i1 = j2;
  286. j1 = j3;
  287. k1 = l2;
  288. l1 = k3 % 4;
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
  295.  
  296. if (d0 < 0.0D)
  297. {
  298. for (int l5 = j - i; l5 <= j + i; ++l5)
  299. {
  300. double d3 = (double)l5 + 0.5D - entityIn.posX;
  301.  
  302. for (int j6 = l - i; j6 <= l + i; ++j6)
  303. {
  304. double d4 = (double)j6 + 0.5D - entityIn.posZ;
  305. label559:
  306.  
  307. for (int i7 = this.worldServerInstance.getActualHeight() - 1; i7 >= 0; --i7)
  308. {
  309. if (this.worldServerInstance.isAirBlock(blockpos$mutableblockpos.set(l5, i7, j6)))
  310. {
  311. while (i7 > 0 && this.worldServerInstance.isAirBlock(blockpos$mutableblockpos.set(l5, i7 - 1, j6)))
  312. {
  313. --i7;
  314. }
  315.  
  316. for (int k7 = i2; k7 < i2 + 2; ++k7)
  317. {
  318. int j8 = k7 % 2;
  319. int j9 = 1 - j8;
  320.  
  321. for (int j10 = 0; j10 < 4; ++j10)
  322. {
  323. for (int j11 = -1; j11 < 4; ++j11)
  324. {
  325. int j12 = l5 + (j10 - 1) * j8;
  326. int i13 = i7 + j11;
  327. int j13 = j6 + (j10 - 1) * j9;
  328. blockpos$mutableblockpos.set(j12, i13, j13);
  329.  
  330. if (j11 < 0 && !this.worldServerInstance.getBlockState(blockpos$mutableblockpos).getMaterial().isSolid() || j11 >= 0 && !this.worldServerInstance.isAirBlock(blockpos$mutableblockpos))
  331. {
  332. continue label559;
  333. }
  334. }
  335. }
  336.  
  337. double d6 = (double)i7 + 0.5D - entityIn.posY;
  338. double d8 = d3 * d3 + d6 * d6 + d4 * d4;
  339.  
  340. if (d0 < 0.0D || d8 < d0)
  341. {
  342. d0 = d8;
  343. i1 = l5;
  344. j1 = i7;
  345. k1 = j6;
  346. l1 = k7 % 2;
  347. }
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }
  354.  
  355. int i6 = i1;
  356. int k2 = j1;
  357. int k6 = k1;
  358. int l6 = l1 % 2;
  359. int i3 = 1 - l6;
  360.  
  361. if (l1 % 4 >= 2)
  362. {
  363. l6 = -l6;
  364. i3 = -i3;
  365. }
  366.  
  367. if (d0 < 0.0D)
  368. {
  369. j1 = MathHelper.clamp_int(j1, 70, this.worldServerInstance.getActualHeight() - 10);
  370. k2 = j1;
  371.  
  372. for (int j7 = -1; j7 <= 1; ++j7)
  373. {
  374. for (int l7 = 1; l7 < 3; ++l7)
  375. {
  376. for (int k8 = -1; k8 < 3; ++k8)
  377. {
  378. int k9 = i6 + (l7 - 1) * l6 + j7 * i3;
  379. int k10 = k2 + k8;
  380. int k11 = k6 + (l7 - 1) * i3 - j7 * l6;
  381. boolean flag = k8 < 0;
  382. this.worldServerInstance.setBlockState(new BlockPos(k9, k10, k11), flag ? RumbleDimension.rumbleBlock.getDefaultState() : Blocks.air.getDefaultState());
  383. }
  384. }
  385. }
  386. }
  387.  
  388. IBlockState iblockstate = RumbleDimension.rPortal.getDefaultState().withProperty(BlockRumblePortal.AXIS, l6 != 0 ? EnumFacing.Axis.X : EnumFacing.Axis.Z);
  389.  
  390. for (int i8 = 0; i8 < 4; ++i8)
  391. {
  392. for (int l8 = 0; l8 < 4; ++l8)
  393. {
  394. for (int l9 = -1; l9 < 4; ++l9)
  395. {
  396. int l10 = i6 + (l8 - 1) * l6;
  397. int l11 = k2 + l9;
  398. int k12 = k6 + (l8 - 1) * i3;
  399. boolean flag1 = l8 == 0 || l8 == 3 || l9 == -1 || l9 == 3;
  400. this.worldServerInstance.setBlockState(new BlockPos(l10, l11, k12), flag1 ? RumbleDimension.rumbleBlock.getDefaultState() : iblockstate, 2);
  401. }
  402. }
  403.  
  404. for (int i9 = 0; i9 < 4; ++i9)
  405. {
  406. for (int i10 = -1; i10 < 4; ++i10)
  407. {
  408. int i11 = i6 + (i9 - 1) * l6;
  409. int i12 = k2 + i10;
  410. int l12 = k6 + (i9 - 1) * i3;
  411. BlockPos blockpos = new BlockPos(i11, i12, l12);
  412. this.worldServerInstance.notifyNeighborsOfStateChange(blockpos, this.worldServerInstance.getBlockState(blockpos).getBlock());
  413. }
  414. }
  415. }
  416.  
  417. return true;
  418. }
  419.  
  420. public void removeStalePortalLocations(long worldTime)
  421. {
  422. if (worldTime % 100L == 0L)
  423. {
  424. Iterator<Long> iterator = this.destinationCoordinateKeys.iterator();
  425. long i = worldTime - 300L;
  426.  
  427. while (iterator.hasNext())
  428. {
  429. Long olong = (Long)iterator.next();
  430. Teleporter.PortalPosition teleporter$portalposition = (Teleporter.PortalPosition)this.destinationCoordinateCache.getValueByKey(olong.longValue());
  431.  
  432. if (teleporter$portalposition == null || teleporter$portalposition.lastUpdateTime < i)
  433. {
  434. iterator.remove();
  435. this.destinationCoordinateCache.remove(olong.longValue());
  436. }
  437. }
  438. }
  439. }
  440.  
  441. public class PortalPosition extends BlockPos
  442. {
  443.  
  444. public long lastUpdateTime;
  445.  
  446. public PortalPosition(BlockPos pos, long lastUpdate)
  447. {
  448. super(pos.getX(), pos.getY(), pos.getZ());
  449. this.lastUpdateTime = lastUpdate;
  450. }
  451. }
  452. }
Add Comment
Please, Sign In to add comment