How to use scheduleSyncDelayedTask [Noob Question]

Discussion in 'Plugin Development' started by Danny247, May 3, 2012.

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

    Danny247

  2. Offline

    r0306

    You need to define your main class inside the class that your task is located in like this:
    Code:
    private yourmainclass plugin;       
    public yourcurrentclass (yourmainclass plugin) {
        this.plugin = plugin;
    }
     
  3. Offline

    VeryBIgCorp

    If it's in your main class, then just use the reserved word 'this,' since your main class SHOULD (to work) be derived from JavaPlugin.
     
  4. Offline

    Danny247

    I originally had it in the main class but because of the 1st reply I made a new class for it (I tried using "this" but it gave me errors with being/not being static everywhere).

    Here's what my new class looks like, I'm getting errors that plugin cannot be null :/

    Code:
    public class HelperFunctions {
        
        private static HungerGames plugin;       
        
        public HelperFunctions (HungerGames plugin) {
            this.plugin = plugin;
        }
        
        
        public static void registrationOpen(){
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                   public void run() {
                       Bukkit.getServer().broadcastMessage("This message is broadcast by the main thread.");
                   }
                }, 100L);
            
        }
     
  5. Offline

    Sagacious_Zed Bukkit Docs

    this refers the current instance of the class it is written in. in this case, you would actually use the variable name.
     
  6. Offline

    VeryBIgCorp

    Yes because you're using a static reference to the registrationOpen method, and due to you not instantiating it (HelperCommands), plugin is never set thus returning a NPE.
     
  7. Offline

    Danny247

    I'm very confused :/ What do I need to change to make this work?

    Could someone tell me what I need to change? Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  8. Offline

    VeryBIgCorp

    You're probably just doing HelperFunctions.registrationOpen(); somewhere, but you haven't given a way for the static HungerGames 'plugin' reference to be initialized. Is there a reason for this to be static?

    PHP:
    // HungerGames.java
    public class HungerGames extends JavaPlugin {
        private static 
    HungerGames instance;
        public 
    void onEnable(){
          
    instance this;
        }
        public static 
    HungerGames getInstance(){
            return 
    instance;
        }
    }
    // HelperFunctions.java
    public class HelperFunctions {
        public static 
    void registrationOpen(){
            
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(HungerGames.getInstance(), new Runnable() {
                  public 
    void run() {
                      
    Bukkit.getServer().broadcastMessage("This message is broadcast by the main thread.");
                  }
            }, 
    100L);
        }
    }
     
  9. Offline

    Danny247

    Awesome VeryBigCorp, thanks!! Problem solved.
     
  10. Offline

    VeryBIgCorp

    No problem! Just like to help :)
     
  11. Offline

    spy85

    Oh and btw I have some ideas in mind of how to fix this but I am curious to know if doing this.cancel() at the end of each runnable will take care of it just like that? Thanks!
     
Thread Status:
Not open for further replies.

Share This Page