Java Code Examples for cn.nukkit.nbt.NBTIO#write()

The following examples show how to use cn.nukkit.nbt.NBTIO#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlockEntitySpawnable.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public void spawnTo(Player player) {
    if (this.closed) {
        return;
    }

    CompoundTag tag = this.getSpawnCompound();
    BlockEntityDataPacket pk = new BlockEntityDataPacket();
    pk.x = (int) this.x;
    pk.y = (int) this.y;
    pk.z = (int) this.z;
    try {
        pk.namedTag = NBTIO.write(tag, ByteOrder.LITTLE_ENDIAN, true);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    player.dataPacket(pk);
}
 
Example 2
Source File: TradingInventory.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onOpen(Player who) {
    CompoundTag nbt = this.getHolder().getOffers();
    if (nbt != null) {
        super.onOpen(who);

        UpdateTradePacket pk1 = new UpdateTradePacket();
        pk1.windowId = (byte) who.getWindowId(this);
        pk1.windowType = 15;
        pk1.unknownVarInt1 = 0;
        pk1.unknownVarInt2 = 0;
        pk1.isWilling = false;
        pk1.trader = this.getHolder().getId();
        pk1.player = who.getId();
        pk1.displayName = this.getHolder().getName();
        try {
            pk1.offers = NBTIO.write(nbt, ByteOrder.LITTLE_ENDIAN, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        who.dataPacket(pk1);
    } else {
        super.onClose(who);
    }
}
 
Example 3
Source File: BlockEntitySpawnable.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public void spawnTo(Player player) {
    if (this.closed) {
        return;
    }

    CompoundTag tag = this.getSpawnCompound();
    BlockEntityDataPacket pk = new BlockEntityDataPacket();
    pk.x = (int) this.x;
    pk.y = (int) this.y;
    pk.z = (int) this.z;
    try {
        pk.namedTag = NBTIO.write(tag, ByteOrder.LITTLE_ENDIAN, true);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    player.dataPacket(pk);
}
 
Example 4
Source File: BlockEntitySpawnable.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public void spawnTo(Player player) {
    if (this.closed) {
        return;
    }

    CompoundTag tag = this.getSpawnCompound();
    BlockEntityDataPacket pk = new BlockEntityDataPacket();
    pk.x = (int) this.x;
    pk.y = (int) this.y;
    pk.z = (int) this.z;
    try {
        pk.namedTag = NBTIO.write(tag, ByteOrder.LITTLE_ENDIAN, true);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    player.dataPacket(pk);
}
 
Example 5
Source File: LevelDB.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void saveLevelData() {
    try {
        byte[] data = NBTIO.write(levelData, ByteOrder.LITTLE_ENDIAN);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(Binary.writeLInt(3));
        outputStream.write(Binary.writeLInt(data.length));
        outputStream.write(data);

        Utils.writeFile(path + "level.dat", new ByteArrayInputStream(outputStream.toByteArray()));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 6
Source File: LevelDB.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void saveLevelData() {
    try {
        byte[] data = NBTIO.write(levelData, ByteOrder.LITTLE_ENDIAN);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(Binary.writeLInt(3));
        outputStream.write(Binary.writeLInt(data.length));
        outputStream.write(data);

        Utils.writeFile(path + "level.dat", new ByteArrayInputStream(outputStream.toByteArray()));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source File: Item.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public byte[] writeCompoundTag(CompoundTag tag) {
    try {
        tag.setName("");
        return NBTIO.write(tag, ByteOrder.LITTLE_ENDIAN);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: Item.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public byte[] writeCompoundTag(CompoundTag tag) {
    try {
        tag.setName("");
        return NBTIO.write(tag, ByteOrder.LITTLE_ENDIAN);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: Item.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public byte[] writeCompoundTag(CompoundTag tag) {
    try {
        tag.setName("");
        return NBTIO.write(tag, ByteOrder.LITTLE_ENDIAN);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: LevelDB.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void saveLevelData() {
    try {
        byte[] data = NBTIO.write(levelData, ByteOrder.LITTLE_ENDIAN);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(Binary.writeLInt(3));
        outputStream.write(Binary.writeLInt(data.length));
        outputStream.write(data);

        Utils.writeFile(path + "level.dat", new ByteArrayInputStream(outputStream.toByteArray()));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 11
Source File: LevelDB.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/db").exists()) {
        new File(path + "/db").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("")
            .putLong("currentTick", 0)
            .putInt("DayCycleStopTime", -1)
            .putInt("GameType", 0)
            .putInt("Generator", Generator.getGeneratorType(generator))
            .putBoolean("hasBeenLoadedInCreative", false)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putFloat("lightningLevel", 0)
            .putInt("lightningTime", new Random().nextInt())
            .putInt("limitedWorldOriginX", 128)
            .putInt("limitedWorldOriginY", 70)
            .putInt("limitedWorldOriginZ", 128)
            .putInt("Platform", 0)
            .putFloat("rainLevel", 0)
            .putInt("rainTime", new Random().nextInt())
            .putLong("RandomSeed", seed)
            .putByte("spawnMobs", 0)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putInt("storageVersion", 4)
            .putLong("Time", 0)
            .putLong("worldStartCount", ((long) Integer.MAX_VALUE) & 0xffffffffL);

    byte[] data = NBTIO.write(levelData, ByteOrder.LITTLE_ENDIAN);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputStream.write(Binary.writeLInt(3));
    outputStream.write(Binary.writeLInt(data.length));
    outputStream.write(data);

    Utils.writeFile(path + "level.dat", new ByteArrayInputStream(outputStream.toByteArray()));

    DB db = Iq80DBFactory.factory.open(new File(path + "/db"), new Options().createIfMissing(true));
    db.close();
}
 
Example 12
Source File: Chunk.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] toFastBinary() {
    CompoundTag nbt = this.getNBT().copy();
    nbt.remove("BiomeColors");

    nbt.putInt("xPos", this.getX());
    nbt.putInt("zPos", this.getZ());

    nbt.putByteArray("Biomes", this.getBiomeIdArray());
    int[] heightInts = new int[256];
    byte[] heightBytes = this.getHeightMapArray();
    for (int i = 0; i < heightInts.length; i++) {
        heightInts[i] = heightBytes[i] & 0xFF;
    }

    for (cn.nukkit.level.format.ChunkSection section : this.getSections()) {
        if (section instanceof EmptyChunkSection) {
            continue;
        }
        CompoundTag s = new CompoundTag(null);
        s.putByte("Y", section.getY());
        s.putByteArray("Blocks", section.getIdArray());
        s.putByteArray("Data", section.getDataArray());
        s.putByteArray("BlockLight", section.getLightArray());
        s.putByteArray("SkyLight", section.getSkyLightArray());
        nbt.getList("Sections", CompoundTag.class).add(s);
    }

    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);

    Set<BlockUpdateEntry> entries = this.provider.getLevel().getPendingBlockUpdates(this);

    if (entries != null) {
        ListTag<CompoundTag> tileTickTag = new ListTag<>("TileTicks");
        long totalTime = this.provider.getLevel().getCurrentTick();

        for (BlockUpdateEntry entry : entries) {
            CompoundTag entryNBT = new CompoundTag()
                    .putString("i", entry.block.getSaveId())
                    .putInt("x", entry.pos.getFloorX())
                    .putInt("y", entry.pos.getFloorY())
                    .putInt("z", entry.pos.getFloorZ())
                    .putInt("t", (int) (entry.delay - totalTime))
                    .putInt("p", entry.priority);
            tileTickTag.add(entryNBT);
        }

        nbt.putList(tileTickTag);
    }

    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 NBTIO.write(chunk, ByteOrder.BIG_ENDIAN);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}
 
Example 13
Source File: Chunk.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] toFastBinary() {
    CompoundTag nbt = this.getNBT().copy();
    nbt.putInt("xPos", this.x);
    nbt.putInt("zPos", this.z);

    nbt.putIntArray("BiomeColors", this.getBiomeColorArray());
    nbt.putIntArray("HeightMap", this.getHeightMapArray());

    for (cn.nukkit.level.format.ChunkSection section : this.getSections()) {
        if (section instanceof EmptyChunkSection) {
            continue;
        }
        CompoundTag s = new CompoundTag(null);
        s.putByte("Y", section.getY());
        s.putByteArray("Blocks", section.getIdArray());
        s.putByteArray("Data", section.getDataArray());
        s.putByteArray("BlockLight", section.getLightArray());
        s.putByteArray("SkyLight", section.getSkyLightArray());
        nbt.getList("Sections", CompoundTag.class).add(s);
    }

    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);

    List<BlockUpdateEntry> entries = this.provider.getLevel().getPendingBlockUpdates(this);

    if (entries != null) {
        ListTag<CompoundTag> tileTickTag = new ListTag<>("TileTicks");
        long totalTime = this.provider.getLevel().getCurrentTick();

        for (BlockUpdateEntry entry : entries) {
            CompoundTag entryNBT = new CompoundTag()
                    .putString("i", entry.block.getClass().getSimpleName())
                    .putInt("x", entry.pos.getFloorX())
                    .putInt("y", entry.pos.getFloorY())
                    .putInt("z", entry.pos.getFloorZ())
                    .putInt("t", (int) (entry.delay - totalTime))
                    .putInt("p", entry.priority);
            tileTickTag.add(entryNBT);
        }

        nbt.putList(tileTickTag);
    }

    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 NBTIO.write(chunk, ByteOrder.BIG_ENDIAN);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}
 
Example 14
Source File: LevelDB.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/db").exists()) {
        new File(path + "/db").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("")
            .putLong("currentTick", 0)
            .putInt("DayCycleStopTime", -1)
            .putInt("GameType", 0)
            .putInt("Generator", Generator.getGeneratorType(generator))
            .putBoolean("hasBeenLoadedInCreative", false)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putFloat("lightningLevel", 0)
            .putInt("lightningTime", new Random().nextInt())
            .putInt("limitedWorldOriginX", 128)
            .putInt("limitedWorldOriginY", 70)
            .putInt("limitedWorldOriginZ", 128)
            .putInt("Platform", 0)
            .putFloat("rainLevel", 0)
            .putInt("rainTime", new Random().nextInt())
            .putLong("RandomSeed", seed)
            .putByte("spawnMobs", 0)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putInt("storageVersion", 4)
            .putLong("Time", 0)
            .putLong("worldStartCount", ((long) Integer.MAX_VALUE) & 0xffffffffL);

    byte[] data = NBTIO.write(levelData, ByteOrder.LITTLE_ENDIAN);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputStream.write(Binary.writeLInt(3));
    outputStream.write(Binary.writeLInt(data.length));
    outputStream.write(data);

    Utils.writeFile(path + "level.dat", new ByteArrayInputStream(outputStream.toByteArray()));

    DB db = Iq80DBFactory.factory.open(new File(path + "/db"), new Options().createIfMissing(true));
    db.close();
}
 
Example 15
Source File: LevelDB.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public AsyncTask requestChunkTask(int x, int z) {
    Chunk 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);
        } 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 (Integer key : extra.values()) {
            extraData.putLInt(key);
            extraData.putLShort(extra.get(key));
        }
    } 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;
}
 
Example 16
Source File: Chunk.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] toFastBinary() {
    CompoundTag nbt = this.getNBT().copy();
    nbt.putInt("xPos", this.getX());
    nbt.putInt("zPos", this.getZ());

    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;
    }

    for (cn.nukkit.level.format.ChunkSection section : this.getSections()) {
        if (section instanceof EmptyChunkSection) {
            continue;
        }
        CompoundTag s = new CompoundTag(null);
        s.putByte("Y", section.getY());
        s.putByteArray("Blocks", section.getIdArray());
        s.putByteArray("Data", section.getDataArray());
        s.putByteArray("BlockLight", section.getLightArray());
        s.putByteArray("SkyLight", section.getSkyLightArray());
        nbt.getList("Sections", CompoundTag.class).add(s);
    }

    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);

    Set<BlockUpdateEntry> entries = this.provider.getLevel().getPendingBlockUpdates(this);

    if (entries != null) {
        ListTag<CompoundTag> tileTickTag = new ListTag<>("TileTicks");
        long totalTime = this.provider.getLevel().getCurrentTick();

        for (BlockUpdateEntry entry : entries) {
            CompoundTag entryNBT = new CompoundTag()
                    .putString("i", entry.block.getSaveId())
                    .putInt("x", entry.pos.getFloorX())
                    .putInt("y", entry.pos.getFloorY())
                    .putInt("z", entry.pos.getFloorZ())
                    .putInt("t", (int) (entry.delay - totalTime))
                    .putInt("p", entry.priority);
            tileTickTag.add(entryNBT);
        }

        nbt.putList(tileTickTag);
    }

    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 NBTIO.write(chunk, ByteOrder.BIG_ENDIAN);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}
 
Example 17
Source File: LevelDB.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/db").exists()) {
        new File(path + "/db").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("")
            .putLong("currentTick", 0)
            .putInt("DayCycleStopTime", -1)
            .putInt("GameType", 0)
            .putInt("Generator", Generator.getGeneratorType(generator))
            .putBoolean("hasBeenLoadedInCreative", false)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putFloat("lightningLevel", 0)
            .putInt("lightningTime", new Random().nextInt())
            .putInt("limitedWorldOriginX", 128)
            .putInt("limitedWorldOriginY", 70)
            .putInt("limitedWorldOriginZ", 128)
            .putInt("Platform", 0)
            .putFloat("rainLevel", 0)
            .putInt("rainTime", new Random().nextInt())
            .putLong("RandomSeed", seed)
            .putByte("spawnMobs", 0)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putInt("storageVersion", 4)
            .putLong("Time", 0)
            .putLong("worldStartCount", ((long) Integer.MAX_VALUE) & 0xffffffffL);

    byte[] data = NBTIO.write(levelData, ByteOrder.LITTLE_ENDIAN);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputStream.write(Binary.writeLInt(3));
    outputStream.write(Binary.writeLInt(data.length));
    outputStream.write(data);

    Utils.writeFile(path + "level.dat", new ByteArrayInputStream(outputStream.toByteArray()));

    DB db = Iq80DBFactory.factory.open(new File(path + "/db"), new Options().createIfMissing(true));
    db.close();
}