Sheduler in static ?

Discussion in 'Plugin Development' started by stonebloodtv, Jan 2, 2015.

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

    stonebloodtv

    Hello :)

    I need to use Sheduler in my NMS class when the entity die any i can't use method this into my sheduler but static method not suported.. i want add a sheduler in a static method, please help..

    Thank.. ;/
     
  2. @stonebloodtv Show the code you currently have. Maybe it doesn't need to be static.
     
  3. Offline

    hexaan

    @stonebloodtv

    I posted this on a different thread a few days ago, but that's the problem when you use the static keyword without knowing why you are using it. You now have a class method that depends on an instance of the class.

    Post your code, maybe there is a chance that you don't need to refer to this to get what you need with a dirty fix. However I would suggest you refactor your class to not use static methods or variables.
     
  4. Offline

    moomaxie

    Create an instance of the Plugin in a constructor then use the instance in replace of "this"

    For example:

    Code:
    public class MyClass {
    
    Main mainClass;
    
    public MyClass(Main main){
      mainClass = main;
    }
    
    public void scheduleThing(){
        Bukkit.getScheduler().scheduleSyncRepeatingTask(mainClass, new Runnable(){
             public void run(){
                     //Do whatever
             }
        },0L,20L);
    }
    
    
    }
    Replace "Main" with your main class.

    If that does not work you can use:

    Code:
    public class MyClass {
    
    public void scheduleThing(){
        Bukkit.getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin("MyPlugin"), new Runnable(){
             public void run(){
                     //Do whatever
             }
        },0L,20L);
    }
    
    
    }
    In this case replace "MyPlugin" with the name of your plugin as specified in the plugin.yml.
     
Thread Status:
Not open for further replies.

Share This Page