Config Issues.

Discussion in 'Plugin Development' started by TheChinski, Apr 25, 2012.

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

    TheChinski

    Hello. I'm fairly new to coding, and I've been having a go at making a chat plugin. However, everytime I start up, I get an error complaining that my co nfig can't be NULL. But it isnt... :confused:

    This is my main:

    Code:
    package com.sludgecraft.WhosOnline;
     
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class WhosOnline extends JavaPlugin
    {
        public static WhosOnline plugin;
        Logger log;
     
        @Override
        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveConfig();
            new myPlayerListener(this);
            log = this.getLogger();
            log.info("WhosOnline Has Been Enabled... O.o");
        }
        public void onDisable(){
            log.info("WhosOnline Has Gone To Sleep...");
        }   
        public class myPlayerListener implements Listener
        {
            public myPlayerListener (WhosOnline plugin)
            {
                plugin.getServer().getPluginManager().registerEvents(this, plugin);
            }
        }
        String op1 = this.getConfig().getString("group1");
        int playerlist = Bukkit.getOnlinePlayers().length;
        public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
            if(cmd.getName().equalsIgnoreCase("who")){
                // doSomething
                ((Player)sender).sendMessage(ChatColor.BLUE + "There are " + ChatColor.BLACK + '[' + ChatColor.RED + playerlist + ChatColor.BLACK + ']' + ChatColor.BLUE + " players online:");
                ((Player)sender).sendMessage(op1 + ":");
                return true;
        }
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
            System.out.println(this.toString() + " enabled");
        return false;
        }
    }
    
    I dont seem to be getting any errors here. And my config:

    Code:
    group1: Founder
    group2: Admin
    group3: VIP
    group4: Default
    group5: Dev
    group6: Cheese
    Its still a work in progress so ignore anything that looks like complete garbage :p - Unless of course its garbage thats stopping the config working...

    On startup it doesn't create a folder with a config file in, which I thought could be the problem. But when I tried manually making a config file in a folder, in the plugins folder, I only got the same response...
     
  2. Offline

    r0306

    Can you post the error code shown on the console?
     
  3. Offline

    TheChinski

    Code:
    07:23:59 [SEVERE] Could not load 'plugins/WhosOnline.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: File cannot be null
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:148)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:207)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:183)
        at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:53)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:156)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:422)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.IllegalArgumentException: File cannot be null
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:171)
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117)
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111)
        at com.sludgecraft.WhosOnline.WhosOnline.<init>(WhosOnline.java:36)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:144)
        ... 8 more
     
  4. Offline

    r0306

    Put quotation marks around the config lines:
    Code:
    group1: "Founder"
    group2: "Admin"
    group3: "VIP"
    group4: "Default"
    group5: "Dev"
    group6: "Cheese"
    Also be sure that the config file is named config.yml and that it's in a folder with the name of your plugin. You should also call up the configuration on startup. Where is the code where you generated the file?
     
  5. Offline

    Iron_Crystal

    You should not have to make the config file. If you did it correctly, then the plugin should auto-generate it for you.
     
    r0306 likes this.
  6. Offline

    TheChinski

    I don't think I've done it right then :p

    But then again, even when I do add a manual configuration, it still fails. I'm new to using configs and I've been following the guide to the new config system on the wiki (or at least trying to). The config is named correctly and I have added the ""'s.

    And I kinda thought the code at the start of onEnable did all the config stuff?
     
  7. Offline

    Iron_Crystal

  8. Offline

    Sagacious_Zed Bukkit Docs

    Its the preferred method over maKing a config in code, which is more clutter and harder to maintain.

     
  9. Offline

    VeryBIgCorp

    1. It supplements a better UX
    2. It's not really hard or cluttering. Just run a method at startup that sets the config values to a default value, save, then reload.
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    1. False, to a user, defaults coming from an internal default file, are indistinguishable. Actually, coping out the file allows you to use inline comments, but you can't even set those programatically.
    2. You can copy out the default config in one method. And the config.yml can be any length, if you do it programmatically your code will grow by the length of the config.
     
  11. Offline

    TheChinski

    Hmmm, I've followed the guide, yet i'm still getting exactly the same error :(

    This is now my onEnable:

    Code:
        public void onEnable() {
            final FileConfiguration config = this.getConfig();
            config.options().header("You can change these defaults to whatever you like:");
            config.addDefault("Groups.Group1", "Founder");
            new myPlayerListener(this);
            log = this.getLogger();
            log.info("WhosOnline Has Been Enabled... O.o");
            config.options().copyDefaults(true);
            saveConfig();
            config.getString("Groups.Group1");
        }
     
  12. Offline

    Sagacious_Zed Bukkit Docs

    According to this error line com.sludgecraft.WhosOnline.WhosOnline.<init>(WhosOnline.java:36)
    Your main class has either a constructor or a static initialize statement on line 36. if you need it make sure it calls the super constructor otherwise get rid of it.
     
  13. Offline

    TheChinski

    Ah, thankyou :)

    I now know how to read error block things just that bit better thanks to you :p
     
  14. Offline

    Lolmewn

    I think you have to create the folder.
    From the top of my head it's something like
    Code:
    this.getPluginFolder().mkdir();
    it might also be getDataFolder, but I'm sure it's something with Folder.
     
  15. Offline

    TheChinski

    I managed to g it working after a line was in the wrong place.

    Although, I was wondering, how would you check for a permission out of all the players online? I thought I had it, but I just get something along the lines of CraftPlayer=TheChinski, or something like that - I have just set off, but when I fet home I can post the code.

    Thanks,
     
  16. Offline

    VeryBIgCorp

    1. Well if the user has to make the configuration on their own or copy a file, that's not as simple as the program fabricating it for them.
    2. You can make a class to handle the config and then the rest of your code will be cleaner thanks to OOP.
     
  17. Offline

    TheChinski

    I seem to have cracked the config thing - But now, even when I have the correct perms, when typing /who, I get [CraftPlayer{name=TheChinski}] on all 6 groups? Here is my code (It isnt done yet btw :p):

    Code:
    package com.sludgecraft.WhosOnline;
     
    import java.util.ArrayList;
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class WhosOnline extends JavaPlugin
    {
        public static WhosOnline plugin;
        public final ArrayList<Player> Group1 = new ArrayList<Player>();
        public final ArrayList<Player> Group2 = new ArrayList<Player>();
        public final ArrayList<Player> Group3 = new ArrayList<Player>();
        public final ArrayList<Player> Group4 = new ArrayList<Player>();
        public final ArrayList<Player> Group5 = new ArrayList<Player>();
        public final ArrayList<Player> Group6 = new ArrayList<Player>();
        Logger log;
     
        @Override
        public void onEnable() {
            final FileConfiguration config = this.getConfig();
            config.options().header("You can change these defaults to whatever you like:");
            config.addDefault("Groups.Group1", "Founder");
            config.addDefault("Groups.Group2", "");
            config.addDefault("Groups.Group3", "");
            config.addDefault("Groups.Group4", "");
            config.addDefault("Groups.Group5", "");
            config.addDefault("Groups.Group6", "");
            new myPlayerListener(this);
            log = this.getLogger();
            log.info("WhosOnline Has Been Enabled... O.o");
            config.options().copyDefaults(true);
            saveConfig();
            config.getString("Groups.Group1");
            config.getString("Groups.Group2");
            config.getString("Groups.Group3");
            config.getString("Groups.Group4");
            config.getString("Groups.Group5");
            config.getString("Groups.Group6");
           
        for(Player player: getServer().getOnlinePlayers())
        {
            String op1 = this.getConfig().getString("Groups.Group1");
            String op2 = this.getConfig().getString("Groups.Group2");
            String op3 = this.getConfig().getString("Groups.Group3");
            String op4 = this.getConfig().getString("Groups.Group4");
            String op5 = this.getConfig().getString("Groups.Group5");
            String op6 = this.getConfig().getString("Groups.Group6");
            if(player.hasPermission("whosonline." + op1))
            {
                Group1.add(player);
            }
            if(player.hasPermission("whosonline." + op2))
            {
                Group2.add(player);
            }
            if(player.hasPermission("whosonline." + op3))
            {
                Group3.add(player);
            }
            if(player.hasPermission("whosonline." + op4))
            {
                Group4.add(player);
            }
            if(player.hasPermission("whosonline." + op5))
            {
                Group5.add(player);
            }
            if(player.hasPermission("whosonline." + op6))
            {
                Group6.add(player);
            }
            else
    {
                Group1.remove(player);
                Group2.remove(player);
                Group3.remove(player);
                Group4.remove(player);
                Group5.remove(player);
                Group6.remove(player);
    }
            }
            }
        public void onDisable(){
            log.info("WhosOnline Has Gone To Sleep...");
        }   
        public class myPlayerListener implements Listener
        {
            public myPlayerListener (WhosOnline plugin)
            {
                plugin.getServer().getPluginManager().registerEvents(this, plugin);
            }
        }
        int playerlist = Bukkit.getOnlinePlayers().length;
        public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
            String op1 = this.getConfig().getString("Groups.Group1");
            String op2 = this.getConfig().getString("Groups.Group2");
            String op3 = this.getConfig().getString("Groups.Group3");
            String op4 = this.getConfig().getString("Groups.Group4");
            String op5 = this.getConfig().getString("Groups.Group5");
            String op6 = this.getConfig().getString("Groups.Group6");
            if(cmd.getName().equalsIgnoreCase("who")){
                ((Player)sender).sendMessage(ChatColor.BLUE + "There are " + ChatColor.BLACK + '[' + ChatColor.RED + playerlist + ChatColor.BLACK + ']' + ChatColor.BLUE + " players online:");
                ((Player)sender).sendMessage("");
                ((Player)sender).sendMessage(op1 + ": " + Group1);
                ((Player)sender).sendMessage(op2 + ": " + Group2);
                ((Player)sender).sendMessage(op3 + ": " + Group3);
                ((Player)sender).sendMessage(op4 + ": " + Group4);
                ((Player)sender).sendMessage(op5 + ": " + Group5);
                ((Player)sender).sendMessage(op6 + ": " + Group6);
                return true;
        }
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
            System.out.println(this.toString() + " enabled");
        return false;
        }
    }
    
    Ah, I think I have found why its coming up for all 6 players - I was op :p

    But when deopped, I dont have the perms to do /Who, even though I shouldn't need any perms? This really has me stumped... :/

    The only permissions that should be needed for it is whosonline.<Group>

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  18. Offline

    TheChinski

    Ah - soughted it! :D

    It wa because I had set the perms in the plugin.yml wrong, and I saw on a tutorial that it didn't matter what perms ou pro in the plugin.yml so I never suspected it...

    Thanks,
     
Thread Status:
Not open for further replies.

Share This Page