Custom Config Problem - Can't get it working from another class

Discussion in 'Plugin Development' started by Schulzi, Apr 8, 2012.

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

    Schulzi

    I have a problem. I want to have some functions seperated in different classes to keep my plugin clean.
    So, I have a main class and a configuration class. Eclipse doesn't give me an Error. But when I start my server I get this error:
    My main file:
    Code:
    package me.Schulzi.Tester;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import me.Schulzi.Tester.Configurtions.Commands;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Tester extends JavaPlugin {
        
        Logger log = Logger.getLogger("Minecraft");
        Commands confCmds = new Commands();
        
        @Override
        public void onDisable() {
            log.info("[Tester] Disabled!");
            super.onDisable();
        }
        
        @Override
        public void onEnable() {
            confCmds.commandsList = confCmds.getCommandsList();
            confCmds.loadCommandsList();
            
            log.info("[Tester] Enabled!");
            super.onEnable();
        }    
    }
    
    My configuration file:
    Code:
    package me.Schulzi.Tester.Configurtions;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Commands extends JavaPlugin {
        
        Logger log = Logger.getLogger("Minecraft");
        
        public FileConfiguration commandsList = null;
        public File commandsListFile = null;
        
        public void loadCommandsList() {
            commandsList.addDefault("this.is.a.complete.test", false);
            this.getCommandsList().options().copyDefaults(true);
            saveCommandsList();
            log.info("[Tester] CommandsList file loaded!");
        }
             
        public void reloadCommandsList() {
            if (commandsListFile == null) {
                commandsListFile = new File(getDataFolder(), "commands.yml");
            }
            commandsList = YamlConfiguration.loadConfiguration(commandsListFile);
             
            // Look for defaults in the jar
            InputStream defConfigStream = getResource("commands.yml");
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                commandsList.setDefaults(defConfig);
            }
        }
             
        public FileConfiguration getCommandsList() {
            if (commandsList == null) {
                reloadCommandsList();
            }
            return commandsList;
        }
             
        public void saveCommandsList() {
            if (commandsList == null || commandsListFile == null) {
                return;
            }
            try {
                commandsList.save(commandsListFile);
            } catch (IOException ex) {
                Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Error saving 'commands.yml' to " + commandsListFile, ex);
            }
        }
        
    }
    
    I'm new to Java scripting and I don't know what the error is.
    I'd be happy if one of you would help me, pls.
     
  2. Offline

    sd5

    You're trying to add defaults to your config, but you didn't get the config before this.

    has to be before this:
     
  3. Offline

    Schulzi

    Thanks for your help, but it doesn't seems to work.
    I have it like this now:
    Code:
    package me.Schulzi.Tester.Configurations;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Commands extends JavaPlugin {
        
        Logger log = Logger.getLogger("Minecraft");
        
        public FileConfiguration commandsList = null;
        public File commandsListFile = null;
        
        public void reloadCommandsList() {
            if (commandsListFile == null) {
                commandsListFile = new File(getDataFolder(), "commands.yml");
            }
            commandsList = YamlConfiguration.loadConfiguration(commandsListFile);
             
            // Look for defaults in the jar
            InputStream defConfigStream = getResource("commands.yml");
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                commandsList.setDefaults(defConfig);
            }
        }
             
        public FileConfiguration getCommandsList() {
            if (commandsList == null) {
                reloadCommandsList();
            }
            return commandsList;
        }
        
        public void loadCommandsList() {
            commandsList.addDefault("this.is.a.complete.test", false);
            this.getCommandsList().options().copyDefaults(true);
            saveCommandsList();
            log.info("[Tester] CommandsList file loaded!");
        }
             
        public void saveCommandsList() {
            if (commandsList == null || commandsListFile == null) {
                return;
            }
            try {
                commandsList.save(commandsListFile);
            } catch (IOException ex) {
                Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Error saving 'commands.yml' to " + commandsListFile, ex);
            }
        }
        
    }
    
    The error is the same. It says, that at this line:
    Code:
    InputStream defConfigStream = getResource("commands.yml");
    
    and this line:
    Code:
    reloadCommandsList();
    
    the error would be.
    :(
     
  4. Offline

    sd5

    Maybe you forgot the commands.yml in your plugin...? It has to be in the same folder as plugin.yml
    The error looks like there is no commands.yml
     
  5. Offline

    Schulzi

    So I have to create the file by myself?
    Doesn't work, too.
    Only when I put the scripts in one class together, the custom config works and creates the commands.yml itself.
     
  6. Offline

    sd5

    Yes, you have to create the commands.yml in your .jar by yourself...

    That's because you try to get the commandlist BEFORE you even loaded it...:

     
  7. Offline

    Schulzi

    Yea, but how should it know what commandsList is? It has to be defined somewhere.
     
Thread Status:
Not open for further replies.

Share This Page