这是使用 YAML 的有效方法吗?

发布于 2024-12-18 02:18:02 字数 920 浏览 0 评论 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 缺乏了解,这使我很难获得好的结果。

I am new to snakeyaml and yaml in general. I need it to store information about "rooms" for a MUD.

The entries for the rooms will look something like this:

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!"

Basically, each room has an id and some text to be displayed to the player when they enter/search it. It also has an array of "objects" which are themselves declared in the same yaml file.

Is this configuration within my yaml file possible? Also, I would need to extract into arrays each room and each object, so it looks like this:

[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 ... ]]

This configuration makes it easy for me to parse the file and create GenericRoom and GenericObject classes by making one single loop and referencing every value by array position. Is this something SnakeYAML can do for me? I've been playing around with some examples but my lack of knowledge in actual YAML is making it hard for me to get good results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的轨迹 2024-12-25 02:18:02

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

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

With this you have to connect objects to rooms yourself:

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}

or SnakeYAML can benefit from anchors and aliases:
(anchors must be defined before aliases are used)

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]

(You can check your documents here: http://www.yaml.org/spec/1.2/spec.html#id2765878)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文