Search in sources :

Example 36 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag 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 37 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag 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);
    }
}
Also used : Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) Player(cn.nukkit.Player) ArrayList(java.util.ArrayList) BinaryStream(cn.nukkit.utils.BinaryStream) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 38 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag 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 39 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.

the class McRegion method generate.

public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/region").exists()) {
        new File(path + "/region").mkdirs();
    }
    CompoundTag levelData = new CompoundTag("Data").putCompound("GameRules", new CompoundTag()).putLong("DayTime", 0).putInt("GameType", 0).putString("generatorName", Generator.getGeneratorName(generator)).putString("generatorOptions", options.containsKey("preset") ? options.get("preset") : "").putInt("generatorVersion", 1).putBoolean("hardcore", false).putBoolean("initialized", true).putLong("LastPlayed", System.currentTimeMillis() / 1000).putString("LevelName", name).putBoolean("raining", false).putInt("rainTime", 0).putLong("RandomSeed", seed).putInt("SpawnX", 128).putInt("SpawnY", 70).putInt("SpawnZ", 128).putBoolean("thundering", false).putInt("thunderTime", 0).putInt("version", 19133).putLong("Time", 0).putLong("SizeOnDisk", 0);
    NBTIO.writeGZIPCompressed(new CompoundTag().putCompound("Data", levelData), new FileOutputStream(path + "level.dat"), ByteOrder.BIG_ENDIAN);
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 40 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.

the class BaseFullChunk method initChunk.

public void initChunk() {
    if (this.getProvider() != null && !this.isInit) {
        boolean changed = false;
        if (this.NBTentities != null) {
            this.getProvider().getLevel().timings.syncChunkLoadEntitiesTimer.startTiming();
            for (CompoundTag nbt : NBTentities) {
                if (!nbt.contains("id")) {
                    this.setChanged();
                    continue;
                }
                ListTag pos = nbt.getList("Pos");
                if ((((NumberTag) pos.get(0)).getData().intValue() >> 4) != this.getX() || ((((NumberTag) pos.get(2)).getData().intValue() >> 4) != this.getZ())) {
                    changed = true;
                    continue;
                }
                Entity entity = Entity.createEntity(nbt.getString("id"), this, nbt);
                if (entity != null) {
                    changed = true;
                    continue;
                }
            }
            this.getProvider().getLevel().timings.syncChunkLoadEntitiesTimer.stopTiming();
            this.NBTentities = null;
        }
        if (this.NBTtiles != null) {
            this.getProvider().getLevel().timings.syncChunkLoadBlockEntitiesTimer.startTiming();
            for (CompoundTag nbt : NBTtiles) {
                if (nbt != null) {
                    if (!nbt.contains("id")) {
                        changed = true;
                        continue;
                    }
                    if ((nbt.getInt("x") >> 4) != this.getX() || ((nbt.getInt("z") >> 4) != this.getZ())) {
                        changed = true;
                        continue;
                    }
                    BlockEntity blockEntity = BlockEntity.createBlockEntity(nbt.getString("id"), this, nbt);
                    if (blockEntity == null) {
                        changed = true;
                        continue;
                    }
                }
            }
            this.getProvider().getLevel().timings.syncChunkLoadBlockEntitiesTimer.stopTiming();
            this.NBTtiles = null;
        }
        this.setChanged(changed);
        this.isInit = true;
    }
}
Also used : Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) NumberTag(cn.nukkit.nbt.tag.NumberTag) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Aggregations

CompoundTag (cn.nukkit.nbt.tag.CompoundTag)74 ListTag (cn.nukkit.nbt.tag.ListTag)28 BlockEntity (cn.nukkit.blockentity.BlockEntity)13 DoubleTag (cn.nukkit.nbt.tag.DoubleTag)13 FloatTag (cn.nukkit.nbt.tag.FloatTag)13 Tag (cn.nukkit.nbt.tag.Tag)13 StringTag (cn.nukkit.nbt.tag.StringTag)12 Map (java.util.Map)8 Entity (cn.nukkit.entity.Entity)7 IOException (java.io.IOException)7 Player (cn.nukkit.Player)5 BinaryStream (cn.nukkit.utils.BinaryStream)5 ArrayList (java.util.ArrayList)5 BlockRail (cn.nukkit.block.BlockRail)4 FullChunk (cn.nukkit.level.format.FullChunk)4 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)4 Rail (cn.nukkit.utils.Rail)4 File (java.io.File)4 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)3 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)3