Thread.sleep() in a separate thread

Discussion in 'Plugin Development' started by jertocvil, Dec 29, 2011.

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

    jertocvil

    Hi, I'm coding a plugin that reads a text file and changes some blocks depending on the contents of this file. I want to emulate a microcontroller. An example of a script that is loaded by the plugin is this:
    Code:
    ON N
    ON S
    WAIT 2000
    ON W
    ON E
    WAIT 2000
    OFF N
    OFF E
    N, S, W and E are the cardinal directions. For example, "ON N" changes the block in the north of the block that acts as a microcontroller to a redstone torch.

    ON and OFF sentences are working fine but I don't know how to implement WAIT. I tried this:

    public class Script extends Thread{
    [...]
    public void run() {
    [...]
    else if("WAIT".equals(args[0])) { Thread.sleep(Long.parseLong(args[1])); return true; }
    [...]

    but server throws "Can't Keep up..." error. Then, I read about Scheduler and tried this, but it does nothing:

    else if("WAIT".equals(args[0])) { Orden.WAIT(Long.parseLong(args[1])); return true; }
    ---
    Code:
        public static void WAIT(long millis){
            plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    
                @Override
        public void run() {
            plugin.getServer().broadcastMessage("Wait");
        }
    }, millis * 50);
        }
    Do you know the proper method to do this? Thanks :)
     
  2. Offline

    ItsHarry

    Making threads won't work, you need to use Bukkit's scheduler, which is the source code you gave at the very bottom.
     
  3. Offline

    Zimp

    Calling Thread.sleep() in the main server thread will generate the "Can't keep up" error since it will suspend important server functionality. You probably need to spawn a separate thread and use that to order your operations and insert delays. However if you do that you will need to make sure that any function calls to the main bukkit api are performed using the Bukkit scheduler, especially for things like changing blocks.

    For example:

    Code:
    new Thread() { @Override public void run() {
        //ON N
        //ON S
        Thread.currentThread().sleep(2000);
        //...etc...
    }}.start();
    
    Where "ON N" and "ON S" would be executed on the server thread using the scheduler, something like:
    Code:
    plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { @Override public void run() {
         //ON N
         //ON S
    }});
    
     
  4. Offline

    jertocvil

    I'm so dumb... The script class extends Thread, but I was calling the run() method instead of start() . Now it works with my code, but I'm going to rewrite it and use schedulers. Thank you :)
     
  5. Offline

    Sleaker

    just be careful, you can't get/set any MC data except for ChunkSnapshots in seperate threads without causing issues/errors with the server. So if you're doing block changes/alterations you can think again. It can't be done in a thread.
     
  6. Offline

    jertocvil

  7. Offline

    Sleaker

    @jertocvil - using scheduleSyncDelayedTask - is the proper way to do it as the task will be executed on the main thread. I was just warning of using the 'thread' example. It will crash/cause issues if you modify data.
     
Thread Status:
Not open for further replies.

Share This Page