PaperMC / Folia

Fork of Paper which adds regionised multithreading to the dedicated server.
GNU General Public License v3.0
3.55k stars 455 forks source link

Dispatching command async #290

Closed kangarko closed 1 month ago

kangarko commented 1 month ago

Expected behavior

Player performs the command successfully.

Observed/Actual behavior

[15:23:05 ERROR]: [ca.spottedleaf.moonrise.common.util.TickThread] Thread Region Scheduler Thread #0 failed main thread check: Dispatching command async
java.lang.Throwable: null
    at ca.spottedleaf.moonrise.common.util.TickThread.ensureTickThread(TickThread.java:58) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:1032) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at org.bukkit.craftbukkit.entity.CraftPlayer.performCommand(CraftPlayer.java:769) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at ChatControl-Red-10.27.9.jar/org.mineacademy.chatcontrol.lib.Common.lambda$dispatchCommandAsPlayer$5(Common.java:1229) ~[ChatControl-Red-10.27.9.jar:?]
    at ChatControl-Red-10.27.9.jar/org.mineacademy.chatcontrol.lib.Common.lambda$runLater$13(Common.java:2635) ~[ChatControl-Red-10.27.9.jar:?]
    at io.papermc.paper.threadedregions.scheduler.FoliaGlobalRegionScheduler$GlobalScheduledTask.run(FoliaGlobalRegionScheduler.java:179) ~[folia-1.21.1.jar:?]
    at io.papermc.paper.threadedregions.scheduler.FoliaGlobalRegionScheduler.tick(FoliaGlobalRegionScheduler.java:37) ~[folia-1.21.1.jar:?]
    at io.papermc.paper.threadedregions.RegionizedServer.globalTick(RegionizedServer.java:293) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at io.papermc.paper.threadedregions.RegionizedServer$GlobalTickTickHandle.tickRegion(RegionizedServer.java:148) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at io.papermc.paper.threadedregions.TickRegionScheduler$RegionScheduleHandle.runTick(TickRegionScheduler.java:418) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at ca.spottedleaf.concurrentutil.scheduler.SchedulerThreadPool$TickThreadRunner.run(SchedulerThreadPool.java:540) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at java.base/java.lang.Thread.run(Thread.java:1583) ~[?:?]
[15:23:05 WARN]: [ChatControlRed] Global task for ChatControlRed v10.27.9 generated an exception
java.lang.IllegalStateException: Dispatching command async
    at ca.spottedleaf.moonrise.common.util.TickThread.ensureTickThread(TickThread.java:59) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:1032) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at org.bukkit.craftbukkit.entity.CraftPlayer.performCommand(CraftPlayer.java:769) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at ChatControl-Red-10.27.9.jar/org.mineacademy.chatcontrol.lib.Common.lambda$dispatchCommandAsPlayer$5(Common.java:1229) ~[ChatControl-Red-10.27.9.jar:?]
    at ChatControl-Red-10.27.9.jar/org.mineacademy.chatcontrol.lib.Common.lambda$runLater$13(Common.java:2635) ~[ChatControl-Red-10.27.9.jar:?]
    at io.papermc.paper.threadedregions.scheduler.FoliaGlobalRegionScheduler$GlobalScheduledTask.run(FoliaGlobalRegionScheduler.java:179) ~[folia-1.21.1.jar:?]
    at io.papermc.paper.threadedregions.scheduler.FoliaGlobalRegionScheduler.tick(FoliaGlobalRegionScheduler.java:37) ~[folia-1.21.1.jar:?]
    at io.papermc.paper.threadedregions.RegionizedServer.globalTick(RegionizedServer.java:293) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at io.papermc.paper.threadedregions.RegionizedServer$GlobalTickTickHandle.tickRegion(RegionizedServer.java:148) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at io.papermc.paper.threadedregions.TickRegionScheduler$RegionScheduleHandle.runTick(TickRegionScheduler.java:418) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at ca.spottedleaf.concurrentutil.scheduler.SchedulerThreadPool$TickThreadRunner.run(SchedulerThreadPool.java:540) ~[folia-1.21.1.jar:1.21.1-DEV-88e0907]
    at java.base/java.lang.Thread.run(Thread.java:1583) ~[?:?]

Steps/models to reproduce

Call Player#performCommand from AsyncPlayerChatEvent or wrap it in runDelayed scheduler.

Plugin and Datapack List

ChatControlRed

Folia version

This server is running Folia version 1.21.1-DEV-dev/1.21.1@88e0907 (2024-09-09T13:10:08Z) (Implementing API version 1.21.1-R0.1-SNAPSHOT)

Other

Hey,

I'd like to learn the proper way to dispatching commands in Folia from AsyncPlayerEvent. Attaching details above. Thanks.

electronicboy commented 1 month ago

commands need to be dispatched in the correct context, i.e. the players tick thread (see the player scheduler)

kangarko commented 1 month ago

Can't I just call Player#chat("/" + command)?

Upon trying, looks like there is a bug in Folia:

[15:56:54 WARN]: java.lang.IllegalStateException: PlayerCommandPreprocessEvent may only be triggered synchronously.
[15:56:54 WARN]:    at io.papermc.paper.plugin.manager.PaperEventManager.callEvent(PaperEventManager.java:42)
[15:56:54 WARN]:    at io.papermc.paper.plugin.manager.PaperPluginManagerImpl.callEvent(PaperPluginManagerImpl.java:131)
[15:56:54 WARN]:    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:628)
[15:56:54 WARN]:    at net.minecraft.server.network.ServerGamePacketListenerImpl.performUnsignedChatCommand(ServerGamePacketListenerImpl.java:2257)
[15:56:54 WARN]:    at net.minecraft.server.network.ServerGamePacketListenerImpl.handleCommand(ServerGamePacketListenerImpl.java:2547)
[15:56:54 WARN]:    at org.bukkit.craftbukkit.entity.CraftPlayer.chat(CraftPlayer.java:755)
electronicboy commented 1 month ago

that stuff expects to be called within the context of the player. That event is a sync event, after all. calling chat() async works for chat because that expects to be dispatched async, commands expect to be ran on the 'main thread'

kangarko commented 1 month ago

Got it. Thank you.