Problem with BukkitRunnable

Discussion in 'Spigot Plugin Development' started by OfirTIM, Jul 7, 2015.

  1. soo i get that i can't use the Main Class as using 'this'... what can i do? i am using static and its not allow me to do 'this'... can someone help me?

    Code (Text):
            BeforeGameStarted = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main, new BukkitRunnable() {
     
     
  2. Go in your main class, before onEnable() add
    Code (Text):
    public static JavaPlugin instance;
    and in onEnable() add
    Code (Text):
    instance = this;
    Then in your scheduler, instead of adding Main add Main.instance
     
  3. ^ I do not recommend doing that. Instead, use 'Bukkit.getServer().getPluginManager().getPlugin("YourPluginName")' instead of 'Main'.
     
  4. Why not?
     
  5. Fixed
     
  6. There's too much for me to say about this topic, and it's always widely debated, especially on the Spigot forums. But static instances of the plugin is 'bad'.

    http://stackoverflow.com/a/7084473/1442718
     
  7. No it isn't bad. Static is not 'evil' you need to know how to use it. And Main.instance is not good. Make your instance variable private static and make a getInstance() method which is returning your main class.
     
  8. I myself use static instances but people always say it is bad on Bukkit and Spigot forums. Maybe it's best to avoid static instances, and there is no harm in passing the instance as a parameter in the constructors of classes...
     
  9. you could also use Class c = new Class(); but yeah, its easier to make an static instance and I find it isn't bad.
     
  10. No, you should not create a new instance of a class extending JavaPlugin. Static instances are easier, and seem fine in terms of performance, but "evil" in terms of the reasons listed in that link I posted previously.

    Edit: Even worse, @CrystallFTW didn't even handle static instances correctly. See this is why static instances are bad, not everyone handles them correctly. He didn't say to put 'instance = null' at the end of the onDisable method.
     
  11. Yeah, what did I say;
    So yes, it is. And he isn't using it correctly, what I said earlier;
    I'm glad we agree! :)
     
    • Like Like x 1