aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/gui/profileviewer/ProfileViewerLibrary.kt
blob: 424865452f0e129001c407d361f7263ff2bb21db (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
/*
 * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

package moe.nea.firmament.gui.profileviewer

import com.mojang.brigadier.StringReader
import io.github.cottonmc.cotton.gui.client.CottonClientScreen
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription
import io.github.cottonmc.cotton.gui.widget.WGridPanel
import io.github.cottonmc.cotton.gui.widget.WText
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
import moe.nea.firmament.gui.WTitledItem
import moe.nea.firmament.util.ScreenUtil
import moe.nea.firmament.util.modifyLore
import moe.nea.lisp.LispData
import moe.nea.lisp.LispExecutionContext
import moe.nea.lisp.LispParser
import moe.nea.lisp.bind.AutoBinder
import moe.nea.lisp.bind.LispBinding
import moe.nea.lisp.bind.UnmapForeignObject
import net.minecraft.command.argument.ItemStringReader
import net.minecraft.item.ItemStack
import net.minecraft.registry.Registries
import net.minecraft.text.Text

class ProfileViewerLibrary {

    @LispBinding("mk-item")
    fun makeItem(itemType: String, title: String, vararg lore: String): LispData.ForeignObject<ItemStack> {
        val item = ItemStringReader.item(Registries.ITEM.readOnlyWrapper, StringReader(itemType))
        val itemStack = ItemStack(item.item.value())
        itemStack.nbt = item.nbt
        itemStack.modifyLore { lore.map { Text.literal(it) } }
        itemStack.setCustomName(Text.literal(title))
        return LispData.ForeignObject(itemStack)
    }

    @LispBinding("def-page")
    fun defPage(name: String, @UnmapForeignObject icon: ItemStack) {
        pages.add(Pair(name, icon))
    }

    val pages = mutableListOf<Pair<String, ItemStack>>()
    val coreEnvironment = LispExecutionContext()

    fun run() {
        val t = coreEnvironment.genBindings()
        val ab = AutoBinder()
        ab.bindTo(this, t)
        val prog = LispParser.parse(
            "testfile.lisp", """
        (def-page "Test" (mk-item "minecraft:tnt" "§aThis is a test page" "§aMore text"))
        (def-page "Skills" (mk-item "minecraft:diamond_sword" "§aThis is a test page" "§aMore text"))
        """.trimIndent()
        )
        coreEnvironment.executeProgram(t, prog)
        val light = LightweightGuiDescription()
        val root = light.rootPanel as WGridPanel
        root.setGaps(8, 8)
        pages.forEachIndexed { i, (name, item) ->
            root.add(WTitledItem(item), 0, i)
            root.add(WText(Text.literal(name)).also { it.verticalAlignment = VerticalAlignment.CENTER }, 1, i, 6, 1)
        }
        ScreenUtil.setScreenLater(CottonClientScreen(light))
    }
}