aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/github/moulberry/notenoughupdates/recipes/generators/RepoExportingContext.kt
blob: 663812423f66daa0cb59d64d8fcf0a22a8ab45f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (C) 2023 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.recipes.generators

import com.google.gson.JsonObject
import io.github.moulberry.notenoughupdates.NEUManager
import io.github.moulberry.notenoughupdates.util.MinecraftExecutor
import io.github.moulberry.notenoughupdates.util.kotlin.Coroutines.continueOn
import io.github.moulberry.notenoughupdates.util.kotlin.Coroutines.waitTicks
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiScreen
import net.minecraft.client.gui.GuiYesNo
import java.io.File
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

class RepoExportingContext(
    val manager: NEUManager,
    val gui: GuiScreen,
) {
    val mc = Minecraft.getMinecraft()
    val nameToItemCache = mutableMapOf<String, String>()

    suspend fun writeFile(file: File, json: JsonObject) {
        if (file.exists()) {
            if (!askYesNo("Overwrite file?", "The file $file already exists."))
                return
        }
        manager.writeJson(json, file)
    }

    suspend fun askYesNo(title: String, subtitle: String): Boolean {
        var wasResolved = false
        continueOn(MinecraftExecutor.OnThread)
        val result = suspendCoroutine {
            mc.displayGuiScreen(object : GuiYesNo({ result, id ->
                if (!wasResolved) {
                    wasResolved = true
                    it.resume(result)
                }
            }, title, subtitle, 0) {
                override fun onGuiClosed() {
                    if (!wasResolved) {
                        wasResolved = true
                        it.resume(null)
                    }
                }
            })
        }
        if (result == null) {
            waitTicks(1) // Wait one tick for gui closing animation
        }
        mc.displayGuiScreen(gui)
        return result ?: false
    }

    suspend fun findItemByName(name: String): String {
        val c = nameToItemCache[name]
        if (c != null) return c
        var wasResolved = false
        continueOn(MinecraftExecutor.OnThread)
        val result = suspendCoroutine { cont ->
            mc.displayGuiScreen(ItemSearchGui(name) {
                if (!wasResolved) {
                    wasResolved = true
                    cont.resume(it)
                }
            })
        }
        waitTicks(1)
        mc.displayGuiScreen(gui)
        result ?: throw RepoExportingInterruptedException()
        nameToItemCache[name] = result
        return result
    }
}