Is there a command for something like wait(10)?

Discussion in 'Plugin Development' started by LukeSFT, Oct 24, 2012.

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

    LukeSFT

    like the title says.
     
  2. Offline

    andf54

    Yes there is, its wait(1000*10);

    But you can't use it in normal methods, because it will halt the whole server.

    Use bukkit scheduler instead.
     
    kroltan likes this.
  3. Offline

    LukeSFT

    but when I do:
    Code:
    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {public void run() {}}, 200000L);
    It doesn't wait.
     
  4. this should work

    myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {

    public void run() {
    getServer().broadcastMessage("This message is broadcast by the main thread");
    }
    }, 10*1000*20);

    10 sec * to millis *20 because of server tickets
     
  5. Offline

    LukeSFT

    But i don't want to send a message.
     
  6. Offline

    Woobie

    Code:
    this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
    
    public voi run() {
    //stuff
    }
    }, 20L);
    
    This would wait 1 second.
     
  7. Offline

    LukeSFT

    but I want to pause the execution of the code for for example 10 seconds.
     
  8. Offline

    Hoolean

    LukeSFT
    Code:
    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {public void run() {/* CODE YOU WANT TO EXECUTE AFTER DELAY GOES HERE */}}, 200000L);
     
    Notice how it says schedule. It tells the serverto execute code after a delay, not do the delay then continue with the code after it.
     
  9. Offline

    desht

    What you're asking for is simply not possible. This is event-driven programming, and you can't just pause everything for 10 seconds, or indeed any length of time. What you can do, as others have already pointed out, is schedule a task to run something after a given delay.

    You need to rethink what you're trying to do. Maybe if you explain what it is you're trying to achieve, we can offer more insight.
     
  10. Offline

    LukeSFT

    Ok thanks.

    is it possible to create a new class that extends Thread
    and then do sleep(10000); to stop the whole class?
     
  11. Offline

    Comphenix

    You still haven't explained what you're trying to achieve. You've only told us how you want to do it.

    But to answer your question - yes, that's possible, although you should probably use scheduleAsyncDelayedTask instead. The trouble is, once you're running in an async task/different thread, you can't simply call normal Bukkit methods (except sending messages, ect.). You MUST synchronize with the main thread for everything else.

    Blocking a thread with a long sleep() is a waste of resources, though. Are you absolutely sure you can't simply use scheduleSyncDelayedTask()?
     
  12. Offline

    Hoolean

    What are you trying to do; why can't you use a scheduled task?
     
  13. Offline

    LukeSFT

    I want to do this:
    Code:
    package de.LukeSFT.SnowPile;
     
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
     
    import org.bukkit.Chunk;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Biome;
    import org.bukkit.block.Block;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Snows extends JavaPlugin {
     
        Chunk[] loadedChunks;
        List<Biome> snowBiomes = new ArrayList<Biome>();
        boolean on = true;
        public void onEnable(){
            this.saveDefaultConfig();
            boolean conf = false;
            while(!conf){conf = config();}
            snow();
            //System.out.println(dur())
            System.out.println("[SnowPile] was enabled.");
       
        }
     
        public void onDisable(){
            on = false;
            System.out.println("[SnowPile] was disabled.");
        }
        public boolean config(){
            if(this.getConfig().getBoolean("biomes.taiga")){
                snowBiomes.add(Biome.TAIGA);
            }
            if(this.getConfig().getBoolean("biomes.frozen_ocean")){
                snowBiomes.add(Biome.FROZEN_OCEAN);
            }
            if(this.getConfig().getBoolean("biomes.frozen_river")){
                snowBiomes.add(Biome.FROZEN_RIVER);
            }
            if(this.getConfig().getBoolean("biomes.ice_mountains")){
                snowBiomes.add(Biome.ICE_MOUNTAINS);
            }
            if(this.getConfig().getBoolean("biomes.ice_plains")){
                snowBiomes.add(Biome.ICE_PLAINS);
            }
            if(this.getConfig().getBoolean("biomes.taiga_hills")){
                snowBiomes.add(Biome.TAIGA_HILLS);
            }
            return true;
        }
        public boolean dur(){
            World w = getServer().getWorld(this.getConfig().getString("world"));
            System.out.println("storm:" + w.hasStorm());
            return w.isThundering();
        }
        public void snow(){
          while(on){
            World w = getServer().getWorld(this.getConfig().getString("world"));
            System.out.println("act");
            if(w.hasStorm()){
                //System.out.println("thunder");
                Random random = new Random();
                loadedChunks = w.getLoadedChunks();
                for(Chunk ch : loadedChunks){
                    int x = 0;
                    while(x != 16){
                        int z = 0;
                        while(z != 16){
                            int y = 127;
                            while(y != -1){
                                Block b = ch.getBlock(x, y, z);
                                if(b.getTypeId() != 0){
                                    if( snowBiomes.contains(b.getBiome()) ){
                                        if (random.nextInt(200) == 1){
                                            b.setType(Material.DIAMOND_BLOCK);
                                            //System.out.println("s");
                                            y = 0;
                                        }
                                    }
                                }
                                y--;
                            }
                            z++;
                            wait(20);
                        }
                        x++;
                    }
                }
            }
            wait(20);
            }
        }
    }
    }
    
    I know, that there is no wait(20) but I used this as placeholder.
     
Thread Status:
Not open for further replies.

Share This Page