Admiral_Damage

Untitled

Mar 12th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 38.08 KB | None | 0 0
  1. package eu.tetrabyte.contentmod.entity;
  2.  
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.UUID;
  6.  
  7. import net.minecraft.block.Block;
  8. import net.minecraft.entity.Entity;
  9. import net.minecraft.entity.EntityLivingBase;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.init.Items;
  12. import net.minecraft.item.Item;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.nbt.NBTTagCompound;
  15. import net.minecraft.util.AxisAlignedBB;
  16. import net.minecraft.util.BlockPos;
  17. import net.minecraft.util.DamageSource;
  18. import net.minecraft.util.EnumParticleTypes;
  19. import net.minecraft.util.MathHelper;
  20. import net.minecraft.world.World;
  21. import net.minecraftforge.fml.relauncher.Side;
  22. import net.minecraftforge.fml.relauncher.SideOnly;
  23.  
  24. /*
  25. import net.row.RoW;
  26. import net.row.config.RoWBlocks;
  27. import net.row.helpers.RotationHelper;
  28. import net.row.helpers.RotativePoint;
  29. import net.row.physhelper.OrientedBB;
  30. import net.row.tileentity.TileEntityGag;
  31. import net.row.tileentity.TileEntityWideRail;
  32. */
  33.  
  34. public class EntityTrain extends Entity{
  35.     protected double cartX;
  36.     protected double cartY;
  37.     protected double cartZ;
  38.     protected double cartYaw;
  39.     public boolean isOnRail;
  40.     public boolean isOnSlope;
  41.     public boolean wasLastSwitchActive;
  42.     public boolean hasFrontCoupler;
  43.     public boolean hasRearCoupler;
  44.     public boolean needsCouplingsCheck;
  45.     public boolean isFrontCoupled;
  46.     public boolean isRearCoupled;
  47.     public boolean doRenderCouplingPositions;
  48.     public boolean isStatic;
  49.     public int railPosX;
  50.     public int railPosY;
  51.     public int railPosZ;
  52.     public float[] tangent;
  53.     public float mass;
  54.     public float maxSpeed;
  55.     public float projectedSpeed;
  56.     public float frontCouplerShift;
  57.     public float frontCouplerX;
  58.     public float frontCouplerZ;
  59.     public float prevFrontCouplerdx;
  60.     public float prevFrontCouplerdz;
  61.     public float rearCouplerShift;
  62.     public float rearCouplerX;
  63.     public float rearCouplerZ;
  64.     public float prevRearCouplerdx;
  65.     public float prevRearCouplerdz;
  66.     public float rotationCos;
  67.     public float rotationSin;
  68.     //public float riderShiftXdef;
  69.     //public float riderShiftZdef;
  70.     public float walkableMinX;
  71.     public float walkableMinZ;
  72.     public float walkableMaxX;
  73.     public float walkableMaxZ;
  74.     public float boundSize;
  75.     @SideOnly(Side.CLIENT)
  76.     public RotativePoint riderPos;
  77.     //public float slopeShift;
  78.     public float[] wheelRadius;
  79.     public float[] wheelAngle;
  80.     //public OrientedBB obb;
  81.     public EntityTrain frontCoupledCart;
  82.     public EntityTrain rearCoupledCart;
  83.     public UUID frontCartUUID;
  84.     public UUID rearCartUUID;
  85.     private boolean isSlope;
  86.     @SideOnly(Side.CLIENT)
  87.     private double velX;
  88.     @SideOnly(Side.CLIENT)
  89.     private double velY;
  90.     @SideOnly(Side.CLIENT)
  91.     private double velZ;
  92.     public CouplerType coupler;
  93.     public Entity riddenBy;
  94.  
  95.     public EntityTrain(World par1World){
  96.         super(par1World);
  97.         this.setSize(1.0F, 1.0F);
  98.         this.boundSize = 0/16F;
  99.         this.isFrontCoupled = false;
  100.         this.isRearCoupled = false;
  101.         this.doRenderCouplingPositions = false;
  102.         this.isStatic = false;
  103.         //this.yOffset = -10F / 16F;
  104.         this.stepHeight = 0.25F;
  105.         this.mass = 1.0F;
  106.         this.entityCollisionReduction = 0.98F;
  107.         this.maxSpeed = 0.5F;
  108.         this.projectedSpeed = 0F;
  109.         this.rotationCos = 1F;
  110.         this.rotationSin = 0F;
  111.         this.frontCouplerShift = 0.0F;
  112.         this.rearCouplerShift = 0.0F;
  113.         //this.riderShiftXdef = 0F;
  114.         //this.riderShiftZdef = 0F;
  115.         //this.riderShiftX = 0F;
  116.         //this.riderShiftZ = 0F;
  117.         //this.riderPos = new RotativePoint((Entity)this, 0F, 0F, 0F);
  118.         this.walkableMinX = 0;//riderShiftXdef;
  119.         this.walkableMinZ = 0;//riderShiftZdef;
  120.         this.walkableMaxX = 0;//riderShiftXdef;
  121.         this.walkableMaxZ = 0;//riderShiftZdef;
  122.         //this.slopeShift = 0F;
  123.         this.frontCoupledCart = null;
  124.         this.rearCoupledCart = null;
  125.         this.riddenByEntity = null;
  126.         this.tangent = new float[]{0F, 0F, 0F};
  127.         this.wheelRadius = new float[]{};
  128.         this.wheelAngle = new float[]{};
  129.         this.dataWatcher.addObject(3, new Float(0)); //RotationYaw
  130.         this.dataWatcher.addObject(4, new Float(0)); //ProjectedSpeed
  131.         //this.obb = new OrientedBB(0, 0, 0, 0, 0, 0, 0, 0, 0);
  132.         this.coupler = CouplerType.NONE;
  133.         this.entityInit();
  134.     }
  135.  
  136.     @Override
  137.     public void entityInit(){
  138.     }
  139.    
  140.     @Override
  141.     public boolean isEntityInvulnerable(DamageSource source){
  142.         return true;
  143.     }
  144.    
  145.     @Override
  146.     public boolean attackEntityFrom(DamageSource par1DamageSource, float par2){
  147.         if(this.isEntityInvulnerable(par1DamageSource)){
  148.             return false;
  149.         }else if(!this.isDead && !this.worldObj.isRemote){
  150.             this.setDead();
  151.             this.setBeenAttacked();
  152.             this.worldObj.removeEntity(this);
  153.             EntityPlayer entityplayer = null;
  154.             if(par1DamageSource.getEntity() instanceof EntityPlayer){
  155.                 entityplayer = (EntityPlayer)par1DamageSource.getEntity();
  156.             }
  157.             if(entityplayer != null && entityplayer.capabilities.isCreativeMode){
  158.                 return true;
  159.             }
  160.         }
  161.         return true;
  162.     }
  163.  
  164.     @Override
  165.     public boolean interactFirst(EntityPlayer player){
  166.         if(player.getCurrentEquippedItem() != null){
  167.             ItemStack equippedItem = player.getCurrentEquippedItem();
  168.             if(equippedItem.getItem() == Items.iron_sword && player.capabilities.isCreativeMode){
  169.                 this.setDead();
  170.                 this.worldObj.removeEntity(this);
  171.                 return true;
  172.             }
  173.         }
  174.         return false;
  175.     }
  176.    
  177.     @Override
  178.     public void onUpdate(){
  179.         this.onEntityUpdate();
  180.     }
  181.  
  182.     @Override
  183.     public void onEntityUpdate(){
  184.         /*if (!this.worldObj.isRemote && this.riddenBy != null && this.riddenBy.isSneaking())
  185.         {
  186.             Entity en = (EntityPlayer)this.riddenBy;
  187.             en.mountEntity((Entity)null);
  188.             en.setSneaking(false);
  189.             this.riderPos.onUpdate();
  190.             en.setPosition(this.riderPos.getX(), this.riderPos.getY(), this.riderPos.getZ());
  191.         }*/
  192.         if(this.isFrontCoupled && this.frontCoupledCart != null && this.frontCoupledCart.isDead){
  193.             this.frontCoupledCart = null;
  194.             this.isFrontCoupled = false;
  195.         }
  196.         if(this.isRearCoupled && this.rearCoupledCart != null && this.rearCoupledCart.isDead){
  197.             this.rearCoupledCart = null;
  198.             this.isRearCoupled = false;
  199.         }
  200.         if(this.ridingEntity != null && this.ridingEntity.isDead){
  201.             this.ridingEntity = null;
  202.         }
  203.  
  204.         // Because if we take minecraft basis and rotate it so that XZ axes are oriented
  205.         // as we are used to, zero angle will be directed to up (usual zero angle + 90 degree);
  206.         // this.rotationCos = (float)Math.cos(Math.toRadians(this.rotationYaw + 90F));
  207.         // this.rotationSin = (float)Math.sin(Math.toRadians(this.rotationYaw + 90F));
  208.  
  209.         // Look into math... cos(a + 90) = -sin(a); sin(a + 90) = cos(a);
  210.         this.rotationCos = (float) -Math.sin(Math.toRadians(this.rotationYaw));
  211.         this.rotationSin = (float)Math.cos(Math.toRadians(this.rotationYaw));
  212.         if(this.worldObj.isRemote){
  213.             if(doRenderCouplingPositions){
  214.                 updateCouplingPos();
  215.                 worldObj.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, this.frontCouplerX, this.posY + 1, this.frontCouplerZ, 0, 0, 0);
  216.                 worldObj.spawnParticle(EnumParticleTypes.REDSTONE, this.rearCouplerX, this.posY + 1, this.rearCouplerZ, 0, 0, 0);
  217.             }
  218.             //this.setPositionAndRotation2(this.posX, this.posY, this.posZ, this.rotationYaw, this.rotationPitch, 0);
  219.             this.rotationYaw = this.dataWatcher.getWatchableObjectFloat(3);
  220.             this.projectedSpeed = this.dataWatcher.getWatchableObjectFloat(4);
  221.             this.motionX = this.projectedSpeed * this.rotationCos;
  222.             this.motionZ = this.projectedSpeed * this.rotationSin;
  223.             this.motionY = this.projectedSpeed * tangent[2];
  224.             for(byte w = 0; w < wheelAngle.length; w++){
  225.                 this.wheelAngle[w] = (float)MathHelper.wrapAngleTo180_double(this.wheelAngle[w]
  226.                         - Math.toDegrees(this.projectedSpeed / this.wheelRadius[w]));
  227.             }
  228.             if(this.riddenByEntity != null && this.riddenByEntity instanceof EntityLivingBase){
  229.                 float rmm = 0.13F;
  230.                 float riderMoveForward = (float)((EntityLivingBase)this.riddenByEntity).moveForward * rmm;
  231.                 float riderMoveStrafing = (float)((EntityLivingBase)this.riddenByEntity).moveStrafing * rmm;
  232.                 float riderMoveCos = (float)Math.cos(Math.toRadians(this.riddenByEntity.rotationYaw - this.rotationYaw));
  233.                 float riderMoveSin = (float)Math.sin(Math.toRadians(this.riddenByEntity.rotationYaw - this.rotationYaw));
  234.                
  235.                 this.riderPos.setRelX(this.riderPos.getRelX() + -riderMoveSin * riderMoveForward + riderMoveCos * riderMoveStrafing);
  236.                 this.riderPos.setRelZ(this.riderPos.getRelZ() + +riderMoveCos * riderMoveForward + riderMoveSin * riderMoveStrafing);
  237.                 if(this.riderPos.getRelX() < walkableMinX)this.riderPos.setRelX(walkableMinX);
  238.                 if(this.riderPos.getRelX() > walkableMaxX)this.riderPos.setRelX(walkableMaxX);
  239.                 if(this.riderPos.getRelZ() < walkableMinZ)this.riderPos.setRelZ(walkableMinZ);
  240.                 if(this.riderPos.getRelZ() > walkableMaxZ)this.riderPos.setRelZ(walkableMaxZ);
  241.             }
  242.         }else{
  243.             if(needsCouplingsCheck){
  244.                 this.isFrontCoupled = false;
  245.                 this.isRearCoupled = false;
  246.                 List list = this.worldObj.getEntitiesWithinAABB(EntityTrain.class,
  247.                         this.getEntityBoundingBox().expand(15.0D, 15.0D, 15.0D));
  248.                 Iterator iterator = list.iterator();
  249.                 while(iterator.hasNext()){
  250.                     EntityTrain cart = (EntityTrain)iterator.next();
  251.                     if(cart.getUniqueID().equals(frontCartUUID) && !cart.isDead && cart != null){
  252.                         this.frontCoupledCart = cart;
  253.                         this.isFrontCoupled = true;
  254.                         break;
  255.                     }
  256.                 }
  257.                 iterator = list.iterator();
  258.                 while(iterator.hasNext()){
  259.                     EntityTrain cart = (EntityTrain)iterator.next();
  260.                     if(cart.getUniqueID().equals(rearCartUUID) && !cart.isDead && cart != null){
  261.                         this.rearCoupledCart = cart;
  262.                         this.isRearCoupled = true;
  263.                         break;
  264.                     }
  265.                 }
  266.                 this.needsCouplingsCheck = false;
  267.             }
  268.             if(this.posY < -64.0D){
  269.                 this.kill();
  270.             }
  271.             this.prevPosX = this.posX;
  272.             this.prevPosY = this.posY;
  273.             this.prevPosZ = this.posZ;
  274.             this.prevRotationPitch = this.rotationPitch;
  275.             this.prevRotationYaw = this.rotationYaw;
  276.             this.motionX *= 0.95;
  277.             this.motionY *= 0.95;
  278.             this.motionZ *= 0.95;
  279.            
  280.             int i = MathHelper.floor_double(posX);
  281.             int j = MathHelper.floor_double(posY);
  282.             int k = MathHelper.floor_double(posZ);
  283.             Block l = worldObj.getBlockState(new BlockPos(i,j,k)).getBlock();
  284.             //int l = worldObj.getBlockState(new BlockPos((i, j, k)));
  285.  
  286.             this.addVelocity(0, -0.04D, 0);
  287.             this.doBlockCollisions();
  288.             List collidingEntities = this.worldObj.getEntitiesWithinAABBExcludingEntity(this,
  289.                     this.getEntityBoundingBox().expand(0.20000000298023224D, 0.0D, 0.20000000298023224D));
  290.             if(collidingEntities != null && !collidingEntities.isEmpty()){
  291.                 for(int p = 0; p < collidingEntities.size(); ++p){
  292.                     Entity entity = (Entity)collidingEntities.get(p);
  293.                     if(entity != this.frontCoupledCart && entity != this.rearCoupledCart
  294.                             && entity != this.riddenByEntity){
  295.                         this.applyEntityCollision(entity);
  296.                     }
  297.                 }
  298.             }  
  299.             this.updateCouplings();
  300.             this.updateCouplingPos();
  301.             this.updateOnRail();
  302.             this.projectedSpeed = (float)(this.motionX * rotationCos + this.motionZ * rotationSin + motionY * tangent[2]);
  303.             this.dataWatcher.updateObject(3, Float.valueOf(this.rotationYaw));
  304.             this.dataWatcher.updateObject(4, Float.valueOf(this.projectedSpeed));
  305.         }
  306.        
  307.         if(this.isStatic){
  308.             this.motionX = 0.0D;
  309.             this.motionY = 0.0D;
  310.             this.motionZ = 0.0D;
  311.         }
  312.         this.moveEntity(this.motionX, this.motionY, this.motionZ);
  313.     }
  314.    
  315.     public void updateCouplings(){
  316.         this.updateCouplingPos();
  317.         float rigidity = 0.25F;
  318.         float desiredDistance = 0;
  319.         float damping = 0.0F;
  320.         if(this.isRearCoupled){
  321.             this.rearCoupledCart.updateCouplingPos();
  322.             float targetMotionX = (float)(this.motionX - this.rearCoupledCart.motionX);
  323.             float targetMotionZ = (float)(this.motionZ - this.rearCoupledCart.motionZ);
  324.             float targetX = (this.rearCouplerX + this.rearCoupledCart.rearCouplerX) / 2;
  325.             float targetZ = (this.rearCouplerZ + this.rearCoupledCart.rearCouplerZ) / 2;
  326.             if(this.rearCoupledCart.frontCoupledCart == this){
  327.                 targetX = (this.rearCouplerX + this.rearCoupledCart.frontCouplerX) / 2;
  328.                 targetZ = (this.rearCouplerZ + this.rearCoupledCart.frontCouplerZ) / 2;
  329.             }
  330.             float vecX = targetX - this.rearCouplerX;
  331.             float vecZ = targetZ - this.rearCouplerZ;
  332.             float hypot = (float)Math.hypot(vecX, vecZ);
  333.  
  334.             float unitX = vecX / hypot;
  335.             float unitZ = vecZ / hypot;
  336.            
  337.             float stretch = hypot - desiredDistance;
  338.            
  339.             float damperX = targetMotionX * damping;
  340.             float damperZ = targetMotionX * damping;
  341.            
  342.             damperX = limitForce(damperX);
  343.             damperZ = limitForce(damperZ);
  344.            
  345.             float forceX = rigidity * stretch * unitX - damperX;
  346.             float forceZ = rigidity * stretch * unitZ - damperZ;
  347.            
  348.             this.addVelocity(forceX, 0.0F, forceZ);
  349.             this.rearCoupledCart.addVelocity(-forceX, 0.0F, -forceZ);
  350.         }
  351.         if(this.isFrontCoupled){
  352.             this.frontCoupledCart.updateCouplingPos();
  353.             float targetMotionX = (float)(this.motionX - this.frontCoupledCart.motionX);
  354.             float targetMotionZ = (float)(this.motionZ - this.frontCoupledCart.motionZ);
  355.             float targetX = (this.frontCouplerX + this.frontCoupledCart.rearCouplerX) / 2;
  356.             float targetZ = (this.frontCouplerZ + this.frontCoupledCart.rearCouplerZ) / 2;
  357.             if(this.frontCoupledCart.frontCoupledCart == this){
  358.                 targetX = (this.frontCouplerX + this.frontCoupledCart.frontCouplerX) / 2;
  359.                 targetZ = (this.frontCouplerZ + this.frontCoupledCart.frontCouplerZ) / 2;
  360.             }
  361.             float vecX = targetX - this.frontCouplerX;
  362.             float vecZ = targetZ - this.frontCouplerZ;
  363.             float hypot = (float)Math.hypot(vecX, vecZ);
  364.  
  365.             float unitX = vecX / hypot;
  366.             float unitZ = vecZ / hypot;
  367.            
  368.             float stretch = hypot - desiredDistance;
  369.            
  370.             float damperX = targetMotionX * damping;
  371.             float damperZ = targetMotionX * damping;
  372.            
  373.             damperX = limitForce(damperX);
  374.             damperZ = limitForce(damperZ);
  375.            
  376.             float forceX = rigidity * stretch * unitX - damperX;
  377.             float forceZ = rigidity * stretch * unitZ - damperZ;
  378.            
  379.             this.addVelocity(forceX, 0.0F, forceZ);
  380.             this.frontCoupledCart.addVelocity(-forceX, 0.0F, -forceZ);
  381.         }
  382.     }
  383.    
  384.     /*
  385.     public void updateCouplings(){
  386.         this.updateCouplingPos();
  387.         float rigidity = 0.5F;
  388.         float desiredDistance = 0;
  389.         if(this.isRearCoupled){
  390.             this.rearCoupledCart.updateCouplingPos();
  391.             float vecX = this.rearCouplerX - this.rearCoupledCart.rearCouplerX;
  392.             float vecZ = this.rearCouplerZ - this.rearCoupledCart.rearCouplerZ;
  393.             if(this.rearCoupledCart.frontCoupledCart == this){
  394.                 vecX = this.rearCouplerX - this.rearCoupledCart.frontCouplerX;
  395.                 vecZ = this.rearCouplerZ - this.rearCoupledCart.frontCouplerZ;
  396.             }
  397.              // 0.25F ?
  398.             double springX = rigidity * vecX * -1F;
  399.             double springZ = rigidity * vecZ * -1F;
  400.             //springX = limitForce(springX);
  401.             //springZ = limitForce(springZ);
  402.             this.addVelocity(springX, 0.0F, springZ);
  403.             this.rearCoupledCart.addVelocity(-springX, 0.0F, -springZ);
  404.         }
  405.         // System.out.println("Updating coupling!");
  406.     }
  407.     /*
  408.     public void updateCouplings(){
  409.         this.updateCouplingPos();
  410.         float k = 0.95F;
  411.         float l = 0.95F;
  412.         // float md = 0.015625F;
  413.        
  414.         if(this.isFrontCoupled){
  415.             this.frontCoupledCart.updateCouplingPos();
  416.             float newCouplerX = (this.frontCouplerX + this.frontCoupledCart.rearCouplerX) / 2;
  417.             float newCouplerZ = (this.frontCouplerZ + this.frontCoupledCart.rearCouplerZ) / 2;
  418.             if(this.frontCoupledCart.frontCoupledCart == this){
  419.                 newCouplerX = (this.frontCouplerX + this.frontCoupledCart.frontCouplerX) / 2;
  420.                 newCouplerZ = (this.frontCouplerZ + this.frontCoupledCart.frontCouplerZ) / 2;
  421.             }
  422.             float dx = (float)(newCouplerX - this.frontCouplerX) * k;
  423.             float dz = (float)(newCouplerZ - this.frontCouplerZ) * k;
  424.             //float dsqr = dx * dx + dz * dz; if(dsqr < md){ dx = dz = 0; }
  425.             this.addVelocity(-this.prevFrontCouplerdx * l, 0.0F, -this.prevFrontCouplerdz * l);
  426.             this.addVelocity(dx, 0.0F, dz);
  427.             this.prevFrontCouplerdx = dx;
  428.             this.prevFrontCouplerdz = dz;
  429.         }
  430.         if(this.isRearCoupled){
  431.             this.rearCoupledCart.updateCouplingPos();
  432.             float newCouplerX = (this.rearCouplerX + this.rearCoupledCart.rearCouplerX) / 2;
  433.             float newCouplerZ = (this.rearCouplerZ + this.rearCoupledCart.rearCouplerZ) / 2;
  434.             if(this.rearCoupledCart.frontCoupledCart == this){
  435.                 newCouplerX = (this.rearCouplerX + this.rearCoupledCart.frontCouplerX) / 2;
  436.                 newCouplerZ = (this.rearCouplerZ + this.rearCoupledCart.frontCouplerZ) / 2;
  437.             }
  438.             float dx = (float)(newCouplerX - this.rearCouplerX) * k;
  439.             float dz = (float)(newCouplerZ - this.rearCouplerZ) * k;
  440.             //float dsqr = dx * dx + dz * dz; if(dsqr < md){ dx = dz = 0; }
  441.             // double sumMx = (this.motionX * this.mass +
  442.             // this.coupledCart.motionX * this.coupledCart.mass)/
  443.             // (this.mass + this.coupledCart.mass);
  444.             // double sumMz = (this.motionZ * this.mass +
  445.             // this.coupledCart.motionZ * this.coupledCart.mass)/
  446.             // (this.mass + this.coupledCart.mass);
  447.             // this.setVelocity(sumMx, this.motionY, sumMz);
  448.             this.addVelocity(-this.prevRearCouplerdx * l, 0.0F, -this.prevRearCouplerdz * l);
  449.             this.addVelocity(dx, 0.0F, dz);
  450.             this.prevRearCouplerdx = dx;
  451.             this.prevRearCouplerdz = dz;
  452.         }
  453.         // System.out.println("Updating coupling!");
  454.     }*/
  455.  
  456.     private float limitForce(double force){
  457.         return (float)Math.copySign(Math.abs(Math.min(Math.abs(force), 6.0F)), force);
  458.     }
  459.    
  460.     public void updateCouplingPos(){
  461.         frontCouplerX = (float)(posX + rotationCos * frontCouplerShift);
  462.         frontCouplerZ = (float)(posZ + rotationSin * frontCouplerShift);
  463.         rearCouplerX = (float)(posX + rotationCos * rearCouplerShift);
  464.         rearCouplerZ = (float)(posZ + rotationSin * rearCouplerShift);
  465.     }
  466.    
  467.     public float[] getTangent(){
  468.         if(worldObj.getBlockState(new BlockPos(railPosX, railPosY, railPosZ)).getBlock() == RoWBlocks.rails.blockID){
  469.             TileEntityWideRail railTile = (TileEntityWideRail)worldObj.getTileEntity(new BlockPos(railPosX, railPosY,
  470.                     railPosZ));
  471.             if(railTile != null){
  472.                 if(railTile.mId == 0 || ((railTile.mId == 5 || railTile.mId == 6) && !wasLastSwitchActive)
  473.                         || ((railTile.mId == 9 || railTile.mId == 10) && !wasLastSwitchActive)){
  474.                     byte dir = (byte)worldObj.getTileEntity(new BlockPos(railPosX, railPosY, railPosZ)).getBlockMetadata();
  475.                     int[] tg = RotationHelper.rotateXZByDir(0, 1, dir);
  476.                     return new float[]{tg[0], tg[1], 0F};
  477.                 }else if(railTile.mId == 3 || railTile.mId == 4
  478.                         || ((railTile.mId == 5 || railTile.mId == 6) && wasLastSwitchActive)){
  479.                     int dir = worldObj.getTileEntity(new BlockPos(railPosX, railPosY, railPosZ)).getBlockMetadata() & 3;
  480.                     float xs = railTile.mId == 3 || railTile.mId == 5?-1:1;
  481.                     float[] c = RotationHelper.rotateXZByDir(xs * 15.5F, -0.5F, dir);
  482.                     c[0] += railPosX + 0.5F;
  483.                     c[1] += railPosZ + 0.5F;
  484.                     float radX = (float)(c[0] - posX);
  485.                     float radZ = (float)(c[1] - posZ);
  486.                     float radA = (float)Math.atan2(radZ, radX);
  487.                     float perA = (float)(radA + Math.PI / 2);
  488.                     float perX = (float)Math.cos(perA);
  489.                     float perZ = (float)Math.sin(perA);
  490.                     float radG = (float)Math.sqrt(radX * radX + radZ * radZ);
  491.                     radX /= radG;
  492.                     radZ /= radG;
  493.                     this.setPosition(c[0] + radX * -15.5, this.railPosY + (6F/16F), c[1] + radZ * -15.5);
  494.                     return new float[]{perX, perZ, 0F};
  495.                 }else if(railTile.mId == 7 || railTile.mId == 8
  496.                         || ((railTile.mId == 9 || railTile.mId == 10) && wasLastSwitchActive)
  497.                         || railTile.mId == 11){
  498.                     float dist = (float)(Math.pow(posX - railPosX, 2) + Math.pow(posZ - railPosZ, 2));
  499.                     int b0 = dist < 62?0:1;
  500.                     int dir = (worldObj.getTileEntity(new BlockPos(railPosX, railPosY, railPosZ)).getBlockMetadata() + b0 * 2) & 3;
  501.                     int dir1 = (worldObj.getTileEntity(new BlockPos(railPosX, railPosY, railPosZ)).getBlockMetadata() & 3;
  502.                     float xs = railTile.mId == 7 || railTile.mId == 9
  503.                             || (railTile.mId == 11 && wasLastSwitchActive)?-1:1;
  504.                     float[] c = RotationHelper.rotateXZByDir(xs * 12.95F, -0.15F, dir);
  505.                     float[] c2 = RotationHelper.rotateXZByDir(xs * 5F * b0, 15F * b0, dir1);
  506.                     c[0] += c2[0] + railPosX + 0.5F;
  507.                     c[1] += c2[1] + railPosZ + 0.5F;
  508.                     float radX = (float)(c[0] - posX);
  509.                     float radZ = (float)(c[1] - posZ);
  510.                     float radA = (float)Math.atan2(radZ, radX);
  511.                     float perA = (float)(radA + Math.PI / 2);
  512.                     float perX = (float)Math.cos(perA);
  513.                     float perZ = (float)Math.sin(perA);
  514.                     float radG = (float)Math.sqrt(radX * radX + radZ * radZ);
  515.                     radX /= radG;
  516.                     radZ /= radG;
  517.                     this.setPosition(c[0] + radX * -12.9F, this.railPosY + (6F/16F), c[1] + radZ * -12.9F);
  518.                     return new float[]{perX, perZ, 0F};
  519.                 }else if(railTile.mId == 12 || railTile.mId == 13){
  520.                     byte dir = (byte)worldObj.getTileEntity(new BlockPos(railPosX, railPosY, railPosZ)).getBlockMetadata();
  521.                     int[] tg = RotationHelper.rotateXZByDir(0, 1, dir);
  522.                     return new float[]{tg[0], tg[1], 1F/(25F * (railTile.mId - 11))};
  523.                 }
  524.             }
  525.         }
  526.         return new float[]{this.rotationCos, this.rotationSin, this.tangent[2]};
  527.     }
  528.    
  529.    
  530.    
  531.     public void updateOnRail(){
  532.         int i = MathHelper.floor_double(posX);
  533.         int j = MathHelper.floor_double(posY);
  534.         int k = MathHelper.floor_double(posZ);
  535.         this.isOnRail = true;
  536.         this.isSlope = true;
  537.         int id = worldObj.getBlockId(i, j, k);
  538.         TileEntityWideRail railTile;
  539.         TileEntityGag gagTile;
  540.         int metaData = 0;
  541.         int mId = 0;
  542.         if(id == RoWBlocks.rails.blockID){
  543.             this.railPosX = i;
  544.             this.railPosY = j;
  545.             this.railPosZ = k;
  546.             railTile = (TileEntityWideRail)worldObj.getTileEntity(new BlockPos(i, j, k));
  547.             metaData = worldObj.getTileEntity(new BlockPos(i, j, k)).getBlockMetadata();
  548.             if(railTile != null && railTile.mId == 0){
  549.                 mId = railTile.mId;
  550.                 if(metaData == 0 || metaData == 2){
  551.                     this.setPosition(railPosX + 0.5F, railPosY + 6/16F, posZ);
  552.                 }else{
  553.                     this.setPosition(posX, railPosY + 6/16F, railPosZ + 0.5F);
  554.                 }
  555.             }else if(railTile != null && railTile.mId != 12 && railTile.mId != 13){
  556.                 this.setPosition(posX, railPosY + 6/16F, posZ);
  557.             }
  558.             this.wasLastSwitchActive = railTile.activated;
  559.         }else if(id == RoWBlocks.railGag.blockID){
  560.             gagTile = (TileEntityGag)worldObj.getTileEntity(new BlockPos(i, j, k)).getBlockMetadata();
  561.             if(gagTile != null && !(gagTile.primary_x == 0 && gagTile.primary_y == 0 && gagTile.primary_z == 0)){
  562.                 this.railPosX = gagTile.primary_x;
  563.                 this.railPosY = gagTile.primary_y;
  564.                 this.railPosZ = gagTile.primary_z;
  565.                 railTile = (TileEntityWideRail)worldObj.getBlockTileEntity(gagTile.primary_x, gagTile.primary_y,
  566.                         gagTile.primary_z);
  567.                 if(railTile != null){
  568.                     metaData = worldObj.getTileEntity(new BlockPos(i, j, k)).getBlockMetadata();
  569.                     mId = railTile.mId;
  570.                     if(railTile != null && mId == 0){
  571.                         if(metaData == 0 || metaData == 2){
  572.                             this.setPosition(railPosX + 0.5F, railPosY + 6F/16F, posZ);
  573.                         }else{
  574.                             this.setPosition(posX, railPosY + 6F/16F, railPosZ + 0.5F);
  575.                         }
  576.                     }else if(railTile != null && railTile.mId != 12 && railTile.mId != 13){
  577.                         this.setPosition(posX, railPosY + 6/16F, posZ);
  578.                     }
  579.                     float ds = (float)Math
  580.                             .sqrt(Math.pow(gagTile.primary_x - i, 2) + Math.pow(gagTile.primary_z - k, 2));
  581.  
  582.                     if((railTile.mId == 5 || railTile.mId == 6 || railTile.mId == 9 || railTile.mId == 10) && ds > 7){
  583.                         this.wasLastSwitchActive = !(gagTile.primary_x == i || gagTile.primary_z == k);
  584.                     }else if((railTile.mId == 11) && ds > 5){
  585.                         if(metaData == 0 || metaData == 2){
  586.                             this.wasLastSwitchActive = metaData == 0?Math.signum(posX - railPosX) < 0:Math.signum(posX
  587.                                     - railPosX) > 0;
  588.                         }else{
  589.                             this.wasLastSwitchActive = metaData == 1?Math.signum(posZ - railPosZ) < 0:Math.signum(posZ
  590.                                     - railPosZ) > 0;
  591.                         }
  592.                     }
  593.                 }
  594.             }
  595.         }else{
  596.             j--;
  597.             mId = 0;
  598.             id = worldObj.getBlockId(i, j, k);
  599.             if(id == RoWBlocks.rails.blockID){
  600.                 this.railPosX = i;
  601.                 this.railPosY = j;
  602.                 this.railPosZ = k;
  603.                 railTile = (TileEntityWideRail)worldObj.getTileEntity(new BlockPos(i, j, k));
  604.                 if(railTile != null){
  605.                     mId = railTile.mId;
  606.                 }
  607.             }else if(id == RoWBlocks.railGag.blockID){
  608.                 gagTile = (TileEntityGag)worldObj.getTileEntity(new BlockPos(i, j, k));
  609.                 if(gagTile != null && !(gagTile.primary_x == 0 && gagTile.primary_y == 0 && gagTile.primary_z == 0)){
  610.                     this.railPosX = gagTile.primary_x;
  611.                     this.railPosY = gagTile.primary_y;
  612.                     this.railPosZ = gagTile.primary_z;
  613.                     railTile = (TileEntityWideRail)worldObj.getBlockTileEntity(gagTile.primary_x, gagTile.primary_y,
  614.                             gagTile.primary_z);
  615.                     if(railTile != null){
  616.                         mId = railTile.mId;
  617.                     }
  618.                 }
  619.             }
  620.             this.isOnRail = mId == 12 || mId == 13;
  621.         }
  622.         this.isOnSlope = mId == 12 || mId == 13;
  623.         this.tangent = getTangent();
  624.         if(this.isOnRail){
  625.             float s = (float)(motionX * tangent[0] + motionZ * tangent[1] + motionY * tangent[2]);
  626.             if(s > maxSpeed) s = maxSpeed;
  627.             if(s < -maxSpeed) s = -maxSpeed;
  628.             this.motionX = this.tangent[0] * s;
  629.             this.motionZ = this.tangent[1] * s;
  630.             this.motionY = this.tangent[2] * s;
  631.         }else{
  632.             float s = (float)(motionX * tangent[0] + motionZ * tangent[1]);
  633.             if(s > maxSpeed) s = maxSpeed;
  634.             if(s < -maxSpeed) s = -maxSpeed;
  635.             this.motionX = this.tangent[0] * s * 0.95;
  636.             this.motionZ = this.tangent[1] * s * 0.95;
  637.         }
  638.        
  639.         this.setVelocity(motionX, motionY, motionZ);
  640.         float trackAngle = (float)Math.toDegrees(-Math.atan2(tangent[0], tangent[1]));
  641.         float y1 = (float)MathHelper.wrapAngleTo180_double(trackAngle - rotationYaw);
  642.         float y2 = (float)Math.toDegrees(Math.atan(this.tangent[2]));
  643.         if(y1 > 90 || y1 < -90){
  644.             this.setRotation((float)MathHelper.wrapAngleTo180_double(180 + trackAngle), -y2);
  645.         }else{
  646.             this.setRotation(trackAngle, y2);
  647.         }
  648.         this.setPosition(posX, posY, posZ);
  649.     }
  650.    
  651.     public void applyForwardMotion(float v){
  652.         this.addVelocity(this.rotationCos * v, 0F, this.rotationSin * v);
  653.     }
  654.    
  655.    
  656.     @Override
  657.     @SideOnly(Side.CLIENT)
  658.     public void setPositionAndRotation2(double x, double y, double z, float yaw, float pitch, int posRotationIncrements, boolean p_180426_10_){
  659.         this.setPosition(x, y, z);
  660.         this.setRotation(yaw, pitch);
  661.     }
  662.  
  663.     @Override
  664.     @SideOnly(Side.CLIENT)
  665.     public void setVelocity(double par1, double par3, double par5){
  666.         /*this.motionX = par1;
  667.         this.motionY = par3;
  668.         this.motionZ = par5;*/
  669.     }
  670.  
  671.     /*@Override
  672.     @SideOnly(Side.CLIENT)
  673.     public void setVelocity(double par1, double par3, double par5)
  674.     {
  675.         this.velX = this.motionX = par1;
  676.         this.velY = this.motionY = par3;
  677.         this.velZ = this.motionZ = par5;
  678.     }
  679.  
  680.     @Override
  681.     @SideOnly(Side.CLIENT)
  682.     public void setPositionAndRotation2(double par1, double par3, double par5, float par7, float par8, int par9)
  683.     {
  684.         this.cartX = par1;
  685.         this.cartY = par3;
  686.         this.cartZ = par5;
  687.         this.cartYaw = (double)par7;
  688.         this.motionX = this.velX;
  689.         this.motionY = this.velY;
  690.         this.motionZ = this.velZ;
  691.     }*/
  692.    
  693.    
  694.     @Override
  695.     public void onCollideWithPlayer(EntityPlayer player) {
  696.         this.applyEntityCollision((Entity)player);
  697.     }
  698.    
  699.     @Override
  700.     public void applyEntityCollision(Entity par1Entity){
  701.         if(!worldObj.isRemote){
  702.             if(par1Entity.riddenByEntity != this && par1Entity.ridingEntity != this){
  703.                 if(par1Entity instanceof EntityLivingBase && !(par1Entity instanceof EntityPlayer)
  704.                         && !(par1Entity instanceof EntityTrain)
  705.                         && this.canBeRidden()
  706.                         && this.motionX * this.motionX + this.motionZ * this.motionZ > 0.01D
  707.                         && this.riddenByEntity == null && par1Entity.ridingEntity == null){
  708.                     par1Entity.mountEntity(this);
  709.                 }else{
  710.                     double d0 = par1Entity.posX - this.posX;
  711.                     double d1 = par1Entity.posZ - this.posZ;
  712.                     double d2 = MathHelper.abs_max(d0, d1);
  713.  
  714.                     if(d2 >= 0.009999999776482582D){
  715.                         d2 = (double)MathHelper.sqrt_double(d2);
  716.                         d0 /= d2;
  717.                         d1 /= d2;
  718.                         double d3 = 1.0D / d2;
  719.  
  720.                         if(d3 > 1.0D){
  721.                             d3 = 1.0D;
  722.                         }
  723.  
  724.                         d0 *= d3;
  725.                         d1 *= d3;
  726.                         d0 *= 0.05D;
  727.                         d1 *= 0.05D;
  728.                         par1Entity.addVelocity(d0, 0.0D, d1);
  729.                         d0 *= (double)(1.0F - this.entityCollisionReduction);
  730.                         d1 *= (double)(1.0F - this.entityCollisionReduction);
  731.                         this.addVelocity(-d0, 0.0D, -d1);
  732.                         float sqrtMotion = (float)(Math.sqrt(Math.pow(motionX, 2) + Math.pow(motionZ, 2)));
  733.                         if(sqrtMotion > 0.05){
  734.                             par1Entity.attackEntityFrom(DamageSource.generic, sqrtMotion * 50);
  735.                         }
  736.                     }
  737.                 }
  738.             }
  739.         }
  740.     }
  741.    
  742.     /*@Override
  743.     public void applyEntityCollision(Entity par1Entity){
  744.         if(par1Entity.riddenByEntity != this && par1Entity.ridingEntity != this){
  745.             double d0 = par1Entity.posX - this.posX;
  746.             double d1 = par1Entity.posZ - this.posZ;
  747.             double d2 = MathHelper.abs_max(d0, d1);
  748.  
  749.             if(d2 >= 0.009999999776482582D){
  750.                 d2 = (double)MathHelper.sqrt_double(d2);
  751.                 d0 /= d2;
  752.                 d1 /= d2;
  753.                 double d3 = 1.0D / d2;
  754.  
  755.                 if(d3 > 1.0D){
  756.                     d3 = 1.0D;
  757.                 }
  758.  
  759.                 d0 *= d3;
  760.                 d1 *= d3;
  761.                 d0 *= 0.05D;
  762.                 d1 *= 0.05D;
  763.                 par1Entity.addVelocity(d0, 0.0D, d1);
  764.                 d0 *= (double)(1.0F - this.entityCollisionReduction);
  765.                 d1 *= (double)(1.0F - this.entityCollisionReduction);
  766.                 this.addVelocity(-d0, 0.0D, -d1);
  767.                 float sqrtMotion = (float)(Math.sqrt(Math.pow(motionX, 2) + Math.pow(motionZ, 2)));
  768.                 if(sqrtMotion > 0.05){
  769.                     par1Entity.attackEntityFrom(DamageSource.generic, sqrtMotion * 50);
  770.                 }
  771.             }
  772.         }
  773.     }*/
  774.  
  775.     @Override
  776.     public void updateRiderPosition(){
  777.         if(this.riddenByEntity != null){
  778.             this.riderPos.onUpdate();
  779.             this.riddenByEntity.setPosition(this.riderPos.getX(), this.riderPos.getY(), this.riderPos.getZ());
  780.         }
  781.     }
  782.    
  783.     public boolean canBeRidden(){
  784.         return false;
  785.     }
  786.    
  787.     @Override
  788.     protected void playStepSound(BlockPos pos, Block blockIn){
  789.     }
  790.  
  791.     @Override
  792.     public AxisAlignedBB getCollisionBox(Entity par1Entity){
  793.         if(par1Entity != riddenByEntity && par1Entity.ridingEntity != this){
  794.             return par1Entity.getEntityBoundingBox();
  795.         }
  796.         return null;
  797.     }
  798.  
  799.     @Override
  800.     public AxisAlignedBB getEntityBoundingBox(){
  801.         float w = this.boundSize / 2F;
  802.         return AxisAlignedBB.fromBounds(this.posX - w, this.posY, this.posZ - w,
  803.                 this.posX + w, this.posY + this.height, this.posZ + w);
  804.     }
  805.  
  806.     @Override
  807.     public boolean shouldRiderSit(){
  808.         return false;
  809.     }
  810.  
  811.     @Override
  812.     public boolean canRiderInteract(){
  813.         return true;
  814.     }
  815.  
  816.     @Override
  817.     public double getMountedYOffset(){
  818.         return 1.0D;
  819.     }
  820.  
  821.     @Override
  822.     public boolean canBeCollidedWith(){
  823.         return true;
  824.     }
  825.  
  826.     @Override
  827.     public boolean canBePushed(){
  828.         return true;
  829.     }
  830.  
  831.     @Override
  832.     public Entity[] getParts(){
  833.         return null;
  834.     }
  835.  
  836.     @Override
  837.     protected void readEntityFromNBT(NBTTagCompound nbttagcompound){
  838.         if(nbttagcompound.hasKey("frontCoupledUUIDMost") && nbttagcompound.hasKey("frontCoupledUUIDLeast")){
  839.             this.frontCartUUID = new UUID(nbttagcompound.getLong("frontCoupledUUIDMost"),
  840.                     nbttagcompound.getLong("frontCoupledUUIDLeast"));
  841.         }
  842.         if(nbttagcompound.hasKey("rearCoupledUUIDMost") && nbttagcompound.hasKey("rearCoupledUUIDLeast")){
  843.             this.rearCartUUID = new UUID(nbttagcompound.getLong("rearCoupledUUIDMost"),
  844.                     nbttagcompound.getLong("rearCoupledUUIDLeast"));
  845.         }
  846.         this.needsCouplingsCheck = true;
  847.     }
  848.  
  849.     @Override
  850.     protected void writeEntityToNBT(NBTTagCompound nbttagcompound){
  851.         if(this.frontCoupledCart != null){
  852.             nbttagcompound.setLong("frontCoupledUUIDMost", this.frontCoupledCart.getUniqueID()
  853.                     .getMostSignificantBits());
  854.             nbttagcompound.setLong("frontCoupledUUIDLeast", this.frontCoupledCart.getUniqueID()
  855.                     .getLeastSignificantBits());
  856.         }
  857.         if(this.rearCoupledCart != null){
  858.             nbttagcompound.setLong("rearCoupledUUIDMost", this.rearCoupledCart.getUniqueID()
  859.                     .getMostSignificantBits());
  860.             nbttagcompound.setLong("rearCoupledUUIDLeast", this.rearCoupledCart.getUniqueID()
  861.                     .getLeastSignificantBits());
  862.         }
  863.     }
  864.    
  865.     public enum CouplerType {
  866.         NONE, CHER, RUSS_BnCH
  867.     }
  868. }
Advertisement
Add Comment
Please, Sign In to add comment