如何从.yml文件正确加载HashMap?
我正在尝试使用标准Bukkit配置文件API从配置文件加载HashMap。如何从.yml文件正确加载HashMap?
的HashMap:
public static HashMap<String, String> banned = new HashMap<String, String>();
这是我试图获取数据的方式:
public static boolean isBanned(String uuid) { if (Dogends.config.getConfigurationSection("Banned").getKeys(true).contains(uuid)) {
return true;
}
return false;
}
如果玩家被禁止,然后它的确定,但是当玩家没有被禁止,则它抛出一个NullPointerException。
NullPointerException异常:
Could not pass event PlayerLoginEvent to Dogends v1.0 org.bukkit.event.EventException
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[cb.jar:git-Bukkit-880a532]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[cb.jar:git-Bukkit-880a532]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [cb.jar:git-Bukkit-880a532]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [cb.jar:git-Bukkit-880a532]
at net.minecraft.server.v1_8_R3.PlayerList.attemptLogin(PlayerList.java:439) [cb.jar:git-Bukkit-880a532]
at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:89) [cb.jar:git-Bukkit-880a532]
at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:53) [cb.jar:git-Bukkit-880a532]
at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:222) [cb.jar:git-Bukkit-880a532]
at net.minecraft.server.v1_8_R3.ServerConnection.c(SourceFile:168) [cb.jar:git-Bukkit-880a532]
at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:744) [cb.jar:git-Bukkit-880a532]
at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [cb.jar:git-Bukkit-880a532]
at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:628) [cb.jar:git-Bukkit-880a532]
at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:536) [cb.jar:git-Bukkit-880a532]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
Caused by: java.lang.NullPointerException
at me.woulfiee.server.ban.BanCommand.isBanned(BanCommand.java:47) ~[?:?]
at me.woulfiee.server.ban.BanCommand.onPlayerLogin(BanCommand.java:103) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[cb.jar:git-Bukkit-880a532]
... 13 more
config.yml:
Ranks: Player:
Players: []
Mythic:
Players: []
Doge:
Players: []
Youtuber:
Players: []
Builder:
Players: []
Mod:
Players: []
Admin:
Players: []
Owner:
Players:
- d166739c-32d3-4b37-a1be-883be57d736c
Broadcast:
Interval: 120
Banned:
d166739c-32d3-4b37-a1be-883be57d736c: "CONSOLE \xa7eHELP"
回答:
为了实现你的愿望,你应该尝试以下操作:
确保你的配置不为空/存在
布尔isBanned(字符串UUID){
FileConfiguration yourConfig;
//Getting the Banned section
ConfigurationSection banned = yourConfig.getConfigurationSection("Banned");
//All the keys inside the banned configuration section
Set<String> keys = banned.getKeys(false); //We don't want it to be deep
if (keys.contains(uuid))return true; //UUID is on the keys list, so the player is banned
return false; //UUID is not on the keys list, so the player is not banned
}
我不相信你真的需要HashMap的,除非你使用它别的东西
回答:
getConfigurationSection:
如果ConfigurationSection不存在,但默认值为 已经过筛指定,这将返回默认值。如果 ConfigurationSection不存在,并且没有指定默认值 ,则它将返回空值。
如果没有禁止用户我猜,没有Banned
部分,所以getConfigurationSection
回报null
,这就是为什么你getKeys()
调用抛出一个NPE。
所以你应该首先检查配置部分是否存在,然后才尝试使用它。
以上是 如何从.yml文件正确加载HashMap? 的全部内容, 来源链接: utcz.com/qa/258448.html