Search in sources :

Example 6 with NBTInputStream

use of com.nukkitx.nbt.NBTInputStream in project JukeboxMC by LucGamesYT.

the class CreativeItems method init.

public static void init() {
    Gson GSON = new Gson();
    try (InputStream inputStream = Objects.requireNonNull(Bootstrap.class.getClassLoader().getResourceAsStream("creative_items.json"))) {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        Map<String, List<Map<String, Object>>> itemEntries = GSON.fromJson(inputStreamReader, Map.class);
        int netIdCounter = 0;
        for (Map<String, Object> itemEntry : itemEntries.get("items")) {
            Item item;
            String identifier = (String) itemEntry.get("id");
            if (itemEntry.containsKey("blockRuntimeId")) {
                item = get(identifier, (int) (double) itemEntry.get("blockRuntimeId"));
            } else {
                item = get(identifier);
            }
            if (itemEntry.containsKey("damage")) {
                item.setMeta((short) (double) itemEntry.get("damage"));
            }
            String nbtTag = (String) itemEntry.get("nbt_b64");
            if (nbtTag != null)
                try (NBTInputStream nbtReader = NbtUtils.createReaderLE(new ByteArrayInputStream(Base64.getDecoder().decode(nbtTag.getBytes())))) {
                    item.setNBT((NbtMap) nbtReader.readTag());
                }
            CREATIVE_ITEMS.add(item.toNetwork(netIdCounter++));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) NBTInputStream(com.nukkitx.nbt.NBTInputStream) InputStream(java.io.InputStream) NbtMap(com.nukkitx.nbt.NbtMap) Gson(com.google.gson.Gson) IOException(java.io.IOException) Item(org.jukeboxmc.item.Item) ByteArrayInputStream(java.io.ByteArrayInputStream) NBTInputStream(com.nukkitx.nbt.NBTInputStream)

Example 7 with NBTInputStream

use of com.nukkitx.nbt.NBTInputStream in project JukeboxMC by LucGamesYT.

the class LevelDB method loadBlockEntities.

public static void loadBlockEntities(Chunk chunk, byte[] blockEntityData) {
    ByteBuf byteBuf = Unpooled.wrappedBuffer(blockEntityData);
    try {
        NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(byteBuf));
        while (byteBuf.readableBytes() > 0) {
            try {
                NbtMap nbtMap = (NbtMap) reader.readTag();
                int x = nbtMap.getInt("x", 0);
                int y = nbtMap.getInt("y", 0);
                int z = nbtMap.getInt("z", 0);
                Block block = chunk.getBlock(x, y, z, 0);
                if (block != null && block.hasBlockEntity()) {
                    BlockEntity blockEntity = BlockEntityType.getBlockEntityById(nbtMap.getString("id"), block);
                    if (blockEntity != null) {
                        blockEntity.fromCompound(nbtMap);
                        chunk.setBlockEntity(x, y, z, blockEntity);
                    }
                }
            } catch (IOException e) {
                break;
            }
        }
    } finally {
        byteBuf.release();
    }
}
Also used : NBTInputStream(com.nukkitx.nbt.NBTInputStream) NbtMap(com.nukkitx.nbt.NbtMap) Block(org.jukeboxmc.block.Block) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) BlockEntity(org.jukeboxmc.blockentity.BlockEntity)

Example 8 with NBTInputStream

use of com.nukkitx.nbt.NBTInputStream in project JukeboxMC by LucGamesYT.

the class IntPalette method readFromStoragePersistent.

public void readFromStoragePersistent(ByteBuf byteBuf, IntPersistentDataDeserializer deserializer) {
    final short header = byteBuf.readUnsignedByte();
    if (!isPersistent(header))
        throw new IllegalArgumentException("Palette is not persistent!");
    final BitArrayVersion version = IntPalette.getVersionFromHeader(header);
    final int wordCount = version.getWordsForSize(SIZE);
    final int[] words = new int[wordCount];
    for (int i = 0; i < wordCount; i++) words[i] = byteBuf.readIntLE();
    this.bitArray = version.createArray(SIZE, words);
    this.palette.clear();
    final int paletteSize = byteBuf.readIntLE();
    try (final ByteBufInputStream bufInputStream = new ByteBufInputStream(byteBuf);
        final NBTInputStream inputStream = NbtUtils.createReaderLE(bufInputStream)) {
        for (int i = 0; i < paletteSize; i++) this.palette.add(deserializer.deserialize((NbtMap) inputStream.readTag()));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : BitArrayVersion(org.jukeboxmc.world.palette.bitarray.BitArrayVersion) NBTInputStream(com.nukkitx.nbt.NBTInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 9 with NBTInputStream

use of com.nukkitx.nbt.NBTInputStream in project JukeboxMC by LucGamesYT.

the class ObjectPalette method readFromStoragePersistent.

public void readFromStoragePersistent(ByteBuf byteBuf, ObjectPersistentDataDeserializer<V> deserializer) {
    final short header = byteBuf.readUnsignedByte();
    final BitArrayVersion version = ObjectPalette.getVersionFromHeader(header);
    final int wordCount = version.getWordsForSize(SIZE);
    final int[] words = new int[wordCount];
    for (int i = 0; i < wordCount; i++) words[i] = byteBuf.readIntLE();
    this.bitArray = version.createArray(SIZE, words);
    this.palette.clear();
    final int paletteSize = byteBuf.readIntLE();
    try (final ByteBufInputStream bufInputStream = new ByteBufInputStream(byteBuf);
        final NBTInputStream inputStream = NbtUtils.createReaderLE(bufInputStream)) {
        for (int i = 0; i < paletteSize; i++) this.palette.add(deserializer.deserialize((NbtMap) inputStream.readTag()));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : BitArrayVersion(org.jukeboxmc.world.palette.bitarray.BitArrayVersion) NBTInputStream(com.nukkitx.nbt.NBTInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 10 with NBTInputStream

use of com.nukkitx.nbt.NBTInputStream in project Protocol by CloudburstMC.

the class BedrockPacketHelper_v431 method readItem.

@Override
public ItemData readItem(ByteBuf buffer, BedrockSession session) {
    int id = VarInts.readInt(buffer);
    if (id == 0) {
        // We don't need to read anything extra.
        return ItemData.AIR;
    }
    int count = buffer.readUnsignedShortLE();
    int damage = VarInts.readUnsignedInt(buffer);
    boolean hasNetId = buffer.readBoolean();
    int netId = 0;
    if (hasNetId) {
        netId = VarInts.readInt(buffer);
    }
    int blockRuntimeId = VarInts.readInt(buffer);
    NbtMap compoundTag = null;
    long blockingTicks = 0;
    String[] canPlace;
    String[] canBreak;
    ByteBuf buf = buffer.readSlice(VarInts.readUnsignedInt(buffer));
    try (LittleEndianByteBufInputStream stream = new LittleEndianByteBufInputStream(buf);
        NBTInputStream nbtStream = new NBTInputStream(stream)) {
        int nbtSize = stream.readShort();
        if (nbtSize > 0) {
            compoundTag = (NbtMap) nbtStream.readTag();
        } else if (nbtSize == -1) {
            int tagCount = stream.readUnsignedByte();
            if (tagCount != 1)
                throw new IllegalArgumentException("Expected 1 tag but got " + tagCount);
            compoundTag = (NbtMap) nbtStream.readTag();
        }
        canPlace = new String[stream.readInt()];
        for (int i = 0; i < canPlace.length; i++) {
            canPlace[i] = stream.readUTF();
        }
        canBreak = new String[stream.readInt()];
        for (int i = 0; i < canBreak.length; i++) {
            canBreak[i] = stream.readUTF();
        }
        if (this.isBlockingItem(id, session)) {
            blockingTicks = stream.readLong();
        }
    } catch (IOException e) {
        throw new IllegalStateException("Unable to read item user data", e);
    }
    if (buf.isReadable()) {
        log.info("Item user data has {} readable bytes left\n{}", buf.readableBytes(), ByteBufUtil.prettyHexDump(buf.readerIndex(0)));
    }
    return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).blockingTicks(blockingTicks).blockRuntimeId(blockRuntimeId).usingNetId(hasNetId).netId(netId).build();
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NBTInputStream(com.nukkitx.nbt.NBTInputStream) LittleEndianByteBufInputStream(com.nukkitx.protocol.bedrock.util.LittleEndianByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

NBTInputStream (com.nukkitx.nbt.NBTInputStream)10 IOException (java.io.IOException)10 NbtMap (com.nukkitx.nbt.NbtMap)8 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)6 ByteBuf (io.netty.buffer.ByteBuf)3 LittleEndianByteBufInputStream (com.nukkitx.protocol.bedrock.util.LittleEndianByteBufInputStream)2 InputStream (java.io.InputStream)2 BitArrayVersion (org.jukeboxmc.world.palette.bitarray.BitArrayVersion)2 Gson (com.google.gson.Gson)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 InputStreamReader (java.io.InputStreamReader)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 Bootstrap (org.jukeboxmc.Bootstrap)1 Block (org.jukeboxmc.block.Block)1 BlockEntity (org.jukeboxmc.blockentity.BlockEntity)1 Item (org.jukeboxmc.item.Item)1