2015-10-31 36 views
-2

我無法保存和加載yaml文件中的配置我已經保存在數據文件夾 - 我聲明文件的方式如下(在我的onEnable()) :更簡單的方法來保存bukkit配置到yaml文件

public static File    configFile; 
public static FileConfiguration config; 

public static File    yellowAllianceFile; 
public static FileConfiguration yellowAlliance; 

public static File    blueAllianceFile; 
public static FileConfiguration blueAlliance; 

public static File    greenAllianceFile; 
public static FileConfiguration greenAlliance; 

public static File    redAllianceFile; 
public static FileConfiguration redAlliance; 

configFile = new File(getDataFolder(), "config.yml"); 
    yellowAllianceFile = new File(getDataFolder(), "yellow.yml"); 
    blueAllianceFile = new File(getDataFolder(), "blue.yml"); 
    greenAllianceFile = new File(getDataFolder(), "green.yml"); 
    redAllianceFile = new File(getDataFolder(), "red.yml"); 

    config = new YamlConfiguration(); 
    yellowAlliance = new YamlConfiguration(); 
    blueAlliance = new YamlConfiguration(); 
    greenAlliance = new YamlConfiguration(); 
    redAlliance = new YamlConfiguration(); 

我將它們設置爲如下 -

public void onDisable(){ 

    log.log(Level.INFO, String.format("[%s] Successfully disabled version %s!", getDescription().getName(), getDescription().getVersion())); 
    try { 
     config.save(configFile); 
     yellowAlliance.save(yellowAllianceFile); 
     blueAlliance.save(blueAllianceFile); 
     greenAlliance.save(greenAllianceFile); 
     redAlliance.save(redAllianceFile); 
     saveAll(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 


    try { 

    if (!yellowAllianceFile.exists()){ 



      yellowAlliance.createSection("Players"); 
      yellowAlliance.save(yellowAllianceFile); 

    } 

if (!blueAllianceFile.exists()){ 



      blueAlliance.createSection("Players"); 
      blueAlliance.save(blueAllianceFile); 

    } 

if (!greenAllianceFile.exists()){ 



    greenAlliance.createSection("Players"); 
    greenAlliance.save(greenAllianceFile); 

} 

if (!redAllianceFile.exists()){ 



    redAlliance.createSection("Players"); 
    redAlliance.save(redAllianceFile); 

} 
    }catch(IOException e){ 
     e.printStackTrace(); 
    } 



} 

public void reloadAll(){ 

    if (!this.config.isSet("Prefix")){ 
     config.set("Prefix", "&9[&4&l%PLUGIN_NAME%&9]"); 
    } 
    if (!this.config.isSet("arrowexplosive")){ 
     config.set("arrowexplosive", 5);  
    } 


    War.prefix = ChatColor.translateAlternateColorCodes('&', config.getString("Prefix").replace("%PLUGIN_NAME%", getDescription().getName())); 
    War.arrowexplosive = config.getDouble("arrowexplosive"); 
    } 
} 

我試圖把這個插件就像是一個衆所周知的一個由「派系」,所以我希望它能夠變有四個不同的配置文件,數據庫,甚至可以存儲聯盟中包含的所有玩家的類。我現在抽象的聯盟類是 -

package me.gmx.war; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.UUID; 

import org.bukkit.ChatColor; 
import org.bukkit.entity.Player; 

public class Alliance { 

    List<UUID> players; 
    String name; 
    ChatColor color; 

    public String getName(){ 
     return name; 
    } 

    public ChatColor getColor(){ 
     return color; 
    } 

    public Alliance(String name, ChatColor color, List<UUID> players){ 
     this.name=name; 
     this.color = color; 
     this.players = players; 
    } 


public void savePlayers(List<UUID> list){ 
     this.players = list; 
    } 

    public void addPlayer(Player player){ 
     players.add(player.getUniqueId()); 
    } 


    public List<UUID> getPlayers(){ 
     return players; 
    } 

    public boolean checkList(List<?> list){ 
     return (list == new ArrayList<UUID>()); 
    } 

} 

然後我有4個不同的類擴展這一個。這是這些類

package me.gmx.war.alliances; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.UUID; 

import org.bukkit.ChatColor; 
import org.bukkit.configuration.file.FileConfiguration; 
import org.bukkit.entity.Player; 

import me.gmx.war.Alliance; 

public class AllianceYellow extends Alliance { 

private static List<UUID> players; 

FileConfiguration config; 
public AllianceYellow(FileConfiguration config) { 
    super("Electrium",ChatColor.YELLOW,(List<UUID>)   config.getList("Players")); 
    this.config = config; 
    this.players = ((List<UUID>) config.get("Players")); 
} 

public static List<String> getPlayerss(){ 
    List<String> playez = new ArrayList<String>(); 
    for (UUID u : players){ 
     playez.add(u.toString()); 
    } 
    return playez; 
} 
public static void addPlayerTo(Player p){ 
    players.add(p.getUniqueId()); 
} 
} 

最後但並非最不重要的一個,這是我在嘗試測試配置(通過命令) -

if (args[0].equalsIgnoreCase("test") && args.length == 1){ 
      Player p = (Player)sender; 
      InvenUtil.classSelInv(p); 
      AllianceYellow.addPlayerTo(p); 
      for (String s : AllianceYellow.getPlayerss()){ 
       p.sendMessage(s); 
      } 


      p.sendMessage((String) config.get("Prefix")); 
      for (String s : AllianceYellow.getPlayerss()){ 
       p.sendMessage(s); 
      } 

我很抱歉,香港專業教育學院給您帶來這麼很多代碼,但我在完全停滯,我不知道如何解決我的問題。我不知道我正在通過嘗試創建配置文件來正確地處理這個問題,但是和幫助將不勝感激,感謝您閱讀

+0

什麼樣的問題,你遇到或什麼樣的預期行爲沒有發生?你在控制檯中是否有任何錯誤? –

+0

你爲什麼不把所有的聯盟放在一個yml中,並用鑰匙把它們分開? – gogobebe2

+0

@ gogobebe2請你解釋一下如何做到這一點? – JavaNoob

回答

0

您可以將所有聯盟放在配置中。 要做到這一點...

  1. 在相同的目錄中plugin.yml,創建一個名爲 config.yml新文件和新創建的文件的第一行添加Alliances:config.yml
  2. 你想現在是什麼do在你的main的onEnable()方法中,在其中的某處放置saveDefaultConfig(); - 在你使用onEnable()中的任何配置之前,例如在Listener類註冊之前它可能使用config。
  3. 耶!配置現在已設置。現在你可以在你的代碼中使用它。所以,如果你想保存聯盟的聯盟數據的名稱String name = "yellow",例如,你可以做getConfig().set("Alliances." + name + ".Prefix", "[Yellow or whatever]");這將被視爲

    Alliances: 
        yellow: 
        Prefix: '[Yellow or whatever]' 
    
    config.yml

  4. 現在,如果你想添加一個新的聯盟只是做getConfig().set("Alliances.Green.arrowexplosive", 2);而配置現在看起來是這樣的:

    Alliances: 
        yellow: 
        Prefix: '[Yellow or whatever]' 
        Green: 
        arrowexplosive: 2 
    
  5. 如果你想檢查是否某項設置,只是做getConfig().isSet("Alliances.yellow.Prefix");

  6. 如果你想列出所有聯盟的名字,你可以這樣做Set<String> allianceNames = getConfig().getConfigurationSection("Alliances").getKeys(false);,那將會返回一個Set的聯盟名稱。現在,您可以使用該列表從每個聯盟得到的東西:

    for (String allianceName : allianceNames) { 
        int prefix; 
        String pathToPrefix = "Alliances." + allianceName + ".Prefix" 
        if (getConfig().isSet(path) { 
         prefix = getConfig().getString(path); 
        } 
    } 
    
相關問題