Search in sources :

Example 36 with Player

use of cn.nukkit.Player in project Nukkit by Nukkit.

the class Level method setBlock.

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;
}
Also used : BlockEntity(cn.nukkit.blockentity.BlockEntity) Entity(cn.nukkit.entity.Entity) Player(cn.nukkit.Player) BlockUpdateEvent(cn.nukkit.event.block.BlockUpdateEvent) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) ItemBlock(cn.nukkit.item.ItemBlock) Block(cn.nukkit.block.Block)

Example 37 with Player

use of cn.nukkit.Player in project Nukkit by Nukkit.

the class Level method chunkRequestCallback.

public void chunkRequestCallback(long timestamp, int x, int z, byte[] payload) {
    this.timings.syncChunkSendTimer.startTiming();
    long index = Level.chunkHash(x, z);
    if (this.cacheChunks) {
        BatchPacket data = Player.getChunkCacheFromData(x, z, payload);
        BaseFullChunk chunk = getChunk(x, z, false);
        if (chunk != null && chunk.getChanges() <= timestamp) {
            chunk.setChunkPacket(data);
        }
        this.sendChunk(x, z, index, data);
        this.timings.syncChunkSendTimer.stopTiming();
        return;
    }
    if (this.chunkSendTasks.containsKey(index)) {
        for (Player player : this.chunkSendQueue.get(index).values()) {
            if (player.isConnected() && player.usedChunks.containsKey(index)) {
                player.sendChunk(x, z, payload);
            }
        }
        this.chunkSendQueue.remove(index);
        this.chunkSendTasks.remove(index);
    }
    this.timings.syncChunkSendTimer.stopTiming();
}
Also used : Player(cn.nukkit.Player) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk)

Example 38 with Player

use of cn.nukkit.Player in project Nukkit by Nukkit.

the class Chunk method toBinary.

@Override
public byte[] toBinary() {
    CompoundTag nbt = this.getNBT().copy();
    nbt.putInt("xPos", this.getX());
    nbt.putInt("zPos", this.getZ());
    if (this.isGenerated()) {
        nbt.putByteArray("Blocks", this.getBlockIdArray());
        nbt.putByteArray("Data", this.getBlockDataArray());
        nbt.putByteArray("SkyLight", this.getBlockSkyLightArray());
        nbt.putByteArray("BlockLight", this.getBlockLightArray());
        nbt.putIntArray("BiomeColors", this.getBiomeColorArray());
        int[] heightInts = new int[256];
        byte[] heightBytes = this.getHeightMapArray();
        for (int i = 0; i < heightInts.length; i++) {
            heightInts[i] = heightBytes[i] & 0xFF;
        }
        nbt.putIntArray("HeightMap", heightInts);
    }
    ArrayList<CompoundTag> entities = new ArrayList<>();
    for (Entity entity : this.getEntities().values()) {
        if (!(entity instanceof Player) && !entity.closed) {
            entity.saveNBT();
            entities.add(entity.namedTag);
        }
    }
    ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
    entityListTag.setAll(entities);
    nbt.putList(entityListTag);
    ArrayList<CompoundTag> tiles = new ArrayList<>();
    for (BlockEntity blockEntity : this.getBlockEntities().values()) {
        blockEntity.saveNBT();
        tiles.add(blockEntity.namedTag);
    }
    ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
    tileListTag.setAll(tiles);
    nbt.putList(tileListTag);
    BinaryStream extraData = new BinaryStream();
    Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
    extraData.putInt(extraDataArray.size());
    for (Integer key : extraDataArray.keySet()) {
        extraData.putInt(key);
        extraData.putShort(extraDataArray.get(key));
    }
    nbt.putByteArray("ExtraData", extraData.getBuffer());
    CompoundTag chunk = new CompoundTag("");
    chunk.putCompound("Level", nbt);
    try {
        return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) Player(cn.nukkit.Player) ArrayList(java.util.ArrayList) BinaryStream(cn.nukkit.utils.BinaryStream) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 39 with Player

use of cn.nukkit.Player in project Nukkit by Nukkit.

the class Chunk method toFastBinary.

@Override
public byte[] toFastBinary() {
    CompoundTag nbt = this.getNBT().copy();
    nbt.putInt("xPos", this.getX());
    nbt.putInt("zPos", this.getZ());
    nbt.putIntArray("BiomeColors", this.getBiomeColorArray());
    int[] heightInts = new int[256];
    byte[] heightBytes = this.getHeightMapArray();
    for (int i = 0; i < heightInts.length; i++) {
        heightInts[i] = heightBytes[i] & 0xFF;
    }
    for (cn.nukkit.level.format.ChunkSection section : this.getSections()) {
        if (section instanceof EmptyChunkSection) {
            continue;
        }
        CompoundTag s = new CompoundTag(null);
        s.putByte("Y", section.getY());
        s.putByteArray("Blocks", section.getIdArray());
        s.putByteArray("Data", section.getDataArray());
        s.putByteArray("BlockLight", section.getLightArray());
        s.putByteArray("SkyLight", section.getSkyLightArray());
        nbt.getList("Sections", CompoundTag.class).add(s);
    }
    ArrayList<CompoundTag> entities = new ArrayList<>();
    for (Entity entity : this.getEntities().values()) {
        if (!(entity instanceof Player) && !entity.closed) {
            entity.saveNBT();
            entities.add(entity.namedTag);
        }
    }
    ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
    entityListTag.setAll(entities);
    nbt.putList(entityListTag);
    ArrayList<CompoundTag> tiles = new ArrayList<>();
    for (BlockEntity blockEntity : this.getBlockEntities().values()) {
        blockEntity.saveNBT();
        tiles.add(blockEntity.namedTag);
    }
    ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
    tileListTag.setAll(tiles);
    nbt.putList(tileListTag);
    Set<BlockUpdateEntry> entries = this.provider.getLevel().getPendingBlockUpdates(this);
    if (entries != null) {
        ListTag<CompoundTag> tileTickTag = new ListTag<>("TileTicks");
        long totalTime = this.provider.getLevel().getCurrentTick();
        for (BlockUpdateEntry entry : entries) {
            CompoundTag entryNBT = new CompoundTag().putString("i", entry.block.getSaveId()).putInt("x", entry.pos.getFloorX()).putInt("y", entry.pos.getFloorY()).putInt("z", entry.pos.getFloorZ()).putInt("t", (int) (entry.delay - totalTime)).putInt("p", entry.priority);
            tileTickTag.add(entryNBT);
        }
        nbt.putList(tileTickTag);
    }
    BinaryStream extraData = new BinaryStream();
    Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
    extraData.putInt(extraDataArray.size());
    for (Integer key : extraDataArray.keySet()) {
        extraData.putInt(key);
        extraData.putShort(extraDataArray.get(key));
    }
    nbt.putByteArray("ExtraData", extraData.getBuffer());
    CompoundTag chunk = new CompoundTag("");
    chunk.putCompound("Level", nbt);
    try {
        return NBTIO.write(chunk, ByteOrder.BIG_ENDIAN);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) Player(cn.nukkit.Player) IOException(java.io.IOException) EmptyChunkSection(cn.nukkit.level.format.generic.EmptyChunkSection) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 40 with Player

use of cn.nukkit.Player in project Nukkit by Nukkit.

the class BaseFullChunk method unload.

@Override
public boolean unload(boolean save, boolean safe) {
    LevelProvider level = this.getProvider();
    if (level == null) {
        return true;
    }
    if (save && this.changes != 0) {
        level.saveChunk(this.getX(), this.getZ());
    }
    if (safe) {
        for (Entity entity : this.getEntities().values()) {
            if (entity instanceof Player) {
                return false;
            }
        }
    }
    for (Entity entity : new ArrayList<>(this.getEntities().values())) {
        if (entity instanceof Player) {
            continue;
        }
        entity.close();
    }
    for (BlockEntity blockEntity : new ArrayList<>(this.getBlockEntities().values())) {
        blockEntity.close();
    }
    this.provider = null;
    return true;
}
Also used : Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) Player(cn.nukkit.Player) LevelProvider(cn.nukkit.level.format.LevelProvider) ArrayList(java.util.ArrayList) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Aggregations

Player (cn.nukkit.Player)85 TranslationContainer (cn.nukkit.lang.TranslationContainer)24 Entity (cn.nukkit.entity.Entity)13 BlockEntity (cn.nukkit.blockentity.BlockEntity)10 Item (cn.nukkit.item.Item)10 Level (cn.nukkit.level.Level)7 IOException (java.io.IOException)6 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)5 Block (cn.nukkit.block.Block)4 ItemBlock (cn.nukkit.item.ItemBlock)4 BlockAir (cn.nukkit.block.BlockAir)3 EventHandler (cn.nukkit.event.EventHandler)3 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)3 InventoryContentPacket (cn.nukkit.network.protocol.InventoryContentPacket)3 InventorySlotPacket (cn.nukkit.network.protocol.InventorySlotPacket)3 User (me.lucko.luckperms.common.model.User)3 IPlayer (cn.nukkit.IPlayer)2 CommandSender (cn.nukkit.command.CommandSender)2 EntityItem (cn.nukkit.entity.item.EntityItem)2 EntityArrow (cn.nukkit.entity.projectile.EntityArrow)2