[Help] Using schedulers in separate classes.

Discussion in 'Plugin Development' started by feff890, Aug 5, 2015.

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

    feff890

    WARNING: Probably a noob question but...

    I am trying to make a scheduler in a separate class but I am stuck somewhere D: Ill show you my code.
    Code:
                        cooldown.add(e.getPlayer());
                        Bukkit.getScheduler().scheduleSyncDelayedTask(Main, new Runnable() {
    
                            @Override
                            public void run() {
                                cooldown.remove(p);
                                p.sendMessage(ChatColor.GREEN + "Ability refreshed!");
                            }
                           
                        }, 50L);
    The <Problem is here> part should be a Plugin. This isn't in my main class so that's why I can't do: this. Please is there any help?
    Future thank you.
     
  2. Offline

    Gater12

    @feff890
    You have to pass your plugin instance into the class. You can use a constructor.
     
  3. Example (A is main class, B is other class):
    Code:
    public class A extends JavaPlugin {
    
    public void onEnable() {
    new B(this); //Passing a reference of this class to the constructor of B's class
    }
    
    }
    Code:
    public class B {
    
    private A instance;
    
    public B(A instance) {
    this.instance = instance; //Assigning the local variable to the reference
    }
    
    }
     
    feff890 likes this.
  4. Offline

    Cycryl

    @FisheyLP @Gater12 @feff890
    Even simpler when doing it for multiple classes:
    In your main add:
    Code:
    public static Main main;
    
    public Main(){
      main = this;
    }
    Then in the other classes do:
    Code:
    Main.main.doSomething("Cycryl is awesome");
     
  5. You forgot to say that you must set the static class instance to null in onDisable or memory leaks occur
     
  6. Offline

    CoolDude53

    @Cycryl

    If you go that route then set the static instance using this method.

    Code:
    public static Main plugin;
    
    public class Main extends JavaPlugin
    {
        @Override
        public void onEnable()
        {
            setPlugin(this);
        }
    
        @Override
        public void onDisable()
        {
            setPlugin(null);
        }
    
        private static synchronized void setPlugin(Main main)
        {
            this.plugin = main;
        }
    }
     
    Last edited: Aug 12, 2015
  7. Why do you create an extra method instead of "plugin = this;" and "plugin = null;"?
     
    Shortninja66 likes this.
  8. Offline

    CoolDude53

    To ensure thread safety, but it is only really helpful in plugins where thread safety is a real concern. Even though this is most likely not the case here, I still would recommend it to eliminate possible problems in future development.
     
  9. @CoolDude53
    Code:
    public Main() extends JavaPlugin
    Bad code examples are bad
     
    FisheyLP likes this.
  10. Offline

    CoolDude53

    @megamichiel woops, did it on my phone while working daycare, fixed now.
     
  11. Offline

    mythbusterma

    @CoolDude53 @megamichiel @Cycryl

    Bad code examples are still bad because they encourage poor coding practice.

    Constructors exist for a reason, and violating OOP practices just because you're lazy is certainly not a viable solution.
     
    Shortninja66 likes this.
  12. Offline

    CoolDude53

Thread Status:
Not open for further replies.

Share This Page