aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/github/moulberry/notenoughupdates/commands/misc/DungeonCommands.kt
blob: ac658e14bb229f0e8d5c14f88bea0fa52d5594b9 (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
/*
 * 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.commands.misc

import com.google.gson.JsonObject
import com.mojang.brigadier.arguments.StringArgumentType.string
import io.github.moulberry.notenoughupdates.NotEnoughUpdates
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe
import io.github.moulberry.notenoughupdates.dungeons.GuiDungeonMapEditor
import io.github.moulberry.notenoughupdates.events.RegisterBrigadierCommandEvent
import io.github.moulberry.notenoughupdates.util.brigadier.*
import net.minecraft.block.material.MapColor
import net.minecraft.client.Minecraft
import net.minecraft.item.ItemMap
import net.minecraft.util.EnumChatFormatting.GREEN
import net.minecraft.util.EnumChatFormatting.RED
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.awt.Color

@NEUAutoSubscribe
class DungeonCommands {
    @SubscribeEvent
    fun onCommands(event: RegisterBrigadierCommandEvent) {
        event.command("dh") {
            thenExecute {
                Minecraft.getMinecraft().thePlayer.sendChatMessage("/warp dungeon_hub")
            }
        }.withHelp("Warps to the dungeon hub")
        event.command("dn") {
            thenExecute {
                Minecraft.getMinecraft().thePlayer.sendChatMessage("/warp dungeon_hub")
                reply("Warping to...")
                reply("Deez nuts lmao")
            }
        }.withHelp("Warps to the dungeon nuts")
        event.command("neumap") {
            thenLiteral("reset") {
                thenExecute {
                    NotEnoughUpdates.INSTANCE.colourMap = null
                    reply("Reset color map")
                }
                requiresDev()
            }.withHelp("Reset the colour map")
            thenLiteral("save") {
                thenArgument("filename", string()) { fileName ->
                    requiresDev()
                    thenExecute {
                        val stack = Minecraft.getMinecraft().thePlayer.heldItem
                        if (stack == null || stack.item !is ItemMap) {
                            reply("Please hold a map item")
                            return@thenExecute
                        }
                        val map = stack.item as ItemMap
                        val mapData = map.getMapData(stack, Minecraft.getMinecraft().theWorld)
                        if (mapData == null) {
                            reply("Could not grab map data (empty map)")
                            return@thenExecute
                        }
                        val json = JsonObject()
                        for (i in 0 until (128 * 128)) {
                            val x = i % 128
                            val y = i / 128
                            val j = mapData.colors[i].toInt() and 255
                            val c = if (j / 4 == 0) {
                                Color((i + i / 128 and 1) * 8 + 16 shl 24, true)
                            } else {
                                Color(MapColor.mapColorArray[j / 4].getMapColor(j and 3), true)
                            }
                            json.addProperty("$x:$y", c.rgb)
                        }
                        try {
                            NotEnoughUpdates.INSTANCE.manager.configLocation.resolve("maps").mkdirs()
                            NotEnoughUpdates.INSTANCE.manager.writeJson(
                                json,
                                NotEnoughUpdates.INSTANCE.manager.configLocation.resolve("maps/${this[fileName]}.json")
                            )
                            reply("${GREEN}Saved to file.")
                        } catch (e: Exception) {
                            e.printStackTrace()
                            reply("${RED}Failed to save.")
                        }
                    }
                }.withHelp("Save a colour map from an item")
            }
            thenLiteral("load") {
                thenArgument("filename", string()) { fileName ->
                    requiresDev()
                    thenExecute {
                        val json = NotEnoughUpdates.INSTANCE.manager.getJsonFromFile(
                            NotEnoughUpdates.INSTANCE.manager.configLocation.resolve(
                                "maps/${this[fileName]}.json"
                            )
                        )
                        NotEnoughUpdates.INSTANCE.colourMap = (0 until 128).map { x ->
                            (0 until 128).map { y ->
                                val key = "$x:$y"
                                json[key]?.asInt?.let { Color(it, true) } ?: Color(0, 0, 0, 0)
                            }.toTypedArray()
                        }.toTypedArray()
                        for (x in 0..127) {
                            for (y in 0..127) {
                                NotEnoughUpdates.INSTANCE.colourMap[x][y] = Color(0, 0, 0, 0)
                            }
                        }
                        reply("Loaded colour map from file")
                    }
                }.withHelp("Load a colour map from a file")
            }
            thenExecute {
                NotEnoughUpdates.INSTANCE.openGui = GuiDungeonMapEditor(null)
            }
        }.withHelp("Open the dungeon map editor")
    }
}