Cant use commands while Thread.Sleep is running!

Discussion in 'Plugin Development' started by masterofsixpack, May 4, 2014.

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

    masterofsixpack

    Hello,

    I recently made a plugin where a player gets there items every three seconds. I made a Runnable with a run method that gives the player the items every three seconds. I made a counter in there that increments every time the run method is called, so I checked if the counter equals 10. If it does, then I stop the run method for 10 seconds by using the Thread.sleep() method. But, the ONLY problem is that then the Thread.sleep() method is called, I cant do any commands or talk during that duration and after 10 seconds what I recently typed/command I performed shows up. It basically laggs. It is not my PC or anything like that. I made this Runnable inside my onEnable() method. I hope you guys know what the problem is with this.

    Thanks!
     
  2. Offline

    Esophose

    masterofsixpack
    Thread.sleep() actually stops the server's thread from running. Try using a Bukkit Runnable instead.
     
  3. Offline

    SaxSalute

    Yep, that's right. What I'd do is make the runnable fire off every second but only give the item every 3rd run. Then you can track how many seconds occur after the 10 item gives.
     
  4. Offline

    masterofsixpack

    Esophose I actually am using BukkitRunnable. Ideas on that?

    SaxSalute Sorry for these questions! 1. Whats the point of making it run every second? How can I track the run so I can give the item every third run? I can do i++ and initialize i = 0, but how would I use an OR statement that checks if i = 3 OR 6 OR 9 or something like that? How can I track how many seconds occur after the 10 items?
     
  5. Offline

    SaxSalute

    Well, you could fudge it and squeeze some more efficiency if you did 3 second cycles and waited 9 between, but the basic concept is this:
    G = Give an item
    W = Wait
    If you had 1 second cycles, you could do this sequence in the scheduler:
    GWWGWWGWWWWWWWWWWWW REPEAT
    GWW would give you a give with it's own 1 second then 2 seconds of wait, totaling 3 seconds for each GWW. Then there are 10 W cycles after the 3rd GWW to do the 10 second wait.
    And you could do the 9 second wait on 3 second cycles like this:
    GGGWWW REPEAT
    Each G will take 3 seconds and each W will also take 3.
    How you actually code it is up to you, but that's the concept.
     
  6. Offline

    Garris0n

  7. Offline

    Plo124

    masterofsixpack
    Simply make the runnable fire every 3 seconds, and do your work there.
    Code:java
    1. Bukkit.getScheduler().runTaskTimer(plugin,new Runnable(){
    2. public void run(){
    3. // give items here
    4. }
    5. },60L);
     
  8. Offline

    Zethariel

    masterofsixpack use modulo (%) to check is something is divisable by 3. Scheduler stuff runs in main thread, so AFAIK everything you put in there applies to the main thread - also sleeping.
    Running it every X seconds is how stuff is being done around here. That is the only reliable way to hook up recurring events.
     
  9. Offline

    masterofsixpack

  10. Offline

    Gater12

    masterofsixpack
    You create a BukkitRunnable and have it cancel itself after x amount of times.
     
  11. Offline

    maxben34

    Garris0n solved your problem quicker than anybody else on this thread. If you have any questions that aren't on that wiki, please ask them but there is no need to tag everybody except for the one person here that gave you a good answer lol.

    masterofsixpack
     
  12. Offline

    masterofsixpack

    Gater12

    How would I do that with the counter?
     
Thread Status:
Not open for further replies.

Share This Page