aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/modules/ExternalDataProvider.kt
blob: 93bb453d4c12ed3016a85c39f4be2e427a2a1c38 (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
42
43
package moe.nea.ledger.modules

import moe.nea.ledger.events.InitializationComplete
import moe.nea.ledger.events.SupplyDebugInfo
import moe.nea.ledger.utils.GsonUtil
import moe.nea.ledger.utils.di.Inject
import moe.nea.ledger.utils.network.Request
import moe.nea.ledger.utils.network.RequestUtil
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.eventhandler.Event
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.concurrent.CompletableFuture

class ExternalDataProvider @Inject constructor(
	val requestUtil: RequestUtil
) {

	fun createAuxillaryDataRequest(path: String): Request {
		return requestUtil.createRequest("https://github.com/nea89o/ledger-auxiliary-data/raw/refs/heads/master/$path")
	}

	private val itemNameFuture: CompletableFuture<Map<String, String>> = CompletableFuture.supplyAsync {
		val request = createAuxillaryDataRequest("data/item_names.json")
		val response = request.execute(requestUtil)
		val nameMap = response.json(GsonUtil.typeToken<Map<String, String>>())
		return@supplyAsync nameMap
	}

	lateinit var itemNames: Map<String, String>

	class DataLoaded(val provider: ExternalDataProvider) : Event()

	@SubscribeEvent
	fun onDebugData(debugInfo: SupplyDebugInfo) {
		debugInfo.record("externalItemsLoaded", itemNameFuture.isDone && !itemNameFuture.isCompletedExceptionally)
	}

	@SubscribeEvent
	fun onInitComplete(event: InitializationComplete) {
		itemNames = itemNameFuture.join()
		MinecraftForge.EVENT_BUS.post(DataLoaded(this))
	}
}