aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/events/FinalizeResourceManagerEvent.kt
blob: 0d411f1e7842327f3c4efc662e455030db094463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package moe.nea.firmament.events

import java.util.concurrent.CompletableFuture
import java.util.concurrent.Executor
import net.minecraft.resource.ReloadableResourceManagerImpl
import net.minecraft.resource.ResourceManager
import net.minecraft.resource.ResourceReloader
import net.minecraft.util.profiler.Profiler

data class FinalizeResourceManagerEvent(
    val resourceManager: ReloadableResourceManagerImpl,
) : FirmamentEvent() {
    companion object : FirmamentEventBus<FinalizeResourceManagerEvent>()

    inline fun registerOnApply(name: String, crossinline function: () -> Unit) {
        resourceManager.registerReloader(object : ResourceReloader {
            override fun reload(
                synchronizer: ResourceReloader.Synchronizer,
                manager: ResourceManager?,
                prepareProfiler: Profiler?,
                applyProfiler: Profiler?,
                prepareExecutor: Executor?,
                applyExecutor: Executor
            ): CompletableFuture<Void> {
                return CompletableFuture.completedFuture(Unit)
                    .thenCompose(synchronizer::whenPrepared)
                    .thenAcceptAsync({ function() }, applyExecutor)
            }

            override fun getName(): String {
                return name
            }
        })
    }
}