Search in sources :

Example 11 with Vector3

use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.

the class BlockRedstoneWire method getIndirectPower.

private int getIndirectPower() {
    int power = 0;
    Vector3 pos = getLocation();
    for (BlockFace face : BlockFace.values()) {
        int blockPower = this.getIndirectPower(pos.getSide(face), face);
        if (blockPower >= 15) {
            return 15;
        }
        if (blockPower > power) {
            power = blockPower;
        }
    }
    return power;
}
Also used : BlockFace(cn.nukkit.math.BlockFace) Vector3(cn.nukkit.math.Vector3)

Example 12 with Vector3

use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.

the class BlockTripWireHook method calculateState.

public void calculateState(boolean onBreak, boolean updateAround, int pos, Block block) {
    BlockFace facing = getFacing();
    Vector3 v = this.getLocation();
    boolean attached = isAttached();
    boolean powered = isPowered();
    boolean canConnect = !onBreak;
    boolean nextPowered = false;
    int distance = 0;
    Block[] blocks = new Block[42];
    for (int i = 1; i < 42; ++i) {
        Vector3 vector = v.getSide(facing, i);
        Block b = this.level.getBlock(vector);
        if (b instanceof BlockTripWireHook) {
            if (((BlockTripWireHook) b).getFacing() == facing.getOpposite()) {
                distance = i;
            }
            break;
        }
        if (b.getId() != Block.TRIPWIRE && i != pos) {
            blocks[i] = null;
            canConnect = false;
        } else {
            if (i == pos) {
                b = block != null ? block : b;
            }
            if (b instanceof BlockTripWire) {
                boolean disarmed = !((BlockTripWire) b).isDisarmed();
                boolean wirePowered = ((BlockTripWire) b).isPowered();
                nextPowered |= disarmed && wirePowered;
                if (i == pos) {
                    this.level.scheduleUpdate(this, 10);
                    canConnect &= disarmed;
                }
            }
            blocks[i] = b;
        }
    }
    canConnect = canConnect & distance > 1;
    nextPowered = nextPowered & canConnect;
    BlockTripWireHook hook = new BlockTripWireHook();
    hook.setAttached(canConnect);
    hook.setPowered(nextPowered);
    if (distance > 0) {
        Vector3 vec = v.getSide(facing, distance);
        BlockFace face = facing.getOpposite();
        hook.setFace(face);
        this.level.setBlock(vec, hook, true, false);
        this.level.updateAroundRedstone(vec, null);
        this.level.updateAroundRedstone(vec.getSide(face.getOpposite()), null);
        this.addSound(vec, canConnect, nextPowered, attached, powered);
    }
    this.addSound(v, canConnect, nextPowered, attached, powered);
    if (!onBreak) {
        hook.setFace(facing);
        this.level.setBlock(v, hook, true, false);
        if (updateAround) {
            this.level.updateAroundRedstone(v, null);
            this.level.updateAroundRedstone(v.getSide(facing.getOpposite()), null);
        }
    }
    if (attached != canConnect) {
        for (int i = 1; i < distance; i++) {
            Vector3 vc = v.getSide(facing, i);
            block = blocks[i];
            if (block != null && this.level.getBlockIdAt(vc.getFloorX(), vc.getFloorY(), vc.getFloorZ()) != Block.AIR) {
                if (canConnect ^ ((block.getDamage() & 0x04) > 0)) {
                    block.setDamage(block.getDamage() ^ 0x04);
                }
                this.level.setBlock(vc, block, true, false);
            }
        }
    }
}
Also used : BlockFace(cn.nukkit.math.BlockFace) Vector3(cn.nukkit.math.Vector3)

Example 13 with Vector3

use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.

the class EntityFallingBlock method onUpdate.

@Override
public boolean onUpdate(int currentTick) {
    if (closed) {
        return false;
    }
    this.timing.startTiming();
    int tickDiff = currentTick - lastUpdate;
    if (tickDiff <= 0 && !justCreated) {
        return true;
    }
    lastUpdate = currentTick;
    boolean hasUpdate = entityBaseTick(tickDiff);
    if (isAlive()) {
        motionY -= getGravity();
        move(motionX, motionY, motionZ);
        float friction = 1 - getDrag();
        motionX *= friction;
        motionY *= 1 - getDrag();
        motionZ *= friction;
        Vector3 pos = (new Vector3(x - 0.5, y, z - 0.5)).round();
        if (onGround) {
            kill();
            Block block = level.getBlock(pos);
            if (block.getId() > 0 && block.isTransparent() && !block.canBeReplaced()) {
                if (this.level.getGameRules().getBoolean(GameRule.DO_ENTITY_DROPS)) {
                    getLevel().dropItem(this, Item.get(this.getBlock(), this.getDamage(), 1));
                }
            } else {
                EntityBlockChangeEvent event = new EntityBlockChangeEvent(this, block, Block.get(getBlock(), getDamage()));
                server.getPluginManager().callEvent(event);
                if (!event.isCancelled()) {
                    getLevel().setBlock(pos, event.getTo(), true);
                    if (event.getTo().getId() == Item.ANVIL) {
                        getLevel().addSound(pos, Sound.RANDOM_ANVIL_LAND);
                    }
                }
            }
            hasUpdate = true;
        }
        updateMovement();
    }
    this.timing.stopTiming();
    return hasUpdate || !onGround || Math.abs(motionX) > 0.00001 || Math.abs(motionY) > 0.00001 || Math.abs(motionZ) > 0.00001;
}
Also used : EntityBlockChangeEvent(cn.nukkit.event.entity.EntityBlockChangeEvent) Block(cn.nukkit.block.Block) Vector3(cn.nukkit.math.Vector3)

Example 14 with Vector3

use of cn.nukkit.math.Vector3 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 15 with Vector3

use of cn.nukkit.math.Vector3 in project Nukkit by Nukkit.

the class EntityMinecartAbstract method applyEntityCollision.

@Override
public void applyEntityCollision(Entity entity) {
    if (entity != riding) {
        if (entity instanceof EntityLiving && !(entity instanceof EntityHuman) && motionX * motionX + motionZ * motionZ > 0.01D && linkedEntity == null && entity.riding == null && blockInside == null) {
            if (riding == null && devs) {
                // TODO: rewrite (weird riding)
                mountEntity(entity);
            }
        }
        double motiveX = entity.x - x;
        double motiveZ = entity.z - z;
        double square = motiveX * motiveX + motiveZ * motiveZ;
        if (square >= 9.999999747378752E-5D) {
            square = Math.sqrt(square);
            motiveX /= square;
            motiveZ /= square;
            double next = 1 / square;
            if (next > 1) {
                next = 1;
            }
            motiveX *= next;
            motiveZ *= next;
            motiveX *= 0.10000000149011612D;
            motiveZ *= 0.10000000149011612D;
            motiveX *= 1 + entityCollisionReduction;
            motiveZ *= 1 + entityCollisionReduction;
            motiveX *= 0.5D;
            motiveZ *= 0.5D;
            if (entity instanceof EntityMinecartAbstract) {
                EntityMinecartAbstract mine = (EntityMinecartAbstract) entity;
                double desinityX = mine.x - x;
                double desinityZ = mine.z - z;
                Vector3 vector = new Vector3(desinityX, 0, desinityZ).normalize();
                Vector3 vec = new Vector3((double) MathHelper.cos((float) yaw * 0.017453292F), 0, (double) MathHelper.sin((float) yaw * 0.017453292F)).normalize();
                double desinityXZ = Math.abs(vector.dot(vec));
                if (desinityXZ < 0.800000011920929D) {
                    return;
                }
                double motX = mine.motionX + motionX;
                double motZ = mine.motionZ + motionZ;
                if (mine.getType().getId() == 2 && getType().getId() != 2) {
                    motionX *= 0.20000000298023224D;
                    motionZ *= 0.20000000298023224D;
                    motionX += mine.motionX - motiveX;
                    motionZ += mine.motionZ - motiveZ;
                    mine.motionX *= 0.949999988079071D;
                    mine.motionZ *= 0.949999988079071D;
                } else if (mine.getType().getId() != 2 && getType().getId() == 2) {
                    mine.motionX *= 0.20000000298023224D;
                    mine.motionZ *= 0.20000000298023224D;
                    motionX += mine.motionX + motiveX;
                    motionZ += mine.motionZ + motiveZ;
                    motionX *= 0.949999988079071D;
                    motionZ *= 0.949999988079071D;
                } else {
                    motX /= 2;
                    motZ /= 2;
                    motionX *= 0.20000000298023224D;
                    motionZ *= 0.20000000298023224D;
                    motionX += motX - motiveX;
                    motionZ += motZ - motiveZ;
                    mine.motionX *= 0.20000000298023224D;
                    mine.motionZ *= 0.20000000298023224D;
                    mine.motionX += motX + motiveX;
                    mine.motionZ += motZ + motiveZ;
                }
            } else {
                motionX -= motiveX;
                motionZ -= motiveZ;
            }
        }
    }
}
Also used : EntityHuman(cn.nukkit.entity.EntityHuman) EntityLiving(cn.nukkit.entity.EntityLiving) Vector3(cn.nukkit.math.Vector3)

Aggregations

Vector3 (cn.nukkit.math.Vector3)62 BlockFace (cn.nukkit.math.BlockFace)16 Block (cn.nukkit.block.Block)5 Player (cn.nukkit.Player)3 Entity (cn.nukkit.entity.Entity)3 BlockIgniteEvent (cn.nukkit.event.block.BlockIgniteEvent)3 Random (java.util.Random)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)3 EntityLiving (cn.nukkit.entity.EntityLiving)2 BlockRedstoneEvent (cn.nukkit.event.block.BlockRedstoneEvent)2 BlockSpreadEvent (cn.nukkit.event.block.BlockSpreadEvent)2 VehicleMoveEvent (cn.nukkit.event.vehicle.VehicleMoveEvent)2 VehicleUpdateEvent (cn.nukkit.event.vehicle.VehicleUpdateEvent)2 ItemBlock (cn.nukkit.item.ItemBlock)2 TranslationContainer (cn.nukkit.lang.TranslationContainer)2 Level (cn.nukkit.level.Level)2 Location (cn.nukkit.level.Location)2 AxisAlignedBB (cn.nukkit.math.AxisAlignedBB)2 NukkitRandom (cn.nukkit.math.NukkitRandom)2 Rail (cn.nukkit.utils.Rail)2