2017-12-27 836 views
1

我正在做一個Minecraft Bukkit插件,並且我的配置文件有問題。配置文件給我值隨意類

當我嘗試從配置中獲取值時,使用getConfig().getString(path),它可以很好地工作。但現在,我想從我的配置加載隊的名單中,YAML,它們分別是:

teams: 
    list: 
     - ArnyminerZ 
    ArnyminerZ: 
     players: [] 
     prefix: '' 
     suffix: '' 
     dispname: ArnyminerZ 
     seeinvbuddies: false 
     friendlyfire: false 
     color: WHITE 

起初,我是用for(String team : config.createSection("teams").getKeys(false)),和它的工作,但suddently,一些修改後,它停止工作,我改變了我的方法加載一個字符串列表,我不知道爲什麼,它有點奇怪。

在我onEnable()方法,我有這樣的:

public FileConfiguration config; 

public static AlcoasUHC plugin; 

@Override 
public void onEnable() { 
    plugin = this; 

    config = this.getConfig(); 
    loadConfigDefaults(); 

    getLogger().info("Alcoas UHC -> Enabled! There are " + scoreboard.getTeams().size() + " teams registered."); 
} 

而且在外部類中,我有方法getTeams()

public List<String> getTeams() { 
    return plugin.config.getStringList("teams.list"); 
} 

loadConfigDefaults()方法只加載某些特定值的配置,與config.addDefault(path, value);config.options().copyDefaults(false);

在這第一種方法中,加載工作正常,它調試Alcoas UHC -> Enabled! There are 1 teams registered.。但是,當我嘗試再次裝入球隊,例如,在這種方法:

} else if (args[0].equalsIgnoreCase("teams")) { 
    List<String> teams = scoreboard.getTeams(); 
    if (teams.size() <= 0) { 
     SendMessage.sendMessage(sender, 
    config.getString("messages.anyTeamCreated")); 
    } else { 
     StringBuilder printingTeams = new StringBuilder(); 
     for (String team : scoreboard.getTeams()) { 
      if (teams.toString().equals("")) { 
       printingTeams.append(AndColor.GOLD).append(team); 
      } else { 
       printingTeams.append(AndColor.GREEN).append(", ").append(AndColor.GOLD).append(team); 
      } 
     } 
     SendMessage.sendMessage(sender, config.getString("messages.availableTeams").replace("%at%", printingTeams)); 
    } 
} 

它返回我的空單在服務器日誌,並在遊戲運行此行config.getString("messages.anyTeamCreated"));,這意味着名單空。

我該怎麼辦?難道我做錯了什麼?

我使用的服務器git-Spigot-549c1fa-45c8386,實現API版本1.12.2-R0.1-SNAPSHOT,與我的世界1.12.2,但我是

IntelliJ IDEA 2017.3.1 (Community Edition) 
Build #IC-173.3942.27, built on December 11, 2017 
JRE: 1.8.0_152-release-1024-b8 amd64 
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o 
Windows 10 10.0 
+1

'createSection'將覆蓋任何設置值。而是使用'getConfigurationSection'。還要加載一次團隊列表,然後在所有計算中使用該列表,而不是每次使用時從配置中加載 – Squiddie

+0

好的。我要試一試 –

+0

確定,完美的工作,謝謝。 –

回答

1

發佈者的bn4t要求編程給Arnyminer機會接受答案並公開解決這個問題。

根據Bukkit的API爲ConfigurationSectioncreateSection(String path)有這樣的描述方法:「以前設置在此路徑將被覆蓋的任何價值。」 這意味着當您執行該方法時,它將實際清理包含您的團隊的部分,因爲它將被覆蓋,導致沒有團隊被加載。

你想使用的是getConfigurationSection(String path),這將返回正確的部分,如果它存在。這不會覆蓋任何內容。

或者你可以使用它們兩者的混合物。如果getConfigurationSection(String path)返回空(如果該部分根本不存在),則使用createSection(String path)創建一個新的。就像這樣:

ConfigurationSection section = getConfigurationSection("your.path.here"); if(section == null) { section = createSection("your.path.here"); }

而且這不會有任何與你的問題,但我強烈建議加載僅當它是必要的,當服務器啓動例如像配置的球隊,而不是每你使用它的時間。

相關問題