Problem with accessor class (getting plugin) ?

Discussion in 'Plugin Development' started by ThunderWaffeMC, Jan 26, 2014.

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

    ThunderWaffeMC

    Hi. I'm trying to access the main class in an accessor class by using the plugin method but I'm getting a few errors:

    My accessor class looks like:

    Code:java
    1.  
    2. public class AccessorClass {
    3. private MainClass plugin;
    4. public AccessorClass(MainClass plugin) {
    5. this.plugin = plugin;
    6. }
    7.  
    8. public static String getGroup(String group) {
    9. String groupConfig = plugin.getGroupFile().getString("Groups." + group);
    10. return groupConfig;
    11. }
    12. }
    13.  


    and when I use the variable "plugin" anywhere, it returns an underlined error saying "cannot make a static reference to the non-static field plugin".

    If I change plugin to static then I return an error in the console on usage (for example, when I use: AccessorClass.getGroup(group)").

    If I remove static from the method (getGroup) then I get an underlined error on use saying "Cannot make a static reference to the non-static method getGroup(String) from the type AccessorClass".

    What can I do to fix this?

    Thanks in advance!
     
  2. Offline

    DeGambler

    ThunderWaffeMC
    Try adding this to your main class:
    Code:java
    1.  
    2. // Main class and other code-stuffs
    3. private static CLASSNAME plugin;
    4.  
    5. public static CLASSNAME getPlugin() {
    6. return plugin;
    7. }

    And in that other class simply do:
    Code:java
    1. public static String getGroup(String group) {
    2. String groupConfig = MAINCLASS.getPlugin().getGroupFile().getString("Groups." + group);
    3. return groupConfig;
    4. }

    Hope it helps :)
     
    ThunderWaffeMC likes this.
Thread Status:
Not open for further replies.

Share This Page