Getting the Instance from the Main Class

Discussion in 'Plugin Development' started by DevFelixlol, Dec 21, 2020.

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

    DevFelixlol

    Hi,
    i have a Bit trouble of how I can get a Instance of the Main Class.
    Tried a couple of things that I found on Google, but I get an Error that saying, that I already initialize the Instance.
    I hope you can show me, how to do this.
    I am honest with you, I don’t work with Instances in Java so far and have a lot of Troubles with it.
    Wishes,
    Felix
     
  2. Offline

    Kars

    Use 'this'. In onEnable or somewhere if you need it in the main class.
    If you need it in another class, pass 'this' into the other class through the constructor.
    PHP:
    // The constructor for OtherClass
    public OtherClass(MainClass mainClass) {
    }

    // and in the main class
    new OtherClass(this);
     
  3. Offline

    DevFelixlol

    Ok. I unfortunately don’t understand this. Ok, I have my 2 Classes and in the Class, where I want to use the Instance in a Method. What should I write?
    I cannot just write new OtherClass(this); in the Main Class.(

    I have like this Method in my other Class:
    Code:
    Bukkit.getPluginManager().registerEvents(listener, //instance); 
    . What should i write instead of instance?

    Got this Error, following your Idea.
    Code:
    [00:19:14] [Server thread/ERROR]: Could not load 'plugins\Spielstand System.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: Abnormal plugin type
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:80) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:394) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:301) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.loadPlugins(CraftServer.java:383) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
            at net.minecraft.server.v1_16_R3.DedicatedServer.init(DedicatedServer.java:185) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:808) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
            at java.lang.Thread.run(Thread.java:832) [?:?]
    Caused by: java.lang.InstantiationException: de.devfelix.Spielstand
            at java.lang.Class.newInstance(Class.java:639) ~[?:?]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
            ... 8 more
    Caused by: java.lang.NoSuchMethodException: de.devfelix.Spielstand.<init>()
            at java.lang.Class.getConstructor0(Class.java:3508) ~[?:?]
            at java.lang.Class.newInstance(Class.java:626) ~[?:?]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
            ... 8 more
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 21, 2020
  4. Offline

    gochi9

    Kars method is very good and i use it most of the times. But there is also another method if you want to try:
    Code:
    public class Main extends JavaPlugin{
       
        static Main instance;
       
        public void onEnable() {
            instance = this;
        }
    
        public static Main getInstance() {
            return instance;
        }
       
    }
    
    I still suggest you use Kars method but again this is a little different with the same result
     
  5. Offline

    Kars

    "this"
     
  6. Offline

    Strahan

    I see this a lot, and I'm curious; what's the benefit of having a getter for a static variable? I don't usually use getters with something that is already accessible. Like why do Main.getInstance().getConfig() instead of just Main.instance.getConfig()? I usually only have a getter for variables that aren't already accessible.
     
  7. Offline

    Kars

    @Strahan because it is best practice in Java to access private variables through getters. In that example, instance is supposed to be private. This is called a class-based Singleton.
     
    Strahan likes this.
  8. Offline

    Strahan

    Ahh, thanks.
     
Thread Status:
Not open for further replies.

Share This Page