Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 28b0c48

Browse files
Failing downloads no longer suspend the updater + added a summary screen
1 parent 079b605 commit 28b0c48

File tree

3 files changed

+91
-20
lines changed

3 files changed

+91
-20
lines changed

src/main/kotlin/mynameisjeff/skyblockclientupdater/gui/UpdateScreen.kt

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package mynameisjeff.skyblockclientupdater.gui
22

33
import kotlinx.coroutines.Dispatchers
4-
import kotlinx.coroutines.job
54
import kotlinx.coroutines.launch
65
import kotlinx.coroutines.runBlocking
76
import mynameisjeff.skyblockclientupdater.SkyClientUpdater
7+
import mynameisjeff.skyblockclientupdater.utils.TickTask
88
import mynameisjeff.skyblockclientupdater.utils.UpdateChecker
99
import net.minecraft.client.gui.GuiButton
1010
import net.minecraft.client.gui.GuiScreen
@@ -22,13 +22,13 @@ import kotlin.concurrent.thread
2222
* https://github.com/Skytils/SkytilsMod/blob/1.x/LICENSE
2323
*/
2424
class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>) : GuiScreen() {
25-
private var failed = false
2625
private var complete = false
2726
private var exited = false
2827
private var backButton: GuiButton = GuiButton(0, 0, 0, 200, 20, "")
2928
private var progress = 0f
3029

31-
private var completedDownloads = 0
30+
private var downloadedMods = arrayListOf<File>()
31+
private var failedDownloadingMods = arrayListOf<File>()
3232

3333
override fun initGui() {
3434
backButton.xPosition = width / 2 - 100
@@ -47,12 +47,12 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
4747
launch(Dispatchers.IO) {
4848
val jarName = update.second
4949
val url = update.third
50-
downloadUpdate(url, File(directory, jarName))
51-
if (!failed) {
50+
val file = File(directory, jarName)
51+
downloadUpdate(url, file)
52+
if (!failedDownloadingMods.contains(file)) {
53+
downloadedMods.add(file)
5254
UpdateChecker.deleteFileOnShutdown(update.first, jarName)
5355
}
54-
}.invokeOnCompletion {
55-
completedDownloads++
5656
}
5757
}
5858
} catch (ex: Exception) {
@@ -63,7 +63,7 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
6363
}
6464

6565
private fun updateText() {
66-
backButton.displayString = if (failed || complete) "Back" else "Cancel"
66+
backButton.displayString = if (exited || complete) "Back" else "Cancel"
6767
}
6868

6969
private fun downloadUpdate(url: String, location: File) {
@@ -75,14 +75,14 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
7575
)
7676
st.connect()
7777
if (st.responseCode != HttpURLConnection.HTTP_OK) {
78-
failed = true
78+
failedDownloadingMods.add(location)
7979
updateText()
8080
println(url + " returned status code " + st.responseCode)
8181
return
8282
}
8383
location.parentFile.mkdirs()
8484
if (!location.exists() && !location.createNewFile()) {
85-
failed = true
85+
failedDownloadingMods.add(location)
8686
updateText()
8787
println("Couldn't create update file directory")
8888
return
@@ -98,7 +98,6 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
9898
// Cancelled
9999
fos.close()
100100
fis.close()
101-
failed = true
102101
return
103102
}
104103
total += count.toLong()
@@ -109,12 +108,11 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
109108
fos.close()
110109
fis.close()
111110
if (exited) {
112-
failed = true
113111
return
114112
}
115113
} catch (ex: Exception) {
116114
ex.printStackTrace()
117-
failed = true
115+
failedDownloadingMods.add(location)
118116
updateText()
119117
}
120118
}
@@ -128,9 +126,9 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
128126
override fun drawScreen(mouseX: Int, mouseY: Int, partialTicks: Float) {
129127
drawDefaultBackground()
130128
when {
131-
failed -> drawCenteredString(
129+
exited -> drawCenteredString(
132130
mc.fontRendererObj,
133-
EnumChatFormatting.RED.toString() + "Update download failed",
131+
EnumChatFormatting.RED.toString() + "Update download exited",
134132
width / 2,
135133
height / 2,
136134
-0x1
@@ -142,13 +140,15 @@ class UpdateScreen(private val updatingMods: List<Triple<File, String, String>>)
142140
height / 2,
143141
0xFFFFFF
144142
)
143+
downloadedMods.size + failedDownloadingMods.size == updatingMods.size -> {
144+
complete = true
145+
updateText()
146+
TickTask(1) {
147+
mc.displayGuiScreen(UpdateSummaryScreen(downloadedMods, failedDownloadingMods))
148+
}
149+
}
145150
else -> {
146151
drawCenteredString(mc.fontRendererObj,"Downloading... ${UpdateChecker.needsDelete.size} / ${updatingMods.size}", width / 2, height / 2, 0xFFFFFF)
147-
if (!failed && completedDownloads == updatingMods.size) {
148-
mc.shutdown()
149-
complete = true
150-
updateText()
151-
}
152152
}
153153
}
154154
super.drawScreen(mouseX, mouseY, partialTicks)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package mynameisjeff.skyblockclientupdater.gui
2+
3+
import net.minecraft.client.gui.GuiButton
4+
import net.minecraft.client.gui.GuiScreen
5+
import java.awt.Color
6+
import java.io.File
7+
8+
class UpdateSummaryScreen(val downloadedMods: ArrayList<File>, val failedDownloadingMods: ArrayList<File>) : GuiScreen() {
9+
10+
override fun initGui() {
11+
buttonList.add(GuiButton(0, width / 2 - 100, height - 75, 200, 20, "Quit Game"))
12+
}
13+
14+
override fun drawScreen(mouseX: Int, mouseY: Int, partialTicks: Float) {
15+
drawDefaultBackground()
16+
17+
drawCenteredString(fontRendererObj, "${downloadedMods.size + failedDownloadingMods.size} updates requested", width / 2, 20, 0xFFFFFF)
18+
drawCenteredString(fontRendererObj, "${downloadedMods.size} updates successful", width / 4, 30, Color.GREEN.rgb)
19+
drawCenteredString(fontRendererObj, "${failedDownloadingMods.size} updates failed", 3 * (width / 4), 30, Color.RED.rgb)
20+
21+
for (i in 0 until downloadedMods.size) drawCenteredString(fontRendererObj,
22+
downloadedMods[i].name, width / 4, 30 + i.inc() * fontRendererObj.FONT_HEIGHT, Color.GREEN.rgb)
23+
for (i in 0 until failedDownloadingMods.size) drawCenteredString(fontRendererObj,
24+
failedDownloadingMods[i].name, 3 * (width / 4), 30 + i.inc() * fontRendererObj.FONT_HEIGHT, Color.RED.rgb)
25+
super.drawScreen(mouseX, mouseY, partialTicks)
26+
}
27+
28+
override fun actionPerformed(button: GuiButton) {
29+
mc.shutdown()
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Skytils - Hypixel Skyblock Quality of Life Mod
3+
* Copyright (C) 2021 Skytils
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published
7+
* by the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package mynameisjeff.skyblockclientupdater.utils
20+
21+
import net.minecraftforge.common.MinecraftForge
22+
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
23+
import net.minecraftforge.fml.common.gameevent.TickEvent
24+
25+
class TickTask(private var ticks: Int = 0, private val task: () -> Unit) {
26+
27+
@SubscribeEvent
28+
fun onTick(event: TickEvent.ClientTickEvent) {
29+
if (event.phase != TickEvent.Phase.START) return
30+
if (ticks <= 0) {
31+
task()
32+
MinecraftForge.EVENT_BUS.unregister(this)
33+
}
34+
ticks--
35+
}
36+
37+
init {
38+
MinecraftForge.EVENT_BUS.register(this)
39+
}
40+
}

0 commit comments

Comments
 (0)