Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eu.tetrabyte.contentmod.entity;
- import java.util.Iterator;
- import java.util.List;
- import java.util.UUID;
- import net.minecraft.block.Block;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Items;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.util.AxisAlignedBB;
- import net.minecraft.util.BlockPos;
- import net.minecraft.util.DamageSource;
- import net.minecraft.util.EnumParticleTypes;
- import net.minecraft.util.MathHelper;
- import net.minecraft.world.World;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- /*
- import net.row.RoW;
- import net.row.config.RoWBlocks;
- import net.row.helpers.RotationHelper;
- import net.row.helpers.RotativePoint;
- import net.row.physhelper.OrientedBB;
- import net.row.tileentity.TileEntityGag;
- import net.row.tileentity.TileEntityWideRail;
- */
- public class EntityTrain extends Entity{
- protected double cartX;
- protected double cartY;
- protected double cartZ;
- protected double cartYaw;
- public boolean isOnRail;
- public boolean isOnSlope;
- public boolean wasLastSwitchActive;
- public boolean hasFrontCoupler;
- public boolean hasRearCoupler;
- public boolean needsCouplingsCheck;
- public boolean isFrontCoupled;
- public boolean isRearCoupled;
- public boolean doRenderCouplingPositions;
- public boolean isStatic;
- public int railPosX;
- public int railPosY;
- public int railPosZ;
- public float[] tangent;
- public float mass;
- public float maxSpeed;
- public float projectedSpeed;
- public float frontCouplerShift;
- public float frontCouplerX;
- public float frontCouplerZ;
- public float prevFrontCouplerdx;
- public float prevFrontCouplerdz;
- public float rearCouplerShift;
- public float rearCouplerX;
- public float rearCouplerZ;
- public float prevRearCouplerdx;
- public float prevRearCouplerdz;
- public float rotationCos;
- public float rotationSin;
- //public float riderShiftXdef;
- //public float riderShiftZdef;
- public float walkableMinX;
- public float walkableMinZ;
- public float walkableMaxX;
- public float walkableMaxZ;
- public float boundSize;
- @SideOnly(Side.CLIENT)
- public RotativePoint riderPos;
- //public float slopeShift;
- public float[] wheelRadius;
- public float[] wheelAngle;
- //public OrientedBB obb;
- public EntityTrain frontCoupledCart;
- public EntityTrain rearCoupledCart;
- public UUID frontCartUUID;
- public UUID rearCartUUID;
- private boolean isSlope;
- @SideOnly(Side.CLIENT)
- private double velX;
- @SideOnly(Side.CLIENT)
- private double velY;
- @SideOnly(Side.CLIENT)
- private double velZ;
- public CouplerType coupler;
- public Entity riddenBy;
- public EntityTrain(World par1World){
- super(par1World);
- this.setSize(1.0F, 1.0F);
- this.boundSize = 0/16F;
- this.isFrontCoupled = false;
- this.isRearCoupled = false;
- this.doRenderCouplingPositions = false;
- this.isStatic = false;
- //this.yOffset = -10F / 16F;
- this.stepHeight = 0.25F;
- this.mass = 1.0F;
- this.entityCollisionReduction = 0.98F;
- this.maxSpeed = 0.5F;
- this.projectedSpeed = 0F;
- this.rotationCos = 1F;
- this.rotationSin = 0F;
- this.frontCouplerShift = 0.0F;
- this.rearCouplerShift = 0.0F;
- //this.riderShiftXdef = 0F;
- //this.riderShiftZdef = 0F;
- //this.riderShiftX = 0F;
- //this.riderShiftZ = 0F;
- //this.riderPos = new RotativePoint((Entity)this, 0F, 0F, 0F);
- this.walkableMinX = 0;//riderShiftXdef;
- this.walkableMinZ = 0;//riderShiftZdef;
- this.walkableMaxX = 0;//riderShiftXdef;
- this.walkableMaxZ = 0;//riderShiftZdef;
- //this.slopeShift = 0F;
- this.frontCoupledCart = null;
- this.rearCoupledCart = null;
- this.riddenByEntity = null;
- this.tangent = new float[]{0F, 0F, 0F};
- this.wheelRadius = new float[]{};
- this.wheelAngle = new float[]{};
- this.dataWatcher.addObject(3, new Float(0)); //RotationYaw
- this.dataWatcher.addObject(4, new Float(0)); //ProjectedSpeed
- //this.obb = new OrientedBB(0, 0, 0, 0, 0, 0, 0, 0, 0);
- this.coupler = CouplerType.NONE;
- this.entityInit();
- }
- @Override
- public void entityInit(){
- }
- @Override
- public boolean isEntityInvulnerable(DamageSource source){
- return true;
- }
- @Override
- public boolean attackEntityFrom(DamageSource par1DamageSource, float par2){
- if(this.isEntityInvulnerable(par1DamageSource)){
- return false;
- }else if(!this.isDead && !this.worldObj.isRemote){
- this.setDead();
- this.setBeenAttacked();
- this.worldObj.removeEntity(this);
- EntityPlayer entityplayer = null;
- if(par1DamageSource.getEntity() instanceof EntityPlayer){
- entityplayer = (EntityPlayer)par1DamageSource.getEntity();
- }
- if(entityplayer != null && entityplayer.capabilities.isCreativeMode){
- return true;
- }
- }
- return true;
- }
- @Override
- public boolean interactFirst(EntityPlayer player){
- if(player.getCurrentEquippedItem() != null){
- ItemStack equippedItem = player.getCurrentEquippedItem();
- if(equippedItem.getItem() == Items.iron_sword && player.capabilities.isCreativeMode){
- this.setDead();
- this.worldObj.removeEntity(this);
- return true;
- }
- }
- return false;
- }
- @Override
- public void onUpdate(){
- this.onEntityUpdate();
- }
- @Override
- public void onEntityUpdate(){
- /*if (!this.worldObj.isRemote && this.riddenBy != null && this.riddenBy.isSneaking())
- {
- Entity en = (EntityPlayer)this.riddenBy;
- en.mountEntity((Entity)null);
- en.setSneaking(false);
- this.riderPos.onUpdate();
- en.setPosition(this.riderPos.getX(), this.riderPos.getY(), this.riderPos.getZ());
- }*/
- if(this.isFrontCoupled && this.frontCoupledCart != null && this.frontCoupledCart.isDead){
- this.frontCoupledCart = null;
- this.isFrontCoupled = false;
- }
- if(this.isRearCoupled && this.rearCoupledCart != null && this.rearCoupledCart.isDead){
- this.rearCoupledCart = null;
- this.isRearCoupled = false;
- }
- if(this.ridingEntity != null && this.ridingEntity.isDead){
- this.ridingEntity = null;
- }
- // Because if we take minecraft basis and rotate it so that XZ axes are oriented
- // as we are used to, zero angle will be directed to up (usual zero angle + 90 degree);
- // this.rotationCos = (float)Math.cos(Math.toRadians(this.rotationYaw + 90F));
- // this.rotationSin = (float)Math.sin(Math.toRadians(this.rotationYaw + 90F));
- // Look into math... cos(a + 90) = -sin(a); sin(a + 90) = cos(a);
- this.rotationCos = (float) -Math.sin(Math.toRadians(this.rotationYaw));
- this.rotationSin = (float)Math.cos(Math.toRadians(this.rotationYaw));
- if(this.worldObj.isRemote){
- if(doRenderCouplingPositions){
- updateCouplingPos();
- worldObj.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, this.frontCouplerX, this.posY + 1, this.frontCouplerZ, 0, 0, 0);
- worldObj.spawnParticle(EnumParticleTypes.REDSTONE, this.rearCouplerX, this.posY + 1, this.rearCouplerZ, 0, 0, 0);
- }
- //this.setPositionAndRotation2(this.posX, this.posY, this.posZ, this.rotationYaw, this.rotationPitch, 0);
- this.rotationYaw = this.dataWatcher.getWatchableObjectFloat(3);
- this.projectedSpeed = this.dataWatcher.getWatchableObjectFloat(4);
- this.motionX = this.projectedSpeed * this.rotationCos;
- this.motionZ = this.projectedSpeed * this.rotationSin;
- this.motionY = this.projectedSpeed * tangent[2];
- for(byte w = 0; w < wheelAngle.length; w++){
- this.wheelAngle[w] = (float)MathHelper.wrapAngleTo180_double(this.wheelAngle[w]
- - Math.toDegrees(this.projectedSpeed / this.wheelRadius[w]));
- }
- if(this.riddenByEntity != null && this.riddenByEntity instanceof EntityLivingBase){
- float rmm = 0.13F;
- float riderMoveForward = (float)((EntityLivingBase)this.riddenByEntity).moveForward * rmm;
- float riderMoveStrafing = (float)((EntityLivingBase)this.riddenByEntity).moveStrafing * rmm;
- float riderMoveCos = (float)Math.cos(Math.toRadians(this.riddenByEntity.rotationYaw - this.rotationYaw));
- float riderMoveSin = (float)Math.sin(Math.toRadians(this.riddenByEntity.rotationYaw - this.rotationYaw));
- this.riderPos.setRelX(this.riderPos.getRelX() + -riderMoveSin * riderMoveForward + riderMoveCos * riderMoveStrafing);
- this.riderPos.setRelZ(this.riderPos.getRelZ() + +riderMoveCos * riderMoveForward + riderMoveSin * riderMoveStrafing);
- if(this.riderPos.getRelX() < walkableMinX)this.riderPos.setRelX(walkableMinX);
- if(this.riderPos.getRelX() > walkableMaxX)this.riderPos.setRelX(walkableMaxX);
- if(this.riderPos.getRelZ() < walkableMinZ)this.riderPos.setRelZ(walkableMinZ);
- if(this.riderPos.getRelZ() > walkableMaxZ)this.riderPos.setRelZ(walkableMaxZ);
- }
- }else{
- if(needsCouplingsCheck){
- this.isFrontCoupled = false;
- this.isRearCoupled = false;
- List list = this.worldObj.getEntitiesWithinAABB(EntityTrain.class,
- this.getEntityBoundingBox().expand(15.0D, 15.0D, 15.0D));
- Iterator iterator = list.iterator();
- while(iterator.hasNext()){
- EntityTrain cart = (EntityTrain)iterator.next();
- if(cart.getUniqueID().equals(frontCartUUID) && !cart.isDead && cart != null){
- this.frontCoupledCart = cart;
- this.isFrontCoupled = true;
- break;
- }
- }
- iterator = list.iterator();
- while(iterator.hasNext()){
- EntityTrain cart = (EntityTrain)iterator.next();
- if(cart.getUniqueID().equals(rearCartUUID) && !cart.isDead && cart != null){
- this.rearCoupledCart = cart;
- this.isRearCoupled = true;
- break;
- }
- }
- this.needsCouplingsCheck = false;
- }
- if(this.posY < -64.0D){
- this.kill();
- }
- this.prevPosX = this.posX;
- this.prevPosY = this.posY;
- this.prevPosZ = this.posZ;
- this.prevRotationPitch = this.rotationPitch;
- this.prevRotationYaw = this.rotationYaw;
- this.motionX *= 0.95;
- this.motionY *= 0.95;
- this.motionZ *= 0.95;
- int i = MathHelper.floor_double(posX);
- int j = MathHelper.floor_double(posY);
- int k = MathHelper.floor_double(posZ);
- Block l = worldObj.getBlockState(new BlockPos(i,j,k)).getBlock();
- //int l = worldObj.getBlockState(new BlockPos((i, j, k)));
- this.addVelocity(0, -0.04D, 0);
- this.doBlockCollisions();
- List collidingEntities = this.worldObj.getEntitiesWithinAABBExcludingEntity(this,
- this.getEntityBoundingBox().expand(0.20000000298023224D, 0.0D, 0.20000000298023224D));
- if(collidingEntities != null && !collidingEntities.isEmpty()){
- for(int p = 0; p < collidingEntities.size(); ++p){
- Entity entity = (Entity)collidingEntities.get(p);
- if(entity != this.frontCoupledCart && entity != this.rearCoupledCart
- && entity != this.riddenByEntity){
- this.applyEntityCollision(entity);
- }
- }
- }
- this.updateCouplings();
- this.updateCouplingPos();
- this.updateOnRail();
- this.projectedSpeed = (float)(this.motionX * rotationCos + this.motionZ * rotationSin + motionY * tangent[2]);
- this.dataWatcher.updateObject(3, Float.valueOf(this.rotationYaw));
- this.dataWatcher.updateObject(4, Float.valueOf(this.projectedSpeed));
- }
- if(this.isStatic){
- this.motionX = 0.0D;
- this.motionY = 0.0D;
- this.motionZ = 0.0D;
- }
- this.moveEntity(this.motionX, this.motionY, this.motionZ);
- }
- public void updateCouplings(){
- this.updateCouplingPos();
- float rigidity = 0.25F;
- float desiredDistance = 0;
- float damping = 0.0F;
- if(this.isRearCoupled){
- this.rearCoupledCart.updateCouplingPos();
- float targetMotionX = (float)(this.motionX - this.rearCoupledCart.motionX);
- float targetMotionZ = (float)(this.motionZ - this.rearCoupledCart.motionZ);
- float targetX = (this.rearCouplerX + this.rearCoupledCart.rearCouplerX) / 2;
- float targetZ = (this.rearCouplerZ + this.rearCoupledCart.rearCouplerZ) / 2;
- if(this.rearCoupledCart.frontCoupledCart == this){
- targetX = (this.rearCouplerX + this.rearCoupledCart.frontCouplerX) / 2;
- targetZ = (this.rearCouplerZ + this.rearCoupledCart.frontCouplerZ) / 2;
- }
- float vecX = targetX - this.rearCouplerX;
- float vecZ = targetZ - this.rearCouplerZ;
- float hypot = (float)Math.hypot(vecX, vecZ);
- float unitX = vecX / hypot;
- float unitZ = vecZ / hypot;
- float stretch = hypot - desiredDistance;
- float damperX = targetMotionX * damping;
- float damperZ = targetMotionX * damping;
- damperX = limitForce(damperX);
- damperZ = limitForce(damperZ);
- float forceX = rigidity * stretch * unitX - damperX;
- float forceZ = rigidity * stretch * unitZ - damperZ;
- this.addVelocity(forceX, 0.0F, forceZ);
- this.rearCoupledCart.addVelocity(-forceX, 0.0F, -forceZ);
- }
- if(this.isFrontCoupled){
- this.frontCoupledCart.updateCouplingPos();
- float targetMotionX = (float)(this.motionX - this.frontCoupledCart.motionX);
- float targetMotionZ = (float)(this.motionZ - this.frontCoupledCart.motionZ);
- float targetX = (this.frontCouplerX + this.frontCoupledCart.rearCouplerX) / 2;
- float targetZ = (this.frontCouplerZ + this.frontCoupledCart.rearCouplerZ) / 2;
- if(this.frontCoupledCart.frontCoupledCart == this){
- targetX = (this.frontCouplerX + this.frontCoupledCart.frontCouplerX) / 2;
- targetZ = (this.frontCouplerZ + this.frontCoupledCart.frontCouplerZ) / 2;
- }
- float vecX = targetX - this.frontCouplerX;
- float vecZ = targetZ - this.frontCouplerZ;
- float hypot = (float)Math.hypot(vecX, vecZ);
- float unitX = vecX / hypot;
- float unitZ = vecZ / hypot;
- float stretch = hypot - desiredDistance;
- float damperX = targetMotionX * damping;
- float damperZ = targetMotionX * damping;
- damperX = limitForce(damperX);
- damperZ = limitForce(damperZ);
- float forceX = rigidity * stretch * unitX - damperX;
- float forceZ = rigidity * stretch * unitZ - damperZ;
- this.addVelocity(forceX, 0.0F, forceZ);
- this.frontCoupledCart.addVelocity(-forceX, 0.0F, -forceZ);
- }
- }
- /*
- public void updateCouplings(){
- this.updateCouplingPos();
- float rigidity = 0.5F;
- float desiredDistance = 0;
- if(this.isRearCoupled){
- this.rearCoupledCart.updateCouplingPos();
- float vecX = this.rearCouplerX - this.rearCoupledCart.rearCouplerX;
- float vecZ = this.rearCouplerZ - this.rearCoupledCart.rearCouplerZ;
- if(this.rearCoupledCart.frontCoupledCart == this){
- vecX = this.rearCouplerX - this.rearCoupledCart.frontCouplerX;
- vecZ = this.rearCouplerZ - this.rearCoupledCart.frontCouplerZ;
- }
- // 0.25F ?
- double springX = rigidity * vecX * -1F;
- double springZ = rigidity * vecZ * -1F;
- //springX = limitForce(springX);
- //springZ = limitForce(springZ);
- this.addVelocity(springX, 0.0F, springZ);
- this.rearCoupledCart.addVelocity(-springX, 0.0F, -springZ);
- }
- // System.out.println("Updating coupling!");
- }
- /*
- public void updateCouplings(){
- this.updateCouplingPos();
- float k = 0.95F;
- float l = 0.95F;
- // float md = 0.015625F;
- if(this.isFrontCoupled){
- this.frontCoupledCart.updateCouplingPos();
- float newCouplerX = (this.frontCouplerX + this.frontCoupledCart.rearCouplerX) / 2;
- float newCouplerZ = (this.frontCouplerZ + this.frontCoupledCart.rearCouplerZ) / 2;
- if(this.frontCoupledCart.frontCoupledCart == this){
- newCouplerX = (this.frontCouplerX + this.frontCoupledCart.frontCouplerX) / 2;
- newCouplerZ = (this.frontCouplerZ + this.frontCoupledCart.frontCouplerZ) / 2;
- }
- float dx = (float)(newCouplerX - this.frontCouplerX) * k;
- float dz = (float)(newCouplerZ - this.frontCouplerZ) * k;
- //float dsqr = dx * dx + dz * dz; if(dsqr < md){ dx = dz = 0; }
- this.addVelocity(-this.prevFrontCouplerdx * l, 0.0F, -this.prevFrontCouplerdz * l);
- this.addVelocity(dx, 0.0F, dz);
- this.prevFrontCouplerdx = dx;
- this.prevFrontCouplerdz = dz;
- }
- if(this.isRearCoupled){
- this.rearCoupledCart.updateCouplingPos();
- float newCouplerX = (this.rearCouplerX + this.rearCoupledCart.rearCouplerX) / 2;
- float newCouplerZ = (this.rearCouplerZ + this.rearCoupledCart.rearCouplerZ) / 2;
- if(this.rearCoupledCart.frontCoupledCart == this){
- newCouplerX = (this.rearCouplerX + this.rearCoupledCart.frontCouplerX) / 2;
- newCouplerZ = (this.rearCouplerZ + this.rearCoupledCart.frontCouplerZ) / 2;
- }
- float dx = (float)(newCouplerX - this.rearCouplerX) * k;
- float dz = (float)(newCouplerZ - this.rearCouplerZ) * k;
- //float dsqr = dx * dx + dz * dz; if(dsqr < md){ dx = dz = 0; }
- // double sumMx = (this.motionX * this.mass +
- // this.coupledCart.motionX * this.coupledCart.mass)/
- // (this.mass + this.coupledCart.mass);
- // double sumMz = (this.motionZ * this.mass +
- // this.coupledCart.motionZ * this.coupledCart.mass)/
- // (this.mass + this.coupledCart.mass);
- // this.setVelocity(sumMx, this.motionY, sumMz);
- this.addVelocity(-this.prevRearCouplerdx * l, 0.0F, -this.prevRearCouplerdz * l);
- this.addVelocity(dx, 0.0F, dz);
- this.prevRearCouplerdx = dx;
- this.prevRearCouplerdz = dz;
- }
- // System.out.println("Updating coupling!");
- }*/
- private float limitForce(double force){
- return (float)Math.copySign(Math.abs(Math.min(Math.abs(force), 6.0F)), force);
- }
- public void updateCouplingPos(){
- frontCouplerX = (float)(posX + rotationCos * frontCouplerShift);
- frontCouplerZ = (float)(posZ + rotationSin * frontCouplerShift);
- rearCouplerX = (float)(posX + rotationCos * rearCouplerShift);
- rearCouplerZ = (float)(posZ + rotationSin * rearCouplerShift);
- }
- public float[] getTangent(){
- if(worldObj.getBlockState(new BlockPos(railPosX, railPosY, railPosZ)).getBlock() == RoWBlocks.rails.blockID){
- TileEntityWideRail railTile = (TileEntityWideRail)worldObj.getTileEntity(new BlockPos(railPosX, railPosY,
- railPosZ));
- if(railTile != null){
- if(railTile.mId == 0 || ((railTile.mId == 5 || railTile.mId == 6) && !wasLastSwitchActive)
- || ((railTile.mId == 9 || railTile.mId == 10) && !wasLastSwitchActive)){
- byte dir = (byte)worldObj.getTileEntity(new BlockPos(railPosX, railPosY, railPosZ)).getBlockMetadata();
- int[] tg = RotationHelper.rotateXZByDir(0, 1, dir);
- return new float[]{tg[0], tg[1], 0F};
- }else if(railTile.mId == 3 || railTile.mId == 4
- || ((railTile.mId == 5 || railTile.mId == 6) && wasLastSwitchActive)){
- int dir = worldObj.getTileEntity(new BlockPos(railPosX, railPosY, railPosZ)).getBlockMetadata() & 3;
- float xs = railTile.mId == 3 || railTile.mId == 5?-1:1;
- float[] c = RotationHelper.rotateXZByDir(xs * 15.5F, -0.5F, dir);
- c[0] += railPosX + 0.5F;
- c[1] += railPosZ + 0.5F;
- float radX = (float)(c[0] - posX);
- float radZ = (float)(c[1] - posZ);
- float radA = (float)Math.atan2(radZ, radX);
- float perA = (float)(radA + Math.PI / 2);
- float perX = (float)Math.cos(perA);
- float perZ = (float)Math.sin(perA);
- float radG = (float)Math.sqrt(radX * radX + radZ * radZ);
- radX /= radG;
- radZ /= radG;
- this.setPosition(c[0] + radX * -15.5, this.railPosY + (6F/16F), c[1] + radZ * -15.5);
- return new float[]{perX, perZ, 0F};
- }else if(railTile.mId == 7 || railTile.mId == 8
- || ((railTile.mId == 9 || railTile.mId == 10) && wasLastSwitchActive)
- || railTile.mId == 11){
- float dist = (float)(Math.pow(posX - railPosX, 2) + Math.pow(posZ - railPosZ, 2));
- int b0 = dist < 62?0:1;
- int dir = (worldObj.getTileEntity(new BlockPos(railPosX, railPosY, railPosZ)).getBlockMetadata() + b0 * 2) & 3;
- int dir1 = (worldObj.getTileEntity(new BlockPos(railPosX, railPosY, railPosZ)).getBlockMetadata() & 3;
- float xs = railTile.mId == 7 || railTile.mId == 9
- || (railTile.mId == 11 && wasLastSwitchActive)?-1:1;
- float[] c = RotationHelper.rotateXZByDir(xs * 12.95F, -0.15F, dir);
- float[] c2 = RotationHelper.rotateXZByDir(xs * 5F * b0, 15F * b0, dir1);
- c[0] += c2[0] + railPosX + 0.5F;
- c[1] += c2[1] + railPosZ + 0.5F;
- float radX = (float)(c[0] - posX);
- float radZ = (float)(c[1] - posZ);
- float radA = (float)Math.atan2(radZ, radX);
- float perA = (float)(radA + Math.PI / 2);
- float perX = (float)Math.cos(perA);
- float perZ = (float)Math.sin(perA);
- float radG = (float)Math.sqrt(radX * radX + radZ * radZ);
- radX /= radG;
- radZ /= radG;
- this.setPosition(c[0] + radX * -12.9F, this.railPosY + (6F/16F), c[1] + radZ * -12.9F);
- return new float[]{perX, perZ, 0F};
- }else if(railTile.mId == 12 || railTile.mId == 13){
- byte dir = (byte)worldObj.getTileEntity(new BlockPos(railPosX, railPosY, railPosZ)).getBlockMetadata();
- int[] tg = RotationHelper.rotateXZByDir(0, 1, dir);
- return new float[]{tg[0], tg[1], 1F/(25F * (railTile.mId - 11))};
- }
- }
- }
- return new float[]{this.rotationCos, this.rotationSin, this.tangent[2]};
- }
- public void updateOnRail(){
- int i = MathHelper.floor_double(posX);
- int j = MathHelper.floor_double(posY);
- int k = MathHelper.floor_double(posZ);
- this.isOnRail = true;
- this.isSlope = true;
- int id = worldObj.getBlockId(i, j, k);
- TileEntityWideRail railTile;
- TileEntityGag gagTile;
- int metaData = 0;
- int mId = 0;
- if(id == RoWBlocks.rails.blockID){
- this.railPosX = i;
- this.railPosY = j;
- this.railPosZ = k;
- railTile = (TileEntityWideRail)worldObj.getTileEntity(new BlockPos(i, j, k));
- metaData = worldObj.getTileEntity(new BlockPos(i, j, k)).getBlockMetadata();
- if(railTile != null && railTile.mId == 0){
- mId = railTile.mId;
- if(metaData == 0 || metaData == 2){
- this.setPosition(railPosX + 0.5F, railPosY + 6/16F, posZ);
- }else{
- this.setPosition(posX, railPosY + 6/16F, railPosZ + 0.5F);
- }
- }else if(railTile != null && railTile.mId != 12 && railTile.mId != 13){
- this.setPosition(posX, railPosY + 6/16F, posZ);
- }
- this.wasLastSwitchActive = railTile.activated;
- }else if(id == RoWBlocks.railGag.blockID){
- gagTile = (TileEntityGag)worldObj.getTileEntity(new BlockPos(i, j, k)).getBlockMetadata();
- if(gagTile != null && !(gagTile.primary_x == 0 && gagTile.primary_y == 0 && gagTile.primary_z == 0)){
- this.railPosX = gagTile.primary_x;
- this.railPosY = gagTile.primary_y;
- this.railPosZ = gagTile.primary_z;
- railTile = (TileEntityWideRail)worldObj.getBlockTileEntity(gagTile.primary_x, gagTile.primary_y,
- gagTile.primary_z);
- if(railTile != null){
- metaData = worldObj.getTileEntity(new BlockPos(i, j, k)).getBlockMetadata();
- mId = railTile.mId;
- if(railTile != null && mId == 0){
- if(metaData == 0 || metaData == 2){
- this.setPosition(railPosX + 0.5F, railPosY + 6F/16F, posZ);
- }else{
- this.setPosition(posX, railPosY + 6F/16F, railPosZ + 0.5F);
- }
- }else if(railTile != null && railTile.mId != 12 && railTile.mId != 13){
- this.setPosition(posX, railPosY + 6/16F, posZ);
- }
- float ds = (float)Math
- .sqrt(Math.pow(gagTile.primary_x - i, 2) + Math.pow(gagTile.primary_z - k, 2));
- if((railTile.mId == 5 || railTile.mId == 6 || railTile.mId == 9 || railTile.mId == 10) && ds > 7){
- this.wasLastSwitchActive = !(gagTile.primary_x == i || gagTile.primary_z == k);
- }else if((railTile.mId == 11) && ds > 5){
- if(metaData == 0 || metaData == 2){
- this.wasLastSwitchActive = metaData == 0?Math.signum(posX - railPosX) < 0:Math.signum(posX
- - railPosX) > 0;
- }else{
- this.wasLastSwitchActive = metaData == 1?Math.signum(posZ - railPosZ) < 0:Math.signum(posZ
- - railPosZ) > 0;
- }
- }
- }
- }
- }else{
- j--;
- mId = 0;
- id = worldObj.getBlockId(i, j, k);
- if(id == RoWBlocks.rails.blockID){
- this.railPosX = i;
- this.railPosY = j;
- this.railPosZ = k;
- railTile = (TileEntityWideRail)worldObj.getTileEntity(new BlockPos(i, j, k));
- if(railTile != null){
- mId = railTile.mId;
- }
- }else if(id == RoWBlocks.railGag.blockID){
- gagTile = (TileEntityGag)worldObj.getTileEntity(new BlockPos(i, j, k));
- if(gagTile != null && !(gagTile.primary_x == 0 && gagTile.primary_y == 0 && gagTile.primary_z == 0)){
- this.railPosX = gagTile.primary_x;
- this.railPosY = gagTile.primary_y;
- this.railPosZ = gagTile.primary_z;
- railTile = (TileEntityWideRail)worldObj.getBlockTileEntity(gagTile.primary_x, gagTile.primary_y,
- gagTile.primary_z);
- if(railTile != null){
- mId = railTile.mId;
- }
- }
- }
- this.isOnRail = mId == 12 || mId == 13;
- }
- this.isOnSlope = mId == 12 || mId == 13;
- this.tangent = getTangent();
- if(this.isOnRail){
- float s = (float)(motionX * tangent[0] + motionZ * tangent[1] + motionY * tangent[2]);
- if(s > maxSpeed) s = maxSpeed;
- if(s < -maxSpeed) s = -maxSpeed;
- this.motionX = this.tangent[0] * s;
- this.motionZ = this.tangent[1] * s;
- this.motionY = this.tangent[2] * s;
- }else{
- float s = (float)(motionX * tangent[0] + motionZ * tangent[1]);
- if(s > maxSpeed) s = maxSpeed;
- if(s < -maxSpeed) s = -maxSpeed;
- this.motionX = this.tangent[0] * s * 0.95;
- this.motionZ = this.tangent[1] * s * 0.95;
- }
- this.setVelocity(motionX, motionY, motionZ);
- float trackAngle = (float)Math.toDegrees(-Math.atan2(tangent[0], tangent[1]));
- float y1 = (float)MathHelper.wrapAngleTo180_double(trackAngle - rotationYaw);
- float y2 = (float)Math.toDegrees(Math.atan(this.tangent[2]));
- if(y1 > 90 || y1 < -90){
- this.setRotation((float)MathHelper.wrapAngleTo180_double(180 + trackAngle), -y2);
- }else{
- this.setRotation(trackAngle, y2);
- }
- this.setPosition(posX, posY, posZ);
- }
- public void applyForwardMotion(float v){
- this.addVelocity(this.rotationCos * v, 0F, this.rotationSin * v);
- }
- @Override
- @SideOnly(Side.CLIENT)
- public void setPositionAndRotation2(double x, double y, double z, float yaw, float pitch, int posRotationIncrements, boolean p_180426_10_){
- this.setPosition(x, y, z);
- this.setRotation(yaw, pitch);
- }
- @Override
- @SideOnly(Side.CLIENT)
- public void setVelocity(double par1, double par3, double par5){
- /*this.motionX = par1;
- this.motionY = par3;
- this.motionZ = par5;*/
- }
- /*@Override
- @SideOnly(Side.CLIENT)
- public void setVelocity(double par1, double par3, double par5)
- {
- this.velX = this.motionX = par1;
- this.velY = this.motionY = par3;
- this.velZ = this.motionZ = par5;
- }
- @Override
- @SideOnly(Side.CLIENT)
- public void setPositionAndRotation2(double par1, double par3, double par5, float par7, float par8, int par9)
- {
- this.cartX = par1;
- this.cartY = par3;
- this.cartZ = par5;
- this.cartYaw = (double)par7;
- this.motionX = this.velX;
- this.motionY = this.velY;
- this.motionZ = this.velZ;
- }*/
- @Override
- public void onCollideWithPlayer(EntityPlayer player) {
- this.applyEntityCollision((Entity)player);
- }
- @Override
- public void applyEntityCollision(Entity par1Entity){
- if(!worldObj.isRemote){
- if(par1Entity.riddenByEntity != this && par1Entity.ridingEntity != this){
- if(par1Entity instanceof EntityLivingBase && !(par1Entity instanceof EntityPlayer)
- && !(par1Entity instanceof EntityTrain)
- && this.canBeRidden()
- && this.motionX * this.motionX + this.motionZ * this.motionZ > 0.01D
- && this.riddenByEntity == null && par1Entity.ridingEntity == null){
- par1Entity.mountEntity(this);
- }else{
- double d0 = par1Entity.posX - this.posX;
- double d1 = par1Entity.posZ - this.posZ;
- double d2 = MathHelper.abs_max(d0, d1);
- if(d2 >= 0.009999999776482582D){
- d2 = (double)MathHelper.sqrt_double(d2);
- d0 /= d2;
- d1 /= d2;
- double d3 = 1.0D / d2;
- if(d3 > 1.0D){
- d3 = 1.0D;
- }
- d0 *= d3;
- d1 *= d3;
- d0 *= 0.05D;
- d1 *= 0.05D;
- par1Entity.addVelocity(d0, 0.0D, d1);
- d0 *= (double)(1.0F - this.entityCollisionReduction);
- d1 *= (double)(1.0F - this.entityCollisionReduction);
- this.addVelocity(-d0, 0.0D, -d1);
- float sqrtMotion = (float)(Math.sqrt(Math.pow(motionX, 2) + Math.pow(motionZ, 2)));
- if(sqrtMotion > 0.05){
- par1Entity.attackEntityFrom(DamageSource.generic, sqrtMotion * 50);
- }
- }
- }
- }
- }
- }
- /*@Override
- public void applyEntityCollision(Entity par1Entity){
- if(par1Entity.riddenByEntity != this && par1Entity.ridingEntity != this){
- double d0 = par1Entity.posX - this.posX;
- double d1 = par1Entity.posZ - this.posZ;
- double d2 = MathHelper.abs_max(d0, d1);
- if(d2 >= 0.009999999776482582D){
- d2 = (double)MathHelper.sqrt_double(d2);
- d0 /= d2;
- d1 /= d2;
- double d3 = 1.0D / d2;
- if(d3 > 1.0D){
- d3 = 1.0D;
- }
- d0 *= d3;
- d1 *= d3;
- d0 *= 0.05D;
- d1 *= 0.05D;
- par1Entity.addVelocity(d0, 0.0D, d1);
- d0 *= (double)(1.0F - this.entityCollisionReduction);
- d1 *= (double)(1.0F - this.entityCollisionReduction);
- this.addVelocity(-d0, 0.0D, -d1);
- float sqrtMotion = (float)(Math.sqrt(Math.pow(motionX, 2) + Math.pow(motionZ, 2)));
- if(sqrtMotion > 0.05){
- par1Entity.attackEntityFrom(DamageSource.generic, sqrtMotion * 50);
- }
- }
- }
- }*/
- @Override
- public void updateRiderPosition(){
- if(this.riddenByEntity != null){
- this.riderPos.onUpdate();
- this.riddenByEntity.setPosition(this.riderPos.getX(), this.riderPos.getY(), this.riderPos.getZ());
- }
- }
- public boolean canBeRidden(){
- return false;
- }
- @Override
- protected void playStepSound(BlockPos pos, Block blockIn){
- }
- @Override
- public AxisAlignedBB getCollisionBox(Entity par1Entity){
- if(par1Entity != riddenByEntity && par1Entity.ridingEntity != this){
- return par1Entity.getEntityBoundingBox();
- }
- return null;
- }
- @Override
- public AxisAlignedBB getEntityBoundingBox(){
- float w = this.boundSize / 2F;
- return AxisAlignedBB.fromBounds(this.posX - w, this.posY, this.posZ - w,
- this.posX + w, this.posY + this.height, this.posZ + w);
- }
- @Override
- public boolean shouldRiderSit(){
- return false;
- }
- @Override
- public boolean canRiderInteract(){
- return true;
- }
- @Override
- public double getMountedYOffset(){
- return 1.0D;
- }
- @Override
- public boolean canBeCollidedWith(){
- return true;
- }
- @Override
- public boolean canBePushed(){
- return true;
- }
- @Override
- public Entity[] getParts(){
- return null;
- }
- @Override
- protected void readEntityFromNBT(NBTTagCompound nbttagcompound){
- if(nbttagcompound.hasKey("frontCoupledUUIDMost") && nbttagcompound.hasKey("frontCoupledUUIDLeast")){
- this.frontCartUUID = new UUID(nbttagcompound.getLong("frontCoupledUUIDMost"),
- nbttagcompound.getLong("frontCoupledUUIDLeast"));
- }
- if(nbttagcompound.hasKey("rearCoupledUUIDMost") && nbttagcompound.hasKey("rearCoupledUUIDLeast")){
- this.rearCartUUID = new UUID(nbttagcompound.getLong("rearCoupledUUIDMost"),
- nbttagcompound.getLong("rearCoupledUUIDLeast"));
- }
- this.needsCouplingsCheck = true;
- }
- @Override
- protected void writeEntityToNBT(NBTTagCompound nbttagcompound){
- if(this.frontCoupledCart != null){
- nbttagcompound.setLong("frontCoupledUUIDMost", this.frontCoupledCart.getUniqueID()
- .getMostSignificantBits());
- nbttagcompound.setLong("frontCoupledUUIDLeast", this.frontCoupledCart.getUniqueID()
- .getLeastSignificantBits());
- }
- if(this.rearCoupledCart != null){
- nbttagcompound.setLong("rearCoupledUUIDMost", this.rearCoupledCart.getUniqueID()
- .getMostSignificantBits());
- nbttagcompound.setLong("rearCoupledUUIDLeast", this.rearCoupledCart.getUniqueID()
- .getLeastSignificantBits());
- }
- }
- public enum CouplerType {
- NONE, CHER, RUSS_BnCH
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment