Skip to content

Commit 26ed9bb

Browse files
fix: make sure DataFetcher only runs once at a time
1 parent 205edae commit 26ed9bb

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/main/kotlin/gg/skytils/skytilsmod/commands/impl/SkytilsCommand.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ object SkytilsCommand : BaseCommand("skytils", listOf("st")) {
134134
} else {
135135
when (args[1].lowercase()) {
136136
"data" -> {
137-
DataFetcher.reloadData().invokeOnCompletion {
137+
DataFetcher.reloadData()
138+
DataFetcher.job?.invokeOnCompletion {
138139
it?.run {
139140
UChat.chat("$failPrefix §cFailed to reload repository data due to a ${it::class.simpleName ?: "error"}: ${it.message}!")
140141
}.ifNull {

src/main/kotlin/gg/skytils/skytilsmod/core/DataFetcher.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import kotlin.concurrent.fixedRateTimer
5151
import kotlin.reflect.jvm.jvmName
5252

5353
object DataFetcher {
54+
var job: Job? = null
55+
5456
private fun loadData(): Job {
5557
return Skytils.IO.launch {
5658
try {
@@ -213,8 +215,13 @@ object DataFetcher {
213215
}
214216

215217
@JvmStatic
216-
fun reloadData() =
217-
loadData()
218+
fun reloadData() {
219+
if (job?.isActive != true) {
220+
job = loadData()
221+
} else {
222+
UChat.chat("$failPrefix §cData fetch requested while already fetching!")
223+
}
224+
}
218225

219226
internal fun preload() {}
220227

src/main/kotlin/gg/skytils/skytilsmod/features/impl/misc/SummonSkins.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ import io.ktor.client.request.*
2626
import io.ktor.client.statement.*
2727
import io.ktor.utils.io.jvm.javaio.*
2828
import kotlinx.coroutines.launch
29+
import java.util.concurrent.ConcurrentHashMap
2930
import javax.imageio.ImageIO
3031

3132
object SummonSkins {
3233
// maps name to url
33-
val skinMap = HashMap<String, String>()
34+
val skinMap = ConcurrentHashMap<String, String>()
3435

3536
// maps name to dynamic resource
3637
val skintextureMap = HashMap<String, DynamicResource>()

0 commit comments

Comments
 (0)