progress bar
This commit is contained in:
@@ -3,7 +3,6 @@ package moe.nea.notenoughupdates
|
|||||||
import com.mojang.brigadier.Command
|
import com.mojang.brigadier.Command
|
||||||
import com.mojang.brigadier.CommandDispatcher
|
import com.mojang.brigadier.CommandDispatcher
|
||||||
import io.github.cottonmc.cotton.gui.client.CottonClientScreen
|
import io.github.cottonmc.cotton.gui.client.CottonClientScreen
|
||||||
import io.github.moulberry.repo.NEURepositoryException
|
|
||||||
import io.ktor.client.*
|
import io.ktor.client.*
|
||||||
import io.ktor.client.plugins.*
|
import io.ktor.client.plugins.*
|
||||||
import io.ktor.client.plugins.contentnegotiation.*
|
import io.ktor.client.plugins.contentnegotiation.*
|
||||||
@@ -67,14 +66,15 @@ object NotEnoughUpdates : ModInitializer, ClientModInitializer {
|
|||||||
@Suppress("UNUSED_PARAMETER")
|
@Suppress("UNUSED_PARAMETER")
|
||||||
_ctx: CommandBuildContext
|
_ctx: CommandBuildContext
|
||||||
) {
|
) {
|
||||||
dispatcher.register(ClientCommandManager.literal("neureload").executes {
|
dispatcher.register(ClientCommandManager.literal("neureload")
|
||||||
|
.then(ClientCommandManager.literal("fetch").executes {
|
||||||
|
it.source.sendFeedback(Component.literal("Trying to redownload the repository")) // TODO better reporting
|
||||||
|
RepoManager.launchAsyncUpdate()
|
||||||
|
Command.SINGLE_SUCCESS
|
||||||
|
})
|
||||||
|
.executes {
|
||||||
it.source.sendFeedback(Component.literal("Reloading repository from disk. This may lag a bit."))
|
it.source.sendFeedback(Component.literal("Reloading repository from disk. This may lag a bit."))
|
||||||
try {
|
|
||||||
RepoManager.reload()
|
RepoManager.reload()
|
||||||
} catch (exc: NEURepositoryException) {
|
|
||||||
it.source.sendError(Component.literal("There has been an error reloading the repository. Please try again. IF this persists, delete the .notenoughupdates folder in your mincraft folder"))
|
|
||||||
exc.printStackTrace()
|
|
||||||
}
|
|
||||||
Command.SINGLE_SUCCESS
|
Command.SINGLE_SUCCESS
|
||||||
})
|
})
|
||||||
dispatcher.register(
|
dispatcher.register(
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package moe.nea.notenoughupdates.hud
|
||||||
|
|
||||||
|
import com.mojang.blaze3d.vertex.PoseStack
|
||||||
|
import io.github.cottonmc.cotton.gui.client.ScreenDrawing
|
||||||
|
import io.github.cottonmc.cotton.gui.widget.WWidget
|
||||||
|
import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment
|
||||||
|
import io.github.cottonmc.cotton.gui.widget.data.Insets
|
||||||
|
import kotlin.math.roundToInt
|
||||||
|
import kotlin.math.sin
|
||||||
|
|
||||||
|
|
||||||
|
val Insets.vertical get() = bottom + top
|
||||||
|
val Insets.horizontal get() = left + right
|
||||||
|
|
||||||
|
class ProgressBar(
|
||||||
|
var label: String,
|
||||||
|
var total: Int?, // If total is null, then make it a bouncy rectangle
|
||||||
|
var progress: Int = 0,
|
||||||
|
) : WWidget() {
|
||||||
|
|
||||||
|
var insets: Insets = Insets(7)
|
||||||
|
override fun canResize(): Boolean = true
|
||||||
|
|
||||||
|
|
||||||
|
fun reportProgress(label: String, progress: Int, total: Int?) {
|
||||||
|
synchronized(this) {
|
||||||
|
this.label = label
|
||||||
|
this.progress = progress
|
||||||
|
this.total = total
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun paint(matrices: PoseStack, x: Int, y: Int, mouseX: Int, mouseY: Int) {
|
||||||
|
ScreenDrawing.coloredRect(matrices, x, y, width, height, 0xFF808080.toInt())
|
||||||
|
val (l, prog) = synchronized(this) {
|
||||||
|
label to (progress to total)
|
||||||
|
}
|
||||||
|
val (p, t) = prog
|
||||||
|
|
||||||
|
if (t == null) {
|
||||||
|
ScreenDrawing.coloredRect(
|
||||||
|
matrices,
|
||||||
|
(x + (1 + sin(System.currentTimeMillis().toDouble() / 1000)) * width * 3 / 4 / 2).roundToInt(),
|
||||||
|
y,
|
||||||
|
width / 4,
|
||||||
|
height,
|
||||||
|
0xFF00FF00.toInt()
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
ScreenDrawing.coloredRect(matrices, x, y, width * p / t, height, 0xFF00FF00.toInt())
|
||||||
|
}
|
||||||
|
ScreenDrawing.drawString(
|
||||||
|
matrices,
|
||||||
|
if (t != null) "$l ($p/$t)" else l,
|
||||||
|
HorizontalAlignment.CENTER,
|
||||||
|
x + insets.left,
|
||||||
|
y + insets.top,
|
||||||
|
width - insets.horizontal,
|
||||||
|
height - insets.vertical,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package moe.nea.notenoughupdates.repo
|
package moe.nea.notenoughupdates.repo
|
||||||
|
|
||||||
import com.mojang.serialization.Dynamic
|
import com.mojang.serialization.Dynamic
|
||||||
|
import io.github.cottonmc.cotton.gui.client.CottonHud
|
||||||
import io.github.moulberry.repo.IReloadable
|
import io.github.moulberry.repo.IReloadable
|
||||||
import io.github.moulberry.repo.NEURepository
|
import io.github.moulberry.repo.NEURepository
|
||||||
import io.github.moulberry.repo.data.NEUItem
|
import io.github.moulberry.repo.data.NEUItem
|
||||||
@@ -79,11 +80,22 @@ object ItemCache : IReloadable {
|
|||||||
val j = job
|
val j = job
|
||||||
if (j != null && j.isActive) {
|
if (j != null && j.isActive) {
|
||||||
j.cancel()
|
j.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
job = NotEnoughUpdates.coroutineScope.launch {
|
job = NotEnoughUpdates.coroutineScope.launch {
|
||||||
repository.items?.items?.values?.forEach {
|
val items = repository.items?.items
|
||||||
|
if (items == null) {
|
||||||
|
CottonHud.remove(RepoManager.progressBar)
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
RepoManager.progressBar.reportProgress("Recache Items", 0, items.size)
|
||||||
|
CottonHud.add(RepoManager.progressBar)
|
||||||
|
var i = 0
|
||||||
|
items.values.forEach {
|
||||||
it.asItemStack() // Rebuild cache
|
it.asItemStack() // Rebuild cache
|
||||||
}
|
RepoManager.progressBar.reportProgress("Recache Items", i++, items.size)
|
||||||
}
|
}
|
||||||
|
CottonHud.remove(RepoManager.progressBar)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package moe.nea.notenoughupdates.repo
|
package moe.nea.notenoughupdates.repo
|
||||||
|
|
||||||
|
import io.github.cottonmc.cotton.gui.client.CottonHud
|
||||||
import io.github.moulberry.repo.NEURepository
|
import io.github.moulberry.repo.NEURepository
|
||||||
import io.github.moulberry.repo.NEURepositoryException
|
import io.github.moulberry.repo.NEURepositoryException
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@@ -7,6 +8,7 @@ import kotlinx.serialization.Serializable
|
|||||||
import kotlinx.serialization.serializer
|
import kotlinx.serialization.serializer
|
||||||
import moe.nea.notenoughupdates.NotEnoughUpdates
|
import moe.nea.notenoughupdates.NotEnoughUpdates
|
||||||
import moe.nea.notenoughupdates.NotEnoughUpdates.logger
|
import moe.nea.notenoughupdates.NotEnoughUpdates.logger
|
||||||
|
import moe.nea.notenoughupdates.hud.ProgressBar
|
||||||
import moe.nea.notenoughupdates.util.ConfigHolder
|
import moe.nea.notenoughupdates.util.ConfigHolder
|
||||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents
|
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents
|
||||||
import net.minecraft.client.Minecraft
|
import net.minecraft.client.Minecraft
|
||||||
@@ -24,6 +26,10 @@ object RepoManager : ConfigHolder<RepoManager.Config>(serializer(), "repo", ::Co
|
|||||||
|
|
||||||
var recentlyFailedToUpdateItemList = false
|
var recentlyFailedToUpdateItemList = false
|
||||||
|
|
||||||
|
val progressBar = ProgressBar("", null, 0).also {
|
||||||
|
it.setSize(180, 22)
|
||||||
|
}
|
||||||
|
|
||||||
val neuRepo: NEURepository = NEURepository.of(RepoDownloadManager.repoSavedLocation).apply {
|
val neuRepo: NEURepository = NEURepository.of(RepoDownloadManager.repoSavedLocation).apply {
|
||||||
registerReloadListener(ItemCache)
|
registerReloadListener(ItemCache)
|
||||||
registerReloadListener {
|
registerReloadListener {
|
||||||
@@ -49,13 +55,18 @@ object RepoManager : ConfigHolder<RepoManager.Config>(serializer(), "repo", ::Co
|
|||||||
|
|
||||||
fun launchAsyncUpdate() {
|
fun launchAsyncUpdate() {
|
||||||
NotEnoughUpdates.coroutineScope.launch {
|
NotEnoughUpdates.coroutineScope.launch {
|
||||||
|
progressBar.reportProgress("Downloading", 0, null)
|
||||||
|
CottonHud.add(progressBar)
|
||||||
RepoDownloadManager.downloadUpdate()
|
RepoDownloadManager.downloadUpdate()
|
||||||
|
progressBar.reportProgress("Download complete", 1, 1)
|
||||||
reload()
|
reload()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun reload() {
|
fun reload() {
|
||||||
try {
|
try {
|
||||||
|
progressBar.reportProgress("Reloading from Disk", 0, null)
|
||||||
|
CottonHud.add(progressBar)
|
||||||
neuRepo.reload()
|
neuRepo.reload()
|
||||||
} catch (exc: NEURepositoryException) {
|
} catch (exc: NEURepositoryException) {
|
||||||
Minecraft.getInstance().player?.sendSystemMessage(
|
Minecraft.getInstance().player?.sendSystemMessage(
|
||||||
|
|||||||
Reference in New Issue
Block a user