Cant access a custom config anywhere but the main class

Discussion in 'Spigot Plugin Development' started by Flyin, Jan 9, 2016.

  1. Hello Spigot. I make a post last night about this same config not loading and that was solved but four hours later I am getting really sick of this bug and yet I feel it will be quite simple to fix. As the title says I can access the config from the main class but if I try from anywhere else I get the following error. If someone would not mind taking a quick look it would be much appreciated.

    Error
    Code (Text):
    [09:43:57 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'dynamichub' in plugin DynamicHub v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_66]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_66]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_66]
    Caused by: java.lang.NullPointerException
        at me.flyin.dynamichub.filecfg.configs.FileConfigurationConfig.reloadConfig(FileConfigurationConfig.java:23) ~[?:?]
        at me.flyin.dynamichub.commands.DynamicHubCommand.onCommand(DynamicHubCommand.java:49) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
    Main Class
    Code (Text):
    public class DynamicHub extends JavaPlugin implements Listener
    {
        PluginManager pm = Bukkit.getPluginManager();
        FileConfigurationConfig conf = new FileConfigurationConfig();

        public void onEnable()
        {
            conf.loadConfig();
            loadCommands();
        }

        public void onDisable()
        {

        }

        public void loadCommands()
        {
            new DynamicHubCommand(this);
        }

        public void loadEvents()
        {
            pm.registerEvents(new PlayerMoveListener(), this);
        }
    }
    DynamicFileConfiguration Class
    Code (Text):
    public class DynamicFileConfiguration extends YamlConfiguration
    {
        private File file;
        private String defaults;
        private Plugin plugin;

        public DynamicFileConfiguration(Plugin plugin, String fileName, String defaultsFile)
        {
            this.plugin = plugin;
            this.defaults = defaultsFile;
            this.file = new File(plugin.getDataFolder(), fileName);
            reload();
        }

        public void save()
        {
            try {
                options().indent(2);
                save(file);
            } catch (IOException exception) {
                plugin.getLogger().severe("[DynamicHub] Error, could not save '" + file.getName() + "'.");
            }
        }

        public void reload()
        {
            if (!file.exists())
            {
                try {
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                } catch (IOException exception) {
                    exception.printStackTrace();
                    plugin.getLogger().severe("[DynamicHub] Error, failed to create '" + file.getName() + "'.");
                }
            }
            try {
                load(file);

                if (!(defaults == null)) {
                    InputStreamReader reader = new InputStreamReader(plugin.getResource(defaults));
                    FileConfiguration defaultsConfig = YamlConfiguration.loadConfiguration(reader);

                    setDefaults(defaultsConfig);
                    options().copyDefaults(true);

                    reader.close();
                    save();
                }

            } catch (IOException exception) {
                exception.printStackTrace();
                plugin.getLogger().severe("Error while loading file " + file.getName());

            } catch (InvalidConfigurationException exception) {
                exception.printStackTrace();
                plugin.getLogger().severe("Error while loading file " + file.getName());
            }
        }
    }
     
    FileConfigurationConfig Class
    Code (Text):
    public class FileConfigurationConfig {

        DynamicFileConfiguration conf;

        public void loadConfig()
        {
            conf = new DynamicFileConfiguration(DynamicHub.getPlugin(DynamicHub.class), "config.yml", "config.yml");
        }

        public FileConfiguration getConfig()
        {
            return conf;
        }

        public void reloadConfig()
        {
            conf.reload();
        }

        public void saveConfig()
        {
            conf.save();
            System.out.println("[DynamicHub] file has saved successfully!");
        }
    }
     
    DynamicHubCommand Class
    Code (Text):
    public class DynamicHubCommand implements CommandExecutor
    {
        DynamicHub plugin;
        //this gets that config
        FileConfigurationConfig conf = new FileConfigurationConfig();

        public DynamicHubCommand(DynamicHub i)
        {
            this.plugin = i;
            this.plugin.getCommand("dynamichub").setExecutor(this);
        }

        public void msg(CommandSender s, String msg)
        {
            s.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
        }

        public boolean onCommand(CommandSender user, Command cmd, String label, String[] args)
        {
            if ((user instanceof Player)) {
                if (!(user.hasPermission("dynamichub.admin")))
                {
                    msg(user, "&c&oDenied! &7You do not have permission to preform that command.");
                    return true;
                }
            }
            if (args.length == 0)
            {
                //only one arg
                return true;
            }
            if (args[0].equalsIgnoreCase("reload"))
            {
                //reload
                conf.reloadConfig();
                return true;
            }
            if (args[0].equalsIgnoreCase("help"))
            {
                //help
                return true;
            }
            else
            {
                //else
            }
            return true;
        }
    }
     
     
  2. make the FileConfiguration public static, by that you can access it anywhere in any class and any package
     
  3. Yes, I understand that would work although I am trying to avoid using statics as much as possible. Do you know of a way I can just get the methods from the class by way of an instance or something like that?...
     
  4. Why are you extending YamlConfiguration? Just create a wrapper for it and pass it through the constructor.
     
  5. Why are you trying to avoid static method, there is really nothing wrong with this method, especially because you are getting a configuration file
     
  6. I extended YamlConfiguration so that it would return that automaticlly. I removed it and I am getting the same error.. Here are all the classes again.

    Main
    Code (Text):
    public class DynamicHub extends JavaPlugin implements Listener
    {
        PluginManager pm = Bukkit.getPluginManager();
        FileConfigurationConfig conf = new FileConfigurationConfig();

        public void onEnable()
        {
            conf.loadConfig();
            loadCommands();
        }

        public void onDisable()
        {

        }

        public void loadCommands()
        {
            new DynamicHubCommand(this);
        }

        public void loadEvents()
        {
            pm.registerEvents(new PlayerMoveListener(), this);
        }
    }
     
    DynamicFileConfiguration
    Code (Text):
    public class DynamicFileConfiguration
    {
        private File file;
        private String defaults;
        private Plugin plugin;
        private YamlConfiguration conf;

        public DynamicFileConfiguration(Plugin plugin, String fileName, String defaultsFile)
        {
            this.plugin = plugin;
            this.defaults = defaultsFile;
            this.file = new File(plugin.getDataFolder(), fileName);
            this.conf = YamlConfiguration.loadConfiguration(file);
            reload();
        }

        public FileConfiguration getConfig()
        {
            return conf;
        }

        public void save()
        {
            try {
                conf.options().indent(2);
                conf.save(file);
            } catch (IOException exception) {
                plugin.getLogger().severe("[DynamicHub] Error, could not save '" + file.getName() + "'.");
            }
        }

        public void reload()
        {
            if (!file.exists())
            {
                try {
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                } catch (IOException exception) {
                    exception.printStackTrace();
                    plugin.getLogger().severe("[DynamicHub] Error, failed to create '" + file.getName() + "'.");
                }
            }
            try {
                conf.load(file);

                if (!(defaults == null)) {
                    InputStreamReader reader = new InputStreamReader(plugin.getResource(defaults));
                    FileConfiguration defaultsConfig = YamlConfiguration.loadConfiguration(reader);

                    conf.setDefaults(defaultsConfig);
                    conf.options().copyDefaults(true);

                    reader.close();
                    save();
                }

            } catch (IOException exception) {
                exception.printStackTrace();
                plugin.getLogger().severe("Error while loading file " + file.getName());

            } catch (InvalidConfigurationException exception) {
                exception.printStackTrace();
                plugin.getLogger().severe("Error while loading file " + file.getName());
            }
        }
    }
     
    FileConfigurationConfig
    Code (Text):
    public class FileConfigurationConfig {

        DynamicFileConfiguration conf;

        public void loadConfig()
        {
            conf = new DynamicFileConfiguration(DynamicHub.getPlugin(DynamicHub.class), "config.yml", "config.yml");
        }

        public FileConfiguration getConfig()
        {
            return conf.getConfig();
        }

        public void reloadConfig()
        {
            conf.reload();
        }

        public void saveConfig()
        {
            conf.save();
            System.out.println("[DynamicHub] file has saved successfully!");
        }
    }
     
    If I cant get this working then I will just make them static