Scheduler not origined in main class

Discussion in 'Plugin Development' started by Robin Bi, Mar 26, 2014.

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

    Robin Bi

    Hey, i need help again ^^


    I'm working on a little minigame. At the moment, i have two classes, a Commands.java and an EventListener.java. I need a Scheduler in the EventListener.java but i'm not sure how to get it.
    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Commands, new Runnable())

    appears not to work but "Commands" replaced with "this" also throws an error...


    I'm looking forward to read your answers :)
    Yours sincerely,
    Robin Bi


    P.S.: Please don't forget to tag me, so I get the alert ;)
     
  2. Offline

    ShadowLAX

    Robin Bi You get an error because "this" is an instance of your current class, not your main one. So, to get an instance of your main class, a constructor or static instance is your friend.
     
  3. Offline

    Robin Bi

  4. Offline

    ShadowLAX

    Robin Bi Can you post the updated Commands and EventListener classes?
     
  5. Offline

    Robin Bi

    ShadowLAX Sure, here you go:

    Commands.java (wrong name at pastebin): http://pastebin.com/k1tCeHwz
    EventListener.java: http://pastebin.com/xBpY3mT0


    Yeah, i know there are no commands in the commands.java, but they'll come later.
    public int random and public Player theOne are comments because they throw errors as well but that's not what we're talking about atm.


    Yours sincerely,
    Robin Bi
     
  6. Offline

    ShadowLAX

    Robin Bi Instead of just calling the new EventListener(this), try doing EventListener eventlistener = new EventListener(this); I've never seen this error before, but I'm sure it has something to do with how you have handled the constructors by looking at the stacktrace.
     
  7. Offline

    GeorgeeeHD

    do something like this

    Code:
    private static Plugin plugin;
     
    onEnable() {
     
        plugin = this;
     
    }
     
    public static Plugin getPlugin() {
            return plugin;
        } 
    then in the other class, where u would normally put "this", put Commands.getPlugin()
     
    Robin Bi likes this.
  8. Offline

    ShadowLAX

    GeorgeeeHD That's just an alternative to using a constructor. Besides, statics are bad if you don't properly handle them onDisable (memory leaks).
     
  9. Offline

    Robin Bi

    ShadowLAX, GeorgeeeHD i tried out the two solutions, unfortunately none of them seem to work.

    [​IMG]


    Yours sincerely,
    Robin Bi
     
  10. Offline

    ShadowLAX

    Robin Bi plugin.getServer().getPluginManager().registerEvents(this, Commands.getPlugin()); That's the correct way. Regardless, you don't need a static variable of the main class, as you already have a constructor for it.
     
    Robin Bi likes this.
  11. Offline

    Robin Bi

    ShadowLAX, I'm new to bukkit and i'm pretty confused now ^^

    I made screenshots of the code so you can see the errors as well.

    [​IMG]

    and

    [​IMG]



    I'm SURE it's something soooo easy...



    Yours sincerely,
    Robin Bi
     
  12. Robin Bi In Commands:
    Get rid of private static Plugin plugin; because you're not using it.
    Change the whole plugin.getServer()... line to getServer().getPluginManager().registerEvents(this, new EventListener(this));

    In EventListener:
    Change the constructor to
    Code:
    public EventListener(Commands plugin) {
        this.main = plugin;
    }
    And change Commands main = new Commands(); to Commands main;
     
    Robin Bi likes this.
  13. Offline

    Robin Bi

    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page