0

我一般是snakeyaml和yaml的新手。我需要它来存储有关MUD的“房间”的信息。

房间的条目将如下所示:

room:
  id: 12
  entry: "Long string"
  description: "Longer more precise string"
  objects:
    ids: 1,23

object:
  id: 1
  name: "chest"
  description: "looks pretty damn old"
  on-text: "the chest has been opened!"
  off-text: "the chest has been closed!"

基本上,每个房间都有一个id和一些文本要在玩家进入/搜索时显示给玩家。它还有一个“对象”数组,它们本身在同一个 yaml 文件中声明。

我的 yaml 文件中的这种配置是否可行?另外,我需要将每个房间和每个对象提取到数组中,所以它看起来像这样:

[12, "long string", "Longer more precise string", [1, "chest", "looks pretty damn old", "the chest has been opened!", "the chest has been closed!"], [ ... item 23 ... ]]

这种配置使我可以轻松地解析文件并通过创建一个循环并通过数组位置引用每个值来创建 GenericRoom 和 GenericObject 类。这是 SnakeYAML 可以为我做的事情吗?我一直在玩一些例子,但我对实际 YAML 缺乏了解,这让我很难得到好的结果。

4

1 回答 1

2

有了这个,您必须自己将对象连接到房间:

room:
  id: 12
  entry: "Long string"
  objects: [1, 23]

objects:
  - { id: 1, text: bla bla }
  - { id: 2, text: bla bla 2 }
  - { id: 23, text: bla bla 23}

或者 SnakeYAML 可以从锚点和别名中受益:(必须在使用别名之前定义锚点)

objects:
  - &id001 {id: 1, text: bla bla }
  - &id002 {id: 2, text: bla bla 2 }
  - &id023 {id: 23, text: bla bla 23 }

room:
  id: 12
  entry: "Long string"
  objects: [ *id001, *id023]

(您可以在此处查看您的文档:http ://www.yaml.org/spec/1.2/spec.html#id2765878 )

于 2011-11-25T11:17:35.537 回答