Java Code Examples for cn.nukkit.math.MathHelper#floor()

The following examples show how to use cn.nukkit.math.MathHelper#floor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityMinecartAbstract.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onUpdate(int currentTick) {
    if (this.closed) {
        return false;
    }

    if (!this.isAlive()) {
        ++this.deadTicks;
        if (this.deadTicks >= 10) {
            this.despawnFromAll();
            this.close();
        }
        return this.deadTicks < 10;
    }

    int tickDiff = currentTick - this.lastUpdate;

    if (tickDiff <= 0) {
        return false;
    }

    this.lastUpdate = currentTick;

    if (isAlive()) {
        super.onUpdate(currentTick);

        // Entity variables
        lastX = x;
        lastY = y;
        lastZ = z;
        motionY -= 0.03999999910593033D;
        int dx = MathHelper.floor(x);
        int dy = MathHelper.floor(y);
        int dz = MathHelper.floor(z);

        // Some hack to check rails
        if (Rail.isRailBlock(level.getBlockIdAt(dx, dy - 1, dz))) {
            --dy;
        }

        Block block = level.getBlock(new Vector3(dx, dy, dz));

        // Ensure that the block is a rail
        if (Rail.isRailBlock(block)) {
            processMovement(dx, dy, dz, (BlockRail) block);
            if (block instanceof BlockRailActivator) {
                // Activate the minecart/TNT
                activate(dx, dy, dz, (block.getDamage() & 0x8) != 0);
            }
        } else {
            setFalling();
        }
        checkBlockCollision();

        // Minecart head
        pitch = 0;
        double diffX = this.lastX - this.x;
        double diffZ = this.lastZ - this.z;
        double yawToChange = yaw;
        if (diffX * diffX + diffZ * diffZ > 0.001D) {
            yawToChange = (Math.atan2(diffZ, diffX) * 180 / 3.141592653589793D);
        }
        
        // Reverse yaw if yaw is below 0
        if (yawToChange < 0) {
            // -90-(-90)-(-90) = 90
            yawToChange -= yawToChange - yawToChange;
        }
        
        setRotation(yawToChange, pitch);

        Location from = new Location(lastX, lastY, lastZ, lastYaw, lastPitch, level);
        Location to = new Location(this.x, this.y, this.z, this.yaw, this.pitch, level);

        this.getServer().getPluginManager().callEvent(new VehicleUpdateEvent(this));

        if (!from.equals(to)) {
            this.getServer().getPluginManager().callEvent(new VehicleMoveEvent(this, from, to));
        }

        // Collisions
        for (Entity entity : level.getNearbyEntities(boundingBox.grow(0.2D, 0, 0.2D), this)) {
            if (entity != linkedEntity && entity instanceof EntityMinecartAbstract) {
                entity.applyEntityCollision(this);
            }
        }

        // Easier
        if ((linkedEntity != null) && (!linkedEntity.isAlive())) {
            if (linkedEntity.riding == this) {
                linkedEntity.riding = null;
            }
            linkedEntity = null;
        }
        // No need to onGround or Motion diff! This always have an update
        return true;
    }
    return false;
}
 
Example 2
Source File: EntityMinecartAbstract.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
private Vector3 getNextRail(double dx, double dy, double dz) {
    int checkX = MathHelper.floor(dx);
    int checkY = MathHelper.floor(dy);
    int checkZ = MathHelper.floor(dz);

    if (Rail.isRailBlock(level.getBlockIdAt(checkX, checkY - 1, checkZ))) {
        --checkY;
    }

    Block block = level.getBlock(new Vector3(checkX, checkY, checkZ));

    if (Rail.isRailBlock(block)) {
        int[][] facing = matrix[((BlockRail) block).getRealMeta()];
        double rail;
        // Genisys mistake (Doesn't check surrounding more exactly)
        double nextOne = (double) checkX + 0.5D + (double) facing[0][0] * 0.5D;
        double nextTwo = (double) checkY + 0.5D + (double) facing[0][1] * 0.5D;
        double nextThree = (double) checkZ + 0.5D + (double) facing[0][2] * 0.5D;
        double nextFour = (double) checkX + 0.5D + (double) facing[1][0] * 0.5D;
        double nextFive = (double) checkY + 0.5D + (double) facing[1][1] * 0.5D;
        double nextSix = (double) checkZ + 0.5D + (double) facing[1][2] * 0.5D;
        double nextSeven = nextFour - nextOne;
        double nextEight = (nextFive - nextTwo) * 2;
        double nextMax = nextSix - nextThree;

        if (nextSeven == 0) {
            rail = dz - (double) checkZ;
        } else if (nextMax == 0) {
            rail = dx - (double) checkX;
        } else {
            double whatOne = dx - nextOne;
            double whatTwo = dz - nextThree;

            rail = (whatOne * nextSeven + whatTwo * nextMax) * 2;
        }

        dx = nextOne + nextSeven * rail;
        dy = nextTwo + nextEight * rail;
        dz = nextThree + nextMax * rail;
        if (nextEight < 0) {
            ++dy;
        }

        if (nextEight > 0) {
            dy += 0.5D;
        }

        return new Vector3(dx, dy, dz);
    } else {
        return null;
    }
}
 
Example 3
Source File: ChunkPosition.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public ChunkPosition(Vector3 vec3d) {
    this(MathHelper.floor(vec3d.x), MathHelper.floor(vec3d.y), MathHelper.floor(vec3d.z));
}
 
Example 4
Source File: EntityMinecartAbstract.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
private Vector3 getNextRail(double dx, double dy, double dz) {
    int checkX = MathHelper.floor(dx);
    int checkY = MathHelper.floor(dy);
    int checkZ = MathHelper.floor(dz);

    if (Rail.isRailBlock(level.getBlockIdAt(checkX, checkY - 1, checkZ))) {
        --checkY;
    }

    Block block = level.getBlock(new Vector3(checkX, checkY, checkZ));

    if (Rail.isRailBlock(block)) {
        int[][] facing = matrix[((BlockRail) block).getRealMeta()];
        double rail;
        // Genisys mistake (Doesn't check surrounding more exactly)
        double nextOne = (double) checkX + 0.5D + (double) facing[0][0] * 0.5D;
        double nextTwo = (double) checkY + 0.5D + (double) facing[0][1] * 0.5D;
        double nextThree = (double) checkZ + 0.5D + (double) facing[0][2] * 0.5D;
        double nextFour = (double) checkX + 0.5D + (double) facing[1][0] * 0.5D;
        double nextFive = (double) checkY + 0.5D + (double) facing[1][1] * 0.5D;
        double nextSix = (double) checkZ + 0.5D + (double) facing[1][2] * 0.5D;
        double nextSeven = nextFour - nextOne;
        double nextEight = (nextFive - nextTwo) * 2;
        double nextMax = nextSix - nextThree;

        if (nextSeven == 0) {
            rail = dz - (double) checkZ;
        } else if (nextMax == 0) {
            rail = dx - (double) checkX;
        } else {
            double whatOne = dx - nextOne;
            double whatTwo = dz - nextThree;

            rail = (whatOne * nextSeven + whatTwo * nextMax) * 2;
        }

        dx = nextOne + nextSeven * rail;
        dy = nextTwo + nextEight * rail;
        dz = nextThree + nextMax * rail;
        if (nextEight < 0) {
            ++dy;
        }

        if (nextEight > 0) {
            dy += 0.5D;
        }

        return new Vector3(dx, dy, dz);
    } else {
        return null;
    }
}
 
Example 5
Source File: OreType.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean spawn(ChunkManager level, NukkitRandom rand, int replaceId, int x, int y, int z) {
    float piScaled = rand.nextFloat() * (float) Math.PI;
    double scaleMaxX = (double) ((float) (x + 8) + MathHelper.sin(piScaled) * (float) clusterSize / 8.0F);
    double scaleMinX = (double) ((float) (x + 8) - MathHelper.sin(piScaled) * (float) clusterSize / 8.0F);
    double scaleMaxZ = (double) ((float) (z + 8) + MathHelper.cos(piScaled) * (float) clusterSize / 8.0F);
    double scaleMinZ = (double) ((float) (z + 8) - MathHelper.cos(piScaled) * (float) clusterSize / 8.0F);
    double scaleMaxY = (double) (y + rand.nextBoundedInt(3) - 2);
    double scaleMinY = (double) (y + rand.nextBoundedInt(3) - 2);

    for (int i = 0; i < clusterSize; ++i) {
        float sizeIncr = (float) i / (float) clusterSize;
        double scaleX = scaleMaxX + (scaleMinX - scaleMaxX) * (double) sizeIncr;
        double scaleY = scaleMaxY + (scaleMinY - scaleMaxY) * (double) sizeIncr;
        double scaleZ = scaleMaxZ + (scaleMinZ - scaleMaxZ) * (double) sizeIncr;
        double randSizeOffset = rand.nextDouble() * (double) clusterSize / 16.0D;
        double randVec1 = (double) (MathHelper.sin((float) Math.PI * sizeIncr) + 1.0F) * randSizeOffset + 1.0D;
        double randVec2 = (double) (MathHelper.sin((float) Math.PI * sizeIncr) + 1.0F) * randSizeOffset + 1.0D;
        int minX = MathHelper.floor(scaleX - randVec1 / 2.0D);
        int minY = MathHelper.floor(scaleY - randVec2 / 2.0D);
        int minZ = MathHelper.floor(scaleZ - randVec1 / 2.0D);
        int maxX = MathHelper.floor(scaleX + randVec1 / 2.0D);
        int maxY = MathHelper.floor(scaleY + randVec2 / 2.0D);
        int maxZ = MathHelper.floor(scaleZ + randVec1 / 2.0D);

        for (int xSeg = minX; xSeg <= maxX; ++xSeg) {
            double xVal = ((double) xSeg + 0.5D - scaleX) / (randVec1 / 2.0D);

            if (xVal * xVal < 1.0D) {
                for (int ySeg = minY; ySeg <= maxY; ++ySeg) {
                    double yVal = ((double) ySeg + 0.5D - scaleY) / (randVec2 / 2.0D);

                    if (xVal * xVal + yVal * yVal < 1.0D) {
                        for (int zSeg = minZ; zSeg <= maxZ; ++zSeg) {
                            double zVal = ((double) zSeg + 0.5D - scaleZ) / (randVec1 / 2.0D);

                            if (xVal * xVal + yVal * yVal + zVal * zVal < 1.0D) {
                                if (level.getBlockIdAt(xSeg, ySeg, zSeg) == replaceBlockId) {
                                    level.setBlockFullIdAt(xSeg, ySeg, zSeg, fullId);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    return true;
}
 
Example 6
Source File: ChunkPosition.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public ChunkPosition(Vector3 vec3d) {
    this(MathHelper.floor(vec3d.x), MathHelper.floor(vec3d.y), MathHelper.floor(vec3d.z));
}
 
Example 7
Source File: EntityMinecartAbstract.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onUpdate(int currentTick) {
    if (this.closed) {
        return false;
    }

    if (!this.isAlive()) {
        ++this.deadTicks;
        if (this.deadTicks >= 10) {
            this.despawnFromAll();
            this.close();
        }
        return this.deadTicks < 10;
    }

    int tickDiff = currentTick - this.lastUpdate;

    if (tickDiff <= 0) {
        return false;
    }

    this.lastUpdate = currentTick;

    if (isAlive()) {
        super.onUpdate(currentTick);

        // Entity variables
        lastX = x;
        lastY = y;
        lastZ = z;
        motionY -= 0.03999999910593033D;
        int dx = MathHelper.floor(x);
        int dy = MathHelper.floor(y);
        int dz = MathHelper.floor(z);

        // Some hack to check rails
        if (Rail.isRailBlock(level.getBlockIdAt(dx, dy - 1, dz))) {
            --dy;
        }

        Block block = level.getBlock(new Vector3(dx, dy, dz));

        // Ensure that the block is a rail
        if (Rail.isRailBlock(block)) {
            processMovement(dx, dy, dz, (BlockRail) block);
            if (block instanceof BlockRailActivator) {
                // Activate the minecart/TNT
                activate(dx, dy, dz, (block.getDamage() & 0x8) != 0);
            }
        } else {
            setFalling();
        }
        checkBlockCollision();

        // Minecart head
        pitch = 0;
        double diffX = this.lastX - this.x;
        double diffZ = this.lastZ - this.z;
        double yawToChange = yaw;
        if (diffX * diffX + diffZ * diffZ > 0.001D) {
            yawToChange = (Math.atan2(diffZ, diffX) * 180 / 3.141592653589793D);
        }

        // Reverse yaw if yaw is below 0
        if (yawToChange < 0) {
            // -90-(-90)-(-90) = 90
            yawToChange -= yawToChange - yawToChange;
        }

        setRotation(yawToChange, pitch);

        Location from = new Location(lastX, lastY, lastZ, lastYaw, lastPitch, level);
        Location to = new Location(this.x, this.y, this.z, this.yaw, this.pitch, level);

        this.getServer().getPluginManager().callEvent(new VehicleUpdateEvent(this));

        if (!from.equals(to)) {
            this.getServer().getPluginManager().callEvent(new VehicleMoveEvent(this, from, to));
        }

        // Collisions
        for (Entity entity : level.getNearbyEntities(boundingBox.grow(0.2D, 0, 0.2D), this)) {
            if (entity != linkedEntity && entity instanceof EntityMinecartAbstract) {
                entity.applyEntityCollision(this);
            }
        }

        // Easier
        if ((linkedEntity != null) && (!linkedEntity.isAlive())) {
            if (linkedEntity.riding == this) {
                linkedEntity.riding = null;
            }
            linkedEntity = null;
        }
        // No need to onGround or Motion diff! This always have an update
        return true;
    }
    return false;
}
 
Example 8
Source File: EntityMinecartAbstract.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
private Vector3 getNextRail(double dx, double dy, double dz) {
    int checkX = MathHelper.floor(dx);
    int checkY = MathHelper.floor(dy);
    int checkZ = MathHelper.floor(dz);

    if (Rail.isRailBlock(level.getBlockIdAt(checkX, checkY - 1, checkZ))) {
        --checkY;
    }

    Block block = level.getBlock(new Vector3(checkX, checkY, checkZ));

    if (Rail.isRailBlock(block)) {
        int[][] facing = matrix[((BlockRail) block).getRealMeta()];
        double rail;
        // Genisys mistake (Doesn't check surrounding more exactly)
        double nextOne = (double) checkX + 0.5D + (double) facing[0][0] * 0.5D;
        double nextTwo = (double) checkY + 0.5D + (double) facing[0][1] * 0.5D;
        double nextThree = (double) checkZ + 0.5D + (double) facing[0][2] * 0.5D;
        double nextFour = (double) checkX + 0.5D + (double) facing[1][0] * 0.5D;
        double nextFive = (double) checkY + 0.5D + (double) facing[1][1] * 0.5D;
        double nextSix = (double) checkZ + 0.5D + (double) facing[1][2] * 0.5D;
        double nextSeven = nextFour - nextOne;
        double nextEight = (nextFive - nextTwo) * 2;
        double nextMax = nextSix - nextThree;

        if (nextSeven == 0) {
            rail = dz - (double) checkZ;
        } else if (nextMax == 0) {
            rail = dx - (double) checkX;
        } else {
            double whatOne = dx - nextOne;
            double whatTwo = dz - nextThree;

            rail = (whatOne * nextSeven + whatTwo * nextMax) * 2;
        }

        dx = nextOne + nextSeven * rail;
        dy = nextTwo + nextEight * rail;
        dz = nextThree + nextMax * rail;
        if (nextEight < 0) {
            ++dy;
        }

        if (nextEight > 0) {
            dy += 0.5D;
        }

        return new Vector3(dx, dy, dz);
    } else {
        return null;
    }
}
 
Example 9
Source File: ChunkPosition.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public ChunkPosition(Vector3 vec3d) {
    this(MathHelper.floor(vec3d.x), MathHelper.floor(vec3d.y), MathHelper.floor(vec3d.z));
}