File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -364,4 +364,30 @@ try (var writer = Files.newBufferedWriter(Path.of("test.snbt"))) {
364364
365365``` java
366366String 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```
You can’t perform that action at this time.
0 commit comments