Getting an int from config

Discussion in 'Plugin Development' started by JoshHuttie, May 2, 2013.

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

    JoshHuttie

    im trying to make a new int that gets a number from my config file,

    my config file has a line "num:" with no speech marks and i cant figure out the code to make the integer grab that line from config and set it as the int number.

    Could anyone please help with this ? :)
     
  2. Offline

    chasechocolate

    Code:java
    1. int number = this.getConfig().getInt("num");
     
  3. Offline

    JoshHuttie

    That doesn't work, it just gives errors when i reload the server.

    Code:
        public void onEnable() {
            Bukkit.getServer().getLogger().info("Plugin has been enabled!");
                this.getConfig().options().copyDefaults(true);
                this.saveConfig();
            }
     
     
        public void onDisable() {
            Bukkit.getServer().getLogger().info("Plugin has been disabled!");
            this.saveConfig();
        }
     
     
        int count = this.getConfig().getInt("num");
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    config.yml
     
  4. Offline

    kasszz

    try to use this.saveDefaultConfig(); on the onEnable instead of this.getconfig and this.saveconfig.

    And do you get a config file or is that a problem aswell ?
     
  5. Offline

    JoshHuttie

    I don't think its anything to do with the onEnable because ive tried setting the int to just a number, EG. "int count = 20" and it worked fine.

    If I have the int set to get the config file for my int it gives me errors and doesn't give me a config file in my plugins folder.

    But if I have it as "int count = 20;" then it works fine and gives me a config file.
     
  6. Offline

    kasszz

    Have you tried setting it in a function, just for test reasons ?
     
  7. Offline

    JoshHuttie

    Sorry im extremely new to Coding, how would i go about doing that?

    Here is my whole class, please say what I would have to do to make it work ive been trying for hours and all i get is errors when starting the server and the plugin not loading.

    Code:
    package me.joshhuttie.counter;
     
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class countermain extends JavaPlugin {
     
     
     
        public void onEnable() {
            Bukkit.getServer().getLogger().info("Plugin has been enabled!");
                this.getConfig().options().copyDefaults(true);
                this.saveConfig();
            }
     
     
        public void onDisable() {
            Bukkit.getServer().getLogger().info("Plugin has been disabled!");
            this.saveConfig();
        }
     
     
     
        int count = this.getConfig().getInt("num");
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
                  if(cmd.getName().equalsIgnoreCase("count")){
     
                      this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
                       
                          public void run(){
                              if(count != -1) {
                              if(count != 0){
                                  Bukkit.broadcastMessage(ChatColor.GREEN + " " + count);
     
                                  count --;
                               
                               
                               
                              } else {
                                  Bukkit.broadcastMessage(ChatColor.DARK_GREEN + "Go!");
                                  count --;
                              }
                              }
                              }
                          }, 0L, 20L);
                      }
                return true;
     
                 
                }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  8. Offline

    desht

    You'd probably have gotten an answer a lot quicker if you'd actually posted the exception stack trace you were getting. Just saying "I get errors" is of no use to anyone.

    Anyway, you can't use this.getConfig() outside of any method in your plugin class. That's basically trying to get the config when your plugin object is being constructed, before onLoad()/onEnable() etc. have been called, and before it has a valid configuration object to return. So I'd guess your error is a NullPointerException at that line.

    Any configuration reading via getConfig() needs to be done during or after your onEnable() has been called.
     
  9. Offline

    JoshHuttie

    That is the error
     
  10. Offline

    desht

    Like I said: you can't use this.getConfig() in your plugin constructor.
     
Thread Status:
Not open for further replies.

Share This Page