Getting config from another thread (not a listener)

Discussion in 'Plugin Development' started by vemacs, Dec 8, 2012.

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

    vemacs

    Assume I have a thread that is called by new Thread(new PluginThread()).start():
    It doesn't implement Listener, and I want to
    Code:
    public class PluginThread implements Runnable {
        @Override
        public void run() {
          String value = Plugin.getConfig.getString("value");
          //do stuff here
        }
    }
    
    I get a getConfig cannot be resolved or is not a field.

    If I do the following inside the first set of brackets:

    Code:
    private Plugin plugin;
    plugin = Plugin;
    
    The top line wants to end with a comma, which is something completely different.
    So my question is, how would I get a config from another thread that isn't a listener?

    Thanks in advance!
     
  2. Offline

    RealDope

    I'm confused, what do you want?

    You have to make an instance of the main class that extends JavaPlugin then call getConfig()
     
  3. Offline

    vemacs

    I want to read the plugin configuration from another thread.
    My main class extends JavaPlugin, and my plugin loads properly.
    I am trying to read the config, but the methods I used to read configs from listeners aren't working for this particular thread.
     
  4. Offline

    RealDope

    lol, I see your problem now. You are calling .getConfig.getString. getConfig is a function so it has to be: .getConfig()
     
  5. Offline

    vemacs

    Right.
    After doing that, my error changes to:
    Cannot make a static reference to the non-static method getConfig() from the type JavaPlugin.

    Adding a static keyword to the class declaration results in an illegal modifier warning.
     
  6. Offline

    RealDope

    Please post the full code from this class and from your main class.
     
  7. Offline

    vemacs

    Thread class:
    Code:
    package com.nullblock.vemacs.BigBen;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
     
    public class BongThread implements Runnable {
        @Override
        public void run() {
            String prefix = BigBen.getConfig().getString("prefix") + " " ;
    //error in the previous line: Cannot make a static reference to the non-static method getConfig() from the type JavaPlugin
            int nextHour = BongLib.nextHour();
            int waitTime = BongLib.secondsUntilNextHour();
            try {
                Thread.sleep(waitTime * 1000);
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
     
            Bukkit.broadcastMessage(prefix + BongLib.bongText(nextHour));
            int hour = nextHour;
            for(;;){
                try {
                    Thread.sleep(3600000);
                } catch(InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }
                hour++;
                hour = hour % 12;
                Bukkit.broadcastMessage(prefix + BongLib.bongText(hour));
            }
        }
    }
    
    Main class (i haven't done setdefaultconfig yet, but it should compile fine even if I don't generate the config):
    Code:
    package com.nullblock.vemacs.BigBen;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class BigBen extends JavaPlugin {
        @Override
        public void onEnable() {
            new Thread(new BongThread()).start();
            new ChatListener(this);
        }
        @Override
        public void onDisable() { 
     
        }
     
    }
    
    You can see the source on GitHub.
     
  8. Offline

    RealDope

    Add this to your class:
    Code:JAVA
    1.  
    2. BigBen BigBen = new BigBen();
    3.  
     
  9. Offline

    vemacs

    Thanks mate! I couldn't believe that it was that obvious...

    Not solved yet, I get a java.lang.IllegalArgumentException: File cannot be null when using that method of getting the config, and this is what's in my onEnable:
    Code:
        @Override
        public void onEnable() {
            this.saveDefaultConfig();
            this.getConfig().set("prefix", "<&6BigBen&r>");
            this.saveConfig();
            new Thread(new BongThread()).start();
            new ChatListener(this);
     
        }
    I'm not sure if it's thread-specific, but it's a critical issue. Any help?
    The config is definitely saving properly, as there is a config.yml in /plugins/BigBen

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

    RealDope

    Post stack trace, as well as identifying which lines the errors are on.
     
  11. Offline

    vemacs

    Code:
    2012-12-08 19:04:41 [SEVERE] Exception in thread "Thread-14"
    2012-12-08 19:04:41 [SEVERE] java.lang.IllegalArgumentException: File cannot be null
    2012-12-08 19:04:41 [SEVERE]    at org.apache.commons.lang.Validate.notNull(Validate.java:203)
    2012-12-08 19:04:41 [SEVERE]    at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:170)
    2012-12-08 19:04:41 [SEVERE]    at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117)
    2012-12-08 19:04:41 [SEVERE]    at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111)
    2012-12-08 19:04:41 [SEVERE]    at com.nullblock.vemacs.BigBen.ChatThread.run(ChatThread.java:13)
    2012-12-08 19:04:41 [SEVERE]    at java.lang.Thread.run(Unknown Source)
    Here is ChatThread.java.
     
  12. Offline

    RealDope

    Code:JAVA
    1.  
    2. public void onEnable() {
    3. this.saveDefaultConfig();
    4. System.out.println(getConfig());
    5. // Other stuff
    6. }
    7.  


    Code:JAVA
    1.  
    2. System.out.println("ChatThread config result: " + BigBen.getConfig());
    3. String prefix = BigBen.getConfig().getString("prefix") + " ";
    4. System.out.println(prefix);
    5. // Other ChatThread stuff
    6.  


    Change to have those debug messages, tell me what it prints.
     
  13. Offline

    vemacs

    I'm going to use getLogger instead of println, but I will do that.
     
  14. Offline

    RealDope

    getLogger requires a string to be used. I don't think it will work.
     
  15. Offline

    vemacs

    Println isn't printing out anything to the console, get getLogger isn't either.
    Do you think these issues are simply related to this being a thread that doesn't implement listener?
    Maybe I should just pass an argument to the BongThread, and then use ChatListener to pass an argument to the ChatThread...
     
Thread Status:
Not open for further replies.

Share This Page