diff options
author | Linnea Gräf <nea@nea.moe> | 2024-12-17 23:07:06 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-12-17 23:07:06 +0100 |
commit | a046b198645811fe1b7db129942505c379aabb03 (patch) | |
tree | d97e68b1cf3a272aec5d8dd863b82fb0c9b0dc82 /src/main/kotlin/moe/nea/ledger/modules/ExternalDataProvider.kt | |
parent | a2f73de90fb9c9d0ea7a5e7e9e6b9e445a8094ee (diff) | |
download | LocalTransactionLedger-a046b198645811fe1b7db129942505c379aabb03.tar.gz LocalTransactionLedger-a046b198645811fe1b7db129942505c379aabb03.tar.bz2 LocalTransactionLedger-a046b198645811fe1b7db129942505c379aabb03.zip |
feat: Add name prefetching
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/modules/ExternalDataProvider.kt')
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/modules/ExternalDataProvider.kt | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/modules/ExternalDataProvider.kt b/src/main/kotlin/moe/nea/ledger/modules/ExternalDataProvider.kt new file mode 100644 index 0000000..93bb453 --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/modules/ExternalDataProvider.kt @@ -0,0 +1,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)) + } +}
\ No newline at end of file |