Skip to content

Added a coroutine dispatcher for server thread#14

Merged
Glease merged 2 commits into
GTNewHorizons:masterfrom
Taskeren:feature/coroutines-support
Jul 19, 2025
Merged

Added a coroutine dispatcher for server thread#14
Glease merged 2 commits into
GTNewHorizons:masterfrom
Taskeren:feature/coroutines-support

Conversation

@Taskeren

@Taskeren Taskeren commented Jul 9, 2025

Copy link
Copy Markdown

This feature requires GTNHLib to be installed (but optional, so that mods not using this feature are ok without GTNHLib).

This is used for mods using coroutines where they want to switch context (thread) to the server thread.

For example, I can launch a background coroutine, looping and setting the block at 0, 0, 0 to air. To prevent weird bugs caused by not doing it in the server thread, I can now use withContext(Dispatchers.MinecraftServer) { world.setBlockToAir(0, 0, 0) } to fix this.
For more details, see the Kotlin Coroutines documents.

@Taskeren Taskeren requested a review from a team July 9, 2025 10:46
@Taskeren Taskeren added the Enhancement Improve an existing mechanic. Please explain the change with a before/after comparison. label Jul 9, 2025

@Glease Glease left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need more documentation: please specify the exact timing of coroutine running (pre server tick? post?), or explicitly state it's unspecified and ask the caller to not rely on this detail

@Taskeren

Copy link
Copy Markdown
Author

The running timing depends on how GTNHLib implements this.
As for now, it runs the task immediately if the caller is on the server thread, otherwise, it will be invoked on the next tick before the pre-tick (at the head of updateTimeLightAndEntities).

But should I add this info to the doc? I mean if the GTNHLib implementation changes, this can be inaccurate.

@Glease

Glease commented Jul 18, 2025

Copy link
Copy Markdown

Then perhaps state "The exact moment when your coroutine will be run is determined by GTNHLib. As of writing this is xxxxx". Besides, your code seem to run the coroutine directly if it's on main thread already, without asking GTNHLib at all. This is not reflected in documentation or your comment at all.

@Taskeren Taskeren requested a review from Glease July 18, 2025 12:35
@Glease Glease merged commit 235ea5b into GTNewHorizons:master Jul 19, 2025
1 check passed
@Taskeren Taskeren deleted the feature/coroutines-support branch July 21, 2025 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement Improve an existing mechanic. Please explain the change with a before/after comparison.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants