aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/github/moulberry/notenoughupdates/miscgui/DynamicLightItemsEditor.kt
blob: ecab9c6e62b10c075a74aeffbb14953d555c32bf (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * 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.miscgui

import io.github.moulberry.notenoughupdates.NotEnoughUpdates
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe
import io.github.moulberry.notenoughupdates.util.Utils
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiScreen
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.item.ItemStack
import net.minecraft.util.ResourceLocation
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import net.minecraftforge.fml.common.registry.GameRegistry
import org.lwjgl.opengl.GL11
import java.awt.Color
import java.io.File
import kotlin.math.ceil

class DynamicLightItemsEditor() : GuiScreen() {

    val isOfLightsEnabled = run {
        val ofDynamicLights = File("optionsof.txt").takeIf { it.exists() }?.readLines()
            ?.find { it.startsWith("ofDynamicLights:") }
        ofDynamicLights != null && !ofDynamicLights.endsWith(":3")
    }

    val background = ResourceLocation("notenoughupdates:dynamic_light_items_editor.png")
    val enabledButton = ResourceLocation("notenoughupdates:enabled_button.png")
    val disabledButton = ResourceLocation("notenoughupdates:disabled_button.png")
    val chestGui = ResourceLocation("textures/gui/container/generic_54.png")
    val widgets = ResourceLocation("textures/gui/widgets.png")
    val help = ResourceLocation("notenoughupdates:help.png")

    var xSize = 217
    var ySize = 88
    var guiLeft = 0
    var guiTop = 0

    var stackToRender: String? = null
    var itemSelected: String? = null

    override fun drawScreen(mouseX: Int, mouseY: Int, partialTicks: Float) {
        drawDefaultBackground()

        val numOfItems = NotEnoughUpdates.INSTANCE.config.hidden.dynamicLightItems.size
        val numOfRows = if (didApplyMixin) ceil(numOfItems / 9f).toInt() else 0
        ySize = 70 + 18 * numOfRows
        guiLeft = (width - xSize) / 2
        guiTop = (height - ySize) / 2

        // Top and bottom half of gui
        Minecraft.getMinecraft().textureManager.bindTexture(background)
        Utils.drawTexturedRect(guiLeft.toFloat(), guiTop.toFloat(), xSize.toFloat(), 24F,
            0F, 1F, 0F, 24 / 88f, GL11.GL_NEAREST)
        Utils.drawTexturedRect(guiLeft.toFloat(), (guiTop + ySize - 46).toFloat(), xSize.toFloat(), 46F,
            0F, 1F, 42 / 88f, 1F, GL11.GL_NEAREST)

        fontRendererObj.drawString("Dynamic Light Items Editor", guiLeft + 10, guiTop + 7, 4210752)

        GlStateManager.color(1f, 1f, 1f, 1f)
        Minecraft.getMinecraft().textureManager.bindTexture(help)
        Utils.drawTexturedRect((guiLeft + xSize + 3).toFloat(), guiTop.toFloat(), 16F, 16F, GL11.GL_NEAREST)
        if (mouseX >= guiLeft + xSize + 3 &&
            mouseX <= guiLeft + xSize + 19 &&
            mouseY >= guiTop &&
            mouseY <= guiTop + 16) {
            val tooltip = listOf(
                "§bDynamic Light Item Editor",
                "§eWhat is this?",
                "§eNEU makes use of OptiFine's feature of certain items",
                "§eemitting dynamic light. By default OptiFine only implements",
                "§ethis feature for a select few minecraft items.",
                "",
                "§eThis editor however, allows you to add specific skyblock",
                "§eitems that will emit dynamic light when held. Simply hold the",
                "§eitem you wish to add, then open this menu again and click",
                "§e'Add Held Item', now if you have OptiFine installed and the",
                "§edynamic lights option enabled, the added items will emit light!",
                "",
                "§eTo remove an item, click the item in this menu and click",
                "§ethe 'Remove Item' button in the bottom right.",
            )
            Utils.drawHoveringText(tooltip, mouseX, mouseY, width, height, -1)
        }

        if (!didApplyMixin) {
            fontRendererObj.drawString("Could not find OptiFine!", guiLeft + 50, guiTop + 22, Color.RED.rgb)
            fontRendererObj.drawString("Go to #neu-support in", guiLeft + 50, guiTop + 32, Color.RED.rgb)
            fontRendererObj.drawString("the discord for help", guiLeft + 52, guiTop + 42, Color.RED.rgb)
            return
        }
        if (!isOfLightsEnabled) {
            fontRendererObj.drawString("Dynamic lights have", guiLeft + 50, guiTop + 22, Color.RED.rgb)
            fontRendererObj.drawString("been disabled in OptiFine.", guiLeft + 50, guiTop + 32, Color.RED.rgb)
            fontRendererObj.drawString("Enable in Video Settings.", guiLeft + 52, guiTop + 42, Color.RED.rgb)
            return
        }

        // Buttons
        GlStateManager.color(1f, 1f, 1f, 1f)
        Minecraft.getMinecraft().textureManager.bindTexture(enabledButton)
        Utils.drawTexturedRect(guiLeft.toFloat() + 15, (guiTop + ySize - 32).toFloat(), 88F, 20F,
            0F, 1F, 0F, 1F, GL11.GL_NEAREST)

        if (itemSelected != null) {
            Minecraft.getMinecraft().textureManager.bindTexture(enabledButton)
        } else {
            Minecraft.getMinecraft().textureManager.bindTexture(disabledButton)
        }
        Utils.drawTexturedRect(guiLeft.toFloat() + 114, (guiTop + ySize - 32).toFloat(), 88F, 20F,
            0F, 1F, 0F, 1F, GL11.GL_NEAREST)

        fontRendererObj.drawString("Add Held Item", guiLeft + 27, guiTop + ySize - 26, 4210752)
        fontRendererObj.drawString("Remove Item", guiLeft + 130, guiTop + ySize - 26, 4210752)

        GlStateManager.color(1f, 1f, 1f, 1f)

        // Add in some part of the gui for every row
        Minecraft.getMinecraft().textureManager.bindTexture(background)
        for (i in 0 until numOfRows) {
            Utils.drawTexturedRect(guiLeft.toFloat(), ((guiTop + 24) + (i * 18)).toFloat(), xSize.toFloat(), 18f,
                0f, 1f, 24 / 88f, 42 / 88f, GL11.GL_NEAREST)
        }

        var hoveredItem: String? = null
        var selectedPosition: Pair<Int, Int> = Pair(-999, -999)

        // Draw a slot for each item and the ItemStack
        for ((index, item) in NotEnoughUpdates.INSTANCE.config.hidden.dynamicLightItems.withIndex()) {
            val i = index % 9
            val j = index / 9
            GlStateManager.color(1f, 1f, 1f, 1f)

            Minecraft.getMinecraft().textureManager.bindTexture(chestGui)
            drawTexturedModalRect(guiLeft + 27 + i % 9 * 18, guiTop + 24 + j * 18, 7, 17, 18, 18)

            val itemStack = resolveItemStack(item) ?: return
            Utils.drawItemStack(itemStack, guiLeft + 28 + i % 9 * 18, guiTop + 25 + j * 18)

            if (mouseX >= guiLeft + 27 + i % 9 * 18 && mouseX <= guiLeft + 45 + i % 9 * 18) {
                if (mouseY >= guiTop + 24 + j * 18 && mouseY <= guiTop + 42 + j * 18) {
                    hoveredItem = item
                    val tooltip = itemStack.getTooltip(Minecraft.getMinecraft().thePlayer, false)
                    Utils.drawHoveringText(tooltip, mouseX, mouseY, width, height, -1)
                }
            }

            if (itemSelected != null && itemSelected.equals(item)) {
                // Save the position, so when we render the selected box its renders on top of everything
                selectedPosition = Pair(guiLeft + 24 + i % 9 * 18,