Search in sources :

Example 1 with LittleEndianByteBufInputStream

use of com.nukkitx.protocol.bedrock.util.LittleEndianByteBufInputStream 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 2 with LittleEndianByteBufInputStream

use of com.nukkitx.protocol.bedrock.util.LittleEndianByteBufInputStream 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)2 NbtMap (com.nukkitx.nbt.NbtMap)2 LittleEndianByteBufInputStream (com.nukkitx.protocol.bedrock.util.LittleEndianByteBufInputStream)2 ByteBuf (io.netty.buffer.ByteBuf)2 IOException (java.io.IOException)2