Solved IllegalStateException: Asynchronous command dispatch!

Discussion in 'Spigot Plugin Help' started by Acoder, Jun 14, 2021.

  1. Hi,

    I am coding a Spigot plugin for 1.16.5 but I get an exception when I dispatch a command in the AsyncPlayerChatEvent. I don't understand why this is happening.
    How do I fix it?

    Exception :

    [10:00:56] [Async Chat Thread - #0/ERROR]: Could not pass event AsyncPlayerChatEvent to MC-GameMaker v1.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot-1.16.5.jar:3059-Spigot-4225eac-7ea8b96]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.16.5.jar:3059-Spigot-4225eac-7ea8b96]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) [spigot-1.16.5.jar:3059-Spigot-4225eac-7ea8b96]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) [spigot-1.16.5.jar:3059-Spigot-4225eac-7ea8b96]
    at net.minecraft.server.v1_16_R3.PlayerConnection.chat(PlayerConnection.java:1857) [spigot-1.16.5.jar:3059-Spigot-4225eac-7ea8b96]
    at net.minecraft.server.v1_16_R3.PlayerConnection.c(PlayerConnection.java:1795) [spigot-1.16.5.jar:3059-Spigot-4225eac-7ea8b96]
    at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:1733) [spigot-1.16.5.jar:3059-Spigot-4225eac-7ea8b96]
    at net.minecraft.server.v1_16_R3.PacketPlayInChat$1.run(PacketPlayInChat.java:44) [spigot-1.16.5.jar:3059-Spigot-4225eac-7ea8b96]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
    at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) [?:?]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) [?:?]
    at java.lang.Thread.run(Thread.java:831) [?:?]
    Caused by: java.lang.IllegalStateException: Asynchronous command dispatch!
    at org.spigotmc.AsyncCatcher.catchOp(AsyncCatcher.java:14) ~[spigot-1.16.5.jar:3059-Spigot-4225eac-7ea8b96]
    at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:760) ~[spigot-1.16.5.jar:3059-Spigot-4225eac-7ea8b96]
    at org.bukkit.Bukkit.dispatchCommand(Bukkit.java:702) ~[spigot-1.16.5.jar:3059-Spigot-4225eac-7ea8b96]
    at name.sgriffeth.MCGameMaker.Command.executeCommands(Command.java:170) ~[?:?]
    at name.sgriffeth.MCGameMaker.Main.event(Main.java:1135) ~[?:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[?:?]
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
    at java.lang.reflect.Method.invoke(Method.java:567) ~[?:?]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:315) ~[spigot-1.16.5.jar:3059-Spigot-4225eac-7ea8b96]
    ... 12 more
     
  2. Please write your code so we can help you
     
  3. What is your code? You are running something that cannot be run async, so you will have to run that part of the code in sync.
     
    • Agree Agree x 1
  4. Is there a better way to post code than just uploading a text file? (It seems i cant upload .java files)
     

    Attached Files:

    #4 Acoder, Jun 14, 2021
    Last edited: Jun 14, 2021
  5. SteelPhoenix

    Moderator

    fam why are you listening to all events and why did you squash everything into one class?!?

    Anyways you can't dispatch commands from async context. Use the BukkitScheduler.
     
    • Agree Agree x 1
  6. Code (Text):
    Bukkit.getScheduler().runTask(plugin, () -> {
    //Your code here
    });
     
  7. I have 6 other classes but i only post my Main class. I am listening to all events because i want players to be able to listen to any event in game and also run commands when those events are called.
     
  8. You will have problems with it. Why would you want for example to run a command every time a players move? Your tps will be on the ground.
     
  9. You have the option to run a command on the PlayerMoveEvent. But if there are no commands scheduled for PlayerMoveEvent then no commands will run on the PlayerMoveEvent. Something you could do is schedule /tellraw @s {"text":"Hello welcome to the Server'',"color":''blue"} to run on PlayerJoinEvent and then whenever a player joins he will get a message saying "Hello welcome to the Server''. And you can do this in game.
     
  10. Ok, did you solved your problem? If you have an async problem, just use what i write before
     
  11. Yes. Its working now thanks.