Search in sources :

Example 1 with VehicleUpdateEvent

use of cn.nukkit.event.vehicle.VehicleUpdateEvent in project Nukkit by Nukkit.

the class EntityMinecartAbstract method onUpdate.

@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;
}
Also used : Entity(cn.nukkit.entity.Entity) BlockRailActivator(cn.nukkit.block.BlockRailActivator) VehicleUpdateEvent(cn.nukkit.event.vehicle.VehicleUpdateEvent) VehicleMoveEvent(cn.nukkit.event.vehicle.VehicleMoveEvent) Block(cn.nukkit.block.Block) Vector3(cn.nukkit.math.Vector3) Location(cn.nukkit.level.Location)

Example 2 with VehicleUpdateEvent

use of cn.nukkit.event.vehicle.VehicleUpdateEvent in project Nukkit by Nukkit.

the class EntityBoat method onUpdate.

@Override
public boolean onUpdate(int currentTick) {
    if (this.closed) {
        return false;
    }
    int tickDiff = currentTick - this.lastUpdate;
    if (tickDiff <= 0 && !this.justCreated) {
        return true;
    }
    this.lastUpdate = currentTick;
    boolean hasUpdate = this.entityBaseTick(tickDiff);
    if (this.isAlive()) {
        super.onUpdate(currentTick);
        this.motionY = (this.level.getBlock(new Vector3(this.x, this.y, this.z)).getBoundingBox() != null || this.isInsideOfWater()) ? getGravity() : -0.08;
        if (this.checkObstruction(this.x, this.y, this.z)) {
            hasUpdate = true;
        }
        this.move(this.motionX, this.motionY, this.motionZ);
        double friction = 1 - this.getDrag();
        if (this.onGround && (Math.abs(this.motionX) > 0.00001 || Math.abs(this.motionZ) > 0.00001)) {
            friction *= this.getLevel().getBlock(this.temporalVector.setComponents((int) Math.floor(this.x), (int) Math.floor(this.y - 1), (int) Math.floor(this.z) - 1)).getFrictionFactor();
        }
        this.motionX *= friction;
        this.motionY *= 1 - this.getDrag();
        this.motionZ *= friction;
        if (this.onGround) {
            this.motionY *= -0.5;
        }
        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));
        }
        this.updateMovement();
    }
    return hasUpdate || !this.onGround || Math.abs(this.motionX) > 0.00001 || Math.abs(this.motionY) > 0.00001 || Math.abs(this.motionZ) > 0.00001;
}
Also used : VehicleUpdateEvent(cn.nukkit.event.vehicle.VehicleUpdateEvent) VehicleMoveEvent(cn.nukkit.event.vehicle.VehicleMoveEvent) Vector3(cn.nukkit.math.Vector3) Location(cn.nukkit.level.Location)

Aggregations

VehicleMoveEvent (cn.nukkit.event.vehicle.VehicleMoveEvent)2 VehicleUpdateEvent (cn.nukkit.event.vehicle.VehicleUpdateEvent)2 Location (cn.nukkit.level.Location)2 Vector3 (cn.nukkit.math.Vector3)2 Block (cn.nukkit.block.Block)1 BlockRailActivator (cn.nukkit.block.BlockRailActivator)1 Entity (cn.nukkit.entity.Entity)1