Time Delay

Discussion in 'Plugin Development' started by Royal_Soda, Jan 22, 2012.

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

    Royal_Soda

    Hey, I'm programming a plugin that does various things, but I've hit a stumbling block. I went to the wiki to find out more about time delays and found this piece of code that would help me out:

    myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {
    public void run() {
    getServer().broadcastMessage("This message is broadcast by the main thread");
    }
    }, 60L);

    It works great, but I need to be able to customize the delay. Since 60L goes to 3 seconds, I infer that 20 --> 1 second. But how can I have an integer that is editable in a config file replace this?

    Please help. :p
     
  2. Offline

    Chiller

    You might be able to do: long.parseLong(config.getInt("delay")) * 20L;
     
  3. Offline

    Royal_Soda

    Chiller
    Would you mind giving me an example in code, please? It makes it easier to understand. :p
     
  4. Offline

    Chiller

    myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {
    public void run() {
    getServer().broadcastMessage("This message is broadcast by the main thread");
    }
    }, long.parseLong(config.getInt("delay in seconds")) * 20L);
     
  5. Offline

    Royal_Soda

    D: Chiller
    "Syntax error on token "long", invalid Expression"
     
  6. Offline

    AbeJ

    Change where it says "long.parseLong" to "Long.parseLong".
     
  7. Offline

    Chiller

    Try capital L for long
     
  8. Offline

    Royal_Soda

    hmm.. I feel dum for missing that. Anyways, couldn't I just use "main.int * 20L"? Without the Long.parseLong...
    Chiller
    AbeJ
     
  9. Offline

    Chiller

    Just cast it to a long instead of parsing it
     
  10. Offline

    AbeJ

    Yeah, you could. I'm not sure why the parseLong was in there.
     
  11. Offline

    Royal_Soda

    ;) Thanks AbeJ and Chiller .
    Furthermore, on the statement, we use the variable "myPlugin" two times.. I have this inside an onPlayerLogin event listener. I can just replace the first myPlugin with the a player variable, but what about the second? Should I just use Plugin "plgn = null;" ?
     
  12. Offline

    Chiller

    You can't set it to null if you need to use it
     
  13. Offline

    Royal_Soda

    Chiller
    What should I be assigning the Plugin variable to then?
     
  14. Offline

    Chiller

    Well what is it being used for and or by?
     
  15. Offline

    Royal_Soda

    Chiller
    myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {
    public void run() {
     
  16. Offline

    Chiller

    Do new MyMainClassName();
     
  17. Offline

    Royal_Soda

    Chiller
    Thanks again. One more question.. What if I wanted something done by the plugin constantly, until a said amount of time passed, how would I do it?
     
  18. Offline

    Chiller

    You would make an asynchronous repeating task and put a variable count up one each time in there.
     
  19. Offline

    nisovin

    No, don't do that. You should use a reference to your actual plugin object. There are many ways to do this. You can pass the plugin to whichever object this code is in, or you can put the plugin object in a static variable somewhere. You just need a reference to your plugin.
     
  20. Offline

    Royal_Soda

    Chiller
    So like..
    Code:
    int i =0;
    plr.getServer().getScheduler().scheduleSyncDelayedTask(new Contributor(), new Runnable() {
    public void run() {
    while (i < Contributor.time + 1) {
    //Content here
    i++;
    }
    }
    },  0L, 20L);
    That would run it constantly, every second until the time is up? (Time measured in seconds.)
     
  21. Offline

    AbeJ

    Please DO NOT listen to Chiller. They don't understand this.
    You don't create a new instance of your plugin class. That will mess things up, and generally not work. Just pass in "this" in the constructor. I'm a bit pressed for time, hopefully someone explains it better below me.
     
  22. Offline

    Royal_Soda

    Will this work, then?
    int i =0;
    plr.getServer().getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable() {
    public void run() {
    while (i < Contributor.time + 1) {
    //Content here
    i++;
    }
    }
    }, 0L, 20L);

    ?

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

    Chiller

    Holy shit?! That was just a quick fix for his problem you don't have to get all bitchy.
     
  24. Offline

    AbeJ

    Sorry :-(. I was really tired when I wrote that.
    Your method simply wouldn't work. That's why I said what I did.
     
Thread Status:
Not open for further replies.

Share This Page