package common.entity.item;

import common.block.tech.BlockRailBase;
import common.block.tech.BlockRailPowered;
import common.entity.DamageSource;
import common.entity.Entity;
import common.entity.EntityType;
import common.entity.types.EntityLiving;
import common.init.Blocks;
import common.init.Items;
import common.item.Item;
import common.item.ItemStack;
import common.tags.TagObject;
import common.tileentity.IWorldNameable;
import common.util.BlockPos;
import common.util.BoundingBox;
import common.util.ExtMath;
import common.util.Vec3;
import common.vars.Vars;
import common.world.State;
import common.world.World;

public abstract class EntityCart extends Entity implements IWorldNameable
{
    private boolean isInReverse;
    private String entityName;

    /** Minecart rotational logic matrix */
    private static final int[][][] matrix = new int[][][] {{{0, 0, -1}, {0, 0, 1}}, {{ -1, 0, 0}, {1, 0, 0}}, {{ -1, -1, 0}, {1, 0, 0}}, {{ -1, 0, 0}, {1, -1, 0}}, {{0, 0, -1}, {0, -1, 1}}, {{0, -1, -1}, {0, 0, 1}}, {{0, 0, 1}, {1, 0, 0}}, {{0, 0, 1}, { -1, 0, 0}}, {{0, 0, -1}, { -1, 0, 0}}, {{0, 0, -1}, {1, 0, 0}}};

    /** appears to be the progress of the turn */
    private int turnProgress;
    private double minecartX;
    private double minecartY;
    private double minecartZ;
    private double minecartYaw;
    private double minecartPitch;
    private double velocityX;
    private double velocityY;
    private double velocityZ;

    public EntityCart(World worldIn)
    {
        super(worldIn);
        this.preventSpawning = true;
        this.setSize(0.98F, 0.7F);
    }

    public static EntityCart getMinecart(World worldIn, double x, double y, double z, EntityCart.EnumMinecartType type)
    {
        switch (type)
        {
            case CHEST:
                return new EntityChestCart(worldIn, x, y, z);

//            case FURNACE:
//                return new EntityMinecartFurnace(worldIn, x, y, z);

            case TNT:
                return new EntityTntCart(worldIn, x, y, z);

//            case SPAWNER:
//                return new EntityMinecartMobSpawner(worldIn, x, y, z);

            case HOPPER:
                return new EntityHopperCart(worldIn, x, y, z);

//            case COMMAND_BLOCK:
//                return new EntityMinecartCommandBlock(worldIn, x, y, z);

            default:
                return new EntityMinecart(worldIn, x, y, z);
        }
    }

    /**
     * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to
     * prevent them from trampling crops
     */
    protected boolean canTriggerWalking()
    {
        return false;
    }

    protected void entityInit()
    {
        this.dataWatcher.addObject(17, 0);
        this.dataWatcher.addObject(18, 1);
        this.dataWatcher.addObject(19, 0);
    }

    /**
     * Returns a boundingBox used to collide the entity with other entities and blocks. This enables the entity to be
     * pushable on contact, like boats or minecarts.
     */
    public BoundingBox getCollisionBox(Entity entityIn)
    {
        return entityIn.canBePushed() ? entityIn.getEntityBoundingBox() : null;
    }

    /**
     * Returns the collision bounding box for this entity
     */
    public BoundingBox getCollisionBoundingBox()
    {
        return null;
    }

    /**
     * Returns true if this entity should push and be pushed by other entities when colliding.
     */
    public boolean canBePushed()
    {
        return true;
    }

    public EntityCart(World worldIn, double x, double y, double z)
    {
        this(worldIn);
        this.setPosition(x, y, z);
        this.motionX = 0.0D;
        this.motionY = 0.0D;
        this.motionZ = 0.0D;
        this.prevX = x;
        this.prevY = y;
        this.prevZ = z;
    }

    /**
     * Returns the Y offset from the entity's position for any entity riding this one.
     */
    public double getMountedYOffset()
    {
        return 0.0D;
    }

    /**
     * Called when the entity is attacked.
     */
    public boolean attackEntityFrom(DamageSource source, int amount)
    {
        if (!this.worldObj.client && !this.dead)
        {
//            if (this.isEntityInvulnerable(source))
//            {
//                return false;
//            }
//            else
//            {
            this.setRollingDirection(-this.getRollingDirection());
            this.setRollingAmplitude(10);
            this.setBeenAttacked();
            this.setDamage(this.getDamage() + (int)amount * 10);
//                boolean flag = source.getEntity().isPlayer() && ((EntityNPC)source.getEntity()).creative;

            if (/* flag || */ this.getDamage() > 40)
            {
                if (this.passenger != null)
                {
                    this.passenger.mountEntity((Entity)null);
                }

//                    if (flag && !this.hasCustomName())
//                    {
//                        this.setDead();
//                    }
//                    else
//                    {
                this.killMinecart(source);
//                    }
            }

            return true;
//            }
        }
        else
        {
            return true;
        }
    }

    public void killMinecart(DamageSource source)
    {
        this.setDead();

        if (Vars.objectDrop)
        {
            ItemStack itemstack = new ItemStack(Items.minecart, 1);

            if (this.entityName != null)
            {
                itemstack.setStackDisplayName(this.entityName);
            }

            this.entityDropItem(itemstack, 0.0F);
        }
    }

    /**
     * Setups the entity to do the hurt animation. Only used by packets in multiplayer.
     */
    public void performHurtAnimation()
    {
        this.setRollingDirection(-this.getRollingDirection());
        this.setRollingAmplitude(10);
        this.setDamage(this.getDamage() + this.getDamage() * 10);
    }

    /**
     * Returns true if other Entities should be prevented from moving through this Entity.
     */
    public boolean canBeCollidedWith()
    {
        return !this.dead;
    }

    /**
     * Will get destroyed next tick.
     */
    public void setDead()
    {
        super.setDead();
    }

    /**
     * Called to update the entity's position/logic.
     */
    public void onUpdate()
    {
        if (this.getRollingAmplitude() > 0)
        {
            this.setRollingAmplitude(this.getRollingAmplitude() - 1);
        }

        if (this.getDamage() > 0)
        {
            this.setDamage(this.getDamage() - 1);
        }

        if (this.posY < (double)(-World.MAX_SIZE_Y) - 64.0)
        {
            this.onVoidUpdate();
        }

        if (!this.worldObj.client)
        {
        	this.checkPortal();
        }

        if (this.worldObj.client)
        {
            if (this.turnProgress > 0)
            {
                double d4 = this.posX + (this.minecartX - this.posX) / (double)this.turnProgress;
                double d5 = this.posY + (this.minecartY - this.posY) / (double)this.turnProgress;
                double d6 = this.posZ + (this.minecartZ - this.posZ) / (double)this.turnProgress;
                double d1 = ExtMath.wrapd(this.minecartYaw - (double)this.rotYaw);
                this.rotYaw = (float)((double)this.rotYaw + d1 / (double)this.turnProgress);
                this.rotPitch = (float)((double)this.rotPitch + (this.minecartPitch - (double)this.rotPitch) / (double)this.turnProgress);
                --this.turnProgress;
                this.setPosition(d4, d5, d6);
                this.setRotation(this.rotYaw, this.rotPitch);
            }
            else
            {
                this.setPosition(this.posX, this.posY, this.posZ);
                this.setRotation(this.rotYaw, this.rotPitch);
            }
        }
        else
        {
            this.prevX = this.posX;
            this.prevY = this.posY;
            this.prevZ = this.posZ;
            this.motionY -= 0.03999999910593033D;
            int k = ExtMath.floord(this.posX);
            int l = ExtMath.floord(this.posY);
            int i1 = ExtMath.floord(this.posZ);

            if (BlockRailBase.isRailBlock(this.worldObj, new BlockPos(k, l - 1, i1)))
            {
                --l;
            }

            BlockPos blockpos = new BlockPos(k, l, i1);
            State iblockstate = this.worldObj.getState(blockpos);

            if (BlockRailBase.isRailBlock(iblockstate))
            {
                this.func_180460_a(blockpos, iblockstate);

                if (iblockstate.getBlock() == Blocks.activator_rail)
                {
                    this.onActivatorRailPass(k, l, i1, ((Boolean)iblockstate.getValue(BlockRailPowered.POWERED)).booleanValue());
                }
            }
            else
            {
                this.moveDerailedMinecart();
            }

            this.doBlockCollisions();
            this.rotPitch = 0.0F;
            double d0 = this.prevX - this.posX;
            double d2 = this.prevZ - this.posZ;

            if (d0 * d0 + d2 * d2 > 0.001D)
            {
                this.rotYaw = (float)(ExtMath.atan2(d2, d0) * 180.0D / Math.PI);

                if (this.isInReverse)
                {
                    this.rotYaw += 180.0F;
                }
            }

            double d3 = (double)ExtMath.wrapf(this.rotYaw - this.prevYaw);

            if (d3 < -170.0D || d3 >= 170.0D)
            {
                this.rotYaw += 180.0F;
                this.isInReverse = !this.isInReverse;
            }

            this.setRotation(this.rotYaw, this.rotPitch);

            for (Entity entity : this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().expand(0.20000000298023224D, 0.0D, 0.20000000298023224D)))
            {
                if (entity != this.passenger && entity.canBePushed() && entity instanceof EntityCart)
                {
                    entity.applyEntityCollision(this);
                }
            }

            if (this.passenger != null && this.passenger.dead)
            {
                if (this.passenger.vehicle == this)
                {
                    this.passenger.vehicle = null;
                }

                this.passenger = null;
            }

            this.handleWaterMovement();
        }
    }

    /**
     * Get's the maximum speed for a minecart
     */
    protected double getMaximumSpeed()
    {
        return 0.4D;
    }

    /**
     * Called every tick the minecart is on an activator rail. Args: x, y, z, is the rail receiving power
     */
    public void onActivatorRailPass(int x, int y, int z, boolean receivingPower)
    {
    }

    /**
     * Moves a minecart that is not attached to a rail
     */
    protected void moveDerailedMinecart()
    {
        double d0 = this.getMaximumSpeed();
        this.motionX = ExtMath.clampd(this.motionX, -d0, d0);
        this.motionZ = ExtMath.clampd(this.motionZ, -d0, d0);

        if (this.onGround)
        {
            this.motionX *= 0.5D;
            this.motionY *= 0.5D;
            this.motionZ *= 0.5D;
        }

        this.moveEntity(this.motionX, this.motionY, this.motionZ);

        if (!this.onGround)
        {
            this.motionX *= 0.949999988079071D;
            this.motionY *= 0.949999988079071D;
            this.motionZ *= 0.949999988079071D;
        }
    }

    
    protected void func_180460_a(BlockPos p_180460_1_, State p_180460_2_)
    {
        this.fallDistance = 0.0F;
        Vec3 vec3 = this.func_70489_a(this.posX, this.posY, this.posZ);
        this.posY = (double)p_180460_1_.getY();
        boolean flag = false;
        boolean flag1 = false;
        BlockRailBase blockrailbase = (BlockRailBase)p_180460_2_.getBlock();

        if (blockrailbase == Blocks.golden_rail)
        {
            flag = ((Boolean)p_180460_2_.getValue(BlockRailPowered.POWERED)).booleanValue();
            flag1 = !flag;
        }

        double d0 = 0.0078125D;
        BlockRailBase.EnumRailDirection blockrailbase$enumraildirection = (BlockRailBase.EnumRailDirection)p_180460_2_.getValue(blockrailbase.getShapeProperty());

        switch (blockrailbase$enumraildirection)
        {
            case ASCENDING_EAST:
                this.motionX -= 0.0078125D;
                ++this.posY;
                break;

            case ASCENDING_WEST:
                this.motionX += 0.0078125D;
                ++this.posY;
                break;

            case ASCENDING_NORTH:
                this.motionZ += 0.0078125D;
                ++this.posY;
                break;

            case ASCENDING_SOUTH:
                this.motionZ -= 0.0078125D;
                ++this.posY;
        }

        int[][] aint = matrix[blockrailbase$enumraildirection.getMetadata()];
        double d1 = (double)(aint[1][0] - aint[0][0]);
        double d2 = (double)(aint[1][2] - aint[0][2]);
        double d3 = Math.sqrt(d1 * d1 + d2 * d2);
        double d4 = this.motionX * d1 + this.motionZ * d2;

        if (d4 < 0.0D)
        {
            d1 = -d1;
            d2 = -d2;
        }

        double d5 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);

        if (d5 > 2.0D)
        {
            d5 = 2.0D;
        }

        this.motionX = d5 * d1 / d3;
        this.motionZ = d5 * d2 / d3;

        if (this.passenger instanceof EntityLiving)
        {
            double d6 = (double)((EntityLiving)this.passenger).moveForward;

            if (d6 > 0.0D)
            {
                double d7 = -Math.sin((double)(this.passenger.rotYaw * (float)Math.PI / 180.0F));
                double d8 = Math.cos((double)(this.passenger.rotYaw * (float)Math.PI / 180.0F));
                double d9 = this.motionX * this.motionX + this.motionZ * this.motionZ;

                if (d9 < 0.01D)
                {
                    this.motionX += d7 * 0.1D;
                    this.motionZ += d8 * 0.1D;
                    flag1 = false;
                }
            }
        }

        if (flag1)
        {
            double d17 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);

            if (d17 < 0.03D)
            {
                this.motionX *= 0.0D;
                this.motionY *= 0.0D;
                this.motionZ *= 0.0D;
            }
            else
            {
                this.motionX *= 0.5D;
                this.motionY *= 0.0D;
                this.motionZ *= 0.5D;
            }
        }

        double d18 = 0.0D;
        double d19 = (double)p_180460_1_.getX() + 0.5D + (double)aint[0][0] * 0.5D;
        double d20 = (double)p_180460_1_.getZ() + 0.5D + (double)aint[0][2] * 0.5D;
        double d21 = (double)p_180460_1_.getX() + 0.5D + (double)aint[1][0] * 0.5D;
        double d10 = (double)p_180460_1_.getZ() + 0.5D + (double)aint[1][2] * 0.5D;
        d1 = d21 - d19;
        d2 = d10 - d20;

        if (d1 == 0.0D)
        {
            this.posX = (double)p_180460_1_.getX() + 0.5D;
            d18 = this.posZ - (double)p_180460_1_.getZ();
        }
        else if (d2 == 0.0D)
        {
            this.posZ = (double)p_180460_1_.getZ() + 0.5D;
            d18 = this.posX - (double)p_180460_1_.getX();
        }
        else
        {
            double d11 = this.posX - d19;
            double d12 = this.posZ - d20;
            d18 = (d11 * d1 + d12 * d2) * 2.0D;
        }

        this.posX = d19 + d1 * d18;
        this.posZ = d20 + d2 * d18;
        this.setPosition(this.posX, this.posY, this.posZ);
        double d22 = this.motionX;
        double d23 = this.motionZ;

        if (this.passenger != null)
        {
            d22 *= 0.75D;
            d23 *= 0.75D;
        }

        double d13 = this.getMaximumSpeed();
        d22 = ExtMath.clampd(d22, -d13, d13);
        d23 = ExtMath.clampd(d23, -d13, d13);
        this.moveEntity(d22, 0.0D, d23);

        if (aint[0][1] != 0 && ExtMath.floord(this.posX) - p_180460_1_.getX() == aint[0][0] && ExtMath.floord(this.posZ) - p_180460_1_.getZ() == aint[0][2])
        {
            this.setPosition(this.posX, this.posY + (double)aint[0][1], this.posZ);
        }
        else if (aint[1][1] != 0 && ExtMath.floord(this.posX) - p_180460_1_.getX() == aint[1][0] && ExtMath.floord(this.posZ) - p_180460_1_.getZ() == aint[1][2])
        {
            this.setPosition(this.posX, this.posY + (double)aint[1][1], this.posZ);
        }

        this.applyDrag();
        Vec3 vec31 = this.func_70489_a(this.posX, this.posY, this.posZ);

        if (vec31 != null && vec3 != null)
        {
            double d14 = (vec3.yCoord - vec31.yCoord) * 0.05D;
            d5 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);

            if (d5 > 0.0D)
            {
                this.motionX = this.motionX / d5 * (d5 + d14);
                this.motionZ = this.motionZ / d5 * (d5 + d14);
            }

            this.setPosition(this.posX, vec31.yCoord, this.posZ);
        }

        int j = ExtMath.floord(this.posX);
        int i = ExtMath.floord(this.posZ);

        if (j != p_180460_1_.getX() || i != p_180460_1_.getZ())
        {
            d5 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
            this.motionX = d5 * (double)(j - p_180460_1_.getX());
            this.motionZ = d5 * (double)(i - p_180460_1_.getZ());
        }

        if (flag)
        {
            double d15 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);

            if (d15 > 0.01D)
            {
                double d16 = 0.06D;
                this.motionX += this.motionX / d15 * d16;
                this.motionZ += this.motionZ / d15 * d16;
            }
            else if (blockrailbase$enumraildirection == BlockRailBase.EnumRailDirection.EAST_WEST)
            {
                if (this.worldObj.getState(p_180460_1_.west()).getBlock().isNormalCube())
                {
                    this.motionX = 0.02D;
                }
                else if (this.worldObj.getState(p_180460_1_.east()).getBlock().isNormalCube())
                {
                    this.motionX = -0.02D;
                }
            }
            else if (blockrailbase$enumraildirection == BlockRailBase.EnumRailDirection.NORTH_SOUTH)
            {
                if (this.worldObj.getState(p_180460_1_.north()).getBlock().isNormalCube())
                {
                    this.motionZ = 0.02D;
                }
                else if (this.worldObj.getState(p_180460_1_.south()).getBlock().isNormalCube())
                {
                    this.motionZ = -0.02D;
                }
            }
        }
    }

    protected void applyDrag()
    {
        if (this.passenger != null)
        {
            this.motionX *= 0.996999979019165D;
            this.motionY *= 0.0D;
            this.motionZ *= 0.996999979019165D;
        }
        else
        {
            this.motionX *= 0.9599999785423279D;
            this.motionY *= 0.0D;
            this.motionZ *= 0.9599999785423279D;
        }
    }

    /**
     * Sets the x,y,z of the entity from the given parameters. Also seems to set up a bounding box.
     */
    public void setPosition(double x, double y, double z)
    {
        this.posX = x;
        this.posY = y;
        this.posZ = z;
        float f = this.width / 2.0F;
        float f1 = this.height;
        this.setEntityBoundingBox(new BoundingBox(x - (double)f, y, z - (double)f, x + (double)f, y + (double)f1, z + (double)f));
    }

    public Vec3 func_70495_a(double p_70495_1_, double p_70495_3_, double p_70495_5_, double p_70495_7_)
    {
        int i = ExtMath.floord(p_70495_1_);
        int j = ExtMath.floord(p_70495_3_);
        int k = ExtMath.floord(p_70495_5_);

        if (BlockRailBase.isRailBlock(this.worldObj, new BlockPos(i, j - 1, k)))
        {
            --j;
        }

        State iblockstate = this.worldObj.getState(new BlockPos(i, j, k));

        if (BlockRailBase.isRailBlock(iblockstate))
        {
            BlockRailBase.EnumRailDirection blockrailbase$enumraildirection = (BlockRailBase.EnumRailDirection)iblockstate.getValue(((BlockRailBase)iblockstate.getBlock()).getShapeProperty());
            p_70495_3_ = (double)j;

            if (blockrailbase$enumraildirection.isAscending())
            {
                p_70495_3_ = (double)(j + 1);
            }

            int[][] aint = matrix[blockrailbase$enumraildirection.getMetadata()];
            double d0 = (double)(aint[1][0] - aint[0][0]);
            double d1 = (double)(aint[1][2] - aint[0][2]);
            double d2 = Math.sqrt(d0 * d0 + d1 * d1);
            d0 = d0 / d2;
            d1 = d1 / d2;
            p_70495_1_ = p_70495_1_ + d0 * p_70495_7_;
            p_70495_5_ = p_70495_5_ + d1 * p_70495_7_;

            if (aint[0][1] != 0 && ExtMath.floord(p_70495_1_) - i == aint[0][0] && ExtMath.floord(p_70495_5_) - k == aint[0][2])
            {
                p_70495_3_ += (double)aint[0][1];
            }
            else if (aint[1][1] != 0 && ExtMath.floord(p_70495_1_) - i == aint[1][0] && ExtMath.floord(p_70495_5_) - k == aint[1][2])
            {
                p_70495_3_ += (double)aint[1][1];
            }

            return this.func_70489_a(p_70495_1_, p_70495_3_, p_70495_5_);
        }
        else
        {
            return null;
        }
    }

    public Vec3 func_70489_a(double p_70489_1_, double p_70489_3_, double p_70489_5_)
    {
        int i = ExtMath.floord(p_70489_1_);
        int j = ExtMath.floord(p_70489_3_);
        int k = ExtMath.floord(p_70489_5_);

        if (BlockRailBase.isRailBlock(this.worldObj, new BlockPos(i, j - 1, k)))
        {
            --j;
        }

        State iblockstate = this.worldObj.getState(new BlockPos(i, j, k));

        if (BlockRailBase.isRailBlock(iblockstate))
        {
            BlockRailBase.EnumRailDirection blockrailbase$enumraildirection = (BlockRailBase.EnumRailDirection)iblockstate.getValue(((BlockRailBase)iblockstate.getBlock()).getShapeProperty());
            int[][] aint = matrix[blockrailbase$enumraildirection.getMetadata()];
            double d0 = 0.0D;
            double d1 = (double)i + 0.5D + (double)aint[0][0] * 0.5D;
            double d2 = (double)j + 0.0625D + (double)aint[0][1] * 0.5D;
            double d3 = (double)k + 0.5D + (double)aint[0][2] * 0.5D;
            double d4 = (double)i + 0.5D + (double)aint[1][0] * 0.5D;
            double d5 = (double)j + 0.0625D + (double)aint[1][1] * 0.5D;
            double d6 = (double)k + 0.5D + (double)aint[1][2] * 0.5D;
            double d7 = d4 - d1;
            double d8 = (d5 - d2) * 2.0D;
            double d9 = d6 - d3;

            if (d7 == 0.0D)
            {
                p_70489_1_ = (double)i + 0.5D;
                d0 = p_70489_5_ - (double)k;
            }
            else if (d9 == 0.0D)
            {
                p_70489_5_ = (double)k + 0.5D;
                d0 = p_70489_1_ - (double)i;
            }
            else
            {
                double d10 = p_70489_1_ - d1;
                double d11 = p_70489_5_ - d3;
                d0 = (d10 * d7 + d11 * d9) * 2.0D;
            }

            p_70489_1_ = d1 + d7 * d0;
            p_70489_3_ = d2 + d8 * d0;
            p_70489_5_ = d3 + d9 * d0;

            if (d8 < 0.0D)
            {
                ++p_70489_3_;
            }

            if (d8 > 0.0D)
            {
                p_70489_3_ += 0.5D;
            }

            return new Vec3(p_70489_1_, p_70489_3_, p_70489_5_);
        }
        else
        {
            return null;
        }
    }

    /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
    protected void readEntity(TagObject tagCompund)
    {
        if (tagCompund.hasString("CustomName") && tagCompund.getString("CustomName").length() > 0)
        {
            this.entityName = tagCompund.getString("CustomName");
        }
    }

    /**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
    protected void writeEntity(TagObject tagCompound)
    {
        if (this.entityName != null && this.entityName.length() > 0)
        {
            tagCompound.setString("CustomName", this.entityName);
        }
    }

    /**
     * Applies a velocity to each of the entities pushing them away from each other. Args: entity
     */
    public void applyEntityCollision(Entity entityIn)
    {
        if (!this.worldObj.client)
        {
            if (!entityIn.noClip && !this.noClip)
            {
                if (entityIn != this.passenger)
                {
                    if (entityIn instanceof EntityLiving && /* !(entityIn.isPlayer()) && */ this.getMinecartType() == EntityCart.EnumMinecartType.RIDEABLE && this.motionX * this.motionX + this.motionZ * this.motionZ > 0.01D && this.passenger == null && entityIn.vehicle == null)
                    {
                        entityIn.mountEntity(this);
                    }

                    double d0 = entityIn.posX - this.posX;
                    double d1 = entityIn.posZ - this.posZ;
                    double d2 = d0 * d0 + d1 * d1;

                    if (d2 >= 9.999999747378752E-5D)
                    {
                        d2 = (double)ExtMath.sqrtd(d2);
                        d0 = d0 / d2;
                        d1 = d1 / d2;
                        double d3 = 1.0D / d2;

                        if (d3 > 1.0D)
                        {
                            d3 = 1.0D;
                        }

                        d0 = d0 * d3;
                        d1 = d1 * d3;
                        d0 = d0 * 0.10000000149011612D;
                        d1 = d1 * 0.10000000149011612D;
                        d0 = d0 * (double)(1.0F - this.collisionReduction);
                        d1 = d1 * (double)(1.0F - this.collisionReduction);
                        d0 = d0 * 0.5D;
                        d1 = d1 * 0.5D;

                        if (entityIn instanceof EntityCart)
                        {
                            double d4 = entityIn.posX - this.posX;
                            double d5 = entityIn.posZ - this.posZ;
                            Vec3 vec3 = (new Vec3(d4, 0.0D, d5)).normalize();
                            Vec3 vec31 = (new Vec3((double)ExtMath.cos(this.rotYaw * (float)Math.PI / 180.0F), 0.0D, (double)ExtMath.sin(this.rotYaw * (float)Math.PI / 180.0F))).normalize();
                            double d6 = Math.abs(vec3.dotProduct(vec31));

                            if (d6 < 0.800000011920929D)
                            {
                                return;
                            }

                            double d7 = entityIn.motionX + this.motionX;
                            double d8 = entityIn.motionZ + this.motionZ;

//                            if (((EntityCart)entityIn).getMinecartType() == EntityCart.EnumMinecartType.FURNACE && this.getMinecartType() != EntityCart.EnumMinecartType.FURNACE)
//                            {
//                                this.motionX *= 0.20000000298023224D;
//                                this.motionZ *= 0.20000000298023224D;
//                                this.addVelocity(entityIn.motionX - d0, 0.0D, entityIn.motionZ - d1);
//                                entityIn.motionX *= 0.949999988079071D;
//                                entityIn.motionZ *= 0.949999988079071D;
//                            }
//                            else if (((EntityCart)entityIn).getMinecartType() != EntityCart.EnumMinecartType.FURNACE && this.getMinecartType() == EntityCart.EnumMinecartType.FURNACE)
//                            {
//                                entityIn.motionX *= 0.20000000298023224D;
//                                entityIn.motionZ *= 0.20000000298023224D;
//                                entityIn.addVelocity(this.motionX + d0, 0.0D, this.motionZ + d1);
//                                this.motionX *= 0.949999988079071D;
//                                this.motionZ *= 0.949999988079071D;
//                            }
//                            else
//                            {
                            d7 = d7 / 2.0D;
                            d8 = d8 / 2.0D;
                            this.motionX *= 0.20000000298023224D;
                            this.motionZ *= 0.20000000298023224D;
                            this.addVelocity(d7 - d0, 0.0D, d8 - d1);
                            entityIn.motionX *= 0.20000000298023224D;
                            entityIn.motionZ *= 0.20000000298023224D;
                            entityIn.addVelocity(d7 + d0, 0.0D, d8 + d1);
//                            }
                        }
                        else
                        {
                            this.addVelocity(-d0, 0.0D, -d1);
                            entityIn.addVelocity(d0 / 4.0D, 0.0D, d1 / 4.0D);
                        }
                    }
                }
            }
        }
    }

    public void setClientPosition(double x, double y, double z, float yaw, float pitch, boolean teleport)
    {
        this.minecartX = x;
        this.minecartY = y;
        this.minecartZ = z;
        this.minecartYaw = (double)yaw;
        this.minecartPitch = (double)pitch;
        this.turnProgress = 5;
        this.motionX = this.velocityX;
        this.motionY = this.velocityY;
        this.motionZ = this.velocityZ;
    }

    /**
     * Sets the velocity to the args. Args: x, y, z
     */
    public void setVelocity(double x, double y, double z)
    {
        this.velocityX = this.motionX = x;
        this.velocityY = this.motionY = y;
        this.velocityZ = this.motionZ = z;
    }

    /**
     * Sets the current amount of damage the minecart has taken. Decreases over time. The cart breaks when this is over
     * 40.
     */
    public void setDamage(int p_70492_1_)
    {
        this.dataWatcher.updateObject(19, Integer.valueOf(p_70492_1_));
    }

    /**
     * Gets the current amount of damage the minecart has taken. Decreases over time. The cart breaks when this is over
     * 40.
     */
    public int getDamage()
    {
        return this.dataWatcher.getWatchableObjectInt(19);
    }

    /**
     * Sets the rolling amplitude the cart rolls while being attacked.
     */
    public void setRollingAmplitude(int p_70497_1_)
    {
        this.dataWatcher.updateObject(17, Integer.valueOf(p_70497_1_));
    }

    /**
     * Gets the rolling amplitude the cart rolls while being attacked.
     */
    public int getRollingAmplitude()
    {
        return this.dataWatcher.getWatchableObjectInt(17);
    }

    /**
     * Sets the rolling direction the cart rolls while being attacked. Can be 1 or -1.
     */
    public void setRollingDirection(int p_70494_1_)
    {
        this.dataWatcher.updateObject(18, Integer.valueOf(p_70494_1_));
    }

    /**
     * Gets the rolling direction the cart rolls while being attacked. Can be 1 or -1.
     */
    public int getRollingDirection()
    {
        return this.dataWatcher.getWatchableObjectInt(18);
    }

    public abstract EntityCart.EnumMinecartType getMinecartType();

    public State getDisplayTile()
    {
        return Blocks.air.getState();
    }

    public int getDisplayTileOffset()
    {
        return 6;
    }

    /**
     * Sets the custom name tag for this entity
     */
    public void setCustomNameTag(String name)
    {
        this.entityName = name;
    }

    /**
     * Get the name of this object. For players this returns their username
     */
    public String getTypeName()
    {
        return this.entityName != null ? this.entityName : super.getTypeName();
    }

    /**
     * Returns true if this thing is named
     */
    public boolean hasCustomName()
    {
        return this.entityName != null;
    }

    public String getCustomNameTag()
    {
        return this.entityName;
    }

    /**
     * Get the formatted ChatComponent that will be used for the sender's username in chat
     */
    public String getName()
    {
        if (this.hasCustomName())
        {
        	return this.entityName;
//            this.formatComponent(chatcomponenttext);
//            return chatcomponenttext;
        }
        else
        {
        	return super.getName();
        }
    }
    
    public int getTrackingRange() {
    	return 80;
    }
    
    public int getUpdateFrequency() {
    	return 3;
    }
    
    public boolean isSendingVeloUpdates() {
    	return true;
    }
	
//	public int getPacketData() {
//		return 1; // type
//	}
	
	public boolean hasSpawnVelocity() {
		return true;
	}
    
    public boolean isMagnetic() {
    	return true;
    }
	
    public Item getItem() {
    	switch(this.getMinecartType()) {
            case CHEST:
                return Items.chest_minecart;
            case TNT:
            	return Items.tnt_minecart;
            case HOPPER:
            	return Items.hopper_minecart;
            default:
            	return Items.minecart;
        }
    }
    
    public EntityType getType() {
    	return EntityType.VEHICLE;
    }

    public static enum EnumMinecartType
    {
        RIDEABLE,
        CHEST,
        TNT,
        HOPPER;
    }
}
