Really starting to bug me...

Discussion in 'Plugin Development' started by Scullyking, Aug 2, 2012.

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

    Scullyking

    Why does this:
    Code:java
    1.  
    2. File configFile = new File(getDataFolder(), fileName);
    3. FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
    4.  

    work in my main class but not another class I made? I get an error on getDataFolder() saying, create method. I didn't make a method in my main class...
     
  2. Offline

    Firefly

    getDataFolder() is a method off of the class that extends JavaPlugin. Therefore, your other class's constructor needs to accept your main class as a parameter and then you can call it like: plugin.getDataFolder() assuming that the global variable in your other class of type YourMainClass is called plugin.
     
  3. Offline

    Scullyking

    So I put:
    Code:
    public static MainClass plugin;
    
    in the secondclass and change the thingy to
    Code:
    plugin.getDataFolder()
    But what goes in the main class? You kinda lost me... :/
     
  4. Offline

    DirtyStarfish

    MyPlugin:
    Code:
    public class MyPlugin extends JavaPlugin {
     
        private MyPlayerListener playerListener;
     
        @Override
        public void onEnable() {
            this.playerListener = new MyPlayerListener(this);
        }
     
    }
    MyPlayerListener:
    Code:
    public class MyPlayerListener implements Listener {
     
        private MyPlugin plugin;
     
        //This is the constructor; it takes the main class as a parameter.
        public MyPlayerListener(MyPlugin plugin) {
            this.plugin = plugin;
        }
     
        public void someMethod() {
            File configFile = new File(this.plugin.getDataFolder(), fileName);
            FileConfiguration config = new YamlConfiguration().loadConfiguration(configFile);
        }
    }
    Its the JavaPlugin class that contains the method getDataFolder(). Seeing as the main class of your plugin extends JavaPlugin, it will have access to the getDataFolder method. By passing around an instance of your main class, you can access that method in other classes too.
     
  5. Offline

    Firefly

    It doesn't have to be a Listener class, I'm guessing that was just used as an example.
     
  6. Offline

    DirtyStarfish

    No it doesn't, but I guessed that the majority of plugins would use a listener, and I thought it would be more relevant than just a random class.
     
  7. Offline

    Scullyking

    It was actually a listener class, thanks for the help!
     
Thread Status:
Not open for further replies.

Share This Page