Scheduled Delayed Tasks

Discussion in 'Plugin Development' started by Requadin, Sep 2, 2012.

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

    Requadin

    I'll explain what I want with an example.
    When a player types /shop he'll be teleported to a specific destination (shop will be there) after 10 seconds he executed the command. And after 30 seconds he will be teleported back to another specific location.
    /shop will execute 2 teleportation commands in an order. First one will run after 10 seconds, and the other one will run after 30 seconds. All will take 40 seconds.
    To do that, I'm using this code that found on Bukkit.wiki:
    Code:
    myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {
      public void run() {
          getServer().broadcastMessage("This message is broadcast by the main thread");
      }
    }, 60L);
    My question is, how to add another delayed task after this? I didn't find copying/pasting this whole code again and again logical so I'm asking for the best way to do it.

     
    ProMCKingz and malandrix_bunny like this.
  2. Offline

    XbannisherX

    well that is the best way to do it
     
  3. Offline

    macmc

    Do a loop,
    Code:
    String[] msgs = {"mymsg 1", "mymsg2"};
    int delay = 40;
    for(int i=0; i<msgs.length;i++) {
      Bukkit.getScheduler.[FONT=Consolas]scheduleSyncDelayedTask[/FONT](myplugin, new Runnable() { public void run() {
          getServer().broadcastMessage(msgs[i]); }
      }, delay*(i+1));
    }
     
  4. Offline

    Requadin

    XbannisherX
    Actually, I think I wasn't clear. So I'm asking in another way. Would that code be true?
    Code:
    myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {
      public void run() {
          getServer().broadcastMessage("This is the first teleportation.");
      }
    }, 60L);
    myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {
      public void run() {
          getServer().broadcastMessage("This is the second teleportation.");
      }
    }, 120L);
     
  5. Offline

    XbannisherX

    That code would work, but its just what you like the most, a loop, or 2 delayed tasks
     
  6. Offline

    Requadin

    XbannisherX
    Hmm, looks like mine scheduler is not starting. Any idea why? :confused:

    Code:
    int taskId1;
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        final Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("ready")){
                if(Prepare.contains(sender.getName())){
                    sender.sendMessage(ChatColor.BLUE + "You have already registered yourself on that game! Type /leave if you want to quit.");
                }
                else{
                    int playing = Players.size();
                    if(playing == 0){
                        int waiting = Prepare.size();     
                            Prepare.add(sender.getName()); 
                            sender.sendMessage(ChatColor.GREEN + "You're ready, waiting for other players:" + Prepare.size() + "/3");
                            if(waiting == 2){             
                                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                                    public void run() {
                                        World w = player.getWorld();
                                        Location map1 = (Location) w.getBlockAt(1, 50, 1);
                                        player.teleport(map1);
                                        Players.addAll(Prepare);
                                        Prepare.clear();
                                        }
                                        }, 400L);
                             
                                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                                    public void run(){
                                        player.sendMessage(ChatColor.GOLD + "The game has started! Ready to die!");
                                        World w = player.getWorld();
                                        Location safezone = (Location) w.getBlockAt(20, 50, 20);
                                        player.teleport(safezone);
                                        player.sendMessage(ChatColor.GREEN + "The game has ended.");
                                        Players.clear();
                                        Zombies.clear();
                                        }
                                        }, 800L);     
                            }
                    }
                    else{ sender.sendMessage(ChatColor.RED + "You can't join now, please wait for the end of the round.");
                        }
                }
            }            
     
  7. Offline

    the_merciless

    I thought you could add a delay before the delayed task started running, like this },200L, 600L); couldnt you do something like this:

    Code:
    plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                                 
                                    //tp player to shop
     
                                    public void run() {
                                        //tp player back
                                        }
     
                                        },200L, 600L);
    Thats probably not right, but hopefully you can see what im trying to do.
     
  8. your wanting 2 delayed tasks, 1 after 200 ticks to teleport the player to the shop, after 600 ticks teleport back to old location

    phsedocode:
    Code:text
    1.  
    2. player did command
    3. schedule a task after 200 ticks:
    4. teleport player
    5. schedule a task after 400 ticks:
    6. teleport back
    7.  
     
  9. Offline

    the_merciless

    So my idea wont work? What are the 2 different ints supported by the sync tasks for?
     
  10. the 2 int are used for an repeating task, let we call the first int "a" and the second "b"

    1. the task waits first "a"ticks
    2. call the runnable
    3. wait "b" ticks
    4. go back to step 2

    ^above is an simplid diagram for how it works, the real code doesn't actually waits, but wil runn other tasks while it is waiting
     
  11. Offline

    the_merciless

    I see, someone told me that when i was using repeating tasks, thought it might work for delayed task too.
     
Thread Status:
Not open for further replies.

Share This Page