aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/features/debug/PowerUserTools.kt
blob: 9341dadbc37e9e0be2873a754be604a6a8dd3660 (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
/*
 * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

package moe.nea.firmament.features.debug

import net.minecraft.block.SkullBlock
import net.minecraft.block.entity.SkullBlockEntity
import net.minecraft.item.ItemStack
import net.minecraft.item.Items
import net.minecraft.nbt.NbtHelper
import net.minecraft.text.Text
import net.minecraft.util.hit.BlockHitResult
import net.minecraft.util.hit.HitResult
import moe.nea.firmament.events.CustomItemModelEvent
import moe.nea.firmament.events.HandledScreenKeyPressedEvent
import moe.nea.firmament.events.ItemTooltipEvent
import moe.nea.firmament.events.ScreenChangeEvent
import moe.nea.firmament.events.TickEvent
import moe.nea.firmament.events.WorldKeyboardEvent
import moe.nea.firmament.features.FirmamentFeature
import moe.nea.firmament.features.texturepack.CustomSkyBlockTextures
import moe.nea.firmament.gui.config.ManagedConfig
import moe.nea.firmament.mixins.accessor.AccessorHandledScreen
import moe.nea.firmament.util.ClipboardUtils
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.focusedItemStack
import moe.nea.firmament.util.getOrCreateCompoundTag
import moe.nea.firmament.util.skyBlockId

object PowerUserTools : FirmamentFeature {
    override val identifier: String
        get() = "power-user"

    object TConfig : ManagedConfig(identifier) {
        val showItemIds by toggle("show-item-id") { false }
        val copyItemId by keyBindingWithDefaultUnbound("copy-item-id")
        val copyTexturePackId by keyBindingWithDefaultUnbound("copy-texture-pack-id")
        val copyNbtData by keyBindingWithDefaultUnbound("copy-nbt-data")
        val copySkullTexture by keyBindingWithDefaultUnbound("copy-skull-texture")
    }

    override val config
        get() = TConfig

    var lastCopiedStack: Pair<ItemStack, Text>? = null
        set(value) {
            field = value
            if (value != null)
                lastCopiedStackViewTime = true
        }
    var lastCopiedStackViewTime = false

    override fun onLoad() {
        ItemTooltipEvent.subscribe {
            if (TConfig.showItemIds) {
                val id = it.stack.skyBlockId ?: return@subscribe
                it.lines.add(Text.stringifiedTranslatable("firmament.tooltip.skyblockid", id.neuItem))
            }
            val (item, text) = lastCopiedStack ?: return@subscribe
            if (!ItemStack.areEqual(item, it.stack)) {
                lastCopiedStack = null
                return@subscribe
            }
            lastCopiedStackViewTime = true
            it.lines.add(text)
        }
        WorldKeyboardEvent.subscribe {
            if (it.matches(TConfig.copySkullTexture)) {
                val p = MC.camera ?: return@subscribe
                val blockHit = p.raycast(20.0, 0.0f, false) ?: return@subscribe
                if (blockHit.type != HitResult.Type.BLOCK || blockHit !is BlockHitResult) {
                    MC.sendChat(Text.translatable("firmament.tooltip.copied.skull.fail"))
                    return@subscribe
                }
                val blockAt = p.world.getBlockState(blockHit.blockPos)?.block
                val entity = p.world.getBlockEntity(blockHit.blockPos)
                if (blockAt !is SkullBlock || entity !is SkullBlockEntity || entity.owner == null) {
                    MC.sendChat(Text.translatable("firmament.tooltip.copied.skull.fail"))
                    return@subscribe
                }
                val id = CustomSkyBlockTextures.getSkullTexture(entity.owner!!)
                if (id == null) {
                    MC.sendChat(Text.translatable("firmament.tooltip.copied.skull.fail"))
                } else {
                    ClipboardUtils.setTextContent(id.toString())
                    MC.sendChat(Text.stringifiedTranslatable("firmament.tooltip.copied.skull", id.toString()))
                }
            }
        }
        TickEvent.subscribe {
            if (!lastCopiedStackViewTime)
                lastCopiedStack = null
            lastCopiedStackViewTime = false
        }
        ScreenChangeEvent.subscribe {
            lastCopiedStack = null
        }
        HandledScreenKeyPressedEvent.subscribe {
            if (it.screen !is AccessorHandledScreen) return@subscribe
            val item = it.screen.focusedItemStack ?: return@subscribe
            if (it.matches(TConfig.copyItemId)) {
                val sbId = item.skyBlockId
                if (sbId == null) {
                    lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.skyblockid.fail"))
                    return@subscribe
                }
                ClipboardUtils.setTextContent(sbId.neuItem)
                lastCopiedStack =
                    Pair(item, Text.stringifiedTranslatable("firmament.tooltip.copied.skyblockid", sbId.neuItem))
            } else if (it.matches(TConfig.copyTexturePackId)) {
                val model = CustomItemModelEvent.getModelIdentifier(item)
                if (model == null) {
                    lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.modelid.fail"))
                    return@subscribe
                }
                ClipboardUtils.setTextContent(model.toString())
                lastCopiedStack =
                    Pair(item, Text.stringifiedTranslatable("firmament.tooltip.copied.modelid", model.toString()))
            } else if (it.matches(TConfig.copyNbtData)) {
                val nbt = item.orCreateNbt.toString()
                ClipboardUtils.setTextContent(nbt)
                lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.nbt"))
            } else if (it.matches(TConfig.copySkullTexture)) {
                if (item.item != Items.PLAYER_HEAD) {
                    lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.skull-id.fail.no-skull"))
                    return@subscribe
                }
                val profile = NbtHelper.toGameProfile(item.orCreateNbt.getOrCreateCompoundTag("SkullOwner"))
                if (profile == null) {
                    lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.skull-id.fail.no-profile"))
                    return@subscribe
                }
                val skullTexture = CustomSkyBlockTextures.getSkullTexture(profile)
                if (skullTexture == null) {
                    lastCopiedStack = Pair(item, Text.translatable("firmament.tooltip.copied.skull-id.fail.no-texture"))
                    return@subscribe
                }
                ClipboardUtils.setTextContent(skullTexture.toString())
                lastCopiedStack =
                    Pair(item, Text.stringifiedTranslatable("firmament.tooltip.copied.skull-id", skullTexture.toString()))
                println("Copied skull id: $skullTexture")
            }
        }
    }


}