Skip to content

Commit

Permalink
Add download cancellation notice
Browse files Browse the repository at this point in the history
  • Loading branch information
kikugie committed Aug 25, 2024
1 parent e49b5e7 commit 4c21e79
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/main/java/dev/kikugie/soundboard/mixin/TitleScreenMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.kikugie.soundboard.mixin;

import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import dev.kikugie.soundboard.audio.download.Downloader;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.TitleScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(TitleScreen.class)
public abstract class TitleScreenMixin extends Screen {
protected TitleScreenMixin(Text title) {
super(title);
}

@WrapMethod(method = "method_19862")
private void downloaderWarning(ButtonWidget button, Operation<Void> operation) {
var downloads = Downloader.downloads();
if (client == null || downloads.isEmpty()) operation.call(button);
else client.setScreen(Downloader.confirmation(this, () -> operation.call(button)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import io.wispforest.owo.ui.core.Surface
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import net.minecraft.client.gui.screen.ConfirmScreen
import net.minecraft.client.gui.screen.Screen
import java.lang.ref.WeakReference
import java.net.URI
import java.nio.file.Path
Expand All @@ -24,8 +26,24 @@ import kotlin.io.path.invariantSeparatorsPathString
object Downloader {
private const val FAILURE = "soundboard.downloader.failure"
private const val SUCCESS = "soundboard.downloader.success"
private const val QUIT = "soundboard.downloader.quit"
private const val CONFIRM = "soundboard.downloader.confirm"
private val downloads: MutableMap<Path, Pair<URI, Job>> = mutableMapOf()

@JvmStatic fun downloads(): Map<Path, Pair<URI, Job>> = downloads
@JvmStatic fun confirmation(parent: Screen, action: () -> Any?): ConfirmScreen {
val title = QUIT.translation()
val paths = downloads.keys.joinToString("\n") {
GAME_DIR.relativize(it).invariantSeparatorsPathString
}.let {
CONFIRM.translation(it)
}
val callback: (Boolean) -> Unit = {
if (it) action() else client.setScreen(parent)
}
return ConfirmScreen(callback, title, paths)
}

fun isDownloading(path: Path) = path in downloads

fun download(url: URI, dest: Path, ref: WeakReference<StackLayout>) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/assets/soundboard/lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ soundboard:
bold: false
failure: §4[Soundboard] Failed to save %s. Check the game log for more details
success: §a[Soundboard] Downloaded %s
quit:
- text: [Soundboard] Downloads are still running!
color: red
confirm: |-
If you quit, the following downloads will be cancelled. Do you want to quit?
%s
tooltip:
path: |-
Save location starting at '.minecraft/config/soundboard/'.
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/soundboard.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"package": "dev.kikugie.soundboard.mixin",
"compatibilityLevel": "JAVA_21",
"mixins": [
"TitleScreenMixin",
"KeyboardMixin",
"owo_ui.OwODrawContextMixin",
"owo_ui.ScrollContainerAccessor"
Expand Down

0 comments on commit 4c21e79

Please sign in to comment.