aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/NEUVersionCheck.kt
blob: 0ad02ad619532d5723ae94edeb078d47fc67be57 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package at.hannibal2.skyhanni.utils

import net.minecraftforge.fml.common.FMLCommonHandler
import java.awt.Desktop
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.net.URI
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JOptionPane
import javax.swing.UIManager

object NEUVersionCheck {

    @JvmStatic
    fun checkIfNeuIsLoaded() {
        try {
            Class.forName("io.github.moulberry.notenoughupdates.NotEnoughUpdates")
        } catch (e: Throwable) {
            neuWarning(
                "NotEnoughUpdates is missing!\n" +
                        "SkyHanni requires the latest version of NotEnoughUpdates to work.\n" +
                        "You currently need NEU version 2.1.1-Alpha-17.1 or later.\n" +
                        "NEU 2.1 is NOT the latest version.\n" +
                        "It is ONLY in the #neu-alphas channel in the NEU discord\n" +
                        "Or in the #neu-updates channel in the SkyHanni discord\n" +
                        "Use these links to download the latest version:"
            )
            return
        }

        try {
            val clazz = Class.forName("io.github.moulberry.notenoughupdates.util.ItemResolutionQuery")

            for (field in clazz.methods) {
                if (field.name == "findInternalNameByDisplayName") return
            }
        } catch (_: Throwable) {
        }
        neuWarning(
            "NotEnoughUpdates is outdated!\n" +
                    "SkyHanni requires the latest version of NotEnoughUpdates to work.\n" +
                    "You currently need NEU version 2.1.1-Alpha-17.1 or later.\n" +
                    "NEU 2.1 is NOT the latest version.\n" +
                    "It is ONLY in the #neu-alphas channel in the NEU discord\n" +
                    "Or in the #neu-updates channel in the SkyHanni discord\n" +
                    "Use these links to download the latest version:"
        )
    }

    private fun neuWarning(text: String) {
        openPopupWindow(
            text,
            Pair("Join SkyHanni Discord", "https://discord.com/invite/8DXVN4BJz3"),
            Pair("Open SkyHanni GitHub", "https://github.com/hannibal002/SkyHanni"),
            Pair("Join NEU Discord", "https://discord.gg/moulberry"),
            Pair("Open NEU GitHub", "https://github.com/NotEnoughUpdates/NotEnoughUpdates"),
        )
        closeMinecraft()
    }

    /**
     * Taken and modified from Skytils
     */
    private fun openPopupWindow(errorMessage: String, vararg options: Pair<String, String>) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }

        val frame = JFrame()
        frame.isUndecorated = true
        frame.isAlwaysOnTop = true
        frame.setLocationRelativeTo(null)
        frame.isVisible = true

        val buttons = mutableListOf<JButton>()
        for ((name, link) in options) {
            val button = JButton(name)
            button.addMouseListener(object : MouseAdapter() {
                override fun mouseClicked(event: MouseEvent) {
                    try {
                        Desktop.getDesktop().browse(URI(link))
                    } catch (e: java.lang.Exception) {
                        e.printStackTrace()
                    }
                }
            })
            buttons.add(button)
        }
        val close = JButton("Close")
        close.addMouseListener(object : MouseAdapter() {
            override fun mouseClicked(event: MouseEvent) {
                try {
                    closeMinecraft()
                } catch (e: java.lang.Exception) {
                    e.printStackTrace()
                }
            }
        })
        buttons.add(close)

        val allOptions = buttons.toTypedArray()
        JOptionPane.showOptionDialog(
            frame,
            errorMessage,
            "SkyHanni Error",
            JOptionPane.DEFAULT_OPTION,
            JOptionPane.ERROR_MESSAGE,
            null,
            allOptions,
            allOptions[0]
        )
    }

    fun closeMinecraft() {
        FMLCommonHandler.instance().handleExit(-1)
        FMLCommonHandler.instance().expectServerStopped()
    }
}