aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt')
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt59
1 files changed, 43 insertions, 16 deletions
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt b/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt
index 27c51e1..c4f3f69 100644
--- a/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt
+++ b/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt
@@ -1,44 +1,71 @@
package moe.nea.notenoughupdates
+import com.mojang.brigadier.Command
import com.mojang.brigadier.CommandDispatcher
-import io.github.moulberry.repo.NEURepository
-import moe.nea.notenoughupdates.repo.ItemCache
+import io.ktor.client.*
+import io.ktor.client.plugins.*
+import io.ktor.client.plugins.contentnegotiation.*
+import io.ktor.serialization.kotlinx.json.*
+import kotlinx.coroutines.*
+import kotlinx.serialization.json.Json
+import moe.nea.notenoughupdates.repo.RepoManager
import net.fabricmc.api.ClientModInitializer
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
-import net.minecraft.client.Minecraft
+import net.fabricmc.loader.api.FabricLoader
import net.minecraft.commands.CommandBuildContext
import net.minecraft.network.chat.Component
-import net.minecraft.network.protocol.game.ClientboundUpdateRecipesPacket
+import org.apache.logging.log4j.LogManager
+import java.nio.file.Files
import java.nio.file.Path
+import kotlin.coroutines.EmptyCoroutineContext
object NotEnoughUpdates : ModInitializer, ClientModInitializer {
- val DATA_DIR = Path.of(".notenoughupdates")
-
const val MOD_ID = "notenoughupdates"
- val neuRepo: NEURepository = NEURepository.of(Path.of("NotEnoughUpdates-REPO")).apply {
- registerReloadListener(ItemCache)
- reload()
- registerReloadListener {
- Minecraft.getInstance().connection?.handleUpdateRecipes(ClientboundUpdateRecipesPacket(mutableListOf()))
+ val DATA_DIR = Path.of(".notenoughupdates").also { Files.createDirectories(it) }
+ val DEBUG = System.getenv("notenoughupdates.debug") == "true"
+ val logger = LogManager.getLogger("NotEnoughUpdates")
+ val metadata by lazy { FabricLoader.getInstance().getModContainer(MOD_ID).orElseThrow().metadata }
+ val version by lazy { metadata.version }
+
+ val json = Json {
+ prettyPrint = DEBUG
+ ignoreUnknownKeys = true
+ }
+
+ val httpClient by lazy {
+ HttpClient {
+ install(ContentNegotiation) {
+ json(json)
+ }
+ install(UserAgent) {
+ agent = "NotEnoughUpdates1.19/$version"
+ }
}
}
- fun registerCommands(
- dispatcher: CommandDispatcher<FabricClientCommandSource>, registryAccess: CommandBuildContext
+ val globalJob = Job()
+ val coroutineScope =
+ CoroutineScope(EmptyCoroutineContext + CoroutineName("NotEnoughUpdates")) + SupervisorJob(globalJob)
+ val coroutineScopeIo = coroutineScope + Dispatchers.IO + SupervisorJob(globalJob)
+
+ private fun registerCommands(
+ dispatcher: CommandDispatcher<FabricClientCommandSource>,
+ @Suppress("UNUSED_PARAMETER")
+ _ctx: CommandBuildContext
) {
dispatcher.register(ClientCommandManager.literal("neureload").executes {
it.source.sendFeedback(Component.literal("Reloading repository from disk. This may lag a bit."))
- neuRepo.reload()
- 0
+ RepoManager.neuRepo.reload()
+ Command.SINGLE_SUCCESS
})
-
}
override fun onInitialize() {
+ RepoManager.launchAsyncUpdate()
ClientCommandRegistrationCallback.EVENT.register(this::registerCommands)
}