java将bukkit配置保存到yaml文件的更简单方法
从保存在data文件夹中的yaml文件中保存和加载配置时遇到问题-我声明文件的方式如下(在我的OneTable()中):
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);
}
很抱歉,我给了你这么多的代码,但是我完全处于停滞状态,我不知道如何解决我的问题。我不知道通过制作配置文件,我是否正确地实现了这一点,但是非常感谢您的帮助,谢谢阅读
# 1 楼答案
您可以将所有联盟放在配置中。 要做到这一点
plugin.yml
相同的目录中,创建一个名为config.yml
并将Alliances:
添加到新创建文件的第一行config.yml
onEnable()
方法中,将saveDefaultConfig();
放在它的某个地方-在您在onEnable()中使用config之前,例如在它可能使用config的侦听器类注册之前李>耶!配置现在已设置。现在您可以在代码中使用它了。因此,如果您想用名称
String name = "yellow"
保存联盟的联盟数据,例如,您可以执行getConfig().set("Alliances." + name + ".Prefix", "[Yellow or whatever]");
,这将被视为在
config.yml
中现在,如果您想添加一个新联盟,只需执行
getConfig().set("Alliances.Green.arrowexplosive", 2);
,配置现在如下所示:如果您想检查是否设置了某些内容,只需执行
getConfig().isSet("Alliances.yellow.Prefix");
如果要列出所有联盟的名称,可以执行
Set<String> allianceNames = getConfig().getConfigurationSection("Alliances").getKeys(false);
,这将返回Set
个联盟名称。现在,您可以使用该列表从每个联盟获取信息: