2016-07-13 168 views
1

我正在嘗試使用標準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" 
+0

你的'if'語句太長了。拆分它。將配置部分拉到本地變量中,並確保它實際上包含您所期望的。通過這種方式,您將能夠找出NPE發生的位置。另外,發佈實際的錯誤消息將提供相當多的有用信息。 –

+0

用這種方式編寫代碼很難說。我看到兩個可能的選項:Main.config.getConfigurationSection(「Banned」)或Main.config.getConfigurationSection(「Banned」)。getKeys(true)獲取null。 –

+0

我添加了堆棧跟蹤。服務器通過第47行引發異常(如果(不是通過getConfigurationSection(),而是通過getKeys(true)...))。 – Woulfiee

回答

0

爲了實現你的願望,你應該嘗試以下操作:

  • 確保你的配置不爲空/存在

    布爾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的,除非你使用它別的東西

+0

謝謝你的回答,但它仍然不起作用。服務器通過「Set keys = banned.getKeys(false);」引發異常。如果它不一定是一個HashMap那麼它應該是什麼?一個列表? – Woulfiee

+0

它顯示的新錯誤是什麼? 這是從配置文件中恢復列表的正確方式 也許你可以在原始問題中編輯更多代碼,因爲錯誤是由於其他原因而被拋出 – Kerooker

+0

它會拋出NPE – Woulfiee

0

getConfigurationSection

如果ConfigurationSection不存在,但默認值爲 已經過篩指定,這將返回默認值。如果 ConfigurationSection不存在,並且沒有指定默認值 ,則它將返回空值。

如果沒有禁止用戶我猜,沒有Banned部分,所以getConfigurationSection回報null,這就是爲什麼你getKeys()調用拋出一個NPE。

所以你應該首先檢查配置部分是否存在,然後才嘗試使用它。