Shynixn / MCCoroutine

MCCoroutine is a library, which adds extensive support for Kotlin Coroutines for Minecraft Server environments.
Other
197 stars 17 forks source link

Folia Support #104

Closed Boy0000 closed 1 year ago

Boy0000 commented 1 year ago

Since MCCoroutine uses the normal bukkit scheduler, it seems to struggle with stuff on Folia Any chance this can get supported?

Shynixn commented 1 year ago

I have heard about Folia before but I considered it to be too experimental at time of announcement. It may have changed since then.

Are you already using Folia on your servers?

Boy0000 commented 1 year ago

No but due to tons of performance issues we have started looking into it. Our plugins basically use coroutines for everything so couldn't get very far as the first startup task in the core plugin essentially throws a warning due to scheduler use.

Boy0000 commented 1 year ago

Was thinking if this might only require a new dispatcher or an entire separate platform impl.

Considering that there are multiple Schedulers in Folia, Global, Regional, Entity etc, i would assume a different impl with dispatchers might be needed, though this probably does break alot of cross-compatibility between Paper and Folia plugins I do know that alot of the core scheduler logic was merged into Paper to make it easier for cross-compatibility but im in no way an expert on it

Im sure the Paper-dev team would be of alot more help..

Shynixn commented 1 year ago

I have done some prototyping and it requires the implementation of a separate platform. The "main" minecraft thread does not exist in folia and many assumption in mccoroutine-bukkit-api do no longer hold. A new mccoroutine-folia-api needs to be implemented.

Everytime you manipulate something in a region (e.g. block type) you need to be on the scheduler for this particular region. This also applies to entities in regions, however to make it easier for the user, each entity has got its own "scheduler", which automatically connects to the correct region scheduler.

    @EventHandler
    fun onEntitySpawnEvent(event: EntitySpawnEvent) {
        plugin.launch {
            delay(2000)

            val entityLocation = withContext(plugin.entityDispatcher(event.entity)) {
                event.entity.customName = "Coroutine Entity"
                event.entity.location
            }

            entityLocation.add(2.0, 0.0, 0.0)
            delay(1000)

            withContext(plugin.regionDispatcher(entityLocation)) {
                entityLocation.block.type = Material.GOLD_BLOCK
            }

            delay(1000)

            withContext(plugin.entityDispatcher(event.entity)) {
                event.entity.teleport(entityLocation)
            }
        }
    }
Boy0000 commented 1 year ago

yeah, I guess this would mean that cross-compat would not be a thing? Aka your plugin would either use Spigot/Paper or Folia? Or you would need to implement against both? Or would the dispatcher, in case of it not being ran on Folia, default to the Bukkit-Scheduler or something?

Shynixn commented 1 year ago

I do not think you can run your plugins out of the box on Folia anyway. You need to check every entity and region manipulation. However, events probably already arrive on the correct thread (have not tested that), so it may work out of the box in most cases.

Extending mccoroutine-bukkit to use folia is not possible as it would make it dependable on the folia api. I do not want it as mccoroutine-bukkit it is compatible to basic spigot and craftbukkit as well. Not just Paper.

However a mccoroutine-folia (region scheduler) implementation could use mccoroutine-bukkit (bukkit scheduler) as fallback implementation. Good idea.

Boy0000 commented 1 year ago

yeah the last one was what i meant, sorry if unclear 👍

Boy0000 commented 1 year ago

Any way that I can try to use the development branch for testing? Seems the devbranch is not published and tried publishing on mavenLocal but got some auth-error Probably not done but would be nice to just get a foundation setup for all our plugins if possible 👍

Shynixn commented 1 year ago

I have published a release version. Try that :)