gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:15:48 25 4
gpt4 key购买 nike

我正在制作我的第一个 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 中读取 HashMap 的常规技术读取“ListOfData”(见下文)。

您必须将来自文件的传入信息视为 的 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

关于yaml - 如何在 YAML 配置文件中设置/获取 HashMap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17774642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com