Solved My scheduleSyncDelay not waiting before execution

Discussion in 'Plugin Development' started by easterex, May 23, 2020.

  1. Offline

    easterex

    Hello!
    Code:
    package io.github.easterex.firstplugin;
    
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.Command;
    import org.bukkit.entity.Player;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    
    
    public final class firstplugin extends JavaPlugin {
        @Override
        public void onEnable() {
            
            getLogger().info("onEnable has been invoked!");
           
        }
       
        @Override
        public void onDisable() {
            
            getLogger().info("onDisable has been invoked!");
        }
       
    
        public void firsttimer( CommandSender sender,  Player target) {
            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                public void run() {
                    return;
                }
            }, 5800L);
        }
        public void countdown(final CommandSender sender, final Player target)
        {
            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                
                  public void run() {
                      Bukkit.broadcastMessage("10");
                } },20L);
            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
               
                public void run() {
                    Bukkit.broadcastMessage("9");
              } },20L);
            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
               
                public void run() {
                    Bukkit.broadcastMessage("8");
              } },20L);
            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
               
                public void run() {
                    Bukkit.broadcastMessage("7");
              } },20L);
            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
               
                public void run() {
                    Bukkit.broadcastMessage("6");
              } },20L);
            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
               
                public void run() {
                    Bukkit.broadcastMessage("5");
              } },20L);
            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
               
                public void run() {
                    Bukkit.broadcastMessage("4");
              } },20L);
            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
               
                public void run() {
                    Bukkit.broadcastMessage("3");
              } },20L);
            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
               
                public void run() {
                    Bukkit.broadcastMessage("2");
              } },20L);
            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
               
                public void run() {
                    Bukkit.broadcastMessage("1"
                            + "");
              } },20L);
            return;
        }
        public void swapper(final CommandSender sender, final Player target)
        {
            Player senderP = (Player) sender;
            Player targetP = (Player) target;
            Location senderLoc = senderP.getLocation();
            Location targetLoc = targetP.getLocation();
            senderP.teleport(targetLoc);
            targetP.teleport(senderLoc);
            return;
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("deathswap")) { 
                // doSomething
                Player player = (Player) sender;
                if (args.length == 1) {
               
                Player target = player.getServer ().getPlayer(args[0]);
                if(target != null) {
                for (int i=0; i<5; i++) {   
                firsttimer(sender, target);
                countdown(sender, target);
                swapper(sender, target);
               
                }
                return true;
                }
                else {
                    getLogger().info("Player " + target + " is not online.");
                }
                }
               
                return false;
               
               
                }
            else {
                    getLogger().info("Invalid number of arguments");
                }    
            return false;
        }
       
       
    }
    
    My problem is this: In the firsttimer and countdown methods, the statements that are supposed to run after a delay using scheduleSyncDelay just seemingly run immediately.
    I don't receive any errors or warnings, and the plugin runs on a server although I haven't gotten to testing it with a different target player yet as I need to smooth out this issue first.

    I realize the code may have other issues as I've just only started learning java and bukkit a few hours ago, but this is the only one I'd like help with, the rest I can probably figure out with some documentation diving and further learning (although I will of course graciously accept any tips given).

    I'm only making a fairly simple plugin for my friend and me to use on my localhost server on a one-time occasion so please forgive my shoddy, unorganized code(or don't haha).
     
  2. Online

    timtower Moderator Moderator

    @easterex That is because all your timers use the same time.
     
    easterex likes this.
  3. Offline

    easterex

    Thank you! I see I misunderstood this fundamentally. I decided to change it to bukkitrunnables, and simplifying it a great deal.
    Code:
    public void countdown(final CommandSender sender, final Player target)
        {
          
            new BukkitRunnable() {
                @Override
          
                public void run() {
                new BukkitRunnable() {
                    int timer = 10;
                  
                      @Override
                      public void run() {
                        
                          if(timer<1)
                          {
                              Player senderP = (Player) sender;
                                Player targetP = (Player) target;
                                Location senderLoc = senderP.getLocation();
                                Location targetLoc = targetP.getLocation();
                                senderP.teleport(targetLoc);
                                targetP.teleport(senderLoc);
                                timer=10;
                                this.cancel();
                          }
                          else
                          {
                              Bukkit.broadcastMessage("Time remaining: " + timer);
                              --timer;
                          }
                        
                       
                    } }.runTaskTimer(instance, 0L, 20L);
            }
            }.runTaskTimer(this, 6000, 6000);
        }
     

Share This Page