Advertisement
Guest User

Untitled

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