Java Code Examples for cn.nukkit.block.Block#isTransparent()

The following examples show how to use cn.nukkit.block.Block#isTransparent() . 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: Entity.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public boolean isInsideOfSolid() {
    double y = this.y + this.getEyeHeight();
    Block block = this.level.getBlock(
            this.temporalVector.setComponents(
                    NukkitMath.floorDouble(this.x),
                    NukkitMath.floorDouble(y),
                    NukkitMath.floorDouble(this.z))
    );

    AxisAlignedBB bb = block.getBoundingBox();

    return bb != null && block.isSolid() && !block.isTransparent() && bb.intersectsWith(this.getBoundingBox());

}
 
Example 2
Source File: Entity.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public boolean isInsideOfSolid() {
    double y = this.y + this.getEyeHeight();
    Block block = this.level.getBlock(
            this.temporalVector.setComponents(
                    NukkitMath.floorDouble(this.x),
                    NukkitMath.floorDouble(y),
                    NukkitMath.floorDouble(this.z))
    );

    AxisAlignedBB bb = block.getBoundingBox();

    return bb != null && block.isSolid() && !block.isTransparent() && bb.intersectsWith(this.getBoundingBox());

}
 
Example 3
Source File: ItemPainting.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
    FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);

    if (chunk == null) {
        return false;
    }

    if (!target.isTransparent() && face.getIndex() > 1 && !block.isSolid()) {
        int[] direction = {2, 0, 1, 3};
        int[] right = {4, 5, 3, 2};

        List<EntityPainting.Motive> validMotives = new ArrayList<>();
        for (EntityPainting.Motive motive : EntityPainting.motives) {
            boolean valid = true;
            for (int x = 0; x < motive.width && valid; x++) {
                for (int z = 0; z < motive.height && valid; z++) {
                    if (target.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isTransparent() ||
                            target.up(z).isTransparent() ||
                            block.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isSolid() ||
                            block.up(z).isSolid()) {
                        valid = false;
                    }
                }
            }

            if (valid) {
                validMotives.add(motive);
            }
        }

        CompoundTag nbt = new CompoundTag()
                .putByte("Direction", direction[face.getIndex() - 2])
                .putString("Motive", validMotives.get(ThreadLocalRandom.current().nextInt(validMotives.size())).title)
                .putList(new ListTag<DoubleTag>("Pos")
                        .add(new DoubleTag("0", target.x))
                        .add(new DoubleTag("1", target.y))
                        .add(new DoubleTag("2", target.z)))
                .putList(new ListTag<DoubleTag>("Motion")
                        .add(new DoubleTag("0", 0))
                        .add(new DoubleTag("1", 0))
                        .add(new DoubleTag("2", 0)))
                .putList(new ListTag<FloatTag>("Rotation")
                        .add(new FloatTag("0", direction[face.getIndex() - 2] * 90))
                        .add(new FloatTag("1", 0)));

        EntityPainting entity = new EntityPainting(chunk, nbt);

        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }
        entity.spawnToAll();

        return true;
    }

    return false;
}
 
Example 4
Source File: EntityFallingBlock.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@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) {
            close();
            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;
}
 
Example 5
Source File: Level.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public synchronized boolean setBlock(int x, int y, int z, Block block, boolean direct, boolean update) {
        if (y < 0 || y >= 256) {
            return false;
        }
        BaseFullChunk chunk = this.getChunk(x >> 4, z >> 4, true);
        Block blockPrevious;
//        synchronized (chunk) {
        blockPrevious = chunk.getAndSetBlock(x & 0xF, y, z & 0xF, block);
        if (blockPrevious.getFullId() == block.getFullId()) {
            return false;
        }
//        }
        block.x = x;
        block.y = y;
        block.z = z;
        block.level = this;
        int cx = x >> 4;
        int cz = z >> 4;
        long index = Level.chunkHash(cx, cz);
        if (direct) {
            this.sendBlocks(this.getChunkPlayers(cx, cz).values().toArray(new Player[0]), new Block[]{block}, UpdateBlockPacket.FLAG_ALL_PRIORITY);
            this.sendBlocks(this.getChunkPlayers(cx, cz).values().toArray(new Player[0]), new Block[]{Block.get(Block.AIR, 0, block)}, UpdateBlockPacket.FLAG_ALL_PRIORITY, 1);
        } else {
            addBlockChange(index, x, y, z);
        }

        for (ChunkLoader loader : this.getChunkLoaders(cx, cz)) {
            loader.onBlockChanged(block);
        }
        if (update) {
            if (blockPrevious.isTransparent() != block.isTransparent() || blockPrevious.getLightLevel() != block.getLightLevel()) {
                addLightUpdate(x, y, z);
            }
            BlockUpdateEvent ev = new BlockUpdateEvent(block);
            this.server.getPluginManager().callEvent(ev);
            if (!ev.isCancelled()) {
                for (Entity entity : this.getNearbyEntities(new SimpleAxisAlignedBB(x - 1, y - 1, z - 1, x + 1, y + 1, z + 1))) {
                    entity.scheduleUpdate();
                }
                block = ev.getBlock();
                block.onUpdate(BLOCK_UPDATE_NORMAL);
                this.updateAround(x, y, z);
            }
        }
        return true;
    }
 
Example 6
Source File: EntityFallingBlock.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@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;
}
 
Example 7
Source File: ItemPainting.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
    FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);

    if (chunk == null) {
        return false;
    }

    if (!target.isTransparent() && face.getIndex() > 1 && !block.isSolid()) {
        int[] direction = {2, 0, 1, 3};
        int[] right = {4, 5, 3, 2};

        List<EntityPainting.Motive> validMotives = new ArrayList<>();
        for (EntityPainting.Motive motive : EntityPainting.motives) {
            boolean valid = true;
            for (int x = 0; x < motive.width && valid; x++) {
                for (int z = 0; z < motive.height && valid; z++) {
                    if (target.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isTransparent() ||
                            target.up(z).isTransparent() ||
                            block.getSide(BlockFace.fromIndex(right[face.getIndex() - 2]), x).isSolid() ||
                            block.up(z).isSolid()) {
                        valid = false;
                    }
                }
            }

            if (valid) {
                validMotives.add(motive);
            }
        }

        CompoundTag nbt = new CompoundTag()
                .putByte("Direction", direction[face.getIndex() - 2])
                .putString("Motive", validMotives.get(ThreadLocalRandom.current().nextInt(validMotives.size())).title)
                .putList(new ListTag<DoubleTag>("Pos")
                        .add(new DoubleTag("0", target.x))
                        .add(new DoubleTag("1", target.y))
                        .add(new DoubleTag("2", target.z)))
                .putList(new ListTag<DoubleTag>("Motion")
                        .add(new DoubleTag("0", 0))
                        .add(new DoubleTag("1", 0))
                        .add(new DoubleTag("2", 0)))
                .putList(new ListTag<FloatTag>("Rotation")
                        .add(new FloatTag("0", direction[face.getIndex() - 2] * 90))
                        .add(new FloatTag("1", 0)));

        EntityPainting entity = new EntityPainting(chunk, nbt);

        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }
        entity.spawnToAll();

        return true;
    }

    return false;
}
 
Example 8
Source File: Level.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean setBlock(int x, int y, int z, Block block, boolean direct, boolean update) {
        if (y < 0 || y >= 256) {
            return false;
        }
        BaseFullChunk chunk = this.getChunk(x >> 4, z >> 4, true);
        Block blockPrevious;
//        synchronized (chunk) {
        blockPrevious = chunk.getAndSetBlock(x & 0xF, y, z & 0xF, block);
        if (blockPrevious.getFullId() == block.getFullId()) {
            return false;
        }
//        }
        block.x = x;
        block.y = y;
        block.z = z;
        block.level = this;
        int cx = x >> 4;
        int cz = z >> 4;
        long index = Level.chunkHash(cx, cz);
        if (direct) {
            this.sendBlocks(this.getChunkPlayers(cx, cz).values().stream().toArray(Player[]::new), new Block[]{block}, UpdateBlockPacket.FLAG_ALL_PRIORITY);
        } else {
            addBlockChange(index, x, y, z);
        }

        for (ChunkLoader loader : this.getChunkLoaders(cx, cz)) {
            loader.onBlockChanged(block);
        }
        if (update) {
            if (blockPrevious.isTransparent() != block.isTransparent() || blockPrevious.getLightLevel() != block.getLightLevel()) {
                addLightUpdate(x, y, z);
            }
            BlockUpdateEvent ev = new BlockUpdateEvent(block);
            this.server.getPluginManager().callEvent(ev);
            if (!ev.isCancelled()) {
                for (Entity entity : this.getNearbyEntities(new SimpleAxisAlignedBB(x - 1, y - 1, z - 1, x + 1, y + 1, z + 1))) {
                    entity.scheduleUpdate();
                }
                block = ev.getBlock();
                block.onUpdate(BLOCK_UPDATE_NORMAL);
                this.updateAround(x, y, z);
            }
        }
        return true;
    }