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

The following examples show how to use cn.nukkit.block.Block#onUpdate() . 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: Level.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public void updateComparatorOutputLevel(Vector3 v) {
    for (BlockFace face : Plane.HORIZONTAL) {
        Vector3 pos = v.getSide(face);

        if (this.isChunkLoaded((int) pos.x >> 4, (int) pos.z >> 4)) {
            Block block1 = this.getBlock(pos);

            if (BlockRedstoneDiode.isDiode(block1)) {
                block1.onUpdate(BLOCK_UPDATE_REDSTONE);
            } else if (block1.isNormalBlock()) {
                pos = pos.getSide(face);
                block1 = this.getBlock(pos);

                if (BlockRedstoneDiode.isDiode(block1)) {
                    block1.onUpdate(BLOCK_UPDATE_REDSTONE);
                }
            }
        }
    }
}
 
Example 2
Source File: Level.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public void updateComparatorOutputLevel(Vector3 v) {
    for (BlockFace face : Plane.HORIZONTAL) {
        Vector3 pos = v.getSide(face);

        if (this.isChunkLoaded((int) pos.x >> 4, (int) pos.z >> 4)) {
            Block block1 = this.getBlock(pos);

            if (BlockRedstoneDiode.isDiode(block1)) {
                block1.onUpdate(BLOCK_UPDATE_REDSTONE);
            } else if (block1.isNormalBlock()) {
                pos = pos.getSide(face);
                block1 = this.getBlock(pos);

                if (BlockRedstoneDiode.isDiode(block1)) {
                    block1.onUpdate(BLOCK_UPDATE_REDSTONE);
                }
            }
        }
    }
}
 
Example 3
Source File: BlockUpdateScheduler.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private void perform(long tick) {
    try {
        lastTick = tick;
        Set<BlockUpdateEntry> updates = pendingUpdates = queuedUpdates.remove(tick);
        if (updates != null) {
            for (BlockUpdateEntry entry : updates) {
                Vector3 pos = entry.pos;
                if (level.isChunkLoaded(NukkitMath.floorDouble(pos.x) >> 4, NukkitMath.floorDouble(pos.z) >> 4)) {
                    Block block = level.getBlock(entry.pos);

                    if (Block.equals(block, entry.block, false)) {
                        block.onUpdate(Level.BLOCK_UPDATE_SCHEDULED);
                    }
                } else {
                    level.scheduleUpdate(entry.block, entry.pos, 0);
                }
            }
        }
    } finally {
        pendingUpdates = null;
    }
}
 
Example 4
Source File: Level.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public void updateComparatorOutputLevel(Vector3 v) {
    for (BlockFace face : Plane.HORIZONTAL) {
        Vector3 pos = v.getSide(face);

        if (this.isChunkLoaded((int) pos.x >> 4, (int) pos.z >> 4)) {
            Block block1 = this.getBlock(pos);

            if (BlockRedstoneDiode.isDiode(block1)) {
                block1.onUpdate(BLOCK_UPDATE_REDSTONE);
            } else if (block1.isNormalBlock()) {
                pos = pos.getSide(face);
                block1 = this.getBlock(pos);

                if (BlockRedstoneDiode.isDiode(block1)) {
                    block1.onUpdate(BLOCK_UPDATE_REDSTONE);
                }
            }
        }
    }
}
 
Example 5
Source File: BlockUpdateScheduler.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private void perform(long tick) {
    try {
        lastTick = tick;
        Set<BlockUpdateEntry> updates = pendingUpdates = queuedUpdates.remove(tick);
        if (updates != null) {
            for (BlockUpdateEntry entry : updates) {
                Vector3 pos = entry.pos;
                if (level.isChunkLoaded(NukkitMath.floorDouble(pos.x) >> 4, NukkitMath.floorDouble(pos.z) >> 4)) {
                    Block block = level.getBlock(entry.pos);

                    if (Block.equals(block, entry.block, false)) {
                        block.onUpdate(level.BLOCK_UPDATE_SCHEDULED);
                    }
                } else {
                    level.scheduleUpdate(entry.block, entry.pos, 0);
                }
            }
        }
    } finally {
        pendingUpdates = null;
    }
}
 
Example 6
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 7
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;
    }