Search in sources :

Example 1 with Location

use of cn.nukkit.level.Location 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 Location

use of cn.nukkit.level.Location in project Nukkit by Nukkit.

the class TeleportCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length < 1 || args.length > 6) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    CommandSender target;
    CommandSender origin = sender;
    if (args.length == 1 || args.length == 3) {
        if (sender instanceof Player) {
            target = sender;
        } else {
            sender.sendMessage(new TranslationContainer("commands.generic.ingame"));
            return true;
        }
        if (args.length == 1) {
            target = sender.getServer().getPlayer(args[0]);
            if (target == null) {
                sender.sendMessage(TextFormat.RED + "Can't find player " + args[0]);
                return true;
            }
        }
    } else {
        target = sender.getServer().getPlayer(args[0]);
        if (target == null) {
            sender.sendMessage(TextFormat.RED + "Can't find player " + args[0]);
            return true;
        }
        if (args.length == 2) {
            origin = target;
            target = sender.getServer().getPlayer(args[1]);
            if (target == null) {
                sender.sendMessage(TextFormat.RED + "Can't find player " + args[1]);
                return true;
            }
        }
    }
    if (args.length < 3) {
        ((Player) origin).teleport((Player) target, PlayerTeleportEvent.TeleportCause.COMMAND);
        Command.broadcastCommandMessage(sender, new TranslationContainer("commands.tp.success", new String[] { origin.getName(), target.getName() }));
        return true;
    } else if (((Player) target).getLevel() != null) {
        int pos;
        if (args.length == 4 || args.length == 6) {
            pos = 1;
        } else {
            pos = 0;
        }
        double x;
        double y;
        double z;
        double yaw;
        double pitch;
        try {
            x = Double.parseDouble(args[pos++]);
            y = Double.parseDouble(args[pos++]);
            z = Double.parseDouble(args[pos++]);
            yaw = ((Player) target).getYaw();
            pitch = ((Player) target).getPitch();
        } catch (NumberFormatException e1) {
            sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
            return true;
        }
        if (y < 0)
            y = 0;
        if (y > 256)
            y = 256;
        if (args.length == 6 || (args.length == 5 && pos == 3)) {
            yaw = Integer.parseInt(args[pos++]);
            pitch = Integer.parseInt(args[pos++]);
        }
        ((Player) target).teleport(new Location(x, y, z, yaw, pitch, ((Player) target).getLevel()), PlayerTeleportEvent.TeleportCause.COMMAND);
        Command.broadcastCommandMessage(sender, new TranslationContainer("commands.tp.success.coordinates", new String[] { target.getName(), String.valueOf(NukkitMath.round(x, 2)), String.valueOf(NukkitMath.round(y, 2)), String.valueOf(NukkitMath.round(z, 2)) }));
        return true;
    }
    sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
    return true;
}
Also used : Player(cn.nukkit.Player) TranslationContainer(cn.nukkit.lang.TranslationContainer) CommandSender(cn.nukkit.command.CommandSender) Location(cn.nukkit.level.Location)

Example 3 with Location

use of cn.nukkit.level.Location 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)

Example 4 with Location

use of cn.nukkit.level.Location in project Nukkit by Nukkit.

the class Entity method teleport.

public boolean teleport(Location location, PlayerTeleportEvent.TeleportCause cause) {
    double yaw = location.yaw;
    double pitch = location.pitch;
    Location from = this.getLocation();
    Location to = location;
    if (cause != null) {
        EntityTeleportEvent ev = new EntityTeleportEvent(this, from, to);
        this.server.getPluginManager().callEvent(ev);
        if (ev.isCancelled()) {
            return false;
        }
        to = ev.getTo();
    }
    this.ySize = 0;
    this.setMotion(this.temporalVector.setComponents(0, 0, 0));
    if (this.setPositionAndRotation(to, yaw, pitch)) {
        this.resetFallDistance();
        this.onGround = true;
        this.updateMovement();
        return true;
    }
    return false;
}
Also used : Location(cn.nukkit.level.Location)

Aggregations

Location (cn.nukkit.level.Location)4 VehicleMoveEvent (cn.nukkit.event.vehicle.VehicleMoveEvent)2 VehicleUpdateEvent (cn.nukkit.event.vehicle.VehicleUpdateEvent)2 Vector3 (cn.nukkit.math.Vector3)2 Player (cn.nukkit.Player)1 Block (cn.nukkit.block.Block)1 BlockRailActivator (cn.nukkit.block.BlockRailActivator)1 CommandSender (cn.nukkit.command.CommandSender)1 Entity (cn.nukkit.entity.Entity)1 TranslationContainer (cn.nukkit.lang.TranslationContainer)1