Get Static Plugin Instance?

Discussion in 'Plugin Development' started by zachoooo, Oct 26, 2012.

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

    zachoooo

    I have a static function and I am trying to schedule a delay for temp gode mode outside of the main plugin class. How can I get an instance of the plugin without having to instantize the plugin?
    Code:
    Bukkit.getScheduler().scheduleSyncDelayedTask(, new Runnable() {
                public void run() {
                    setTempGodMode(false);
                    ChatManager.sendMessage("You are no longer invincible.");
                }
            }, 100L);
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    you have to pass the reference in.
     
  3. Offline

    zachoooo

    How so?
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    You other classes must accept a reference from your main plugin itself.
     
  5. Offline

    zachoooo

    How do I reference it?
     
  6. Offline

    Milkywayz

    Add this near the top of your class under imports:
    Code:
    private static final <nameofyourclass> instance = new <nameofyourclass>();
    Obviously replace <nameofyourclass> with the name of your class.
    Add this somewhere within your class:
    Code:
    public static final <nameofyourclass> getPlugin() {
    return instance;
    }
    This is using static, you can pass the main classes instance in other ways.
    Such as:
    Code:
    public static ClassName main;
     
    @Override
    public void onEnable() {
    main = this
    }
    Then in another class use ClassName.main
     
    zachoooo likes this.
  7. Offline

    zachoooo

    Thank you kind sir.
     
Thread Status:
Not open for further replies.

Share This Page