Search in sources :

Example 1 with McRegion

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

Aggregations

FullChunk (cn.nukkit.level.format.FullChunk)1 LevelProvider (cn.nukkit.level.format.LevelProvider)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 McRegion (cn.nukkit.level.format.mcregion.McRegion)1 RegionLoader (cn.nukkit.level.format.mcregion.RegionLoader)1 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)1 LevelException (cn.nukkit.utils.LevelException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Matcher (java.util.regex.Matcher)1