1.15.2 Error: Asynchronous command dispatch!

Discussion in 'Spigot Plugin Development' started by Fabio030, Feb 10, 2020.

  1. Trying to update CommandSync client to 1.15.2 I got this error:

    [19:33:29] [Thread-13/WARN]: Exception in thread "Thread-13"
    [19:33:29] [Thread-13/WARN]: java.lang.IllegalStateException: Asynchronous command dispatch!
    [19:33:29] [Thread-13/WARN]: at org.spigotmc.AsyncCatcher.catchOp(AsyncCatcher.java:14)
    [19:33:29] [Thread-13/WARN]: at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:709)
    [19:33:29] [Thread-13/WARN]: at com.fuzzoland.CommandSyncClient.ClientThread.run(ClientThread.java:63)


    This is the cut of code that causes the error (Lines 61,62,63,64,65 at ClientThread.java class):

    Code (Java):

    if(data[0].equals("console")) {

                      String command = data[2].replaceAll("\\+", " ");

                      Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command);

                      plugin.debugger.debug("Ran command /" + command + ".");

                    }
     
     
  2. You're trying to use a Bukkit method on an Asynchronous thread. You can only call Bukkit methods via sync
     
  3. How can I do that?
     
  4. Well somewhere in your code you've got something similar to:
    getServer#getScheduler#runTaskAsynchronously(plugin, () -> {
    So either re-arrange or remove it entirely.

    All your code runs on the main thread unless explicitly told not to like the code above does
     
  5. Ah, iirc extending Thread creates another thread (Essentially off-main thread). In this case, try surrounding the bukkit code with
    plugin.getServer#getScheduler#scheduleSyncDelayedTask(plugin, () -> {}
    However, this isn't the preferred way to do this as I assume you've created a new thread for a reason
     
  6. I haven’t any idea how to do that
     
  7. Can you make me a little example?
     
  8. Code (Java):
    getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> {
    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command);
    });
    But please take care! This doesn't by any means "update" the plugin. It's simply a work around. I don't know how that plugin you're trying to update works but this definitely isn't the greatest method!
     
  9. Thanks Harry. It works. Since it is an urgent thing for my server for the moment I keep it that way, then I calmly rearrange the class.
     
  10. Glad I could help :)
     
  11. Judging by the server/client socket jargon, I'd assume this was probably a plugin from before the plugin messaging API, which should make what you're trying to do much much easier. I'd suggest looking into it here.
     
    • Agree Agree x 1
  12. https://github.com/YoFuzzy3/CommandSync/pull/12
    This pr could be updated. So "new" technologies can be applied. A refactoring would be good as well.
     
    • Like Like x 1
  13. Instead of doing major rewrites to the plugin that could effectively render your plugin inoperable for several days that is being used on a production server, simply resolve the issue at hand and work on the bigger problems in a separate branch. What I mean is, push the following fix to your current branch and then create a new branch and work on the afformentioned fixes by the people above over time.

    Replace the following line of code
    Code (Java):
    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command);
    With this code which complies with the new craftbukkit standard where you cannot dispatch commands asyncronously
    Code (Java):
    Bukkit.getScheduler().runTask(plugin, () -> {
        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
    });
    It didn't use to be that you had to dispatch commands from the main thread, that was added as the life cycle of the Bukkit project went on.
    Please take care to consider what several people here have also suggested, that is, using updated and more reliable methods.
     
    • Friendly Friendly x 1