我正在制作我的第一个 bukkit 插件。我想以编程方式创建一个代表 HashMap
的 YAML 文件.如何设置和获取这个数据结构?
HashMap
参数看起来像 <Signature, Location>
, 其中Signature
我的类是存储 4 个整数,Location
是org.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 中读取 HashMap 的常规技术读取“ListOfData”(见下文)。
您必须将来自文件的传入信息视为
对于读取 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
关于yaml - 如何在 YAML 配置文件中设置/获取 HashMap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17774642/