Java 自定义哈希映射的 YAML 配置

Java YAML Configuration to a custom hashmap

我目前正在编写一个 bukkit 插件,它在 YAML 配置文件中存储有关播放器的大量信息。现在我希望插件在服务器启动时读取 YAML 文件,然后添加该信息。我有我的装载机,但我不能使用它,因为我的插件使用自定义地图。这是地图的代码:

Map<Integer, Map<String, Object>>

下面是从文件中获取信息的代码:

info = (Map<Integer, Map<String, Object>>) ticket.getConfigurationSection("tickets");

但是当我尝试 运行 使用那行代码的插件时,我得到了这个错误:

Caused by: java.lang.ClassCastException: org.bukkit.configuration.MemorySection cannot be cast to java.util.Map

完整代码张贴在这里:http://pastebin.com/Xgu8hwM0

解决这个问题的方法是不使用自定义地图。您已经从配置中获得 MemorySection

使用它。您应该使用以下方法代替强制转换:getValues(boolean) which returns a Map<String, Object> 包含所有相关信息并由接口 ConfigurationSection.

指定
ticket.getConfigurationSection("tickets").getValues();

另请参阅 bukkit's Configuration API Reference 处的相关摘录:

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.

是的,我解决了这个问题。我 HAD 使用 Map<String, Object> 但它起作用了,因为我使用它的方式 (Map<Integer, Map<String, Object>>) 是第二个部分!