aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/repo/ModernOverlaysData.kt
blob: 543b800fd5621d3aa697b570d42d1d93a28510c4 (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
36
37
38
39
40
41
package moe.nea.firmament.repo

import io.github.moulberry.repo.IReloadable
import io.github.moulberry.repo.NEURepository
import java.nio.file.Path
import kotlin.io.path.extension
import kotlin.io.path.isDirectory
import kotlin.io.path.listDirectoryEntries
import kotlin.io.path.nameWithoutExtension
import moe.nea.firmament.util.SkyblockId

// TODO: move this over to the repo parser
class ModernOverlaysData : IReloadable {
	data class OverlayFile(
		val version: Int,
		val path: Path,
	)

	var overlays: Map<SkyblockId, List<OverlayFile>> = mapOf()
	override fun reload(repo: NEURepository) {
		val items = mutableMapOf<SkyblockId, MutableList<OverlayFile>>()
		repo.baseFolder.resolve("itemsOverlay")
			.takeIf { it.isDirectory() }
			?.listDirectoryEntries()
			?.forEach { versionFolder ->
				val version = versionFolder.fileName.toString().toIntOrNull() ?: return@forEach
				versionFolder.listDirectoryEntries()
					.forEach { item ->
						if (item.extension != "snbt") return@forEach
						val itemId = item.nameWithoutExtension
						items.getOrPut(SkyblockId(itemId)) { mutableListOf() }.add(OverlayFile(version, item))
					}
			}
		this.overlays = items
	}

	fun getOverlayFiles(skyblockId: SkyblockId) = overlays[skyblockId] ?: listOf()
	fun getMostModernReadableOverlay(skyblockId: SkyblockId, version: Int) = getOverlayFiles(skyblockId)
		.filter { it.version <= version }
		.maxByOrNull { it.version }
}