Solved Problems to start two schedulers in behind

Discussion in 'Plugin Development' started by xImCadium, May 3, 2016.

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

    xImCadium

    Hey guys, I wanted to add to my plugin a function, so that when I execute the command /startkw, the players (didn't add that yet) get teleportet into the lobby, then a scheduler starts for 30 seconds (or longer) and after that the players get teleportet into the the Arena, when they got teleportet there will be a second timer for 10 seconds.

    The first scheduler in my code works, but I dont get the second scheduler to run. When I send a text msg right after the first scheduler, the text message gets send immediatly when the scheduler starts. And the second scheduler doesn't run at all.

    Here's my code:
    Code:
    public class Start implements CommandExecutor {
    
        public KitWars plugin;
    
        public Start(KitWars plugin) {
            this.plugin = plugin;
        }
    
        setLobbySpawn lobbyspawn = new setLobbySpawn(plugin);
    
        int highlobby = 31;
        int countlobby;
        int countwarmup;
        int highwarmup = 51;
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
            if(cmd.getName().equalsignorcase("startkw"){
              if(sender instanceof Player){
         
            Player player = (Player) sender;
            plugin.setGameState(GameState.LOBBY);
            //if (cmd.getName().equalsIgnoreCase("startkw")) {
             
                Location loclobby = player.getLocation();
             
                plugin.setGameState(GameState.LOBBY);
    
                loclobby.setX(plugin.getConfig().getDouble("LobbyX"));
                loclobby.setY(plugin.getConfig().getDouble("LobbyY"));
                loclobby.setZ(plugin.getConfig().getDouble("LobbyZ"));
    
                player.teleport(loclobby);
    
                countlobby = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    
                    @Override
                    public void run() {
                        if (highlobby != 1) {
                            highlobby--;
                         
                            player.sendMessage(plugin.getPrefix() + " Spiel wird gestartet in " + highlobby + " sekunden!");
                            player.setLevel(highlobby);
                            player.setExp(highlobby / 30);
                        } else {
                            // TitlesAPI.sendTitle(player, 0, 20, 0, "Spiel
                            // startet", null);
                            player.sendMessage("Spiel gestartet!");
                            plugin.setGameState(GameState.WARMUP);
                            highlobby = 31;
                            Bukkit.getScheduler().cancelTask(countlobby);
                        }
                    }
                }, 0, 20);
    
                if (plugin.getGameState() == GameState.WARMUP)
    
                {
    
                    Location l = player.getLocation();
                    l.setX(plugin.getConfig().getDouble("ArenaX1"));
                    l.setY(plugin.getConfig().getDouble("ArenaY1"));
                    l.setZ(plugin.getConfig().getDouble("ArenaZ1"));
    
                    player.teleport(l);
    
                    countwarmup = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    
                        @Override
                        public void run() {
                            if (highwarmup != 1) {
                                highwarmup--;
                                player.sendMessage(
                                        plugin.getPrefix() + " Spiel wird gestartet in " + highwarmup + " sekunden!");
                                player.setLevel(highwarmup);
                                player.setExp(highwarmup / 50);
                            } else {
                                // TitlesAPI.sendTitle(player, 0, 20, 0, "Spiel
                                // gestartet",
                                // null);
                                player.sendMessage("Lets go!");
                                plugin.setGameState(GameState.WARMUP);
                                highwarmup = 51;
                                Bukkit.getScheduler().cancelTask(countwarmup);
                            }
                        }
                    }, 0, 20);
                }
            }
    }
            return false;
        }
    
    }
    Maybe you have an idea of how to fix my problem, or my maybe you know another way to add my wanted function :D
     
    Last edited: May 4, 2016
  2. Offline

    mine-care

    Im pretty sure it should be
    Code:
    cmd.getName().equalsIgnoreCase(String param).
    Did that even compile???

    Also, have you debuged?
     
  3. Offline

    xImCadium

    @mine-care
    Yeah it did, I can start the command, the first scheduler runs as usual.
     
    Last edited: May 3, 2016
  4. Offline

    Firestar311

    Try an AsyncRepeatingTask for both of them
     
  5. Offline

    xImCadium

    Nah, won't work:/
     
  6. Offline

    WinX64

    The second one will not run because there's no code telling it to run.
    What I mean is, your first task isn't scheduling the second one.
     
  7. Offline

    xImCadium

    Last edited: May 6, 2016
  8. Offline

    WinX64

    You know, schedule your second task after the first one finished executing. That's what you want, right?
     
  9. Offline

    xImCadium

    @WinX64 Yeah, that's right, but I don't know schedule the second tast after the first one finished, with my code the second doesn't execute at all.
     
  10. Offline

    WinX64

    You got the right code, just ordered it incorrectly.
    Let's go step by step on what the command is actually doing:

    1) Set the GameState to LOBBY
    2) Schedule the first task
    3) Schedule the second task only if the GameState is set to WARMUP, which will never be at this point

    Instead of doing this, simply re-order your code. Schedule the second task right after you cancel the first one, and it should be good to go.
     
  11. Offline

    xImCadium

    Yeah! Now it works, thank you all for your help. :D
     
Thread Status:
Not open for further replies.

Share This Page