new: 2B2T Queue HUD Element (#2167)

Co-authored-by: lv <~@l1v.in>
This commit is contained in:
Jacob Herd 2021-03-30 01:21:44 -04:00 committed by GitHub
parent 8f74136c6f
commit 8ba37378ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 97 additions and 1 deletions

@ -1 +1 @@
Subproject commit e37ece8c734f1b0101f33301756621001e24e3fc
Subproject commit acc4b81f76ba1bd68582ed5ad3c03d3edee0d722

View File

@ -0,0 +1,96 @@
package org.kamiblue.client.gui.hudgui.elements.misc
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import net.minecraftforge.fml.common.gameevent.TickEvent
import org.kamiblue.client.KamiMod
import org.kamiblue.client.event.SafeClientEvent
import org.kamiblue.client.gui.hudgui.LabelHud
import org.kamiblue.client.util.TickTimer
import org.kamiblue.client.util.TimeUnit
import org.kamiblue.client.util.WebUtils
import org.kamiblue.client.util.text.MessageSendHelper
import org.kamiblue.client.util.threads.safeListener
import org.kamiblue.commons.utils.grammar
internal object Queue2B2T : LabelHud(
name = "2B2T Queue",
category = Category.MISC,
description = "Length of 2B2T Queue"
) {
private const val apiUrl = "https://2bqueue.info/queue"
private val gson = Gson()
private val queueData = QueueData(0, 0, 0, 0)
private val lastUpdateTimer = TickTimer(TimeUnit.SECONDS)
private val dataUpdateTimer = TickTimer(TimeUnit.SECONDS)
private val hasShownWarning = setting("Has Shown Warning", false, { false })
init {
safeListener<TickEvent.ClientTickEvent> {
if (dataUpdateTimer.tick(15L)) {
updateQueueData()
}
}
}
override fun SafeClientEvent.updateText() {
if (!hasShownWarning.value) {
MessageSendHelper.sendWarningMessage(
"This module uses an external API, 2bqueue.info, which is operated by Tycrek at the time of writing." +
"If you do not trust this external API / have not verified the safety yourself, disable this HUD component."
)
hasShownWarning.value = true
}
displayText.add("Priority: ", primaryColor)
displayText.add("${queueData.priority}", secondaryColor)
displayText.add("Regular: ", primaryColor)
displayText.addLine("${queueData.regular}", secondaryColor)
displayText.add("Last updated ${queueData.getLastUpdate()} ago", primaryColor)
}
private fun updateQueueData() {
val tmpData = try {
val json = WebUtils.getUrlContents(apiUrl)
gson.fromJson(json, QueueData::class.java)
} catch (e: Exception) {
KamiMod.LOG.debug("Exception in ${this.javaClass.simpleName}", e)
return
}
// Instead of overwriting the object, copy the values
// This is because of the lastUpdateCache
queueData.priority = tmpData.priority
queueData.regular = tmpData.regular
queueData.total = tmpData.total
queueData.lastUpdated = tmpData.lastUpdated
}
private data class QueueData(
@SerializedName("prio")
var priority: Int,
var regular: Int,
var total: Int,
@SerializedName("timems")
var lastUpdated: Long
) {
private var lastUpdateCache: String = "0 minutes, 0 seconds"
fun getLastUpdate(): String {
if (lastUpdateTimer.tick(1L)) {
val difference = System.currentTimeMillis() - lastUpdated
val minuteAmt = difference / 60000L % 60L
val secondAmt = difference / 1000L % 60L
val minutes = grammar(minuteAmt.toInt(), "minute", "minutes")
val seconds = grammar(secondAmt.toInt(), "second", "seconds")
lastUpdateCache = "$minutes, $seconds"
}
return lastUpdateCache
}
}
}