    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005
    006    import java.util.ArrayList;
    007    import java.util.List;
    008    import net.minecraftforge.common.IMinecartCollisionHandler;
    009    import net.minecraftforge.common.MinecartRegistry;
    010    import net.minecraftforge.common.MinecraftForge;
    011    import net.minecraftforge.event.entity.minecart.MinecartCollisionEvent;
    012    import net.minecraftforge.event.entity.minecart.MinecartInteractEvent;
    013    import net.minecraftforge.event.entity.minecart.MinecartUpdateEvent;
    014
    015    public class EntityMinecart extends Entity implements IInventory
    016    {
    017        /** Array of item stacks stored in minecart (for storage minecarts). */
    018        protected ItemStack[] cargoItems;
    019        protected int fuel;
    020        protected boolean field_70499_f;
    021
    022        /** The type of minecart, 2 for powered, 1 for storage. */
    023        public int minecartType;
    024        public double pushX;
    025        public double pushZ;
    026        protected final IUpdatePlayerListBox field_82344_g;
    027        protected boolean field_82345_h;
    028        protected static final int[][][] field_70500_g = 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}}};
    029
    030        /** appears to be the progress of the turn */
    031        protected int turnProgress;
    032        protected double minecartX;
    033        protected double minecartY;
    034        protected double minecartZ;
    035        protected double minecartYaw;
    036        protected double minecartPitch;
    037        @SideOnly(Side.CLIENT)
    038        protected double velocityX;
    039        @SideOnly(Side.CLIENT)
    040        protected double velocityY;
    041        @SideOnly(Side.CLIENT)
    042        protected double velocityZ;
    043
    044        /* Forge: Minecart Compatibility Layer Integration. */
    045        public static float defaultMaxSpeedRail = 0.4f;
    046        public static float defaultMaxSpeedGround = 0.4f;
    047        public static float defaultMaxSpeedAirLateral = 0.4f;
    048        public static float defaultMaxSpeedAirVertical = -1f;
    049        public static double defaultDragRidden = 0.996999979019165D;
    050        public static double defaultDragEmpty = 0.9599999785423279D;
    051        public static double defaultDragAir = 0.94999998807907104D;
    052        protected boolean canUseRail = true;
    053        protected boolean canBePushed = true;
    054        private static IMinecartCollisionHandler collisionHandler = null;
    055
    056        /* Instance versions of the above physics properties */
    057        protected float maxSpeedRail;
    058        protected float maxSpeedGround;
    059        protected float maxSpeedAirLateral;
    060        protected float maxSpeedAirVertical;
    061        protected double dragAir;
    062
    063        public EntityMinecart(World par1World)
    064        {
    065            super(par1World);
    066            this.cargoItems = new ItemStack[36];
    067            this.fuel = 0;
    068            this.field_70499_f = false;
    069            this.field_82345_h = true;
    070            this.preventEntitySpawning = true;
    071            this.setSize(0.98F, 0.7F);
    072            this.yOffset = this.height / 2.0F;
    073            this.field_82344_g = par1World != null ? par1World.func_82735_a(this) : null;
    074
    075            maxSpeedRail = defaultMaxSpeedRail;
    076            maxSpeedGround = defaultMaxSpeedGround;
    077            maxSpeedAirLateral = defaultMaxSpeedAirLateral;
    078            maxSpeedAirVertical = defaultMaxSpeedAirVertical;
    079            dragAir = defaultDragAir;
    080        }
    081
    082        public EntityMinecart(World world, int type)
    083        {
    084            this(world);
    085            minecartType = type;
    086        }
    087
    088        /**
    089         * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to
    090         * prevent them from trampling crops
    091         */
    092        protected boolean canTriggerWalking()
    093        {
    094            return false;
    095        }
    096
    097        protected void entityInit()
    098        {
    099            this.dataWatcher.addObject(16, new Byte((byte)0));
    100            this.dataWatcher.addObject(17, new Integer(0));
    101            this.dataWatcher.addObject(18, new Integer(1));
    102            this.dataWatcher.addObject(19, new Integer(0));
    103        }
    104
    105        /**
    106         * Returns a boundingBox used to collide the entity with other entities and blocks. This enables the entity to be
    107         * pushable on contact, like boats or minecarts.
    108         */
    109        public AxisAlignedBB getCollisionBox(Entity par1Entity)
    110        {
    111            if (getCollisionHandler() != null)
    112            {
    113                return getCollisionHandler().getCollisionBox(this, par1Entity);
    114            }
    115            return par1Entity.boundingBox;
    116        }
    117
    118        /**
    119         * returns the bounding box for this entity
    120         */
    121        public AxisAlignedBB getBoundingBox()
    122        {
    123            if (getCollisionHandler() != null)
    124            {
    125                return getCollisionHandler().getBoundingBox(this);
    126            }
    127            return null;
    128        }
    129
    130        /**
    131         * Returns true if this entity should push and be pushed by other entities when colliding.
    132         */
    133        public boolean canBePushed()
    134        {
    135            return canBePushed;
    136        }
    137
    138        public EntityMinecart(World par1World, double par2, double par4, double par6, int par8)
    139        {
    140            this(par1World);
    141            this.setPosition(par2, par4 + (double)this.yOffset, par6);
    142            this.motionX = 0.0D;
    143            this.motionY = 0.0D;
    144            this.motionZ = 0.0D;
    145            this.prevPosX = par2;
    146            this.prevPosY = par4;
    147            this.prevPosZ = par6;
    148            this.minecartType = par8;
    149        }
    150
    151        /**
    152         * Returns the Y offset from the entity's position for any entity riding this one.
    153         */
    154        public double getMountedYOffset()
    155        {
    156            return (double)this.height * 0.0D - 0.30000001192092896D;
    157        }
    158
    159        /**
    160         * Called when the entity is attacked.
    161         */
    162        public boolean attackEntityFrom(DamageSource par1DamageSource, int par2)
    163        {
    164            if (!this.worldObj.isRemote && !this.isDead)
    165            {
    166                this.func_70494_i(-this.func_70493_k());
    167                this.func_70497_h(10);
    168                this.setBeenAttacked();
    169                this.setDamage(this.getDamage() + par2 * 10);
    170
    171                if (par1DamageSource.getEntity() instanceof EntityPlayer && ((EntityPlayer)par1DamageSource.getEntity()).capabilities.isCreativeMode)
    172                {
    173                    this.setDamage(100);
    174                }
    175
    176                if (this.getDamage() > 40)
    177                {
    178                    if (this.riddenByEntity != null)
    179                    {
    180                        this.riddenByEntity.mountEntity(this);
    181                    }
    182
    183                    this.setDead();
    184                    dropCartAsItem();
    185                }
    186
    187                return true;
    188            }
    189            else
    190            {
    191                return true;
    192            }
    193        }
    194
    195        @SideOnly(Side.CLIENT)
    196
    197        /**
    198         * Setups the entity to do the hurt animation. Only used by packets in multiplayer.
    199         */
    200        public void performHurtAnimation()
    201        {
    202            this.func_70494_i(-this.func_70493_k());
    203            this.func_70497_h(10);
    204            this.setDamage(this.getDamage() + this.getDamage() * 10);
    205        }
    206
    207        /**
    208         * Returns true if other Entities should be prevented from moving through this Entity.
    209         */
    210        public boolean canBeCollidedWith()
    211        {
    212            return !this.isDead;
    213        }
    214
    215        /**
    216         * Will get destroyed next tick.
    217         */
    218        public void setDead()
    219        {
    220            if (this.field_82345_h)
    221            {
    222                for (int var1 = 0; var1 < this.getSizeInventory(); ++var1)
    223                {
    224                    ItemStack var2 = this.getStackInSlot(var1);
    225
    226                    if (var2 != null)
    227                    {
    228                        float var3 = this.rand.nextFloat() * 0.8F + 0.1F;
    229                        float var4 = this.rand.nextFloat() * 0.8F + 0.1F;
    230                        float var5 = this.rand.nextFloat() * 0.8F + 0.1F;
    231
    232                        while (var2.stackSize > 0)
    233                        {
    234                            int var6 = this.rand.nextInt(21) + 10;
    235
    236                            if (var6 > var2.stackSize)
    237                            {
    238                                var6 = var2.stackSize;
    239                            }
    240
    241                            var2.stackSize -= var6;
    242                            EntityItem var7 = new EntityItem(this.worldObj, this.posX + (double)var3, this.posY + (double)var4, this.posZ + (double)var5, new ItemStack(var2.itemID, var6, var2.getItemDamage()));
    243
    244                            if (var2.hasTagCompound())
    245                            {
    246                                var7.item.setTagCompound((NBTTagCompound)var2.getTagCompound().copy());
    247                            }
    248
    249                            float var8 = 0.05F;
    250                            var7.motionX = (double)((float)this.rand.nextGaussian() * var8);
    251                            var7.motionY = (double)((float)this.rand.nextGaussian() * var8 + 0.2F);
    252                            var7.motionZ = (double)((float)this.rand.nextGaussian() * var8);
    253                            this.worldObj.spawnEntityInWorld(var7);
    254                        }
    255                    }
    256                }
    257            }
    258
    259            super.setDead();
    260
    261            if (this.field_82344_g != null)
    262            {
    263                this.field_82344_g.update();
    264            }
    265        }
    266
    267        public void travelToTheEnd(int par1)
    268        {
    269            this.field_82345_h = false;
    270            super.travelToTheEnd(par1);
    271        }
    272
    273        /**
    274         * Called to update the entity's position/logic.
    275         */
    276        public void onUpdate()
    277        {
    278            if (this.field_82344_g != null)
    279            {
    280                this.field_82344_g.update();
    281            }
    282
    283            if (this.func_70496_j() > 0)
    284            {
    285                this.func_70497_h(this.func_70496_j() - 1);
    286            }
    287
    288            if (this.getDamage() > 0)
    289            {
    290                this.setDamage(this.getDamage() - 1);
    291            }
    292
    293            if (this.posY < -64.0D)
    294            {
    295                this.kill();
    296            }
    297
    298            if (this.isMinecartPowered() && this.rand.nextInt(4) == 0 && minecartType == 2 && getClass() == EntityMinecart.class)
    299            {
    300                this.worldObj.spawnParticle("largesmoke", this.posX, this.posY + 0.8D, this.posZ, 0.0D, 0.0D, 0.0D);
    301            }
    302
    303            if (this.worldObj.isRemote)
    304            {
    305                if (this.turnProgress > 0)
    306                {
    307                    double var45 = this.posX + (this.minecartX - this.posX) / (double)this.turnProgress;
    308                    double var46 = this.posY + (this.minecartY - this.posY) / (double)this.turnProgress;
    309                    double var5 = this.posZ + (this.minecartZ - this.posZ) / (double)this.turnProgress;
    310                    double var7 = MathHelper.wrapAngleTo180_double(this.minecartYaw - (double)this.rotationYaw);
    311                    this.rotationYaw = (float)((double)this.rotationYaw + var7 / (double)this.turnProgress);
    312                    this.rotationPitch = (float)((double)this.rotationPitch + (this.minecartPitch - (double)this.rotationPitch) / (double)this.turnProgress);
    313                    --this.turnProgress;
    314                    this.setPosition(var45, var46, var5);
    315                    this.setRotation(this.rotationYaw, this.rotationPitch);
    316                }
    317                else
    318                {
    319                    this.setPosition(this.posX, this.posY, this.posZ);
    320                    this.setRotation(this.rotationYaw, this.rotationPitch);
    321                }
    322            }
    323            else
    324            {
    325                this.prevPosX = this.posX;
    326                this.prevPosY = this.posY;
    327                this.prevPosZ = this.posZ;
    328                this.motionY -= 0.03999999910593033D;
    329                int var1 = MathHelper.floor_double(this.posX);
    330                int var2 = MathHelper.floor_double(this.posY);
    331                int var3 = MathHelper.floor_double(this.posZ);
    332
    333                if (BlockRail.isRailBlockAt(this.worldObj, var1, var2 - 1, var3))
    334                {
    335                    --var2;
    336                }
    337
    338                double var4 = 0.4D;
    339                double var6 = 0.0078125D;
    340                int var8 = this.worldObj.getBlockId(var1, var2, var3);
    341
    342                if (canUseRail() && BlockRail.isRailBlock(var8))
    343                {
    344                    Vec3 var9 = this.func_70489_a(this.posX, this.posY, this.posZ);
    345                    int var10 = ((BlockRail)Block.blocksList[var8]).getBasicRailMetadata(worldObj, this, var1, var2, var3);
    346                    this.posY = (double)var2;
    347                    boolean var11 = false;
    348                    boolean var12 = false;
    349
    350                    if (var8 == Block.railPowered.blockID)
    351                    {
    352                        var11 = (worldObj.getBlockMetadata(var1, var2, var3) & 8) != 0;
    353                        var12 = !var11;
    354                    }
    355
    356                    if (((BlockRail)Block.blocksList[var8]).isPowered())
    357                    {
    358                        var10 &= 7;
    359                    }
    360
    361                    if (var10 >= 2 && var10 <= 5)
    362                    {
    363                        this.posY = (double)(var2 + 1);
    364                    }
    365
    366                    adjustSlopeVelocities(var10);
    367
    368                    int[][] var13 = field_70500_g[var10];
    369                    double var14 = (double)(var13[1][0] - var13[0][0]);
    370                    double var16 = (double)(var13[1][2] - var13[0][2]);
    371                    double var18 = Math.sqrt(var14 * var14 + var16 * var16);
    372                    double var20 = this.motionX * var14 + this.motionZ * var16;
    373
    374                    if (var20 < 0.0D)
    375                    {
    376                        var14 = -var14;
    377                        var16 = -var16;
    378                    }
    379
    380                    double var22 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
    381                    this.motionX = var22 * var14 / var18;
    382                    this.motionZ = var22 * var16 / var18;
    383                    double var24;
    384                    double var26;
    385
    386                    if (this.riddenByEntity != null)
    387                    {
    388                        var24 = this.riddenByEntity.motionX * this.riddenByEntity.motionX + this.riddenByEntity.motionZ * this.riddenByEntity.motionZ;
    389                        var26 = this.motionX * this.motionX + this.motionZ * this.motionZ;
    390
    391                        if (var24 > 1.0E-4D && var26 < 0.01D)
    392                        {
    393                            this.motionX += this.riddenByEntity.motionX * 0.1D;
    394                            this.motionZ += this.riddenByEntity.motionZ * 0.1D;
    395                            var12 = false;
    396                        }
    397                    }
    398
    399                    if (var12 && shouldDoRailFunctions())
    400                    {
    401                        var24 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
    402
    403                        if (var24 < 0.03D)
    404                        {
    405                            this.motionX *= 0.0D;
    406                            this.motionY *= 0.0D;
    407                            this.motionZ *= 0.0D;
    408                        }
    409                        else
    410                        {
    411                            this.motionX *= 0.5D;
    412                            this.motionY *= 0.0D;
    413                            this.motionZ *= 0.5D;
    414                        }
    415                    }
    416
    417                    var24 = 0.0D;
    418                    var26 = (double)var1 + 0.5D + (double)var13[0][0] * 0.5D;
    419                    double var28 = (double)var3 + 0.5D + (double)var13[0][2] * 0.5D;
    420                    double var30 = (double)var1 + 0.5D + (double)var13[1][0] * 0.5D;
    421                    double var32 = (double)var3 + 0.5D + (double)var13[1][2] * 0.5D;
    422                    var14 = var30 - var26;
    423                    var16 = var32 - var28;
    424                    double var34;
    425                    double var36;
    426
    427                    if (var14 == 0.0D)
    428                    {
    429                        this.posX = (double)var1 + 0.5D;
    430                        var24 = this.posZ - (double)var3;
    431                    }
    432                    else if (var16 == 0.0D)
    433                    {
    434                        this.posZ = (double)var3 + 0.5D;
    435                        var24 = this.posX - (double)var1;
    436                    }
    437                    else
    438                    {
    439                        var34 = this.posX - var26;
    440                        var36 = this.posZ - var28;
    441                        var24 = (var34 * var14 + var36 * var16) * 2.0D;
    442                    }
    443
    444                    this.posX = var26 + var14 * var24;
    445                    this.posZ = var28 + var16 * var24;
    446                    this.setPosition(this.posX, this.posY + (double)this.yOffset, this.posZ);
    447
    448                    moveMinecartOnRail(var1, var2, var3);
    449
    450                    if (var13[0][1] != 0 && MathHelper.floor_double(this.posX) - var1 == var13[0][0] && MathHelper.floor_double(this.posZ) - var3 == var13[0][2])
    451                    {
    452                        this.setPosition(this.posX, this.posY + (double)var13[0][1], this.posZ);
    453                    }
    454                    else if (var13[1][1] != 0 && MathHelper.floor_double(this.posX) - var1 == var13[1][0] && MathHelper.floor_double(this.posZ) - var3 == var13[1][2])
    455                    {
    456                        this.setPosition(this.posX, this.posY + (double)var13[1][1], this.posZ);
    457                    }
    458
    459                    applyDragAndPushForces();
    460
    461                    Vec3 var52 = this.func_70489_a(this.posX, this.posY, this.posZ);
    462
    463                    if (var52 != null && var9 != null)
    464                    {
    465                        double var39 = (var9.yCoord - var52.yCoord) * 0.05D;
    466                        var22 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
    467
    468                        if (var22 > 0.0D)
    469                        {
    470                            this.motionX = this.motionX / var22 * (var22 + var39);
    471                            this.motionZ = this.motionZ / var22 * (var22 + var39);
    472                        }
    473
    474                        this.setPosition(this.posX, var52.yCoord, this.posZ);
    475                    }
    476
    477                    int var51 = MathHelper.floor_double(this.posX);
    478                    int var53 = MathHelper.floor_double(this.posZ);
    479
    480                    if (var51 != var1 || var53 != var3)
    481                    {
    482                        var22 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
    483                        this.motionX = var22 * (double)(var51 - var1);
    484                        this.motionZ = var22 * (double)(var53 - var3);
    485                    }
    486
    487                    double var41;
    488
    489                    updatePushForces();
    490
    491                    if(shouldDoRailFunctions())
    492                    {
    493                        ((BlockRail)Block.blocksList[var8]).onMinecartPass(worldObj, this, var1, var2, var3);
    494                    }
    495
    496                    if (var11 && shouldDoRailFunctions())
    497                    {
    498                        var41 = Math.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
    499
    500                        if (var41 > 0.01D)
    501                        {
    502                            double var43 = 0.06D;
    503                            this.motionX += this.motionX / var41 * var43;
    504                            this.motionZ += this.motionZ / var41 * var43;
    505                        }
    506                        else if (var10 == 1)
    507                        {
    508                            if (this.worldObj.isBlockNormalCube(var1 - 1, var2, var3))
    509                            {
    510                                this.motionX = 0.02D;
    511                            }
    512                            else if (this.worldObj.isBlockNormalCube(var1 + 1, var2, var3))
    513                            {
    514                                this.motionX = -0.02D;
    515                            }
    516                        }
    517                        else if (var10 == 0)
    518                        {
    519                            if (this.worldObj.isBlockNormalCube(var1, var2, var3 - 1))
    520                            {
    521                                this.motionZ = 0.02D;
    522                            }
    523                            else if (this.worldObj.isBlockNormalCube(var1, var2, var3 + 1))
    524                            {
    525                                this.motionZ = -0.02D;
    526                            }
    527                        }
    528                    }
    529                }
    530                else
    531                {
    532                    moveMinecartOffRail(var1, var2, var3);
    533                }
    534
    535                this.doBlockCollisions();
    536                this.rotationPitch = 0.0F;
    537                double var47 = this.prevPosX - this.posX;
    538                double var48 = this.prevPosZ - this.posZ;
    539
    540                if (var47 * var47 + var48 * var48 > 0.001D)
    541                {
    542                    this.rotationYaw = (float)(Math.atan2(var48, var47) * 180.0D / Math.PI);
    543
    544                    if (this.field_70499_f)
    545                    {
    546                        this.rotationYaw += 180.0F;
    547                    }
    548                }
    549
    550                double var49 = (double)MathHelper.wrapAngleTo180_float(this.rotationYaw - this.prevRotationYaw);
    551
    552                if (var49 < -170.0D || var49 >= 170.0D)
    553                {
    554                    this.rotationYaw += 180.0F;
    555                    this.field_70499_f = !this.field_70499_f;
    556                }
    557
    558                this.setRotation(this.rotationYaw, this.rotationPitch);
    559
    560                AxisAlignedBB box = null;
    561                if (getCollisionHandler() != null)
    562                {
    563                    box = getCollisionHandler().getMinecartCollisionBox(this);
    564                }
    565                else
    566                {
    567                    box = boundingBox.expand(0.2D, 0.0D, 0.2D);
    568                }
    569
    570                List var15 = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, box);
    571
    572                if (var15 != null && !var15.isEmpty())
    573                {
    574                    for (int var50 = 0; var50 < var15.size(); ++var50)
    575                    {
    576                        Entity var17 = (Entity)var15.get(var50);
    577
    578                        if (var17 != this.riddenByEntity && var17.canBePushed() && var17 instanceof EntityMinecart)
    579                        {
    580                            var17.applyEntityCollision(this);
    581                        }
    582                    }
    583                }
    584
    585                if (this.riddenByEntity != null && this.riddenByEntity.isDead)
    586                {
    587                    if (this.riddenByEntity.ridingEntity == this)
    588                    {
    589                        this.riddenByEntity.ridingEntity = null;
    590                    }
    591
    592                    this.riddenByEntity = null;
    593                }
    594
    595                updateFuel();
    596                MinecraftForge.EVENT_BUS.post(new MinecartUpdateEvent(this, var1, var2, var3));
    597            }
    598        }
    599
    600        @SideOnly(Side.CLIENT)
    601        public Vec3 func_70495_a(double par1, double par3, double par5, double par7)
    602        {
    603            int var9 = MathHelper.floor_double(par1);
    604            int var10 = MathHelper.floor_double(par3);
    605            int var11 = MathHelper.floor_double(par5);
    606
    607            if (BlockRail.isRailBlockAt(this.worldObj, var9, var10 - 1, var11))
    608            {
    609                --var10;
    610            }
    611
    612            int var12 = this.worldObj.getBlockId(var9, var10, var11);
    613
    614            if (!BlockRail.isRailBlock(var12))
    615            {
    616                return null;
    617            }
    618            else
    619            {
    620                int var13 = ((BlockRail)Block.blocksList[var12]).getBasicRailMetadata(worldObj, this, var9, var10, var11);
    621
    622                par3 = (double)var10;
    623
    624                if (var13 >= 2 && var13 <= 5)
    625                {
    626                    par3 = (double)(var10 + 1);
    627                }
    628
    629                int[][] var14 = field_70500_g[var13];
    630                double var15 = (double)(var14[1][0] - var14[0][0]);
    631                double var17 = (double)(var14[1][2] - var14[0][2]);
    632                double var19 = Math.sqrt(var15 * var15 + var17 * var17);
    633                var15 /= var19;
    634                var17 /= var19;
    635                par1 += var15 * par7;
    636                par5 += var17 * par7;
    637
    638                if (var14[0][1] != 0 && MathHelper.floor_double(par1) - var9 == var14[0][0] && MathHelper.floor_double(par5) - var11 == var14[0][2])
    639                {
    640                    par3 += (double)var14[0][1];
    641                }
    642                else if (var14[1][1] != 0 && MathHelper.floor_double(par1) - var9 == var14[1][0] && MathHelper.floor_double(par5) - var11 == var14[1][2])
    643                {
    644                    par3 += (double)var14[1][1];
    645                }
    646
    647                return this.func_70489_a(par1, par3, par5);
    648            }
    649        }
    650
    651        public Vec3 func_70489_a(double par1, double par3, double par5)
    652        {
    653            int var7 = MathHelper.floor_double(par1);
    654            int var8 = MathHelper.floor_double(par3);
    655            int var9 = MathHelper.floor_double(par5);
    656
    657            if (BlockRail.isRailBlockAt(this.worldObj, var7, var8 - 1, var9))
    658            {
    659                --var8;
    660            }
    661
    662            int var10 = this.worldObj.getBlockId(var7, var8, var9);
    663
    664            if (BlockRail.isRailBlock(var10))
    665            {
    666                int var11 = ((BlockRail)Block.blocksList[var10]).getBasicRailMetadata(worldObj, this, var7, var8, var9);
    667                par3 = (double)var8;
    668
    669                if (var11 >= 2 && var11 <= 5)
    670                {
    671                    par3 = (double)(var8 + 1);
    672                }
    673
    674                int[][] var12 = field_70500_g[var11];
    675                double var13 = 0.0D;
    676                double var15 = (double)var7 + 0.5D + (double)var12[0][0] * 0.5D;
    677                double var17 = (double)var8 + 0.5D + (double)var12[0][1] * 0.5D;
    678                double var19 = (double)var9 + 0.5D + (double)var12[0][2] * 0.5D;
    679                double var21 = (double)var7 + 0.5D + (double)var12[1][0] * 0.5D;
    680                double var23 = (double)var8 + 0.5D + (double)var12[1][1] * 0.5D;
    681                double var25 = (double)var9 + 0.5D + (double)var12[1][2] * 0.5D;
    682                double var27 = var21 - var15;
    683                double var29 = (var23 - var17) * 2.0D;
    684                double var31 = var25 - var19;
    685
    686                if (var27 == 0.0D)
    687                {
    688                    par1 = (double)var7 + 0.5D;
    689                    var13 = par5 - (double)var9;
    690                }
    691                else if (var31 == 0.0D)
    692                {
    693                    par5 = (double)var9 + 0.5D;
    694                    var13 = par1 - (double)var7;
    695                }
    696                else
    697                {
    698                    double var33 = par1 - var15;
    699                    double var35 = par5 - var19;
    700                    var13 = (var33 * var27 + var35 * var31) * 2.0D;
    701                }
    702
    703                par1 = var15 + var27 * var13;
    704                par3 = var17 + var29 * var13;
    705                par5 = var19 + var31 * var13;
    706
    707                if (var29 < 0.0D)
    708                {
    709                    ++par3;
    710                }
    711
    712                if (var29 > 0.0D)
    713                {
    714                    par3 += 0.5D;
    715                }
    716
    717                return this.worldObj.func_82732_R().getVecFromPool(par1, par3, par5);
    718            }
    719            else
    720            {
    721                return null;
    722            }
    723        }
    724
    725        /**
    726         * (abstract) Protected helper method to write subclass entity data to NBT.
    727         */
    728        protected void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
    729        {
    730            par1NBTTagCompound.setInteger("Type", this.minecartType);
    731
    732            if (isPoweredCart())
    733            {
    734                par1NBTTagCompound.setDouble("PushX", this.pushX);
    735                par1NBTTagCompound.setDouble("PushZ", this.pushZ);
    736                par1NBTTagCompound.setInteger("Fuel", this.fuel);
    737            }
    738
    739            if (getSizeInventory() > 0)
    740            {
    741                NBTTagList var2 = new NBTTagList();
    742
    743                for (int var3 = 0; var3 < this.cargoItems.length; ++var3)
    744                {
    745                    if (this.cargoItems[var3] != null)
    746                    {
    747                        NBTTagCompound var4 = new NBTTagCompound();
    748                        var4.setByte("Slot", (byte)var3);
    749                        this.cargoItems[var3].writeToNBT(var4);
    750                        var2.appendTag(var4);
    751                    }
    752                }
    753
    754                par1NBTTagCompound.setTag("Items", var2);
    755            }
    756        }
    757
    758        /**
    759         * (abstract) Protected helper method to read subclass entity data from NBT.
    760         */
    761        protected void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
    762        {
    763            this.minecartType = par1NBTTagCompound.getInteger("Type");
    764
    765            if (isPoweredCart())
    766            {
    767                this.pushX = par1NBTTagCompound.getDouble("PushX");
    768                this.pushZ = par1NBTTagCompound.getDouble("PushZ");
    769                try
    770                {
    771                    this.fuel = par1NBTTagCompound.getInteger("Fuel");
    772                }
    773                catch (ClassCastException e)
    774                {
    775                    this.fuel = par1NBTTagCompound.getShort("Fuel");
    776                }
    777            }
    778
    779            if (getSizeInventory() > 0)
    780            {
    781                NBTTagList var2 = par1NBTTagCompound.getTagList("Items");
    782                this.cargoItems = new ItemStack[this.getSizeInventory()];
    783
    784                for (int var3 = 0; var3 < var2.tagCount(); ++var3)
    785                {
    786                    NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
    787                    int var5 = var4.getByte("Slot") & 255;
    788
    789                    if (var5 >= 0 && var5 < this.cargoItems.length)
    790                    {
    791                        this.cargoItems[var5] = ItemStack.loadItemStackFromNBT(var4);
    792                    }
    793                }
    794            }
    795        }
    796
    797        @SideOnly(Side.CLIENT)
    798        public float getShadowSize()
    799        {
    800            return 0.0F;
    801        }
    802
    803        /**
    804         * Applies a velocity to each of the entities pushing them away from each other. Args: entity
    805         */
    806        public void applyEntityCollision(Entity par1Entity)
    807        {
    808            MinecraftForge.EVENT_BUS.post(new MinecartCollisionEvent(this, par1Entity));
    809            if (getCollisionHandler() != null)
    810            {
    811                getCollisionHandler().onEntityCollision(this, par1Entity);
    812                return;
    813            }
    814            if (!this.worldObj.isRemote)
    815            {
    816                if (par1Entity != this.riddenByEntity)
    817                {
    818                    if (par1Entity instanceof EntityLiving && !(par1Entity instanceof EntityPlayer) && !(par1Entity instanceof EntityIronGolem) && canBeRidden() && this.motionX * this.motionX + this.motionZ * this.motionZ > 0.01D && this.riddenByEntity == null && par1Entity.ridingEntity == null)
    819                    {
    820                        par1Entity.mountEntity(this);
    821                    }
    822
    823                    double var2 = par1Entity.posX - this.posX;
    824                    double var4 = par1Entity.posZ - this.posZ;
    825                    double var6 = var2 * var2 + var4 * var4;
    826
    827                    if (var6 >= 9.999999747378752E-5D)
    828                    {
    829                        var6 = (double)MathHelper.sqrt_double(var6);
    830                        var2 /= var6;
    831                        var4 /= var6;
    832                        double var8 = 1.0D / var6;
    833
    834                        if (var8 > 1.0D)
    835                        {
    836                            var8 = 1.0D;
    837                        }
    838
    839                        var2 *= var8;
    840                        var4 *= var8;
    841                        var2 *= 0.10000000149011612D;
    842                        var4 *= 0.10000000149011612D;
    843                        var2 *= (double)(1.0F - this.entityCollisionReduction);
    844                        var4 *= (double)(1.0F - this.entityCollisionReduction);
    845                        var2 *= 0.5D;
    846                        var4 *= 0.5D;
    847
    848                        if (par1Entity instanceof EntityMinecart)
    849                        {
    850                            double var10 = par1Entity.posX - this.posX;
    851                            double var12 = par1Entity.posZ - this.posZ;
    852                            Vec3 var14 = this.worldObj.func_82732_R().getVecFromPool(var10, 0.0D, var12).normalize();
    853                            Vec3 var15 = this.worldObj.func_82732_R().getVecFromPool((double)MathHelper.cos(this.rotationYaw * (float)Math.PI / 180.0F), 0.0D, (double)MathHelper.sin(this.rotationYaw * (float)Math.PI / 180.0F)).normalize();
    854                            double var16 = Math.abs(var14.dotProduct(var15));
    855
    856                            if (var16 < 0.800000011920929D)
    857                            {
    858                                return;
    859                            }
    860
    861                            double var18 = par1Entity.motionX + this.motionX;
    862                            double var20 = par1Entity.motionZ + this.motionZ;
    863
    864                            if (((EntityMinecart)par1Entity).isPoweredCart() && !isPoweredCart())
    865                            {
    866                                this.motionX *= 0.20000000298023224D;
    867                                this.motionZ *= 0.20000000298023224D;
    868                                this.addVelocity(par1Entity.motionX - var2, 0.0D, par1Entity.motionZ - var4);
    869                                par1Entity.motionX *= 0.949999988079071D;
    870                                par1Entity.motionZ *= 0.949999988079071D;
    871                            }
    872                            else if (!((EntityMinecart)par1Entity).isPoweredCart() && isPoweredCart())
    873                            {
    874                                par1Entity.motionX *= 0.20000000298023224D;
    875                                par1Entity.motionZ *= 0.20000000298023224D;
    876                                par1Entity.addVelocity(this.motionX + var2, 0.0D, this.motionZ + var4);
    877                                this.motionX *= 0.949999988079071D;
    878                                this.motionZ *= 0.949999988079071D;
    879                            }
    880                            else
    881                            {
    882                                var18 /= 2.0D;
    883                                var20 /= 2.0D;
    884                                this.motionX *= 0.20000000298023224D;
    885                                this.motionZ *= 0.20000000298023224D;
    886                                this.addVelocity(var18 - var2, 0.0D, var20 - var4);
    887                                par1Entity.motionX *= 0.20000000298023224D;
    888                                par1Entity.motionZ *= 0.20000000298023224D;
    889                                par1Entity.addVelocity(var18 + var2, 0.0D, var20 + var4);
    890                            }
    891                        }
    892                        else
    893                        {
    894                            this.addVelocity(-var2, 0.0D, -var4);
    895                            par1Entity.addVelocity(var2 / 4.0D, 0.0D, var4 / 4.0D);
    896                        }
    897                    }
    898                }
    899            }
    900        }
    901
    902        /**
    903         * Returns the number of slots in the inventory.
    904         */
    905        public int getSizeInventory()
    906        {
    907            return (minecartType == 1 && getClass() == EntityMinecart.class ? 27 : 0);
    908        }
    909
    910        /**
    911         * Returns the stack in slot i
    912         */
    913        public ItemStack getStackInSlot(int par1)
    914        {
    915            return this.cargoItems[par1];
    916        }
    917
    918        /**
    919         * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
    920         * new stack.
    921         */
    922        public ItemStack decrStackSize(int par1, int par2)
    923        {
    924            if (this.cargoItems[par1] != null)
    925            {
    926                ItemStack var3;
    927
    928                if (this.cargoItems[par1].stackSize <= par2)
    929                {
    930                    var3 = this.cargoItems[par1];
    931                    this.cargoItems[par1] = null;
    932                    return var3;
    933                }
    934                else
    935                {
    936                    var3 = this.cargoItems[par1].splitStack(par2);
    937
    938                    if (this.cargoItems[par1].stackSize == 0)
    939                    {
    940                        this.cargoItems[par1] = null;
    941                    }
    942
    943                    return var3;
    944                }
    945            }
    946            else
    947            {
    948                return null;
    949            }
    950        }
    951
    952        /**
    953         * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
    954         * like when you close a workbench GUI.
    955         */
    956        public ItemStack getStackInSlotOnClosing(int par1)
    957        {
    958            if (this.cargoItems[par1] != null)
    959            {
    960                ItemStack var2 = this.cargoItems[par1];
    961                this.cargoItems[par1] = null;
    962                return var2;
    963            }
    964            else
    965            {
    966                return null;
    967            }
    968        }
    969
    970        /**
    971         * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
    972         */
    973        public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
    974        {
    975            this.cargoItems[par1] = par2ItemStack;
    976
    977            if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
    978            {
    979                par2ItemStack.stackSize = this.getInventoryStackLimit();
    980            }
    981        }
    982
    983        /**
    984         * Returns the name of the inventory.
    985         */
    986        public String getInvName()
    987        {
    988            return "container.minecart";
    989        }
    990
    991        /**
    992         * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
    993         * this more of a set than a get?*
    994         */
    995        public int getInventoryStackLimit()
    996        {
    997            return 64;
    998        }
    999
    1000        /**
    1001         * Called when an the contents of an Inventory change, usually
    1002         */
    1003        public void onInventoryChanged() {}
    1004
    1005        /**
    1006         * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
    1007         */
    1008        public boolean interact(EntityPlayer par1EntityPlayer)
    1009        {
    1010            if (MinecraftForge.EVENT_BUS.post(new MinecartInteractEvent(this, par1EntityPlayer)))
    1011            {
    1012                return true;
    1013            }
    1014
    1015            if (canBeRidden())
    1016            {
    1017                if (this.riddenByEntity != null && this.riddenByEntity instanceof EntityPlayer && this.riddenByEntity != par1EntityPlayer)
    1018                {
    1019                    return true;
    1020                }
    1021
    1022                if (!this.worldObj.isRemote)
    1023                {
    1024                    par1EntityPlayer.mountEntity(this);
    1025                }
    1026            }
    1027            else if (getSizeInventory() > 0)
    1028            {
    1029                if (!this.worldObj.isRemote)
    1030                {
    1031                    par1EntityPlayer.displayGUIChest(this);
    1032                }
    1033            }
    1034            else if (this.minecartType == 2 && getClass() == EntityMinecart.class)
    1035            {
    1036                ItemStack var2 = par1EntityPlayer.inventory.getCurrentItem();
    1037
    1038                if (var2 != null && var2.itemID == Item.coal.shiftedIndex)
    1039                {
    1040                    if (--var2.stackSize == 0)
    1041                    {
    1042                        par1EntityPlayer.inventory.setInventorySlotContents(par1EntityPlayer.inventory.currentItem, (ItemStack)null);
    1043                    }
    1044
    1045                    this.fuel += 3600;
    1046                }
    1047
    1048                this.pushX = this.posX - par1EntityPlayer.posX;
    1049                this.pushZ = this.posZ - par1EntityPlayer.posZ;
    1050            }
    1051
    1052            return true;
    1053        }
    1054
    1055        @SideOnly(Side.CLIENT)
    1056
    1057        /**
    1058         * Sets the position and rotation. Only difference from the other one is no bounding on the rotation. Args: posX,
    1059         * posY, posZ, yaw, pitch
    1060         */
    1061        public void setPositionAndRotation2(double par1, double par3, double par5, float par7, float par8, int par9)
    1062        {
    1063            this.minecartX = par1;
    1064            this.minecartY = par3;
    1065            this.minecartZ = par5;
    1066            this.minecartYaw = (double)par7;
    1067            this.minecartPitch = (double)par8;
    1068            this.turnProgress = par9 + 2;
    1069            this.motionX = this.velocityX;
    1070            this.motionY = this.velocityY;
    1071            this.motionZ = this.velocityZ;
    1072        }
    1073
    1074        /**
    1075         * Do not make give this method the name canInteractWith because it clashes with Container
    1076         */
    1077        public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
    1078        {
    1079            return this.isDead ? false : par1EntityPlayer.getDistanceSqToEntity(this) <= 64.0D;
    1080        }
    1081
    1082        @SideOnly(Side.CLIENT)
    1083
    1084        /**
    1085         * Sets the velocity to the args. Args: x, y, z
    1086         */
    1087        public void setVelocity(double par1, double par3, double par5)
    1088        {
    1089            this.velocityX = this.motionX = par1;
    1090            this.velocityY = this.motionY = par3;
    1091            this.velocityZ = this.motionZ = par5;
    1092        }
    1093
    1094        /**
    1095         * Is this minecart powered (Fuel > 0)
    1096         */
    1097        public boolean isMinecartPowered()
    1098        {
    1099            return (this.dataWatcher.getWatchableObjectByte(16) & 1) != 0;
    1100        }
    1101
    1102        /**
    1103         * Set if this minecart is powered (Fuel > 0)
    1104         */
    1105        protected void setMinecartPowered(boolean par1)
    1106        {
    1107            if (par1)
    1108            {
    1109                this.dataWatcher.updateObject(16, Byte.valueOf((byte)(this.dataWatcher.getWatchableObjectByte(16) | 1)));
    1110            }
    1111            else
    1112            {
    1113                this.dataWatcher.updateObject(16, Byte.valueOf((byte)(this.dataWatcher.getWatchableObjectByte(16) & -2)));
    1114            }
    1115        }
    1116
    1117        public void openChest() {}
    1118
    1119        public void closeChest() {}
    1120
    1121        /**
    1122         * Sets the current amount of damage the minecart has taken. Decreases over time. The cart breaks when this is over
    1123         * 40.
    1124         */
    1125        public void setDamage(int par1)
    1126        {
    1127            this.dataWatcher.updateObject(19, Integer.valueOf(par1));
    1128        }
    1129
    1130        /**
    1131         * Gets the current amount of damage the minecart has taken. Decreases over time. The cart breaks when this is over
    1132         * 40.
    1133         */
    1134        public int getDamage()
    1135        {
    1136            return this.dataWatcher.getWatchableObjectInt(19);
    1137        }
    1138
    1139        public void func_70497_h(int par1)
    1140        {
    1141            this.dataWatcher.updateObject(17, Integer.valueOf(par1));
    1142        }
    1143
    1144        public int func_70496_j()
    1145        {
    1146            return this.dataWatcher.getWatchableObjectInt(17);
    1147        }
    1148
    1149        public void func_70494_i(int par1)
    1150        {
    1151            this.dataWatcher.updateObject(18, Integer.valueOf(par1));
    1152        }
    1153
    1154        public int func_70493_k()
    1155        {
    1156            return this.dataWatcher.getWatchableObjectInt(18);
    1157        }
    1158
    1159        /**
    1160         * Drops the cart as a item. The exact item dropped is defined by getItemDropped().
    1161         */
    1162        public void dropCartAsItem()
    1163        {
    1164            for(ItemStack item : getItemsDropped())
    1165            {
    1166                entityDropItem(item, 0);
    1167            }
    1168        }
    1169
    1170        /**
    1171         * Override this to define which items your cart drops when broken.
    1172         * This does not include items contained in the inventory,
    1173         * that is handled elsewhere.
    1174         * @return A list of items dropped.
    1175         */
    1176        public List<ItemStack> getItemsDropped()
    1177        {
    1178            List<ItemStack> items = new ArrayList<ItemStack>();
    1179            items.add(new ItemStack(Item.minecartEmpty));
    1180
    1181            switch(minecartType)
    1182            {
    1183                case 1:
    1184                    items.add(new ItemStack(Block.chest));
    1185                    break;
    1186                case 2:
    1187                    items.add(new ItemStack(Block.stoneOvenIdle));
    1188                    break;
    1189            }
    1190            return items;
    1191        }
    1192
    1193        /**
    1194         * This function returns an ItemStack that represents this cart.
    1195         * This should be an ItemStack that can be used by the player to place the cart.
    1196         * This is the item that was registered with the cart via the registerMinecart function,
    1197         * but is not necessary the item the cart drops when destroyed.
    1198         * @return An ItemStack that can be used to place the cart.
    1199         */
    1200        public ItemStack getCartItem()
    1201        {
    1202            return MinecartRegistry.getItemForCart(this);
    1203        }
    1204
    1205        /**
    1206         * Returns true if this cart is self propelled.
    1207         * @return True if powered.
    1208         */
    1209        public boolean isPoweredCart()
    1210        {
    1211            return minecartType == 2 && getClass() == EntityMinecart.class;
    1212        }
    1213
    1214        /**
    1215         * Returns true if this cart is a storage cart
    1216         * Some carts may have inventories but not be storage carts
    1217         * and some carts without inventories may be storage carts.
    1218         * @return True if this cart should be classified as a storage cart.
    1219         */
    1220        public boolean isStorageCart()
    1221        {
    1222            return minecartType == 1 && getClass() == EntityMinecart.class;
    1223        }
    1224
    1225        /**
    1226         * Returns true if this cart can be ridden by an Entity.
    1227         * @return True if this cart can be ridden.
    1228         */
    1229        public boolean canBeRidden()
    1230        {
    1231            if(minecartType == 0 && getClass() == EntityMinecart.class)
    1232            {
    1233                return true;
    1234            }
    1235            return false;
    1236        }
    1237
    1238        /**
    1239         * Returns true if this cart can currently use rails.
    1240         * This function is mainly used to gracefully detach a minecart from a rail.
    1241         * @return True if the minecart can use rails.
    1242         */
    1243        public boolean canUseRail()
    1244        {
    1245            return canUseRail;
    1246        }
    1247
    1248        /**
    1249         * Set whether the minecart can use rails.
    1250         * This function is mainly used to gracefully detach a minecart from a rail.
    1251         * @param use Whether the minecart can currently use rails.
    1252         */
    1253        public void setCanUseRail(boolean use)
    1254        {
    1255            canUseRail = use;
    1256        }
    1257
    1258        /**
    1259         * Return false if this cart should not call IRail.onMinecartPass() and should ignore Powered Rails.
    1260         * @return True if this cart should call IRail.onMinecartPass().
    1261         */
    1262        public boolean shouldDoRailFunctions()
    1263        {
    1264            return true;
    1265        }
    1266
    1267        /**
    1268         * Simply returns the minecartType variable.
    1269         * @return minecartType
    1270         */
    1271        public int getMinecartType()
    1272        {
    1273            return minecartType;
    1274        }
    1275
    1276        /**
    1277         * Gets the current global Minecart Collision handler if none
    1278         * is registered, returns null
    1279         * @return The collision handler or null
    1280         */
    1281        public static IMinecartCollisionHandler getCollisionHandler()
    1282        {
    1283            return collisionHandler;
    1284        }
    1285
    1286        /**
    1287         * Sets the global Minecart Collision handler, overwrites any
    1288         * that is currently set.
    1289         * @param handler The new handler
    1290         */
    1291        public static void setCollisionHandler(IMinecartCollisionHandler handler)
    1292        {
    1293            collisionHandler = handler;
    1294        }
    1295
    1296        /**
    1297         * Carts should return their drag factor here
    1298         * @return The drag rate.
    1299         */
    1300        protected double getDrag()
    1301        {
    1302            return riddenByEntity != null ? defaultDragRidden : defaultDragEmpty;
    1303        }
    1304
    1305        /**
    1306         * Moved to allow overrides.
    1307         * This code applies drag and updates push forces.
    1308         */
    1309        protected void applyDragAndPushForces()
    1310        {
    1311            if(isPoweredCart())
    1312            {
    1313                double d27 = MathHelper.sqrt_double(pushX * pushX + pushZ * pushZ);
    1314                if(d27 > 0.01D)
    1315                {
    1316                    pushX /= d27;
    1317                    pushZ /= d27;
    1318                    double d29 = 0.04;
    1319                    motionX *= 0.8D;
    1320                    motionY *= 0.0D;
    1321                    motionZ *= 0.8D;
    1322                    motionX += pushX * d29;
    1323                    motionZ += pushZ * d29;
    1324                }
    1325                else
    1326                {
    1327                    motionX *= 0.9D;
    1328                    motionY *= 0.0D;
    1329                    motionZ *= 0.9D;
    1330                }
    1331            }
    1332            motionX *= getDrag();
    1333            motionY *= 0.0D;
    1334            motionZ *= getDrag();
    1335        }
    1336
    1337        /**
    1338         * Moved to allow overrides.
    1339         * This code updates push forces.
    1340         */
    1341        protected void updatePushForces()
    1342        {
    1343            if(isPoweredCart())
    1344            {
    1345                double push = MathHelper.sqrt_double(pushX * pushX + pushZ * pushZ);
    1346                if(push > 0.01D && motionX * motionX + motionZ * motionZ > 0.001D)
    1347                {
    1348                    pushX /= push;
    1349                    pushZ /= push;
    1350                    if(pushX * motionX + pushZ * motionZ < 0.0D)
    1351                    {
    1352                        pushX = 0.0D;
    1353                        pushZ = 0.0D;
    1354                    }
    1355                    else
    1356                    {
    1357                        pushX = motionX;
    1358                        pushZ = motionZ;
    1359                    }
    1360                }
    1361            }
    1362        }
    1363
    1364        /**
    1365         * Moved to allow overrides.
    1366         * This code handles minecart movement and speed capping when on a rail.
    1367         */
    1368        protected void moveMinecartOnRail(int i, int j, int k)
    1369        {
    1370            int id = worldObj.getBlockId(i, j, k);
    1371            if (!BlockRail.isRailBlock(id))
    1372            {
    1373                    return;
    1374            }
    1375            float railMaxSpeed = ((BlockRail)Block.blocksList[id]).getRailMaxSpeed(worldObj, this, i, j, k);
    1376
    1377            double maxSpeed = Math.min(railMaxSpeed, getMaxSpeedRail());
    1378            double mX = motionX;
    1379            double mZ = motionZ;
    1380            if(riddenByEntity != null)
    1381            {
    1382                mX *= 0.75D;
    1383                mZ *= 0.75D;
    1384            }
    1385            if(mX < -maxSpeed) mX = -maxSpeed;
    1386            if(mX >  maxSpeed) mX =  maxSpeed;
    1387            if(mZ < -maxSpeed) mZ = -maxSpeed;
    1388            if(mZ >  maxSpeed) mZ =  maxSpeed;
    1389            moveEntity(mX, 0.0D, mZ);
    1390        }
    1391
    1392        /**
    1393         * Moved to allow overrides.
    1394         * This code handles minecart movement and speed capping when not on a rail.
    1395         */
    1396        protected void moveMinecartOffRail(int i, int j, int k)
    1397        {
    1398            double d2 = getMaxSpeedGround();
    1399            if(!onGround)
    1400            {
    1401                d2 = getMaxSpeedAirLateral();
    1402            }
    1403            if(motionX < -d2) motionX = -d2;
    1404            if(motionX >  d2) motionX =  d2;
    1405            if(motionZ < -d2) motionZ = -d2;
    1406            if(motionZ >  d2) motionZ =  d2;
    1407            double moveY = motionY;
    1408            if(getMaxSpeedAirVertical() > 0 && motionY > getMaxSpeedAirVertical())
    1409            {
    1410                moveY = getMaxSpeedAirVertical();
    1411                if(Math.abs(motionX) < 0.3f && Math.abs(motionZ) < 0.3f)
    1412                {
    1413                    moveY = 0.15f;
    1414                    motionY = moveY;
    1415                }
    1416            }
    1417            if(onGround)
    1418            {
    1419                motionX *= 0.5D;
    1420                motionY *= 0.5D;
    1421                motionZ *= 0.5D;
    1422            }
    1423            moveEntity(motionX, moveY, motionZ);
    1424            if(!onGround)
    1425            {
    1426                motionX *= getDragAir();
    1427                motionY *= getDragAir();
    1428                motionZ *= getDragAir();
    1429            }
    1430        }
    1431
    1432        /**
    1433         * Moved to allow overrides.
    1434         * This code applies fuel consumption.
    1435         */
    1436        protected void updateFuel()
    1437        {
    1438            if (fuel > 0) fuel--;
    1439            if (fuel <= 0) pushX = pushZ = 0.0D;
    1440            setMinecartPowered(fuel > 0);
    1441        }
    1442
    1443        /**
    1444         * Moved to allow overrides, This code handle slopes affecting velocity.
    1445         * @param metadata The blocks position metadata
    1446         */
    1447        protected void adjustSlopeVelocities(int metadata)
    1448        {
    1449            double acceleration = 0.0078125D;
    1450            if (metadata == 2)
    1451            {
    1452                motionX -= acceleration;
    1453            }
    1454            else if (metadata == 3)
    1455            {
    1456                motionX += acceleration;
    1457            }
    1458            else if (metadata == 4)
    1459            {
    1460                motionZ += acceleration;
    1461            }
    1462            else if (metadata == 5)
    1463            {
    1464                motionZ -= acceleration;
    1465            }
    1466        }
    1467
    1468        /**
    1469         * Getters/setters for physics variables
    1470         */
    1471
    1472        /**
    1473         * Returns the carts max speed.
    1474         * Carts going faster than 1.1 cause issues with chunk loading.
    1475         * Carts cant traverse slopes or corners at greater than 0.5 - 0.6.
    1476         * This value is compared with the rails max speed to determine
    1477         * the carts current max speed. A normal rails max speed is 0.4.
    1478         * @return Carts max speed.
    1479         */
    1480        public float getMaxSpeedRail()
    1481        {
    1482            return maxSpeedRail;
    1483        }
    1484
    1485        public void setMaxSpeedRail(float value)
    1486        {
    1487            maxSpeedRail = value;
    1488        }
    1489
    1490        public float getMaxSpeedGround()
    1491        {
    1492            return maxSpeedGround;
    1493        }
    1494
    1495        public void setMaxSpeedGround(float value)
    1496        {
    1497            maxSpeedGround = value;
    1498        }
    1499
    1500        public float getMaxSpeedAirLateral()
    1501        {
    1502            return maxSpeedAirLateral;
    1503        }
    1504
    1505        public void setMaxSpeedAirLateral(float value)
    1506        {
    1507            maxSpeedAirLateral = value;
    1508        }
    1509
    1510        public float getMaxSpeedAirVertical()
    1511        {
    1512            return maxSpeedAirVertical;
    1513        }
    1514
    1515        public void setMaxSpeedAirVertical(float value)
    1516        {
    1517            maxSpeedAirVertical = value;
    1518        }
    1519
    1520        public double getDragAir()
    1521        {
    1522            return dragAir;
    1523        }
    1524
    1525        public void setDragAir(double value)
    1526        {
    1527            dragAir = value;
    1528        }
    1529    }