Thread.sleep(), wait(), TimerTask, AHHH!

Discussion in 'Plugin Development' started by 7rmb7, Aug 11, 2011.

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

    7rmb7

    Can someone help me out? All I need is to know how to make my plugin wait. (Without making Bukkit wait too.)

    Please, someone... I've been trying for two days.

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

    bassfader

  3. Offline

    7rmb7

    Okay... so would I have to put the rest of my program in one of those or will it pause until that time? Otherwise, it would go past the runnable and run the rest of my program.
     
  4. Offline

    bassfader

    No it will still continue, see it as a seperate thread that waits and then executes what is put in the brackets. You could also create a new class which implements "Runnable" and put all the code which is meant to be run delayed there (thats the method I prefer).

    Like this:

    Main class
    Code:
    public class TestPlugin extends JavaPlugin {
        public final Logger log = Logger.getLogger("Minecraft");
    
        public void onEnable()
        {
            log.info("I am Enabled!");
            startDelayedCode();
        }
    
        public void onDisable()
        {
            log.info("I am Disabled!");
        }
    
        public void startDelayedCode()
        {
            getServer().getScheduler().scheduleAsyncDelayedTask(this, new TestSchedule(this), 20L * 60);
            log.info("I will still be run instantly without delay!");
        }
    }
    scheduler class
    Code:
    public class TestSchedule implements Runnable {
        private TestPlugin plugin;
    
        public TestSchedule(TestPlugin instance)
        {
            plugin = instance;
        }
    
        public void run()
        {
            plugin.log.info("I've been run delayed!");
        }
    }
    In that example on enabling the Plugin you first would see the message "I am Enabled!" directly followed by "I will still be run instantly without delay!", and about a minute after that it would display the message "I've been run delayed!"
     
  5. Offline

    7rmb7

    Okay, thanks. I'll have to have them nested.


    EDIT: Couldn't I use TimerTasks?
     
Thread Status:
Not open for further replies.

Share This Page