How I load custom configs?

Discussion in 'Spigot Plugin Development' started by relampagorojo93, Jun 20, 2015.

  1. Well, I'm creating a new plugin, my third plugin, and it's my first plugin that I use more than one config file. I'm creating a file that contents the status players, if they have the plugin enabled or disabled. My problem is that: When I enable the plugin, it don't loads the config. I want to ask what's the code to load custom yml, because when I join, I put a code that creates a section with the player name and False, and the True of the config is replaced.

    public void onPlayerJoin(PlayerJoinEvent e) {
    Player p = e.getPlayer();
    String name = p.getName();
    if (!e.getPlayer().hasPlayedBefore() || !(yamlFile.contains(name))) {
    try {
    yamlFile.createSection("Players." + name);
    yamlFile.set("Players." + name, "False");
    yamlFile.save(f);
    reloadConfig();

    } catch (IOException ev) {
    ev.printStackTrace();
    }
    }
    }

    On enable:

    Players:
    relampagorojo93: 'True'
    ShadowRaquel: 'True'
    Globuss: 'False'

    When I join:

    Players:
    relampagorojo93: 'False'
    ShadowRaquel: 'True'
    Globuss: 'False'
     
  2. public class Main extends JavaPlugin implements Listener
    {
    File f = new File("plugins/RideablePlayers/Players.yml");
    YamlConfiguration yamlFile = YamlConfiguration.loadConfiguration(f);
    public void onEnable()
    {
    getConfig().options().copyDefaults(true);
    saveConfig();
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    if (!f.exists()){
    try {
    f.createNewFile();
    yamlFile.createSection("Players");
    yamlFile.save(f);
    reloadConfig();
    } catch (IOException ev) {
    ev.printStackTrace();
    }
    }
    if (f.exists()){
    try {
    yamlFile.save(f);
    } catch (IOException ev) {
    ev.printStackTrace();
    }
    }
    if (getConfig().getString("Silent-loading") == "false")
    {
    Bukkit.getServer().getLogger().info("[" + getConfig().getString("Prefix") + "] Loaded Succesfully");
    Bukkit.getServer().getLogger().info("[" + getConfig().getString("Prefix") + "] Right Click in a player to ride him");
    }
    else if (((getConfig().getString("Silent-loading") == "false" ? 1 : 0) | (getConfig().getString("Silent-loading") == "true" ? 1 : 0)) == 0)
    {
    Bukkit.getServer().getLogger().info("[" + getConfig().getString("Prefix") + "] Loaded Succesfully");
    Bukkit.getServer().getLogger().info("[" + getConfig().getString("Prefix") + "] Silent-loading must be true or false");
    }
    }

    public void onDisable()
    {
    if (getConfig().getString("Silent-loading") == "false")
    {
    Bukkit.getServer().getLogger().info("[" + getConfig().getString("Prefix") + "] Unloaded Succesfully");
    Bukkit.getServer().getLogger().info("[" + getConfig().getString("Prefix") + "] Thank you for using my plugin :)");
    }
    else if (((getConfig().getString("Silent-loading") == "false" ? 1 : 0) | (getConfig().getString("Silent-loading") == "true" ? 1 : 0)) == 0)
    {
    Bukkit.getServer().getLogger().info("[" + getConfig().getString("Prefix") + "] Unloaded Succesfully");
    Bukkit.getServer().getLogger().info("[" + getConfig().getString("Prefix") + "] Silent-loading must be true or false");
    }
    getConfig().options().copyDefaults(true);
    saveConfig();
    }
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
    Player p = e.getPlayer();
    String name = p.getName();
    if (!e.getPlayer().hasPlayedBefore() || !(yamlFile.contains(name))) {
    try {
    yamlFile.createSection("Players." + name);
    yamlFile.set("Players." + name, "False");
    yamlFile.save(f);
    reloadConfig();
    } catch (IOException ev) {
    ev.printStackTrace();
    }
    }
    }

    This is the essential part of this problem. The commands are configured good, the problem is the enable code, I don't now how to load the players.yml. The other code is only for right click and commands.
     
  3. Try typing

    this.saveFile and this.saveConfig
     
  4. Using this I need to specify the file or it's going to save the file?
     
    #5 relampagorojo93, Jun 20, 2015
    Last edited: Jun 20, 2015
  5. What do you mean by load? reload would simply be this.reloadConfig or something. Try this.reloadFile(f) or something... it shouldn't be this hard...
     
  6. Well, ok. I tryed now: the reload command don't load the new config. this.reloadConfig doesn't work.
     
  7. remove the setSection parts,

    don't use this.reloadConfig(), thats for config.yml and not your custom config

    EDIT: Ill be pushing some code to github in a minute, ill link you to it. Should help explain things better
     
  8. The setSection is to create the yml. The yml isn't created in physical, and this put the first part of the yml. Only I want to know the code to load custom yml. With that I will finish it.
     
  9. The ymal file already exists, you made it in the onEnable method.
     
  10. Yes, I know it, the code of onEnable method creates the file if it doesn't exists. But the code doesn't load the current file with their configs.
     
  11. Now nothing, I found the code.