Schedulers

Discussion in 'Plugin Development' started by number1_Master, Jan 9, 2012.

Thread Status:
Not open for further replies.
  1. How do you use a scheduler? Yes, I did read a post about them, but it isn't really clear (to me)... Well, I understand most of it. Anyways, I want to use a scheduler in my plugin (or I want a delay), within and onCommand statement. How would I do that?
    THANKS
     
  2. Something like this.

    getServer().getScheduler().scheduleSyncRepeatingTask(this, rm, 0L, (long) delay);
     
  3. what does rm, oL, stand for
    and should i replace delay with the amount of ticks or something!?
    and, can i just put this in my code, and will it delay for my set delay? if so, then this is pretty much what im looking for
     
  4. Guess so :|
     
  5. What is your problem then?
    (of course you'll use a syncTask, but the syntax is the same).
     
  6. i want to use something like thread.sleep, and that is usable within an onCommand statement! :)
     
  7. there's another example for syncDelayed tasks, too... <.<
     
  8. but someone told me not to use Thread.sleep with bukkit api
     
  9. That's why you use a sync delayed task, which will get executed after the delay... <.< did you even read the wiki article? It explains that...
     
  10. yes multiple times i got that link in another thread i have !
    in the original post i even said i read that!

    what i want is a delayed task within a while loop

    Edit: when i type the syncdelay task, will empty {} then the delay work, and give a delay for that time?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  11. Then I really don't understand your issue. Use a syncDelayed task if you want to execute some code after some delay. Use a syncRepeating task if you want to execute some code multiple times with some delay between each execution. All that is described at the wiki and all I do is telling you what you can read there...
     
  12. ... i have read the wiki multiple times
    what i am asking is what exactly would i use within and onCommand statement
    you can't really use public void within it!
     
  13. If your command gets executed you start a new task. the public void run() { // delayed code here } inside of the Runnable will get executed after the delay. The Runnable can also be it's own class (but it has to implement Runnable).

    //EDIT: And again: All this is described at the wiki...
     
  14. within the onCommand statement?
     
  15. You know Java? You know how to execute commands?
    ...
    Code:java
    1. public boolean onCommand(CommandSender sender, Command command,
    2. String label, String[] args)
    3. {
    4. //code which will executed right at the command execution here...
    5. getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() { public void run() {// code which will get executed after the delay here } }, delay);
    6. return true;
    7. }
     
  16. that is what i was asking 2-3 of my posts ago :) i was double checking
    i know this turned into a full out argument, but your help is greatly appreciated
    *bow*
    :)

    !!!!!!!!!! one more thing
    if i put the code above (just that), would it just delay it for the set delay, then continue on to the next code?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  17. No. It will only delay the code in the Runnable. Every code before and after it is executed instantly. I really thing you need a better understanding of Java. :(
     
  18. -_-
    im not used to the bukkit api and have never used a scheduler before ok!
    anyways, i just get bunch of errors, so ill just try different things
     
  19. Well, if you handle a class to a function called scheduler it should be clear that the classes run function is executed from the scheduler after the time passes. It's impossible that any code outside of that Runnable class is delayed, too (the scheduler can't know it, you didn't give anything else to it). That has nothing to do with Bukkit, it's the understanding of Java syntax. :(
     
  20. well all i have heard of was Thread.delay ...
    i think i got it anyways, ima just test it

    i think this doesn't work within a while loop, does it...

    Edit: or within a for loop...

    this is the closest code that i got to what i need, but yet it still doesnt working
    *rips out hair*
    !!!!!!!!!!!!!!!!!!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  21. @V10lator
    i figured out schedulers, a little, and yet im still having a little bit of trouble
    I also want to say sorry for the trouble i put into you. I never realized how I should of stated what I wanted out of the schedulers... Sorry, but I understand schedulers better, at least :)
    ANYWAYS... What I want is to have lightning strike every x milliseconds. Before I had it where it would be in a while loop (now switched to a for loop) and lightning would strike every .15 seconds (using Thread.sleep). Then I found out that it is not good to put the main thread to sleep, and that I should use schedulers. Schedulers confused my very much (but now I somewhat understand them).
    So, what I want is a scheduler that would replace Thread.sleep, but so far that is not working within my for loop. If you understand, is there a better way (or a way) of doing this?
    Before it was organized like so (sorry I am in a rush):
    Code:java
    1. for(int counter=1;counter<=20;counter++)
    2. {
    3. //lightning strike
    4. Thread.sleep(150);
    5. //and so on with the try and catch
    6. }
     
  22. Offline

    TheFieldZy

    You do realize that when you sleep a thread, you are sleeping the main thread which means the server doesn't run? Like, for example, when you type /reload, nothing happens for about 2 seconds because thread.sleep is called.
     
  23. yes, i know that, and i even mentioned that, AND i was told that over 100000 TIMES!!!! Well not that many but I said i know it puts the main thread to sleep! I'm not stupid :p
     
  24. Offline

    TheFieldZy

    Woops, misread, I was in a rush. But you want something that strikes lightning 20 times every .15 seconds? Something like this should work, I am too busy to do the math for the ticks, but here is the basic layout:

    Code:java
    1.  
    2. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    3. public void run(){
    4. world.strikeLightning(loc);
    5. }
    6. }, initialDelayL, timeTillNextStrikeL);
    7.  


    You can make a for loop in there to keep track of how many times its been run and return it when its done I think. Shouldn't be hard.
     
  25. OOO ya i just made a mistake about it
    i thought the first number (initial delay) is when it would start, and the second is when it would end... which i know makes no sence
    ima try it :)
     
  26. Code:java
    1. UUID uuid = UUID.RandomUUID;
    2. aMapWhichStoresThePids.add(uuid, getServer().getScheduler().scheduleSyncRepeatingTask(this, new StrikeTask(this, uuid), 0L, 5L);

    Code:java
    1. public class StrikeTask implements Runnable
    2. {
    3. private final YourPlugin plugin;
    4. private final UUID uuid;
    5. private int counter = 1;
    6.  
    7. public StrikeTask(YourPlugin plugin, UUID uuid)
    8. {
    9. plugin = plugin;
    10. uuid = uuid;
    11. }
    12.  
    13. public void run()
    14. {
    15. if(counter > 20)
    16. {
    17. plugin.getServer().getScheduler().cancelTask(plugin.aMapWhichStoresThePids.get(uuid));
    18. plugin.aMapWhichStoresThePids.remove(uuid);
    19. return;
    20. }
    21. //Strike your lightning here...
    22. counter++;
    23. }
    24. }

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

Share This Page