1.12.2 Bukkit.getScheduler().isCurrentlyRunning always throws java.lang.IllegalStateException: Not schedule

Discussion in 'Spigot Plugin Development' started by CabbageRoll_, Oct 17, 2020.

  1. Each time i want to start a game command initGame function is called and i want it to function both as start and restart at same time (if the task is already running cancel it). I thought this exception should be thrown only on the "start" case and not in "restart" case. Any idea what could be wrong? And if i can somehow get rid of the try & catch altogether?
    In the "restart" case another task will run alongside with old ones but i need to destroy the old one. If 1 task is running the output would be 0123456... and when i start about 10 of them numbers are completely scrambled meaning old ones are still running

    The runnable part:
    Code (Text):

    int looptick;
    BukkitRunnable loop=new BukkitRunnable(){
            @Override
            public void run() {
            /*********irrelevant code*********/
               System.out.println(looptick++%20);
            }
        };
     

    initGame:
    Code (Text):

    void initGame(){
            try{
                if(Bukkit.getScheduler().isCurrentlyRunning(loop.getTaskId())){
                    Bukkit.getScheduler().cancelTask(loop.getTaskId());
                }
            }catch(Exception e){
                System.out.println("illegal state exception not scheduled yet");
            }
         
            /*****irrelevant code****/

            looptick=0;
            playGame();
        }
     

    playGame:
    Code (Text):

    void playGame(){
              loop.runTaskTimer(Pluginmain.plugin, 0, 0);
        }
     

    Without try and catch (for the first run, so it won't be able to ever go on, as expected):
    java.lang.IllegalStateException: Not scheduled yet
    With try and catch, the exception is thrown every time (not as expected)
     
  2. Instead of catching the error and printing out an error message please provide us with the full stacktrace.
    Also catching the general exception and then printing another one while it could be any exception could lead to confusion.
     
  3. [17:12:40 WARN]: java.lang.IllegalStateException: Not scheduled yet
    [17:12:40 WARN]: at org.bukkit.scheduler.BukkitRunnable.checkScheduled(BukkitRunnable.java:149)
    [17:12:40 WARN]: at org.bukkit.scheduler.BukkitRunnable.getTaskId(BukkitRunnable.java:143)
    [17:12:40 WARN]: at cabbageroll.tetrdotjar.Table.initGame(Table.java:305)
    [17:12:40 WARN]: at cabbageroll.tetrdotjar.Startgame.onCommand(Startgame.java:25)
    [17:12:40 WARN]: at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [17:12:40 WARN]: at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
    [17:12:40 WARN]: at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648)
    [17:12:40 WARN]: at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1399)
    [17:12:40 WARN]: at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1234)
    [17:12:40 WARN]: at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45)
    [17:12:40 WARN]: at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1)
    [17:12:40 WARN]: at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13)
    [17:12:40 WARN]: at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    [17:12:40 WARN]: at java.util.concurrent.FutureTask.run(Unknown Source)
    [17:12:40 WARN]: at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46)
    [17:12:40 WARN]: at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748)
    [17:12:40 WARN]: at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406)
    [17:12:40 WARN]: at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679)
    [17:12:40 WARN]: at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577)
    [17:12:40 WARN]: at java.lang.Thread.run(Unknown Source)

    line 305:
    if(Bukkit.getScheduler().isCurrentlyRunning(loop.getTaskId()))

    I know that the solution would be some magic command such as checkScheduled but it doesnt exist?
     
  4. You are checking if the runnable is currently running while it hasn't been scheduled to run yet (you didn't start it yet!)
    This will by the looks of it throw an error since it's an unexpected case.

    I would suggest you just check if loop is not equal to null. If that's the case, simply cancel it using the in build method in runnable.
     
  5. loop will never be equal to null, which part exactly of loop do i need to check for null?
     
  6. I would be just another way to check if you have ever set the runnable. Without much of the code, the check you are trying to perform won't work because the runnable has never started.

    I would just recommend creating a new Runnable when you start the game and not using the same one over and over again.
     
  7. Sorry it turns out i were dumb, the problem was actually in the outside part of the code, i made a new object each time so it wiped out everything that was there. I now understand the difference between BukkitRunnable and BukkitTask and why it would give me an error.
    Anyway, there actually were some changes at the end so this is my final code:

    Code (Text):

    BukkitTask task;
    //................

    void initGame(){
           
            if(task!=null){
                task.cancel();
            }
           
            //.................
            looptick=0;
            playGame();
        }
    //................
    void playGame(){
              task=new BukkitRunnable(){
                  @Override
                  public void run() {
                      //..............
                      System.out.println(looptick++%20);
                 }
              }.runTaskTimer(Pluginmain.plugin, 0, 0);
        }
     
     
    #7 CabbageRoll_, Oct 17, 2020
    Last edited: Oct 17, 2020