如何从 for 循环遍历 YAML 数据

How to iterate through YAML data from for loop

我在遍历 YAML 文件时遇到问题, config.yml:

motd:
  1: |
    &6MG&1Hub &fWHITELIST &3free
    Factions:
  2: |
    &6MG&1Hub &fWHITELIST &3free
    Bingo:
  3: |
    &6MG&1Hub &fWHITELIST &3free
    Vaaraiant:
  4: |
    &6MG&1Hub &fWHITELIST &3free
    Test:
  5: |
    &6MG&1Hub &fWHITELIST &3free
    PvP:

我可以使用

阅读所有这些内容
this.getConfig().get("motd.1").toString();

但是有一个问题,我无法访问多个。 我知道我可以做类似

的事情
this.getConfig().get("motd."+i).toString();

但问题是,如果我请求类似

this.getConfig().get("motd.6").toString();

program/plugin 会崩溃。

请记住,'motds' 编辑 YAML 的人想要的数量可以有多少。

怎么样

this.getConfig().getConfigurationSection("motd").getValues(false)

来自Bukkit documentation

To get a HashMap, a ConfigurationSection must must first be retrieved. You can return the configuration with getConfigurationSection method. The getValues method will return the values in the ConfigurationSection as a map, it takes a boolean which controls if the nested maps will be returned in the map. You can obtain the original map by invoking getValues(false) on the returned ConfigurationSection. Due to the way Java handles generic classes, type information will be lost, thus a cast will need to be performed to set the original type information. The API makes no guarantees that the cast you perform will be safe.

然后您可以根据需要迭代 HashMap