Search in sources :

Example 1 with PoweredRailBlock

use of net.minecraft.block.PoweredRailBlock in project minecolonies by ldtteam.

the class MinecoloniesMinecart method moveAlongTrack.

@Override
protected void moveAlongTrack(BlockPos pos, BlockState state) {
    this.fallDistance = 0.0F;
    double x = this.getX();
    double y = this.getY();
    double z = this.getZ();
    Vector3d posVec = this.getPos(x, y, z);
    y = pos.getY();
    boolean isPowered = false;
    boolean flag = false;
    AbstractRailBlock abstractrailblock = (AbstractRailBlock) state.getBlock();
    if (abstractrailblock instanceof PoweredRailBlock && !((PoweredRailBlock) abstractrailblock).isActivatorRail()) {
        isPowered = state.getValue(PoweredRailBlock.POWERED);
        flag = !isPowered;
    }
    RailShape railshape = ((AbstractRailBlock) state.getBlock()).getRailDirection(state, this.level, pos, this);
    switch(railshape) {
        case ASCENDING_EAST:
        case ASCENDING_WEST:
        case ASCENDING_NORTH:
        case ASCENDING_SOUTH:
            ++y;
            break;
        default:
            break;
    }
    Vector3d motion = this.getDeltaMovement();
    Pair<Vector3i, Vector3i> pair = getShapeMatrix(railshape);
    Vector3i vecIn = pair.getFirst();
    Vector3i vecOut = pair.getSecond();
    double xDif = (vecOut.getX() - vecIn.getX());
    double zDif = (vecOut.getZ() - vecIn.getZ());
    double difSq = Math.sqrt(xDif * xDif + zDif * zDif);
    double difMotion = motion.x * xDif + motion.z * zDif;
    if (difMotion < 0.0D) {
        xDif = -xDif;
        zDif = -zDif;
    }
    double veloc = Math.min(2.0D, Math.sqrt(getHorizontalDistanceSqr(motion)));
    motion = new Vector3d(veloc * xDif / difSq, motion.y, veloc * zDif / difSq);
    this.setDeltaMovement(motion);
    if (flag && shouldDoRailFunctions()) {
        double tempMot = Math.sqrt(getHorizontalDistanceSqr(this.getDeltaMovement()));
        if (tempMot < 0.03D) {
            this.setDeltaMovement(Vector3d.ZERO);
        } else {
            this.setDeltaMovement(this.getDeltaMovement().multiply(0.5D, 0.0D, 0.5D));
        }
    }
    double xInDif = (double) pos.getX() + 0.5D + (double) vecIn.getX() * 0.5D;
    double zInDif = (double) pos.getZ() + 0.5D + (double) vecIn.getZ() * 0.5D;
    double xOutDif = (double) pos.getX() + 0.5D + (double) vecOut.getX() * 0.5D;
    double zOutDif = (double) pos.getZ() + 0.5D + (double) vecOut.getZ() * 0.5D;
    xDif = xOutDif - xInDif;
    zDif = zOutDif - zInDif;
    double xzDif;
    if (xDif == 0.0D) {
        xzDif = z - (double) pos.getZ();
    } else if (zDif == 0.0D) {
        xzDif = x - (double) pos.getX();
    } else {
        double d15 = x - xInDif;
        double d16 = z - zInDif;
        xzDif = (d15 * xDif + d16 * zDif) * 2.0D;
    }
    x = xInDif + xDif * xzDif;
    z = zInDif + zDif * xzDif;
    this.setPos(x, y, z);
    this.moveMinecartOnRail(pos);
    if (vecIn.getY() != 0 && MathHelper.floor(this.getX()) - pos.getX() == vecIn.getX() && MathHelper.floor(this.getZ()) - pos.getZ() == vecIn.getZ()) {
        this.setPos(this.getX(), this.getY() + (double) vecIn.getY(), this.getZ());
    } else if (vecOut.getY() != 0 && MathHelper.floor(this.getX()) - pos.getX() == vecOut.getX() && MathHelper.floor(this.getZ()) - pos.getZ() == vecOut.getZ()) {
        this.setPos(this.getX(), this.getY() + (double) vecOut.getY(), this.getZ());
    }
    this.applyNaturalSlowdown();
    Vector3d newPos = this.getPos(this.getX(), this.getY(), this.getZ());
    if (newPos != null && posVec != null) {
        double yMot = (posVec.y - newPos.y) * 0.05D;
        Vector3d tempMot = this.getDeltaMovement();
        double tempVeloc = Math.sqrt(getHorizontalDistanceSqr(tempMot));
        if (tempVeloc > 0.0D) {
            this.setDeltaMovement(tempMot.multiply((tempVeloc + yMot) / tempVeloc, 1.0D, (tempVeloc + yMot) / tempVeloc));
        }
        this.setPos(this.getX(), newPos.y, this.getZ());
    }
    int xFloor = MathHelper.floor(this.getX());
    int zFloor = MathHelper.floor(this.getZ());
    if (xFloor != pos.getX() || zFloor != pos.getZ()) {
        Vector3d tempMot = this.getDeltaMovement();
        double temoVeloc = Math.sqrt(getHorizontalDistanceSqr(tempMot));
        this.setDeltaMovement(temoVeloc * (double) (xFloor - pos.getX()), tempMot.y, temoVeloc * (double) (zFloor - pos.getZ()));
    }
    if (shouldDoRailFunctions()) {
        ((AbstractRailBlock) state.getBlock()).onMinecartPass(state, level, pos, this);
    }
    if (isPowered && shouldDoRailFunctions()) {
        Vector3d tempMot = this.getDeltaMovement();
        double tempVeloc = Math.sqrt(getHorizontalDistanceSqr(tempMot));
        if (tempVeloc > 0.01D) {
            this.setDeltaMovement(tempMot.add(tempMot.x / tempVeloc * 0.06D, 0.0D, tempMot.z / tempVeloc * 0.06D));
        } else {
            Vector3d mot = this.getDeltaMovement();
            double tempX = mot.x;
            double tempZ = mot.z;
            if (railshape == RailShape.EAST_WEST) {
                if (this.isNormalCube(pos.west())) {
                    tempX = 0.02D;
                } else if (this.isNormalCube(pos.east())) {
                    tempX = -0.02D;
                }
            } else {
                if (railshape != RailShape.NORTH_SOUTH) {
                    return;
                }
                if (this.isNormalCube(pos.north())) {
                    tempZ = 0.02D;
                } else if (this.isNormalCube(pos.south())) {
                    tempZ = -0.02D;
                }
            }
            this.setDeltaMovement(tempX, mot.y, tempZ);
        }
    }
}
Also used : PoweredRailBlock(net.minecraft.block.PoweredRailBlock) Vector3d(net.minecraft.util.math.vector.Vector3d) RailShape(net.minecraft.state.properties.RailShape) Vector3i(net.minecraft.util.math.vector.Vector3i) AbstractRailBlock(net.minecraft.block.AbstractRailBlock)

Example 2 with PoweredRailBlock

use of net.minecraft.block.PoweredRailBlock in project minecolonies by Minecolonies.

the class MinecoloniesMinecart method moveAlongTrack.

@Override
protected void moveAlongTrack(BlockPos pos, BlockState state) {
    this.fallDistance = 0.0F;
    double x = this.getX();
    double y = this.getY();
    double z = this.getZ();
    Vector3d posVec = this.getPos(x, y, z);
    y = pos.getY();
    boolean isPowered = false;
    boolean flag = false;
    AbstractRailBlock abstractrailblock = (AbstractRailBlock) state.getBlock();
    if (abstractrailblock instanceof PoweredRailBlock && !((PoweredRailBlock) abstractrailblock).isActivatorRail()) {
        isPowered = state.getValue(PoweredRailBlock.POWERED);
        flag = !isPowered;
    }
    RailShape railshape = ((AbstractRailBlock) state.getBlock()).getRailDirection(state, this.level, pos, this);
    switch(railshape) {
        case ASCENDING_EAST:
        case ASCENDING_WEST:
        case ASCENDING_NORTH:
        case ASCENDING_SOUTH:
            ++y;
            break;
        default:
            break;
    }
    Vector3d motion = this.getDeltaMovement();
    Pair<Vector3i, Vector3i> pair = getShapeMatrix(railshape);
    Vector3i vecIn = pair.getFirst();
    Vector3i vecOut = pair.getSecond();
    double xDif = (vecOut.getX() - vecIn.getX());
    double zDif = (vecOut.getZ() - vecIn.getZ());
    double difSq = Math.sqrt(xDif * xDif + zDif * zDif);
    double difMotion = motion.x * xDif + motion.z * zDif;
    if (difMotion < 0.0D) {
        xDif = -xDif;
        zDif = -zDif;
    }
    double veloc = Math.min(2.0D, Math.sqrt(getHorizontalDistanceSqr(motion)));
    motion = new Vector3d(veloc * xDif / difSq, motion.y, veloc * zDif / difSq);
    this.setDeltaMovement(motion);
    if (flag && shouldDoRailFunctions()) {
        double tempMot = Math.sqrt(getHorizontalDistanceSqr(this.getDeltaMovement()));
        if (tempMot < 0.03D) {
            this.setDeltaMovement(Vector3d.ZERO);
        } else {
            this.setDeltaMovement(this.getDeltaMovement().multiply(0.5D, 0.0D, 0.5D));
        }
    }
    double xInDif = (double) pos.getX() + 0.5D + (double) vecIn.getX() * 0.5D;
    double zInDif = (double) pos.getZ() + 0.5D + (double) vecIn.getZ() * 0.5D;
    double xOutDif = (double) pos.getX() + 0.5D + (double) vecOut.getX() * 0.5D;
    double zOutDif = (double) pos.getZ() + 0.5D + (double) vecOut.getZ() * 0.5D;
    xDif = xOutDif - xInDif;
    zDif = zOutDif - zInDif;
    double xzDif;
    if (xDif == 0.0D) {
        xzDif = z - (double) pos.getZ();
    } else if (zDif == 0.0D) {
        xzDif = x - (double) pos.getX();
    } else {
        double d15 = x - xInDif;
        double d16 = z - zInDif;
        xzDif = (d15 * xDif + d16 * zDif) * 2.0D;
    }
    x = xInDif + xDif * xzDif;
    z = zInDif + zDif * xzDif;
    this.setPos(x, y, z);
    this.moveMinecartOnRail(pos);
    if (vecIn.getY() != 0 && MathHelper.floor(this.getX()) - pos.getX() == vecIn.getX() && MathHelper.floor(this.getZ()) - pos.getZ() == vecIn.getZ()) {
        this.setPos(this.getX(), this.getY() + (double) vecIn.getY(), this.getZ());
    } else if (vecOut.getY() != 0 && MathHelper.floor(this.getX()) - pos.getX() == vecOut.getX() && MathHelper.floor(this.getZ()) - pos.getZ() == vecOut.getZ()) {
        this.setPos(this.getX(), this.getY() + (double) vecOut.getY(), this.getZ());
    }
    this.applyNaturalSlowdown();
    Vector3d newPos = this.getPos(this.getX(), this.getY(), this.getZ());
    if (newPos != null && posVec != null) {
        double yMot = (posVec.y - newPos.y) * 0.05D;
        Vector3d tempMot = this.getDeltaMovement();
        double tempVeloc = Math.sqrt(getHorizontalDistanceSqr(tempMot));
        if (tempVeloc > 0.0D) {
            this.setDeltaMovement(tempMot.multiply((tempVeloc + yMot) / tempVeloc, 1.0D, (tempVeloc + yMot) / tempVeloc));
        }
        this.setPos(this.getX(), newPos.y, this.getZ());
    }
    int xFloor = MathHelper.floor(this.getX());
    int zFloor = MathHelper.floor(this.getZ());
    if (xFloor != pos.getX() || zFloor != pos.getZ()) {
        Vector3d tempMot = this.getDeltaMovement();
        double temoVeloc = Math.sqrt(getHorizontalDistanceSqr(tempMot));
        this.setDeltaMovement(temoVeloc * (double) (xFloor - pos.getX()), tempMot.y, temoVeloc * (double) (zFloor - pos.getZ()));
    }
    if (shouldDoRailFunctions()) {
        ((AbstractRailBlock) state.getBlock()).onMinecartPass(state, level, pos, this);
    }
    if (isPowered && shouldDoRailFunctions()) {
        Vector3d tempMot = this.getDeltaMovement();
        double tempVeloc = Math.sqrt(getHorizontalDistanceSqr(tempMot));
        if (tempVeloc > 0.01D) {
            this.setDeltaMovement(tempMot.add(tempMot.x / tempVeloc * 0.06D, 0.0D, tempMot.z / tempVeloc * 0.06D));
        } else {
            Vector3d mot = this.getDeltaMovement();
            double tempX = mot.x;
            double tempZ = mot.z;
            if (railshape == RailShape.EAST_WEST) {
                if (this.isNormalCube(pos.west())) {
                    tempX = 0.02D;
                } else if (this.isNormalCube(pos.east())) {
                    tempX = -0.02D;
                }
            } else {
                if (railshape != RailShape.NORTH_SOUTH) {
                    return;
                }
                if (this.isNormalCube(pos.north())) {
                    tempZ = 0.02D;
                } else if (this.isNormalCube(pos.south())) {
                    tempZ = -0.02D;
                }
            }
            this.setDeltaMovement(tempX, mot.y, tempZ);
        }
    }
}
Also used : PoweredRailBlock(net.minecraft.block.PoweredRailBlock) Vector3d(net.minecraft.util.math.vector.Vector3d) RailShape(net.minecraft.state.properties.RailShape) Vector3i(net.minecraft.util.math.vector.Vector3i) AbstractRailBlock(net.minecraft.block.AbstractRailBlock)

Aggregations

AbstractRailBlock (net.minecraft.block.AbstractRailBlock)2 PoweredRailBlock (net.minecraft.block.PoweredRailBlock)2 RailShape (net.minecraft.state.properties.RailShape)2 Vector3d (net.minecraft.util.math.vector.Vector3d)2 Vector3i (net.minecraft.util.math.vector.Vector3i)2