Advertisement
Guest User

Untitled

a guest
Apr 16th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.37 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. d6 = (double)(blockpattern$patternhelper.getPos().getY() + 1) - entityIn.getLastPortalVec().yCoord * (double)blockpattern$patternhelper.func_181119_e();
  139.  
  140. if (flag1)
  141. {
  142. ++d2;
  143. }
  144.  
  145. if (blockpattern$patternhelper.getFinger().getAxis() == EnumFacing.Axis.X)
  146. {
  147. d7 = d2 + (1.0D - entityIn.getLastPortalVec().xCoord) * (double)blockpattern$patternhelper.func_181118_d() * (double)blockpattern$patternhelper.getFinger().rotateY().getAxisDirection().getOffset();
  148. }
  149. else
  150. {
  151. d5 = d2 + (1.0D - entityIn.getLastPortalVec().xCoord) * (double)blockpattern$patternhelper.func_181118_d() * (double)blockpattern$patternhelper.getFinger().rotateY().getAxisDirection().getOffset();
  152. }
  153.  
  154. float f = 0.0F;
  155. float f1 = 0.0F;
  156. float f2 = 0.0F;
  157. float f3 = 0.0F;
  158.  
  159. if (blockpattern$patternhelper.getFinger().getOpposite() == entityIn.getTeleportDirection())
  160. {
  161. f = 1.0F;
  162. f1 = 1.0F;
  163. }
  164. else if (blockpattern$patternhelper.getFinger().getOpposite() == entityIn.getTeleportDirection().getOpposite())
  165. {
  166. f = -1.0F;
  167. f1 = -1.0F;
  168. }
  169. else if (blockpattern$patternhelper.getFinger().getOpposite() == entityIn.getTeleportDirection().rotateY())
  170. {
  171. f2 = 1.0F;
  172. f3 = -1.0F;
  173. }
  174. else
  175. {
  176. f2 = -1.0F;
  177. f3 = 1.0F;
  178. }
  179.  
  180. double d3 = entityIn.motionX;
  181. double d4 = entityIn.motionZ;
  182. entityIn.motionX = d3 * (double)f + d4 * (double)f3;
  183. entityIn.motionZ = d3 * (double)f2 + d4 * (double)f1;
  184. entityIn.rotationYaw = rotationYaw - (float)(entityIn.getTeleportDirection().getOpposite().getHorizontalIndex() * 90) + (float)(blockpattern$patternhelper.getFinger().getHorizontalIndex() * 90);
  185.  
  186. if (entityIn instanceof EntityPlayerMP)
  187. {
  188. ((EntityPlayerMP)entityIn).playerNetServerHandler.setPlayerLocation(d5, d6, d7, entityIn.rotationYaw, entityIn.rotationPitch);
  189. }
  190. else
  191. {
  192. entityIn.setLocationAndAngles(d5, d6, d7, entityIn.rotationYaw, entityIn.rotationPitch);
  193. }
  194.  
  195. return true;
  196. }
  197. else
  198. {
  199. return false;
  200. }
  201. }
  202. public boolean makePortal(Entity entityIn)
  203. {
  204. int i = 16;
  205. double d0 = -1.0D;
  206. int j = MathHelper.floor_double(entityIn.posX);
  207. int k = MathHelper.floor_double(entityIn.posY);
  208. int l = MathHelper.floor_double(entityIn.posZ);
  209. int i1 = j;
  210. int j1 = k;
  211. int k1 = l;
  212. int l1 = 0;
  213. int i2 = this.rand.nextInt(4);
  214. BlockPos.MutableBlockPos blockpos$mutableblockpos = new BlockPos.MutableBlockPos();
  215.  
  216. for (int j2 = j - i; j2 <= j + i; ++j2)
  217. {
  218. double d1 = (double)j2 + 0.5D - entityIn.posX;
  219.  
  220. for (int l2 = l - i; l2 <= l + i; ++l2)
  221. {
  222. double d2 = (double)l2 + 0.5D - entityIn.posZ;
  223. label142:
  224.  
  225. for (int j3 = this.worldServerInstance.getActualHeight() - 1; j3 >= 0; --j3)
  226. {
  227. if (this.worldServerInstance.isAirBlock(blockpos$mutableblockpos.set(j2, j3, l2)))
  228. {
  229. while (j3 > 0 && this.worldServerInstance.isAirBlock(blockpos$mutableblockpos.set(j2, j3 - 1, l2)))
  230. {
  231. --j3;
  232. }
  233.  
  234. for (int k3 = i2; k3 < i2 + 4; ++k3)
  235. {
  236. int l3 = k3 % 2;
  237. int i4 = 1 - l3;
  238.  
  239. if (k3 % 4 >= 2)
  240. {
  241. l3 = -l3;
  242. i4 = -i4;
  243. }
  244.  
  245. for (int j4 = 0; j4 < 3; ++j4)
  246. {
  247. for (int k4 = 0; k4 < 4; ++k4)
  248. {
  249. for (int l4 = -1; l4 < 4; ++l4)
  250. {
  251. int i5 = j2 + (k4 - 1) * l3 + j4 * i4;
  252. int j5 = j3 + l4;
  253. int k5 = l2 + (k4 - 1) * i4 - j4 * l3;
  254. blockpos$mutableblockpos.set(i5, j5, k5);
  255.  
  256. if (l4 < 0 && !this.worldServerInstance.getBlockState(blockpos$mutableblockpos).getMaterial().isSolid() || l4 >= 0 && !this.worldServerInstance.isAirBlock(blockpos$mutableblockpos))
  257. {
  258. continue label142;
  259. }
  260. }
  261. }
  262. }
  263.  
  264. double d5 = (double)j3 + 0.5D - entityIn.posY;
  265. double d7 = d1 * d1 + d5 * d5 + d2 * d2;
  266.  
  267. if (d0 < 0.0D || d7 < d0)
  268. {
  269. d0 = d7;
  270. i1 = j2;
  271. j1 = j3;
  272. k1 = l2;
  273. l1 = k3 % 4;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }
  280.  
  281. if (d0 < 0.0D)
  282. {
  283. for (int l5 = j - i; l5 <= j + i; ++l5)
  284. {
  285. double d3 = (double)l5 + 0.5D - entityIn.posX;
  286.  
  287. for (int j6 = l - i; j6 <= l + i; ++j6)
  288. {
  289. double d4 = (double)j6 + 0.5D - entityIn.posZ;
  290. label559:
  291.  
  292. for (int i7 = this.worldServerInstance.getActualHeight() - 1; i7 >= 0; --i7)
  293. {
  294. if (this.worldServerInstance.isAirBlock(blockpos$mutableblockpos.set(l5, i7, j6)))
  295. {
  296. while (i7 > 0 && this.worldServerInstance.isAirBlock(blockpos$mutableblockpos.set(l5, i7 - 1, j6)))
  297. {
  298. --i7;
  299. }
  300.  
  301. for (int k7 = i2; k7 < i2 + 2; ++k7)
  302. {
  303. int j8 = k7 % 2;
  304. int j9 = 1 - j8;
  305.  
  306. for (int j10 = 0; j10 < 4; ++j10)
  307. {
  308. for (int j11 = -1; j11 < 4; ++j11)
  309. {
  310. int j12 = l5 + (j10 - 1) * j8;
  311. int i13 = i7 + j11;
  312. int j13 = j6 + (j10 - 1) * j9;
  313. blockpos$mutableblockpos.set(j12, i13, j13);
  314.  
  315. if (j11 < 0 && !this.worldServerInstance.getBlockState(blockpos$mutableblockpos).getMaterial().isSolid() || j11 >= 0 && !this.worldServerInstance.isAirBlock(blockpos$mutableblockpos))
  316. {
  317. continue label559;
  318. }
  319. }
  320. }
  321.  
  322. double d6 = (double)i7 + 0.5D - entityIn.posY;
  323. double d8 = d3 * d3 + d6 * d6 + d4 * d4;
  324.  
  325. if (d0 < 0.0D || d8 < d0)
  326. {
  327. d0 = d8;
  328. i1 = l5;
  329. j1 = i7;
  330. k1 = j6;
  331. l1 = k7 % 2;
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339.  
  340. int i6 = i1;
  341. int k2 = j1;
  342. int k6 = k1;
  343. int l6 = l1 % 2;
  344. int i3 = 1 - l6;
  345.  
  346. if (l1 % 4 >= 2)
  347. {
  348. l6 = -l6;
  349. i3 = -i3;
  350. }
  351.  
  352. if (d0 < 0.0D)
  353. {
  354. j1 = MathHelper.clamp_int(j1, 70, this.worldServerInstance.getActualHeight() - 10);
  355. k2 = j1;
  356.  
  357. for (int j7 = -1; j7 <= 1; ++j7)
  358. {
  359. for (int l7 = 1; l7 < 3; ++l7)
  360. {
  361. for (int k8 = -1; k8 < 3; ++k8)
  362. {
  363. int k9 = i6 + (l7 - 1) * l6 + j7 * i3;
  364. int k10 = k2 + k8;
  365. int k11 = k6 + (l7 - 1) * i3 - j7 * l6;
  366. boolean flag = k8 < 0;
  367. this.worldServerInstance.setBlockState(new BlockPos(k9, k10, k11), flag ? RumbleDimension.rumbleBlock.getDefaultState() : Blocks.air.getDefaultState());
  368. }
  369. }
  370. }
  371. }
  372.  
  373. IBlockState iblockstate = RumbleDimension.rPortal.getDefaultState().withProperty(BlockRumblePortal.AXIS, l6 != 0 ? EnumFacing.Axis.X : EnumFacing.Axis.Z);
  374.  
  375. for (int i8 = 0; i8 < 4; ++i8)
  376. {
  377. for (int l8 = 0; l8 < 4; ++l8)
  378. {
  379. for (int l9 = -1; l9 < 4; ++l9)
  380. {
  381. int l10 = i6 + (l8 - 1) * l6;
  382. int l11 = k2 + l9;
  383. int k12 = k6 + (l8 - 1) * i3;
  384. boolean flag1 = l8 == 0 || l8 == 3 || l9 == -1 || l9 == 3;
  385. this.worldServerInstance.setBlockState(new BlockPos(l10, l11, k12), flag1 ? RumbleDimension.rumbleBlock.getDefaultState() : iblockstate, 2);
  386. }
  387. }
  388.  
  389. for (int i9 = 0; i9 < 4; ++i9)
  390. {
  391. for (int i10 = -1; i10 < 4; ++i10)
  392. {
  393. int i11 = i6 + (i9 - 1) * l6;
  394. int i12 = k2 + i10;
  395. int l12 = k6 + (i9 - 1) * i3;
  396. BlockPos blockpos = new BlockPos(i11, i12, l12);
  397. this.worldServerInstance.notifyNeighborsOfStateChange(blockpos, this.worldServerInstance.getBlockState(blockpos).getBlock());
  398. }
  399. }
  400. }
  401.  
  402. return true;
  403. }
  404.  
  405. public void removeStalePortalLocations(long worldTime)
  406. {
  407. if (worldTime % 100L == 0L)
  408. {
  409. Iterator<Long> iterator = this.destinationCoordinateKeys.iterator();
  410. long i = worldTime - 300L;
  411.  
  412. while (iterator.hasNext())
  413. {
  414. Long olong = (Long)iterator.next();
  415. Teleporter.PortalPosition teleporter$portalposition = (Teleporter.PortalPosition)this.destinationCoordinateCache.getValueByKey(olong.longValue());
  416.  
  417. if (teleporter$portalposition == null || teleporter$portalposition.lastUpdateTime < i)
  418. {
  419. iterator.remove();
  420. this.destinationCoordinateCache.remove(olong.longValue());
  421. }
  422. }
  423. }
  424. }
  425.  
  426. public class PortalPosition extends BlockPos
  427. {
  428.  
  429. public long lastUpdateTime;
  430.  
  431. public PortalPosition(BlockPos pos, long lastUpdate)
  432. {
  433. super(pos.getX(), pos.getY(), pos.getZ());
  434. this.lastUpdateTime = lastUpdate;
  435. }
  436. }
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement