Reputation: 6073
I am making a Minecraft Bukkit Plugin, and I have an issue with my configuration file.
When I try to get values from the configuration, with getConfig().getString(path)
, it works perfectly. But now, I am trying to load a list of teams from my config, in YAML, they are:
teams:
list:
- ArnyminerZ
ArnyminerZ:
players: []
prefix: ''
suffix: ''
dispname: ArnyminerZ
seeinvbuddies: false
friendlyfire: false
color: WHITE
At first, I was using for(String team : config.createSection("teams").getKeys(false))
, and it worked, but suddently, after some modifications, it stopped working, and I changed my method to loading a string list, and I don't know why, it works kind of strange.
In my onEnable()
method, I have this:
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.");
}
And in an external class, I have the method getTeams()
:
public List<String> getTeams() {
return plugin.config.getStringList("teams.list");
}
The loadConfigDefaults()
method simply loads some predefined values to the configuration, with config.addDefault(path, value);
and config.options().copyDefaults(false);
.
In this first method, the loading works ok, it debugs Alcoas UHC -> Enabled! There are 1 teams registered.
. But when I try to load the teams again, for example, in this method:
} 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));
}
}
It returns me an empty list in the server log, and in the game it runs this line config.getString("messages.anyTeamCreated"));
, meaning that the list is empty.
What can I do? Am I doing something wrong?
I am using as Server git-Spigot-549c1fa-45c8386
, Implementing API version 1.12.2-R0.1-SNAPSHOT
, with Minecraft 1.12.2, and I am programming with
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
Upvotes: 0
Views: 155
Reputation:
Posted by the request of bn4t to give Arnyminer a chance to accept the answer and solve this question publicly.
According to Bukkit's API for ConfigurationSection the method createSection(String path)
has this description: "Any value that was previously set at this path will be overwritten."
This means when you execute that method, it will actually clean the section containing your teams as it will be overwritten, resulting in no teams being loaded.
What you want to use is getConfigurationSection(String path)
, this will return the proper section if it exists. This doesn't override anything.
Or you could use a mixture of both of them. If getConfigurationSection(String path)
returns null (if the section doesn't exist at all), create a new one using createSection(String path)
. Like this:
ConfigurationSection section = getConfigurationSection("your.path.here");
if(section == null) {
section = createSection("your.path.here");
}
Also this doesn't have anything to do with your problem, but I highly recommend loading the teams from the config only when it's necessary, like when server starts for example, instead of every time you're using it.
Upvotes: 1