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;
}
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();
}
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);
}
}
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);
}
}
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;
}
Aggregations