如何在YAML配置文件中设置/获取HashMap?

时间:2013-07-21 16:51:38

标签: yaml minecraft bukkit

我正在制作我的第一个bukkit插件。我想以编程方式创建一个代表HashMap的YAML文件。如何设置和获取此数据结构?

HashMap参数看起来像<Signature, Location>,其中Signature是我的类,存储4个整数,Locationorg.bukkit.Location

我想我希望YAML文件看起来像这样,但我不确定这种结构是否最好:

MyPlugin:
    ListOfData:
        - signature: [1,2,3,4]    # this is a unique set of 4 integers
          location: [122,64,254]  # non-unique set of 3 integers
        - signature: [4,2,1,2]
          location: [91,62,101]
        - signature: [3,3,1,3]
          location: [190,64,321]
可以根据需要修改

Signature,如有必要,我可以为Location创建一个包装器。

谢谢!

1 个答案:

答案 0 :(得分:2)

这是一个建议的解决方案。我不知道这是不是最好的方式...... :) 您可能希望将此视为您的yaml结构:

MyPlugin:
    ListOfData:
        '[1,2,3,4]': '[122,64,254]'
        '[4,2,1,2]': '[91,62,101]'
        '[3,3,1,3]': '[190,64,321]'
        anothersignature:anotherlocation
        ...

这将让你阅读&#34; ListOfData&#34;使用常规技术从YAMLConfiguration读取哈希映射(见下文)。

您必须将来自文件的传入信息视为&lt; String,String&gt;的HashMap。并从那里进行任何翻译(例如将122,64,254转入某个地点)。

用于阅读HashMap:

this.getConfig().getConfigurationSection("path.to.map").getValues(false)

编写HashMap(仍然需要调用saveConfig()来写入磁盘):

this.getConfig().createSection("path.to.map", MyMap)

这里有一些细节和细微之处,值得仔细阅读(同一页面,但不同的非连续部分):

http://wiki.bukkit.org/Configuration_API_Reference#HashMaps http://wiki.bukkit.org/Configuration_API_Reference#HashMaps_2