cn.nukkit.block.BlockRailActivator Java Examples

The following examples show how to use cn.nukkit.block.BlockRailActivator. 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 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;
}