aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/gui/config/FirmamentConfigScreenProvider.kt
blob: 82e151efe44e4614aaca6fccca666382c2244d29 (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
package moe.nea.firmament.gui.config

import java.util.ServiceLoader
import kotlin.streams.asSequence
import net.minecraft.client.gui.screen.Screen
import moe.nea.firmament.Firmament

interface FirmamentConfigScreenProvider {
	val key: String
	val isEnabled: Boolean get() = true

	fun open(parent: Screen?): Screen

	companion object {
		private val loader = ServiceLoader.load(FirmamentConfigScreenProvider::class.java)

		val providers by lazy {
			loader.stream().asSequence().mapNotNull { service ->
				kotlin.runCatching { service.get() }
					.getOrElse {
						Firmament.logger.warn("Could not load config provider ${service.type()}", it)
						null
					}
			}.filter { it.isEnabled }
				.sortedWith(Comparator.comparing(
					{ it.key },
					Comparator<String> { left, right ->
						if (left == "builtin") return@Comparator -1
						if (right == "builtin") return@Comparator 1
						return@Comparator left.compareTo(right)
					})).toList()
		}
	}
}