Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.70 KB | None | 0 0
  1.  
  2. package net.tagteam.sparkaid.worldgen;
  3.  
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Random;
  9.  
  10. import net.minecraft.entity.Entity;
  11. import net.minecraft.init.Blocks;
  12. import net.minecraft.util.Direction;
  13. import net.minecraft.util.LongHashMap;
  14. import net.minecraft.util.MathHelper;
  15. import net.minecraft.world.ChunkCoordIntPair;
  16. import net.minecraft.world.Teleporter;
  17. import net.minecraft.world.WorldServer;
  18. import net.tagteam.sparkaid.blocks.SparkBlocks;
  19.  
  20. public class DreScaTeleportHandler extends Teleporter {
  21.     private final WorldServer worldServerInstance;
  22.  
  23.     /** A private Random() function in Teleporter */
  24.     private final Random random;
  25.     private final LongHashMap hash = new LongHashMap(); // hash = c
  26.     @SuppressWarnings("rawtypes")
  27.     private final List list = new ArrayList(); // list = d
  28.  
  29.     public DreScaTeleportHandler(WorldServer ws)    {
  30.         super(ws);
  31.         this.worldServerInstance = ws;
  32.         this.random = new Random(ws.getSeed());
  33.     }
  34.  
  35.     /**
  36.      * Place an entity in a nearby portal, creating one if necessary.
  37.      */
  38.     public void placeInPortal(Entity par1Entity, double par2, double par4, double par6, float par8) {
  39.         if (this.worldServerInstance.provider.dimensionId != 1) {
  40.             if (!this.placeInExistingPortal(par1Entity, par2, par4, par6, par8)) {
  41.                 this.makePortal(par1Entity);
  42.                 this.placeInExistingPortal(par1Entity, par2, par4, par6, par8);
  43.                 }
  44.         }
  45.         else {
  46.             int i = MathHelper.floor_double(par1Entity.posX);
  47.             int j = MathHelper.floor_double(par1Entity.posY) - 1;
  48.             int k = MathHelper.floor_double(par1Entity.posZ);
  49.             byte b0 = 1;
  50.             byte b1 = 0;
  51.  
  52.             for (int l = -2; l <= 2; ++l) {
  53.                 for (int i1 = -2; i1 <= 2; ++i1) {
  54.                     for (int j1 = -1; j1 < 3; ++j1) {
  55.                         int k1 = i + i1 * b0 + l * b1;
  56.                         int l1 = j + j1;
  57.                         int i2 = k + i1 * b1 - l * b0;
  58.                         boolean flag = j1 < 0;
  59.                         this.worldServerInstance.setBlock(k1, l1, i2, flag ? SparkBlocks.SparkAltarRing : Blocks.air);
  60.                     }
  61.                 }
  62.             }
  63.  
  64.             par1Entity.setLocationAndAngles((double) i, (double) j, (double) k, par1Entity.rotationYaw, 0.0F);
  65.             par1Entity.motionX = par1Entity.motionY = par1Entity.motionZ = 0.0D;
  66.         }
  67.     }
  68.  
  69.     /**
  70.      * Place an entity in a nearby portal which already exists.
  71.      */
  72.     @SuppressWarnings("unchecked")
  73.     public boolean placeInExistingPortal(Entity entity, double x, double y, double z, float rot)    {
  74.         short short1 = 128;
  75.         double d3 = -1.0D;
  76.         int i = 0;
  77.         int j = 0;
  78.         int k = 0;
  79.         int l = MathHelper.floor_double(entity.posX);
  80.         int i1 = MathHelper.floor_double(entity.posZ);
  81.         long j1 = ChunkCoordIntPair.chunkXZ2Int(l, i1);
  82.         boolean flag = true;
  83.         double d4;
  84.         int k1;
  85.  
  86.         if (this.hash.containsItem(j1)) {
  87.             PortalPosition ppos = (PortalPosition) this.hash.getValueByKey(j1);
  88.             d3 = 0.0D;
  89.             i = ppos.posX;
  90.             j = ppos.posY;
  91.             k = ppos.posZ;
  92.             ppos.lastUpdateTime = this.worldServerInstance.getTotalWorldTime();
  93.             flag = false;
  94.         }
  95.        
  96.         else {
  97.             for (k1 = l - short1; k1 <= l + short1; ++k1) {
  98.                 double d5 = (double) k1 + 0.5D - entity.posX;
  99.  
  100.                 for (int l1 = i1 - short1; l1 <= i1 + short1; ++l1) {
  101.                     double d6 = (double) l1 + 0.5D - entity.posZ;
  102.  
  103.                     for (int i2 = this.worldServerInstance.getActualHeight() - 1; i2 >= 0; --i2) {
  104.                         if (this.worldServerInstance.getBlock(k1, i2, l1) == SparkBlocks.SparkAltarCenter) {
  105.                             while (this.worldServerInstance.getBlock(k1, i2 - 1, l1) == SparkBlocks.SparkAltarCenter) {
  106.                                 --i2;
  107.                             }
  108.  
  109.                             d4 = (double) i2 + 0.5D - entity.posY;
  110.                             double d7 = d5 * d5 + d4 * d4 + d6 * d6;
  111.  
  112.                             if (d3 < 0.0D || d7 < d3) {
  113.                                 d3 = d7;
  114.                                 i = k1;
  115.                                 j = i2;
  116.                                 k = l1;
  117.                             }
  118.                         }
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.  
  124.         if (d3 >= 0.0D) {
  125.             if (flag) {
  126.                 this.hash.add(j1, new PortalPosition(i, j, k, this.worldServerInstance.getTotalWorldTime()));
  127.                 this.list.add(Long.valueOf(j1));
  128.             }
  129.  
  130.             double d8 = (double) i + 0.5D;
  131.             double d9 = (double) j + 0.5D;
  132.             d4 = (double) k + 0.5D;
  133.             int j2 = -1;
  134.  
  135.             if (this.worldServerInstance.getBlock(i - 1, j, k) == SparkBlocks.SparkAltarCenter) {
  136.                 j2 = 2;
  137.             }
  138.  
  139.             if (this.worldServerInstance.getBlock(i + 1, j, k) == SparkBlocks.SparkAltarCenter) {
  140.                 j2 = 0;
  141.             }
  142.  
  143.             if (this.worldServerInstance.getBlock(i, j, k - 1) == SparkBlocks.SparkAltarCenter) {
  144.                 j2 = 3;
  145.             }
  146.  
  147.             if (this.worldServerInstance.getBlock(i, j, k + 1) == SparkBlocks.SparkAltarCenter) {
  148.                 j2 = 1;
  149.             }
  150.  
  151.             int k2 = entity.getTeleportDirection();
  152.  
  153.             if (j2 > -1) {
  154.                 int l2 = Direction.rotateLeft[j2];
  155.                 int i3 = Direction.offsetX[j2];
  156.                 int j3 = Direction.offsetZ[j2];
  157.                 int k3 = Direction.offsetX[l2];
  158.                 int l3 = Direction.offsetZ[l2];
  159.                 boolean flag1 = !this.worldServerInstance.isAirBlock(i + i3 + k3, j, k + j3 + l3) || !this.worldServerInstance.isAirBlock(i + i3 + k3, j + 1, k + j3 + l3);
  160.                 boolean flag2 = !this.worldServerInstance.isAirBlock(i + i3, j, k + j3) || !this.worldServerInstance.isAirBlock(i + i3, j + 1, k + j3);
  161.  
  162.                 if (flag1 && flag2) {
  163.                     j2 = Direction.rotateOpposite[j2];
  164.                     l2 = Direction.rotateOpposite[l2];
  165.                     i3 = Direction.offsetX[j2];
  166.                     j3 = Direction.offsetZ[j2];
  167.                     k3 = Direction.offsetX[l2];
  168.                     l3 = Direction.offsetZ[l2];
  169.                     k1 = i - k3;
  170.                     d8 -= (double) k3;
  171.                     int i4 = k - l3;
  172.                     d4 -= (double) l3;
  173.                     flag1 = !this.worldServerInstance.isAirBlock(k1 + i3 + k3, j, i4 + j3 + l3) || !this.worldServerInstance.isAirBlock(k1 + i3 + k3, j + 1, i4 + j3 + l3);
  174.                     flag2 = !this.worldServerInstance.isAirBlock(k1 + i3, j, i4 + j3) || !this.worldServerInstance.isAirBlock(k1 + i3, j + 1, i4 + j3);
  175.                 }
  176.  
  177.                 float f1 = 0.5F;
  178.                 float f2 = 0.5F;
  179.  
  180.                 if (!flag1 && flag2) {
  181.                     f1 = 1.0F;
  182.                 }
  183.                 else if (flag1 && !flag2) {
  184.                     f1 = 0.0F;
  185.                 }
  186.                 else if (flag1 && flag2) {
  187.                     f2 = 0.0F;
  188.                 }
  189.  
  190.                 d8 += (double) ((float) k3 * f1 + f2 * (float) i3);
  191.                 d4 += (double) ((float) l3 * f1 + f2 * (float) j3);
  192.                 float f3 = 0.0F;
  193.                 float f4 = 0.0F;
  194.                 float f5 = 0.0F;
  195.                 float f6 = 0.0F;
  196.  
  197.                 if (j2 == k2) {
  198.                     f3 = 1.0F;
  199.                     f4 = 1.0F;
  200.                 }
  201.                 else if (j2 == Direction.rotateOpposite[k2]) {
  202.                     f3 = -1.0F;
  203.                     f4 = -1.0F;
  204.                 }
  205.                 else if (j2 == Direction.rotateRight[k2]) {
  206.                     f5 = 1.0F;
  207.                     f6 = -1.0F;
  208.                 }
  209.                 else {
  210.                     f5 = -1.0F;
  211.                     f6 = 1.0F;
  212.                 }
  213.  
  214.                 double d10 = entity.motionX;
  215.                 double d11 = entity.motionZ;
  216.                 entity.motionX = d10 * (double) f3 + d11 * (double) f6;
  217.                 entity.motionZ = d10 * (double) f5 + d11 * (double) f4;
  218.                 entity.rotationYaw = rot - (float) (k2 * 90) + (float) (j2 * 90);
  219.             }
  220.             else {
  221.                 entity.motionX = entity.motionY = entity.motionZ = 0.0D;
  222.             }
  223.  
  224.             entity.setLocationAndAngles(d8, d9, d4, entity.rotationYaw, entity.rotationPitch);
  225.             return true;
  226.         }
  227.         else {
  228.             return false;
  229.         }
  230.     }
  231.  
  232.     public boolean makePortal(Entity par1Entity) {
  233.         byte b0 = 16;
  234.         double d0 = -1.0D;
  235.         int i = MathHelper.floor_double(par1Entity.posX);
  236.         int j = MathHelper.floor_double(par1Entity.posY);
  237.         int k = MathHelper.floor_double(par1Entity.posZ);
  238.         int l = i;
  239.         int i1 = j;
  240.         int j1 = k;
  241.         int k1 = 0;
  242.         int l1 = this.random.nextInt(4);
  243.         int i2;
  244.         double d1;
  245.         double d2;
  246.         int j2;
  247.         int k2;
  248.         int l2;
  249.         int i3;
  250.         int j3;
  251.         int k3;
  252.         int l3;
  253.         int i4;
  254.         int j4;
  255.         int k4;
  256.         double d3;
  257.         double d4;
  258.  
  259.         for (i2 = i - b0; i2 <= i + b0; ++i2) {
  260.             d1 = (double) i2 + 0.5D - par1Entity.posX;
  261.  
  262.             for (j2 = k - b0; j2 <= k + b0; ++j2) {
  263.                 d2 = (double) j2 + 0.5D - par1Entity.posZ;
  264.                 label274:
  265.  
  266.                 for (k2 = this.worldServerInstance.getActualHeight() - 1; k2 >= 0; --k2) {
  267.                     if (this.worldServerInstance.isAirBlock(i2, k2, j2)) {
  268.                         while (k2 > 0 && this.worldServerInstance.isAirBlock(i2, k2 - 1, j2)) {
  269.                             --k2;
  270.                         }
  271.  
  272.                         for (i3 = l1; i3 < l1 + 4; ++i3) {
  273.                             l2 = i3 % 2;
  274.                             k3 = 1 - l2;
  275.  
  276.                             if (i3 % 4 >= 2) {
  277.                                 l2 = -l2;
  278.                                 k3 = -k3;
  279.                             }
  280.  
  281.                             for (j3 = 0; j3 < 3; ++j3) {
  282.                                 for (i4 = 0; i4 < 4; ++i4) {
  283.                                     for (l3 = -1; l3 < 4; ++l3) {
  284.                                         k4 = i2 + (i4 - 1) * l2 + j3 * k3;
  285.                                         j4 = k2 + l3;
  286.                                         int l4 = j2 + (i4 - 1) * k3 - j3 * l2;
  287.  
  288.                                         if (l3 < 0 && !this.worldServerInstance.getBlock(k4, j4, l4).getMaterial().isSolid() || l3 >= 0 && !this.worldServerInstance.isAirBlock(k4, j4, l4)) {
  289.                                             continue label274;
  290.                                         }
  291.                                     }
  292.                                 }
  293.                             }
  294.  
  295.                             d4 = (double) k2 + 0.5D - par1Entity.posY;
  296.                             d3 = d1 * d1 + d4 * d4 + d2 * d2;
  297.  
  298.                             if (d0 < 0.0D || d3 < d0) {
  299.                                 d0 = d3;
  300.                                 l = i2;
  301.                                 i1 = k2;
  302.                                 j1 = j2;
  303.                                 k1 = i3 % 4;
  304.                             }
  305.                         }
  306.                     }
  307.                 }
  308.             }
  309.         }
  310.  
  311.         if (d0 < 0.0D) {
  312.             for (i2 = i - b0; i2 <= i + b0; ++i2) {
  313.                 d1 = (double) i2 + 0.5D - par1Entity.posX;
  314.  
  315.                 for (j2 = k - b0; j2 <= k + b0; ++j2) {
  316.                     d2 = (double) j2 + 0.5D - par1Entity.posZ;
  317.                     label222:
  318.  
  319.                     for (k2 = this.worldServerInstance.getActualHeight() - 1; k2 >= 0; --k2) {
  320.                         if (this.worldServerInstance.isAirBlock(i2, k2, j2)) {
  321.                             while (k2 > 0 && this.worldServerInstance.isAirBlock(i2, k2 - 1, j2)) {
  322.                                 --k2;
  323.                             }
  324.  
  325.                             for (i3 = l1; i3 < l1 + 2; ++i3) {
  326.                                 l2 = i3 % 2;
  327.                                 k3 = 1 - l2;
  328.  
  329.                                 for (j3 = 0; j3 < 4; ++j3) {
  330.                                     for (i4 = -1; i4 < 4; ++i4) {
  331.                                         l3 = i2 + (j3 - 1) * l2;
  332.                                         k4 = k2 + i4;
  333.                                         j4 = j2 + (j3 - 1) * k3;
  334.  
  335.                                         if (i4 < 0 && !this.worldServerInstance.getBlock(l3, k4, j4).getMaterial().isSolid() || i4 >= 0 && !this.worldServerInstance.isAirBlock(l3, k4, j4)) {
  336.                                             continue label222;
  337.                                         }
  338.                                     }
  339.                                 }
  340.  
  341.                                 d4 = (double) k2 + 0.5D - par1Entity.posY;
  342.                                 d3 = d1 * d1 + d4 * d4 + d2 * d2;
  343.  
  344.                                 if (d0 < 0.0D || d3 < d0) {
  345.                                     d0 = d3;
  346.                                     l = i2;
  347.                                     i1 = k2;
  348.                                     j1 = j2;
  349.                                     k1 = i3 % 2;
  350.                                 }
  351.                             }
  352.                         }
  353.                     }
  354.                 }
  355.             }
  356.         }
  357.  
  358.         int i5 = l;
  359.         int j5 = i1;
  360.         j2 = j1;
  361.         int k5 = k1 % 2;
  362.         int l5 = 1 - k5;
  363.  
  364.         if (k1 % 4 >= 2) {
  365.             k5 = -k5;
  366.             l5 = -l5;
  367.         }
  368.  
  369.         boolean flag;
  370.  
  371.         if (d0 < 0.0D) {
  372.             if (i1 < 70) {
  373.                 i1 = 70;
  374.             }
  375.  
  376.             if (i1 > this.worldServerInstance.getActualHeight() - 10) {
  377.                 i1 = this.worldServerInstance.getActualHeight() - 10;
  378.             }
  379.  
  380.             j5 = i1;
  381.  
  382.             for (k2 = -1; k2 <= 1; ++k2) {
  383.                 for (i3 = 1; i3 < 3; ++i3) {
  384.                     for (l2 = -1; l2 < 3; ++l2) {
  385.                         k3 = i5 + (i3 - 1) * k5 + k2 * l5;
  386.                         j3 = j5 + l2;
  387.                         i4 = j2 + (i3 - 1) * l5 - k2 * k5;
  388.                         flag = l2 < 0;
  389.                         this.worldServerInstance.setBlock(k3, j3, i4, flag ? SparkBlocks.SparkAltarRing : Blocks.air);
  390.                     }
  391.                 }
  392.             }
  393.         }
  394.  
  395.         for (k2 = 0; k2 < 4; ++k2) {
  396.             for (i3 = 0; i3 < 4; ++i3) {
  397.                 for (l2 = -1; l2 < 4; ++l2) {
  398.                     k3 = i5 + (i3 - 1) * k5;
  399.                     j3 = j5 + l2;
  400.                     i4 = j2 + (i3 - 1) * l5;
  401.                     flag = i3 == 0 || i3 == 3 || l2 == -1 || l2 == 3;
  402.                     this.worldServerInstance.setBlock(k3, j3, i4, flag ? SparkBlocks.SparkAltarRing : SparkBlocks.SparkAltarCenter, 0, 2);
  403.                 }
  404.             }
  405.  
  406.             for (i3 = 0; i3 < 4; ++i3) {
  407.                 for (l2 = -1; l2 < 4; ++l2) {
  408.                     k3 = i5 + (i3 - 1) * k5;
  409.                     j3 = j5 + l2;
  410.                     i4 = j2 + (i3 - 1) * l5;
  411.                     this.worldServerInstance.notifyBlocksOfNeighborChange(k3, j3, i4, this.worldServerInstance.getBlock(k3, j3, i4));
  412.                 }
  413.             }
  414.         }
  415.  
  416.         return true;
  417.     }
  418.  
  419.     @SuppressWarnings("rawtypes")
  420.     public void func_85189_a(long longp) {
  421.         if (longp % 100L == 0L) {
  422.             Iterator iterator = this.list.iterator();
  423.             long l = longp - 600L;
  424.  
  425.             while (iterator.hasNext()) {
  426.                 Long olong = (Long) iterator.next();
  427.                 PortalPosition ppos = (PortalPosition) this.hash.getValueByKey(olong.longValue());
  428.  
  429.                 if (ppos == null || ppos.lastUpdateTime < l) {
  430.                     iterator.remove();
  431.                     this.hash.remove(olong.longValue());
  432.                 }
  433.             }
  434.         }
  435.     }
  436.  
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement