Solved BukkitRunnable problem - Not scheduled yet

Discussion in 'Plugin Development' started by Aypro, Jan 20, 2021.

  1. Offline

    Aypro

    So I am working on my plugin and I have added a code to teleport players to the spawn once the time reaches 0 but when ever the time reaches 0 the whole server freezes.
    Code:
    Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, new BukkitRunnable() {
                    @Override
                    public void run() {
                        if (startIn > 0) {
                            startIn--;
                        }
                        for (Player p : Bukkit.getOnlinePlayers()) {
                            p.sendMessage("The game will start in " + startIn);
                            System.out.println("Sent the msg thing  for start in");
    
                        }
    
                        if (player.getWorld().getName().equals(player.getWorld().getName()) && startIn == 0) {
                            for (Player p : Bukkit.getOnlinePlayers()) {
                                Location l = player.getWorld().getSpawnLocation();
                                p.teleport(l);
                                TitleAPI.sendTitle(p, 4, 20, 4, "The game will start in soon...", "We will start the game in " + startIn + " seconds. This time is to wait for additional players.");
                                System.out.println("TP'ed players");
                            }
                        }
    
                        if (startIn <= 0) {
                            System.out.println("enter cancel loop thingy");
                            startIn = 0;
                            System.out.println("set startin to 0");
                            gameStarted = true;
                            System.out.println("gameStarted is truel");
                            this.cancel();
                            return;
                        }
                    }
                }, 0L, 20L);
    it teleports all the player properly but after it teleports everyone I get this error in console and the server is stuck:
    Code:
    [15:16:34 WARN]: Exception in thread "Craft Scheduler Thread - 2"
    [15:16:34 WARN]: org.apache.commons.lang.UnhandledException: Plugin TNTRun v0.0.1 generated an exception while executing task 6
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:56)
            at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
            at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
            at java.base/java.lang.Thread.run(Thread.java:832)
    Caused by: java.lang.IllegalStateException: Not scheduled yet
            at org.bukkit.scheduler.BukkitRunnable.getTaskId(BukkitRunnable.java:134)
            at org.bukkit.scheduler.BukkitRunnable.cancel(BukkitRunnable.java:18)
            at com.MysteriousK.TNTRun.events.SeksiEvents$1.run(SeksiEvents.java:132)
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
            ... 3 more
     
  2. Offline

    Strahan

    Don't use deprecated methods. Do it like:
    Code:
    new BukkitRunnable() {
      // runnable code
    }.runTaskTimer(plugin, 0L, 20L);
    Also
    Code:
    TitleAPI.sendTitle(p, 4, 20, 4, "The game will start in soon...", "We will start the game in " + startIn + " seconds. This time is to wait for additional players.");
    startIn is zero at this point. The time to wait for add'l players is... zero? That doesn't make any sense. Also you should not start the loop by decrementing, you should do that at the end.
     

Share This Page