Solved Config error

Discussion in 'Plugin Development' started by Forword, Apr 15, 2018.

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

    Forword

    So I coded a yml config thats supposed to:
    Create a file if message.yml (copy defaults)
    However, the file is created, although the defaults aren't copied

    YML creator thingy

    Code:
    package me.forword.server;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public class Message {
       
        private Core plugin = Core.getPlugin(Core.class);
       
        public static FileConfiguration messagecfg;
        public File message;
       
        public void setup() {
            if (!plugin.getDataFolder().exists()) {
                plugin.getDataFolder().mkdirs();
            }
           
            message = new File(plugin.getDataFolder(), "message.yml");
           
            if (!message.exists()) {
                try {
                    message.createNewFile();
                }catch (IOException e) {
                    Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Could not create the file!");
                }
            }
               
            messagecfg = YamlConfiguration.loadConfiguration(message);
            Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "The message.yml file has been created!");
        }
       
        public static FileConfiguration getMessage() {
            return messagecfg;
        }
       
        public void savePlayers() {
            try {
                messagecfg.save(message);
                Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "The message.yml file has been saved!");
       
            }catch (IOException e) {
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Could not save the players.yml file!");
            }
        }
    
    }
    
    
    Heres the main class

    Code:
    package me.forword.server;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.forword.server.commands.Hello;
    import me.forword.server.stuff.Message;
    
    public class Core extends JavaPlugin {
    
        private Message cfgm;
    
        public void onEnable() {
        
            loadConfigManager();
            loadMessage();
        
            this.getCommand("hello").setExecutor(new Hello());
        
        }
    
        public void loadConfigManager() {
            cfgm = new Message();
            cfgm.setup();
        }
    
        public void loadMessage() {
            getConfig().options().copyDefaults(true);
            Bukkit.getConsoleSender().sendMessage(ChatColor.GOLD + "This is the first part of the save! Copying defaults!");
            saveConfig();
        }
    
    }
    
    Heres the message.yml file

    Code:
    hello: Hi person, this is a test message!
    
     
    Last edited: Apr 16, 2018
  2. Offline

    AyWuzzUp

    Why the use of static that might be your issue (its been a bit) but you should try doing the following in your Message class instead
    Code:
    private Core plugin;
    
    public Message(Core plugin) {
    this.plugin = plugin;
    }
    
    Also the following doesn't need to be static: (that includes your methods as well)
    Code:
    public static FileConfiguration messagecfg;
    
    Do the following in your main class:
    Code:
    private Message message;
    
    public void onEnable() {
    this.message = new Message(this);
    message.(method you want to run from that class);
    }
    
     
Thread Status:
Not open for further replies.

Share This Page