我在保存和加载我保存在数据文件夹中的yaml文件中的配置时遇到问题 - 我声明文件的方式如下(在我的onEnable()中):
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)
您可以将所有联盟放入配置中。 要做到这一点......
NSString
相同的目录中,创建一个名为的新文件
plugin.yml
并在新创建的文件config.yml
Alliances:
config.yml
方法中,将onEnable()
放在其中的某个地方 - 然后你在onEnable()中使用config做任何事情,例如在监听器之前可以使用配置的类注册。耶!现在设置配置。现在您可以在代码中使用它。因此,如果您想要使用名称saveDefaultConfig();
保存联盟的联盟数据,您可以执行String name = "yellow"
这将被视为
getConfig().set("Alliances." + name + ".Prefix", "[Yellow or whatever]");
<{1>}。
现在,如果您想添加新的联盟,请执行Alliances:
yellow:
Prefix: '[Yellow or whatever]'
,配置现在将如下所示:
config.yml
如果您想检查是否设置了某些内容,请执行getConfig().set("Alliances.Green.arrowexplosive", 2);
如果您要列出所有联盟的名称,您可以执行Alliances:
yellow:
Prefix: '[Yellow or whatever]'
Green:
arrowexplosive: 2
并返回getConfig().isSet("Alliances.yellow.Prefix");
个联盟名称。现在,您可以使用该列表从每个联盟中获取信息:
Set<String> allianceNames = getConfig().getConfigurationSection("Alliances").getKeys(false);