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