Resource For the people wondering how to access their main class from a different class.

Discussion in 'Spigot Plugin Development' started by TheCyberCode, Feb 6, 2019.

Thread Status:
Not open for further replies.
  1. How do I access my main class/config?

    I see a lot of questions asking how do i access my main class from other classes... here is how:

    In your main class do this:
    Code (Text):

    //In your main class use the code something like this:
    private static [Main file name] plugin;
    // Change [Main file name] to your actual class name.

    public void onEnable(){
    plugin = this;
    }


    public static [Main file name] getInstance(){
    return plugin;
    }
     
    In your other classes to access the main class you can do this:
    Code (Java):


    // If you want to just get the main class do this code
    [Main file name].getInstance().[whatever it is you need from the class]
    And to get a string from your config:
    Code (Java):

    // where you want to get the string from your config put this code in that class
    [Main file name].getInstance().getConfig().getString("Your-String-To-Get-Here")
     
  2. Choco

    Moderator

    Congratulations, you just recommended the way most people advise against lol
     
    • Agree Agree x 3
  3. Whats wrong with this way?
     
  4. I know there is multiple ways and i use different ways myself but if this method works for beginners why not use it?
    I was just trying to suggest something simple.
     
  5. Bukkit.getPlugin([My plugin file].class)
    or use encapsulation, or DI which is kinda like that ^ or some other form, but not that, the class is already built as a singleton, there's no need to introduce that kind of structure

    EDIT: plus the Bukkit.getPlugin... way is 10x simpler than what you've just suggested.
     
  6. :shrug:
     
  7. Why reinvent a structure which is already there? You've overcomplicated a system built into spigot. This is something I've seen md_5 talk about quite frequently. Why are you trying to build a workaround or another thing for Bukkit which is built into Bukkit (or maybe it was some other person can't remember). This is the same complication as getting friendly names from materials or items.
     
  8. Maybe because I may of forgot that method... And just trying to help [code still works]
     
  9. Clearly. I'll just leave you with a shorter quote then.
     
  10. Code works but you have a non-final static variable in your code (hint you probably shouldn't be doing this). Having non-final static variables can cause memory issues and such alongside various side-effects. ESPECIALLY with the /reload command. Teaching this imo isn't really helping out. Also, just because something works doesn't mean it's good code.
    Code (Java):

    public static void printHelloWorld() {
        for(int i = 0; i < 1_000_000; i+) {

        }
    }
     
    • Optimistic Optimistic x 1
Thread Status:
Not open for further replies.