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



我正在制作我的第一个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创建一个包装器。

谢谢!

这是一个建议的解决方案。我不知道这是否是最好的方式…:)您可能想将其视为您的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
        ...

这将允许您使用从YAMLConfiguration读取哈希映射的常规技术读取中的"ListOfData"(请参阅下文)。

您必须将来自文件的传入信息视为<字符串,字符串>并从那里进行您需要的任何翻译(例如,将122,64254转到一个位置)。

用于读取哈希映射:

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

对于写入HashMap(仍然需要调用saveConfig()才能写入磁盘):

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

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

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

最新更新