我無法保存和加載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);
}
我很抱歉,香港專業教育學院給您帶來這麼很多代碼,但我在完全停滯,我不知道如何解決我的問題。我不知道我正在通過嘗試創建配置文件來正確地處理這個問題,但是和幫助將不勝感激,感謝您閱讀
什麼樣的問題,你遇到或什麼樣的預期行爲沒有發生?你在控制檯中是否有任何錯誤? –
你爲什麼不把所有的聯盟放在一個yml中,並用鑰匙把它們分開? – gogobebe2
@ gogobebe2請你解釋一下如何做到這一點? – JavaNoob