Solved Custom Config Always Resetting

Discussion in 'Plugin Development' started by Epicballzy, Apr 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Epicballzy

    I made a custom config and I've set it so it will set the strings in the config, however, the strings get set but every time I try to change the config and reload the server, the config just resets it self to its original state.

    Code:java
    1. File messages = null;
    2. FileConfiguration messageConf = null;
    3.  
    4. public void onEnable(){
    5. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    6. this.messages = new File(getDataFolder(), "messages.yml");
    7. this.messageConf = YamlConfiguration.loadConfiguration(this.messages);
    8. this.messageConf.options().copyDefaults(true);
    9. messageConf.set("Join", "&3[&bJoin&3] &9%PLAYER has arrived!");
    10. messageConf.set("Quit", "&3[&bQuit&3] &9%PLAYER has left!");
    11. saveConf();
    12. }


    How would I fix this?
     
  2. Offline

    BillyGalbreath

    Check if the file already exists before trying to load it. Then you can put those defaults in an if condition.
     
  3. Offline

    Epicballzy

    BillyGalbreath
    I've come up with this:
    Code:java
    1. File messages = null;
    2. FileConfiguration messageConf = null;
    3.  
    4. public void onEnable(){
    5. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    6. this.messages = new File(getDataFolder(), "messages.yml");
    7. this.messageConf = YamlConfiguration.loadConfiguration(this.messages);
    8. this.messageConf.options().copyDefaults(true);
    9. try {
    10. saveConf();
    11. setupConfig(messageConf);
    12. saveConf();
    13. } catch(Exception e) {
    14. e.printStackTrace();
    15. }
    16. }
    17.  
    18. public void saveConf(){
    19. try{
    20. messageConf.save(messages);
    21. }catch(Exception e){
    22. e.printStackTrace();
    23. }
    24. }
    25.  
    26. private void setupConfig(FileConfiguration config) throws IOException {
    27. if(!new File(getDataFolder(), "messages.yml").exists()) {
    28. new File(getDataFolder(), "messages.yml").createNewFile();
    29. messageConf.set("Join", "&3[&bJoin&3] &9%PLAYER has arrived!");
    30. messageConf.set("Quit", "&3[&bQuit&3] &9%PLAYER has left!");
    31. }
    32. }


    But now, the yml file gets created but the yml file does not generate the strings Join: and Quit: ... Any ideas?
     
  4. Offline

    tbrooks23

    instead of
    Code:java
    1. messageConf.set(....);
    2.  
    3. //Put
    4.  
    5. messageConf.addDefault(....);
    6. messageConf.options().copyDefaults(true);
     
  5. Offline

    Epicballzy

    tbrooks23 It still doesn't work. Ill just show you my whole class.

    Code:java
    1. package com.config;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14. import org.bukkit.event.player.PlayerQuitEvent;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Main extends JavaPlugin implements Listener {
    18.  
    19. public final Logger logger = Logger.getLogger("Minecraft");
    20.  
    21. File messages = null;
    22. FileConfiguration messageConf = null;
    23.  
    24. public void onEnable(){
    25. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    26. this.messages = new File(getDataFolder(), "messages.yml");
    27. this.messageConf = YamlConfiguration.loadConfiguration(this.messages);
    28. this.messageConf.options().copyDefaults(true);
    29. try {
    30. saveConf();
    31. setupConfig(messageConf);
    32. saveConf();
    33. } catch(Exception e) {
    34. e.printStackTrace();
    35. }
    36. }
    37.  
    38. public void saveConf(){
    39. try{
    40. messageConf.save(messages);
    41. }catch(Exception e){
    42. e.printStackTrace();
    43. }
    44. }
    45. @EventHandler
    46. public void onPlayerJoin(PlayerJoinEvent e) {
    47. e.setJoinMessage(ChatColor.translateAlternateColorCodes(
    48. "&".charAt(0), messageConf.getString("Join").replaceAll("%PLAYER",
    49. e.getPlayer().getName().toString())));
    50. }
    51.  
    52. @EventHandler
    53. public void onPlayerQuit(PlayerQuitEvent e) {
    54. e.setQuitMessage(ChatColor.translateAlternateColorCodes(
    55. "&".charAt(0), messageConf.getString("Quit").replaceAll("%PLAYER",
    56. e.getPlayer().getName().toString())));
    57. }
    58.  
    59. private void setupConfig(FileConfiguration config) throws IOException {
    60. if(!new File(getDataFolder(), "messages.yml").exists()) {
    61. new File(getDataFolder(), "messages.yml").createNewFile();
    62. this.messageConf.addDefault("Join", "&3[&bJoin&3] &9%PLAYER has arrived!");
    63. this.messageConf.addDefault("Quit", "&3[&bQuit&3] &9%PLAYER has left!");
    64. this.messageConf.addDefault("Test", "testing ^,^");
    65. this.messageConf.options().copyDefaults(true);
    66. }
    67. }
    68. }
     
  6. Offline

    TGRHavoc

    Epicballzy
    Check if the file exists, if it doesn't then create the config.
     
  7. Offline

    Epicballzy

    TGRHavoc Isn't that what I did?
    Code:java
    1. private void setupConfig(FileConfiguration config) throws IOException {
    2. if(!new File(getDataFolder(), "messages.yml").exists()) {
    3. new File(getDataFolder(), "messages.yml").createNewFile();
    4. this.messageConf.addDefault("Join", "&3[&bJoin&3] &9%PLAYER has arrived!");
    5. this.messageConf.addDefault("Quit", "&3[&bQuit&3] &9%PLAYER has left!");
    6. this.messageConf.addDefault("Test", "testing ^,^");
    7. this.messageConf.options().copyDefaults(true);
    8. }
    9. }
     
  8. Offline

    TGRHavoc

    Epicballzy
    Ah sorry, didn't see that :p My mistake.
     
  9. Offline

    Epicballzy

    Yeah, but for some reason. it doesn't generate the Join: and Quit: strings..
     
  10. Offline

    Minesuchtiiii

    add this.saveFile(this.messageConf();

    this.saveFile(this.messageConf();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  11. Offline

    Epicballzy

Thread Status:
Not open for further replies.

Share This Page