use of cn.nukkit.level.format.generic.BaseFullChunk in project Nukkit by Nukkit.
the class Level method getChunk.
public BaseFullChunk getChunk(int chunkX, int chunkZ, boolean create) {
long index = Level.chunkHash(chunkX, chunkZ);
BaseFullChunk chunk = this.provider.getLoadedChunk(index);
if (chunk == null) {
chunk = this.forceLoadChunk(index, chunkX, chunkZ, create);
}
return chunk;
}
use of cn.nukkit.level.format.generic.BaseFullChunk 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.level.format.generic.BaseFullChunk in project Nukkit by Nukkit.
the class LevelProviderConverter method perform.
LevelProvider perform() throws IOException {
new File(path).mkdir();
File dat = new File(provider.getPath(), "level.dat.old");
new File(provider.getPath(), "level.dat").renameTo(dat);
Utils.copyFile(dat, new File(path, "level.dat"));
LevelProvider result;
try {
if (provider instanceof LevelDB) {
try (FileInputStream stream = new FileInputStream(path + "level.dat")) {
stream.skip(8);
CompoundTag levelData = NBTIO.read(stream, ByteOrder.LITTLE_ENDIAN);
if (levelData != null) {
NBTIO.writeGZIPCompressed(new CompoundTag().putCompound("Data", levelData), new FileOutputStream(path + "level.dat"));
} else {
throw new IOException("LevelData can not be null");
}
} catch (IOException e) {
throw new LevelException("Invalid level.dat");
}
}
result = toClass.getConstructor(Level.class, String.class).newInstance(level, path);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (toClass == Anvil.class) {
if (provider instanceof McRegion) {
new File(path, "region").mkdir();
for (File file : new File(provider.getPath() + "region/").listFiles()) {
Matcher m = Pattern.compile("-?\\d+").matcher(file.getName());
int regionX, regionZ;
try {
if (m.find()) {
regionX = Integer.parseInt(m.group());
} else
continue;
if (m.find()) {
regionZ = Integer.parseInt(m.group());
} else
continue;
} catch (NumberFormatException e) {
continue;
}
RegionLoader region = new RegionLoader(provider, regionX, regionZ);
for (Integer index : region.getLocationIndexes()) {
int chunkX = index & 0x1f;
int chunkZ = index >> 5;
BaseFullChunk old = region.readChunk(chunkX, chunkZ);
if (old == null)
continue;
int x = (regionX << 5) | chunkX;
int z = (regionZ << 5) | chunkZ;
FullChunk chunk = new ChunkConverter(result).from(old).to(Chunk.class).perform();
result.saveChunk(x, z, chunk);
}
region.close();
}
}
if (provider instanceof LevelDB) {
new File(path, "region").mkdir();
for (byte[] key : ((LevelDB) provider).getTerrainKeys()) {
int x = getChunkX(key);
int z = getChunkZ(key);
BaseFullChunk old = ((LevelDB) provider).readChunk(x, z);
FullChunk chunk = new ChunkConverter(result).from(old).to(Chunk.class).perform();
result.saveChunk(x, z, chunk);
}
}
result.doGarbageCollection();
}
return result;
}
use of cn.nukkit.level.format.generic.BaseFullChunk in project Nukkit by Nukkit.
the class McRegion method requestChunkTask.
@Override
public AsyncTask requestChunkTask(int x, int z) throws ChunkException {
BaseFullChunk chunk = this.getChunk(x, z, false);
if (chunk == null) {
throw new ChunkException("Invalid Chunk Sent");
}
long timestamp = chunk.getChanges();
byte[] tiles = new byte[0];
if (!chunk.getBlockEntities().isEmpty()) {
List<CompoundTag> tagList = new ArrayList<>();
for (BlockEntity blockEntity : chunk.getBlockEntities().values()) {
if (blockEntity instanceof BlockEntitySpawnable) {
tagList.add(((BlockEntitySpawnable) blockEntity).getSpawnCompound());
}
}
try {
tiles = NBTIO.write(tagList, ByteOrder.LITTLE_ENDIAN, true);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Map<Integer, Integer> extra = chunk.getBlockExtraDataArray();
BinaryStream extraData;
if (!extra.isEmpty()) {
extraData = new BinaryStream();
extraData.putLInt(extra.size());
for (Map.Entry<Integer, Integer> entry : extra.entrySet()) {
extraData.putLInt(entry.getKey());
extraData.putLShort(entry.getValue());
}
} else {
extraData = null;
}
BinaryStream stream = new BinaryStream();
stream.put(chunk.getBlockIdArray());
stream.put(chunk.getBlockDataArray());
stream.put(chunk.getBlockSkyLightArray());
stream.put(chunk.getBlockLightArray());
stream.put(chunk.getHeightMapArray());
for (int color : chunk.getBiomeColorArray()) {
stream.put(Binary.writeInt(color));
}
if (extraData != null) {
stream.put(extraData.getBuffer());
} else {
stream.putLInt(0);
}
stream.put(tiles);
this.getLevel().chunkRequestCallback(timestamp, x, z, stream.getBuffer());
return null;
}
use of cn.nukkit.level.format.generic.BaseFullChunk in project Nukkit by Nukkit.
the class Anvil method doGarbageCollection.
@Override
public void doGarbageCollection(long time) {
long start = System.currentTimeMillis();
int maxIterations = size();
if (lastPosition > maxIterations)
lastPosition = 0;
ObjectIterator<BaseFullChunk> iter = getChunks();
if (lastPosition != 0)
iter.skip(lastPosition);
int i;
for (i = 0; i < maxIterations; i++) {
if (!iter.hasNext()) {
iter = getChunks();
}
BaseFullChunk chunk = iter.next();
if (chunk == null)
continue;
if (chunk.isGenerated() && chunk.isPopulated() && chunk instanceof Chunk) {
Chunk anvilChunk = (Chunk) chunk;
for (cn.nukkit.level.format.ChunkSection section : anvilChunk.getSections()) {
if (section instanceof ChunkSection) {
ChunkSection anvilSection = (ChunkSection) section;
if (!anvilSection.isEmpty()) {
anvilSection.compress();
}
}
}
if (System.currentTimeMillis() - start >= time)
break;
}
}
lastPosition += i;
}
Aggregations