I have got class:
class Region{
String name;
List<String> BlockPlayers = new ArrayList<>();
}
Then I made a List of objects in my program like this
List<Region> playersRegions = new ArrayList<>();
Now I need to make a function that will pass objects from playersRegions list to config file. Then I need to make function that will load everything from config file and pass to playersRegions list
I have made something like this
private void save_regions_inConfig()
{
getConfig().set("locs", playersRegions);
saveConfig();
}
But I have no idea how to load it from file to playersRegions. I just want to keep everything after I close the program and open it once more.
CodePudding user response:
Is it gonna work and load everything from file to List?
public List load_playerRegions_fromConfig(List<Region> playersRegions) {
return getConfig().getList("locs");
CodePudding user response:
This should work
public List load_playerRegions_fromConfig(List<Region> playersRegions) {
return getConfig().getList("locs", playersRegions);
}
CodePudding user response:
You should define a way to format the region.
Let assume method are on your JavaPlugin
class and everything else is well made (plugin.yml
valid etc)
To write them, use something like this:
public void saveRegion(Region regions) {
FileConfiguration config = getConfig();
int i = 0;
for(Region r : regions) {
config.set("regions." i ".name", r.name);
config.set("regions." i ".blockplayers", r.BlockPlayers);
i ;
}
saveConfig();
}
The config file will be like this:
regions:
0:
name: "Region 1"
blockplayers:
- "Something"
Now, to read it, you should do something like that:
public List<Region> getRegion() {
ConfigurationSection config = getConfig().getConfigurationSection("regions");
if(config == null) // no region set yet
return new ArrayList<>();
List<Region> list = new ArrayList<>();
for(String keys : config.getKeys(false)) {
ConfigurationSection regionConfig = config.getConfigurationSection(keys);
list.add(new Region(regionConfig.getString("name"), region.getStringList("blockplayers")));
}
return list;
}
Note: you should define a constructor in your Region
object like that:
public class Region{
String name;
List<String> blockPlayers = new ArrayList<>();
public Region(String name, List<String> blockPlayers) {
this.name = name;
this.blockPlayers = blockPlayers;
}
}
Finally, some of your name (method or variable) doesn't meet the Java convention. Specially about method, but that's a detail.