Skip to content

Commit a2cbea3

Browse files
committed
Add documentation for heterogeneous List tags in Chinese tutorial
1 parent eed7c72 commit a2cbea3

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

docs/Tutorial_zh.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,30 @@ try (var writer = Files.newBufferedWriter(Path.of("test.snbt"))) {
364364

365365
```java
366366
String snbt = SNBTCodec.of().toString(tag);
367+
```
368+
369+
### 异构 List 标签
370+
371+
与 NBT 的二进制表示不同,SNBT 支持异构 List 标签。
372+
373+
在 SNBT 中,如果 List 标签包含不同类型的元素,那么所有非 Compound 标签都会被转换为一个 Compound 标签,
374+
这个 Compound 标签中包含一个名称为空的子标签,子标签的值为原始的非 Compound 标签。
375+
376+
HelloNBT 的 `ListTag` 类支持模拟这种行为。
377+
378+
为了方便的模拟异构 List 标签,在构造 `ListTag` 时不应该传入元素类型参数:
379+
380+
```java
381+
ListTag<Tag> listTag = new ListTag<>();
382+
```
383+
384+
对于这样的 List 标签,应当使用 `addAnyTag` 方法添加子标签:
385+
386+
```java
387+
listTag.addAnyTag(new IntTag(123))
388+
listTag.addAnyTag(new StringTag("HelloNBT"));
389+
390+
assert listTag.equals(new ListTag<>(TagType.COMPOUND)
391+
.addTag(new CompoundTag().addInt("", 123))
392+
.addTag(new CompoundTag().addString("", "HelloNBT")));
367393
```

0 commit comments

Comments
 (0)