Search code examples
javaminecraftbukkit

SPIGOT: Calling a non-static method without re-initializing the plugin


I've spent upwards of the last 4 days researching and trying to figure this issue out. Sadly because of the poor documentation and lack of community support related to my issue I've finally decided to turn to StackOverflow.

Here's my issue, every Java programmer knows or should know, that you should essentially NEVER use static variables, classes, methods etc correct? Well, it seems as tho any time I try to call a method from an external class (By starting a new instance of my main class where the non-static method is stored), Spigot throws a java.lang.IllegalArgumentException: Plugin already initialized! error!

So here's my question, how do I get an instance of my main class and run a method from said class (from an external class), without triggering this error? As I've said, I've scoured the internet trying to find an answer to this issue. The most logically sound answer I've gotten can be found here posted by Father of Time: https://bukkit.org/threads/how-do-you-refer-to-a-non-static-method.69920/

I've changed Father of Time's given answer to match my needs (The method I'm trying to access is located in my main class as opposed to some other class) Here's my current, applicable code:

Main Class

public final class MainClass extends JavaPlugin implements Listener {
    private static final MainClass mainClass = new MainClass();

...

    public MainClass() {}

    public static MainClass getMainClass()
    {
        return mainClass;
    }

    private void exampleMethod() {
    }

...

}

Secondary Class

import com.example.namespace.MainClass;

...

public class CommandClass implements CommandExecutor {

...
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

         MainClass.getMainClass().exampleMethod()

    }

...

}

The reasoning behind Father of Time's description is that we don't make the class itself or any of its variables/methods static. But rather, simply grab an instance of the class using a static getter. Everything about this looks like it should work (it compiles just fine too).

I am pretty new to Java and honestly, lots of the stuff related to cross-class communication has been hard for me to wrap my head around. To be clear, this is simply me making experimental plugins to help myself learn the language (I learn faster by doing as opposed to just reading haha). I already know that going static crazy is a bad practice which is why I'm so frustrated that, even tho I'm trying to do the right thing, I can't find a single answer on the internet that works!

Any help/constructive criticism is greatly appreciated! I hope to eventually get to a point where I can start writing awesome plugins for Minecraft, but I need to get used to the Java environment first.


Solution

  • Okay I can't even begin to say how slightly annoyed but also relieved I am to have figured this out after spending half an hour writing out my question XD Prompted by something @Sweeper said in a comment. I ran another google search. and I found this link:

    https://www.spigotmc.org/threads/best-method-for-getting-instance-of-main-class.365480/

    I was immediately able to replace my code and get everything working! As I said, I'm still very noob in this field, to me when I add the new MainClass() line in my MainClass I saw THAT as my calling the class' instance. So I never even thought that I wasn't doing it.

    When Sweeper said "I'm asking you to show how and where you are creating an instance of CommandClass" I was like, "Wait I thought X was me getting the instance" thus causing me to search it a bit more and come to the conclusion that I'm.... an idiot.

    As simple of an issue, it was, I can't believe it stumped me for so long! Thank you Sweeper for you indirectly answering my question! :D