IllegalStateException Bukkit Runnable.

Discussion in 'Spigot Plugin Development' started by NaJated, May 9, 2015.

  1. Hey all, I'm unable to cancel my BukkitRunnable.

    Code (Text):
                        if (args[0].equalsIgnoreCase("start") && sender.hasPermission("happyhour.start")) {
                            if (!Main.getInstance().getConfig().getBoolean("HAPPY_HOUR_ACTIVE"))  {
                                try {
                                    final int loops = Integer.parseInt(args[3]);
                                    int length = Integer.parseInt(args[1]);
                                    if (length != 0 && loops != 0) {
                                        Main.getInstance().getConfig().set("HAPPY_HOUR_LENGTH_SECONDS", length);
                                        Main.getInstance().getConfig().set("HAPPY_HOUR_ACTIVE", true);
                                        Main.getInstance().getConfig().set("HAPPY_HOUR_LOOPS", loops);
                                        String specifiedCommand = args[2].replace("_", " ");
                                        Main.getInstance().getConfig().set("HAPPY_HOUR_CMD", specifiedCommand);
                                        Main.getInstance().saveConfig();
                                        run = new BukkitRunnable() {

                                            public void run() {
                                                if (Main.getInstance().getConfig().getInt("HAPPY_HOUR_LOOPS") != 0) {
                                                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Main.getInstance().getConfig().getString("HAPPY_HOUR_CMD"));
                                                    Main.getInstance().getConfig().set("HAPPY_HOUR_LOOPS", Main.getInstance().getConfig().getInt("HAPPY_HOUR_LOOPS") - 1);
                                                    Main.getInstance().saveConfig();
                                                }else{
                                                    for (Player all : Bukkit.getOnlinePlayers()) {
                                                        Main.getInstance().getConfig().set("HAPPY_HOUR_LENGTH_SECONDS", 0);
                                                        System.out.println("HAPPY HOUR CANCELLED");
                                                        Main.getInstance().getConfig().set("HAPPY_HOUR_LOOPS", 0);
                                                        Main.getInstance().getConfig().set("HAPPY_HOUR_ACTIVE", false);
                                                        Main.getInstance().getConfig().set("HAPPY_HOUR_CMD", "nothing");
                                                        Main.getInstance().saveConfig();
                                                        all.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a&lH&b&lA&c&lPP&d&lY &e&lH&f&lO&9&lU&5&lR&6: &fHappy hour is now over."));
                                                        int i = this.getTaskId();
                                                        Bukkit.getScheduler().cancelTask(i);
                                                        all.playSound(all.getLocation(), Sound.CHICKEN_EGG_POP, 10, 10);
                                                    }
                                                }
                                            }
                                        };
                                        if (Main.getInstance().getConfig().getBoolean("HAPPY_HOUR_ACTIVE") && !Main.getInstance().getConfig().getBoolean("HAPPY_HOUR_TASK_RUN_BOOL")) {
                                            Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getInstance().plugin(), run, Main.getInstance().getConfig().getInt("HAPPY_HOUR_LENGTH_SECONDS") * 20, Main.getInstance().getConfig().getInt("HAPPY_HOUR_LENGTH_SECONDS") * 20);
                                            Main.getInstance().getConfig().set("HAPPY_HOUR_TASK_RUN_BOOL", true);
                                        }
     
  2. @IPv420 You are cancelling the BukkitRunnable once per player that is online, move the cancelTask() method outside of the loop through all players. I think it will throw an exception when you try to cancel a task that is already cancelled, so that is what is going wrong. Also please post the full exception if you have one, that help to determine the problem.
     
  3. PHP:
    [14:29:09 WARN]: [MineChill] Task #141 for MineChill v1.0 generated an exception

    java.lang.IllegalStateException: Not scheduled yet
            at org.bukkit.scheduler.BukkitRunnable.getTaskId(BukkitRunnable.java:134
    ) ~[spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at me.TheGamesHawk2001.MineChill.HappyHour$1.run(HappyHour.java:57) ~[?:
    ?]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7
    18) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    >
     
  4. I don't know the full details of what's going on; if you could provide the exact stack trace that would definitely help. With that said I can probably make a guess as to what the issue is. You're canceling the task inside the player loop, something that should only need to be done once is being down multiple times. When it is done the first time it set it to canceled, and then when it did it again, it threw that exception because it was no longer running and was already canceled. Here's the fix, and I optimized a bit more for you.


    Code (Text):
                        if (args[0].equalsIgnoreCase("start") && sender.hasPermission("happyhour.start")) {
                            if (!Main.getInstance().getConfig().getBoolean("HAPPY_HOUR_ACTIVE"))  {
                                try {
                                    final int loops = Integer.parseInt(args[3]);
                                    int length = Integer.parseInt(args[1]);
                                    if (length != 0 && loops != 0) {
                                        Main.getInstance().getConfig().set("HAPPY_HOUR_LENGTH_SECONDS", length);
                                        Main.getInstance().getConfig().set("HAPPY_HOUR_ACTIVE", true);
                                        Main.getInstance().getConfig().set("HAPPY_HOUR_LOOPS", loops);
                                        String specifiedCommand = args[2].replace("_", " ");
                                        Main.getInstance().getConfig().set("HAPPY_HOUR_CMD", specifiedCommand);
                                        Main.getInstance().saveConfig();
                                        run = new BukkitRunnable() {

                                            public void run() {
                                                if (Main.getInstance().getConfig().getInt("HAPPY_HOUR_LOOPS") != 0) {
                                                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), Main.getInstance().getConfig().getString("HAPPY_HOUR_CMD"));
                                                    Main.getInstance().getConfig().set("HAPPY_HOUR_LOOPS", Main.getInstance().getConfig().getInt("HAPPY_HOUR_LOOPS") - 1);
                                                    Main.getInstance().saveConfig();
                                                } else{
                                                    for (Player all : Bukkit.getOnlinePlayers()) {
                                                        all.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a&lH&b&lA&c&lPP&d&lY &e&lH&f&lO&9&lU&5&lR&6: &fHappy hour is now over."));
                                                        all.playSound(all.getLocation(), Sound.CHICKEN_EGG_POP, 10, 10);
                                                    }
                                                    System.out.println("HAPPY HOUR CANCELLED");
                                                    Main.getInstance().getConfig().set("HAPPY_HOUR_LENGTH_SECONDS", 0);
                                                    Main.getInstance().getConfig().set("HAPPY_HOUR_LOOPS", 0);
                                                    Main.getInstance().getConfig().set("HAPPY_HOUR_ACTIVE", false);
                                                    Main.getInstance().getConfig().set("HAPPY_HOUR_CMD", "nothing");
                                                    Main.getInstance().saveConfig();
                                                    Bukkit.getScheduler().cancelTask(this.getTaskId());
                                                }
                                            }
                                        };
                                        if (Main.getInstance().getConfig().getBoolean("HAPPY_HOUR_ACTIVE") && !Main.getInstance().getConfig().getBoolean("HAPPY_HOUR_TASK_RUN_BOOL")) {
                                            Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getInstance().plugin(), run, Main.getInstance().getConfig().getInt("HAPPY_HOUR_LENGTH_SECONDS") * 20, Main.getInstance().getConfig().getInt("HAPPY_HOUR_LENGTH_SECONDS") * 20);
                                            Main.getInstance().getConfig().set("HAPPY_HOUR_TASK_RUN_BOOL", true);
                                        }
     
  5. Cant you add
    Code (Text):
    cancel();
    to cancel the bukkitrunnable? Thats what I use.
     
  6. Whats line 57?
     
  7. int i = this.getTaskId();
     
  8. @IPv420 That is a different error than I expected, instead of these lines:
    Code (Text):
    int i = this.getTaskId();
    Bukkit.getScheduler().cancelTask(i);
    Just use cancel (and still move it outside of the players loop):
    Code (Text):
    this.cancel();
     
    • Like Like x 1
  9. did that before. Gave same error. I also replied with a print scrn of the for loop t'ing.


    Code:

    http://prntscr.com/739pfo

    Error:
    Code (Text):
    [14:36:57 WARN]: [MineChill] Task #110 for MineChill v1.0 generated an exception

    java.lang.IllegalStateException: Not scheduled yet
            at org.bukkit.scheduler.BukkitRunnable.getTaskId(BukkitRunnable.java:134
    ) ~[spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at org.bukkit.scheduler.BukkitRunnable.cancel(BukkitRunnable.java:18) ~[
    spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at me.TheGamesHawk2001.MineChill.HappyHour$1.run(HappyHour.java:59) ~[?:
    ?]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7
    18) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    >
     
  10. Code (Text):
    [14:39:57 WARN]: [MineChill] Task #106 for MineChill v1.0 generated an exception

    java.lang.IllegalStateException: Not scheduled yet
            at org.bukkit.scheduler.BukkitRunnable.getTaskId(BukkitRunnable.java:134
    ) ~[spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at me.TheGamesHawk2001.MineChill.HappyHour$1.run(HappyHour.java:59) ~[?:
    ?]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7
    18) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot-1.8.3.jar:git-Spigot-beb883d-234e607]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    >
     
  11. Check my reply, you can also replace that same piece of code with this.cancel(), but for my example it will work. The reason it wasn't working is it's removing the task from the scheduler for the first player, and then for the second it's calling it again, but this time since it's removed it no longer possesses a task id.


    So it didn't work? I'm quite confused then, let me look into the api for a second to see what would cause that error.
     
  12. Okay.
     
  13. I figured out the problem, it's partially because of an API change, but the main problem is BukkitRunnable isn't supported for Sync Repeating. Don't worry though, there's something that does exactly the same thing, so you just have to use this.

    Change this
    Code (Text):
    Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getInstance().plugin(), run, Main.getInstance().getConfig().getInt("HAPPY_HOUR_LENGTH_SECONDS") * 20, Main.getInstance().getConfig().getInt("HAPPY_HOUR_LENGTH_SECONDS") * 20);
    to this
    Code (Text):

    run.runTaskTimer(Main.getInstance().plugin(), Main.getInstance().getConfig().getInt("HAPPY_HOUR_LENGTH_SECONDS") * 20, Main.getInstance().getConfig().getInt("HAPPY_HOUR_LENGTH_SECONDS") * 20);
     
    That should fix your problem :)
     
    • Like Like x 2
  14. Yes laddddddddddddd! <3 :p :D