Search in sources :

Example 1 with NBTInputStream

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

the class BedrockPacketHelper_v332 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 aux = VarInts.readInt(buffer);
    int damage = (short) (aux >> 8);
    if (damage == Short.MAX_VALUE)
        damage = -1;
    int count = aux & 0xff;
    int nbtSize = buffer.readShortLE();
    NbtMap compoundTag = null;
    if (nbtSize > 0) {
        try (NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(buffer.readSlice(nbtSize)))) {
            compoundTag = (NbtMap) reader.readTag();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    } else if (nbtSize == -1) {
        int tagCount = buffer.readUnsignedByte();
        if (tagCount != 1)
            throw new IllegalArgumentException("Expected 1 tag but got " + tagCount);
        try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
            compoundTag = (NbtMap) reader.readTag();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    }
    String[] canPlace = new String[VarInts.readInt(buffer)];
    for (int i = 0; i < canPlace.length; i++) {
        canPlace[i] = this.readString(buffer);
    }
    String[] canBreak = new String[VarInts.readInt(buffer)];
    for (int i = 0; i < canBreak.length; i++) {
        canBreak[i] = this.readString(buffer);
    }
    return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).build();
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NBTInputStream(com.nukkitx.nbt.NBTInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 2 with NBTInputStream

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

the class BedrockPacketHelper_v340 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 aux = VarInts.readInt(buffer);
    int damage = (short) (aux >> 8);
    if (damage == Short.MAX_VALUE)
        damage = -1;
    int count = aux & 0xff;
    int nbtSize = buffer.readShortLE();
    NbtMap compoundTag = null;
    if (nbtSize > 0) {
        try (NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(buffer.readSlice(nbtSize)))) {
            compoundTag = (NbtMap) reader.readTag();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    } else if (nbtSize == -1) {
        int tagCount = buffer.readUnsignedByte();
        if (tagCount != 1)
            throw new IllegalArgumentException("Expected 1 tag but got " + tagCount);
        try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
            compoundTag = (NbtMap) reader.readTag();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    }
    String[] canPlace = new String[VarInts.readInt(buffer)];
    for (int i = 0; i < canPlace.length; i++) {
        canPlace[i] = this.readString(buffer);
    }
    String[] canBreak = new String[VarInts.readInt(buffer)];
    for (int i = 0; i < canBreak.length; i++) {
        canBreak[i] = this.readString(buffer);
    }
    long blockingTicks = 0;
    if (this.isBlockingItem(id, session)) {
        blockingTicks = VarInts.readLong(buffer);
    }
    return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).blockingTicks(blockingTicks).build();
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NBTInputStream(com.nukkitx.nbt.NBTInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 3 with NBTInputStream

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

the class BedrockPacketHelper_v291 method readItem.

@Override
public ItemData readItem(ByteBuf buffer, BedrockSession session) {
    Preconditions.checkNotNull(buffer, "buffer");
    int id = VarInts.readInt(buffer);
    if (id == 0) {
        // We don't need to read anything extra.
        return ItemData.AIR;
    }
    int aux = VarInts.readInt(buffer);
    int damage = (short) (aux >> 8);
    if (damage == Short.MAX_VALUE)
        damage = -1;
    int count = aux & 0xff;
    short nbtSize = buffer.readShortLE();
    NbtMap compoundTag = null;
    if (nbtSize > 0) {
        try (NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(buffer.readSlice(nbtSize)))) {
            Object tag = reader.readTag();
            if (tag instanceof NbtMap) {
                compoundTag = (NbtMap) tag;
            }
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    }
    String[] canPlace = readArray(buffer, new String[0], this::readString);
    String[] canBreak = readArray(buffer, new String[0], this::readString);
    return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).build();
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NBTInputStream(com.nukkitx.nbt.NBTInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 4 with NBTInputStream

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

the class BedrockPacketHelper_v431 method readItemInstance.

@Override
public ItemData readItemInstance(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);
    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).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)

Example 5 with NBTInputStream

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

the class BlockPalette method init.

public static void init() {
    InputStream resourceAsStream = Bootstrap.class.getClassLoader().getResourceAsStream("block_palette.nbt");
    if (resourceAsStream != null) {
        try (NBTInputStream nbtReader = new NBTInputStream(new DataInputStream(new GZIPInputStream(resourceAsStream)))) {
            NbtMap nbtMap = (NbtMap) nbtReader.readTag();
            for (NbtMap blockMap : nbtMap.getList("blocks", NbtType.COMPOUND)) {
                int runtimeId = RUNTIME_COUNTER.getAndIncrement();
                BLOCK_PALETTE.put(runtimeId, blockMap);
                BLOCK_DATA.put(new BlockData(blockMap.getString("name"), blockMap.getCompound("states")), runtimeId);
                DEFAULTS.putIfAbsent(blockMap.getString("name"), runtimeId);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) DataInputStream(java.io.DataInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) NBTInputStream(com.nukkitx.nbt.NBTInputStream) InputStream(java.io.InputStream) NBTInputStream(com.nukkitx.nbt.NBTInputStream) NbtMap(com.nukkitx.nbt.NbtMap) Bootstrap(org.jukeboxmc.Bootstrap) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

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