Search in sources :

Example 6 with BaseFullChunk

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;
}
Also used : BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk)

Example 7 with BaseFullChunk

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();
}
Also used : Player(cn.nukkit.Player) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk)

Example 8 with BaseFullChunk

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;
}
Also used : LevelDB(cn.nukkit.level.format.leveldb.LevelDB) Matcher(java.util.regex.Matcher) LevelProvider(cn.nukkit.level.format.LevelProvider) LevelException(cn.nukkit.utils.LevelException) IOException(java.io.IOException) Chunk(cn.nukkit.level.format.anvil.Chunk) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) FullChunk(cn.nukkit.level.format.FullChunk) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) LevelException(cn.nukkit.utils.LevelException) RegionLoader(cn.nukkit.level.format.mcregion.RegionLoader) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) FullChunk(cn.nukkit.level.format.FullChunk) ChunkConverter(cn.nukkit.level.format.generic.ChunkConverter) FileOutputStream(java.io.FileOutputStream) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) McRegion(cn.nukkit.level.format.mcregion.McRegion) File(java.io.File) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 9 with BaseFullChunk

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;
}
Also used : BlockEntitySpawnable(cn.nukkit.blockentity.BlockEntitySpawnable) ArrayList(java.util.ArrayList) BinaryStream(cn.nukkit.utils.BinaryStream) IOException(java.io.IOException) ChunkException(cn.nukkit.utils.ChunkException) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) HashMap(java.util.HashMap) Map(java.util.Map) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 10 with BaseFullChunk

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;
}
Also used : BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) FullChunk(cn.nukkit.level.format.FullChunk)

Aggregations

BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)26 IOException (java.io.IOException)5 BlockEntity (cn.nukkit.blockentity.BlockEntity)4 FullChunk (cn.nukkit.level.format.FullChunk)4 Player (cn.nukkit.Player)3 Entity (cn.nukkit.entity.Entity)3 Level (cn.nukkit.level.Level)3 Block (cn.nukkit.block.Block)2 ItemBlock (cn.nukkit.item.ItemBlock)2 SimpleChunkManager (cn.nukkit.level.SimpleChunkManager)2 BaseRegionLoader (cn.nukkit.level.format.generic.BaseRegionLoader)2 Generator (cn.nukkit.level.generator.Generator)2 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)2 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)2 Long2ObjectMap (it.unimi.dsi.fastutil.longs.Long2ObjectMap)2 Long2ObjectOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)1 BlockUpdateEvent (cn.nukkit.event.block.BlockUpdateEvent)1 LevelProvider (cn.nukkit.level.format.LevelProvider)1