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();
}
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();
}
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();
}
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();
}
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();
}
}
}
Aggregations