Search in sources :

Example 1 with LevelProvider

use of cn.nukkit.level.format.LevelProvider 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 2 with LevelProvider

use of cn.nukkit.level.format.LevelProvider 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)

Example 3 with LevelProvider

use of cn.nukkit.level.format.LevelProvider in project Nukkit by Nukkit.

the class Chunk method toBinary.

public byte[] toBinary(boolean saveExtra) {
    try {
        LevelProvider provider = this.getProvider();
        if (saveExtra && provider instanceof LevelDB) {
            List<CompoundTag> entities = new ArrayList<>();
            for (Entity entity : this.getEntities().values()) {
                if (!(entity instanceof Player) && !entity.closed) {
                    entity.saveNBT();
                    entities.add(entity.namedTag);
                }
            }
            EntitiesKey entitiesKey = EntitiesKey.create(this.getX(), this.getZ());
            if (!entities.isEmpty()) {
                ((LevelDB) provider).getDatabase().put(entitiesKey.toArray(), NBTIO.write(entities));
            } else {
                ((LevelDB) provider).getDatabase().delete(entitiesKey.toArray());
            }
            List<CompoundTag> tiles = new ArrayList<>();
            for (BlockEntity blockEntity : this.getBlockEntities().values()) {
                if (!blockEntity.closed) {
                    blockEntity.saveNBT();
                    entities.add(blockEntity.namedTag);
                }
            }
            TilesKey tilesKey = TilesKey.create(this.getX(), this.getZ());
            if (!tiles.isEmpty()) {
                ((LevelDB) provider).getDatabase().put(tilesKey.toArray(), NBTIO.write(tiles));
            } else {
                ((LevelDB) provider).getDatabase().delete(tilesKey.toArray());
            }
            ExtraDataKey extraDataKey = ExtraDataKey.create(this.getX(), this.getZ());
            if (!this.getBlockExtraDataArray().isEmpty()) {
                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));
                }
                ((LevelDB) provider).getDatabase().put(extraDataKey.toArray(), extraData.getBuffer());
            } else {
                ((LevelDB) provider).getDatabase().delete(extraDataKey.toArray());
            }
        }
        byte[] heightMap = this.getHeightMapArray();
        byte[] biomeColors = new byte[this.getBiomeColorArray().length * 4];
        for (int i = 0; i < this.getBiomeColorArray().length; i++) {
            byte[] bytes = Binary.writeInt(this.getBiomeColorArray()[i]);
            biomeColors[i * 4] = bytes[0];
            biomeColors[i * 4 + 1] = bytes[1];
            biomeColors[i * 4 + 2] = bytes[2];
            biomeColors[i * 4 + 3] = bytes[3];
        }
        return Binary.appendBytes(Binary.writeLInt(this.getX()), Binary.writeLInt(this.getZ()), this.getBlockIdArray(), this.getBlockDataArray(), this.getBlockSkyLightArray(), this.getBlockLightArray(), heightMap, biomeColors, new byte[] { (byte) (((this.isLightPopulated ? 0x04 : 0) | (this.isPopulated() ? 0x02 : 0) | (this.isGenerated() ? 0x01 : 0)) & 0xff) });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ExtraDataKey(cn.nukkit.level.format.leveldb.key.ExtraDataKey) Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) Player(cn.nukkit.Player) LevelProvider(cn.nukkit.level.format.LevelProvider) BinaryStream(cn.nukkit.utils.BinaryStream) EntitiesKey(cn.nukkit.level.format.leveldb.key.EntitiesKey) IOException(java.io.IOException) TilesKey(cn.nukkit.level.format.leveldb.key.TilesKey) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Aggregations

LevelProvider (cn.nukkit.level.format.LevelProvider)3 Player (cn.nukkit.Player)2 BlockEntity (cn.nukkit.blockentity.BlockEntity)2 Entity (cn.nukkit.entity.Entity)2 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)2 IOException (java.io.IOException)2 FullChunk (cn.nukkit.level.format.FullChunk)1 Chunk (cn.nukkit.level.format.anvil.Chunk)1 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)1 ChunkConverter (cn.nukkit.level.format.generic.ChunkConverter)1 LevelDB (cn.nukkit.level.format.leveldb.LevelDB)1 EntitiesKey (cn.nukkit.level.format.leveldb.key.EntitiesKey)1 ExtraDataKey (cn.nukkit.level.format.leveldb.key.ExtraDataKey)1 TilesKey (cn.nukkit.level.format.leveldb.key.TilesKey)1 McRegion (cn.nukkit.level.format.mcregion.McRegion)1 RegionLoader (cn.nukkit.level.format.mcregion.RegionLoader)1 BinaryStream (cn.nukkit.utils.BinaryStream)1 LevelException (cn.nukkit.utils.LevelException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1