aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/features/inventory/SlotLocking.kt
blob: 43243f1ce602955c45869df8fd3ca070e0771e78 (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
/*
 * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
@file:UseSerializers(DashlessUUIDSerializer::class)

package moe.nea.firmament.features.inventory

import com.mojang.blaze3d.systems.RenderSystem
import java.util.*
import org.lwjgl.glfw.GLFW
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import kotlinx.serialization.serializer
import net.minecraft.client.gui.screen.ingame.HandledScreen
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.screen.GenericContainerScreenHandler
import net.minecraft.screen.slot.SlotActionType
import net.minecraft.util.Identifier
import moe.nea.firmament.events.HandledScreenKeyPressedEvent
import moe.nea.firmament.events.IsSlotProtectedEvent
import moe.nea.firmament.events.SlotRenderEvents
import moe.nea.firmament.features.FirmamentFeature
import moe.nea.firmament.gui.config.ManagedConfig
import moe.nea.firmament.keybindings.SavedKeyBinding
import moe.nea.firmament.mixins.accessor.AccessorHandledScreen
import moe.nea.firmament.util.CommonSoundEffects
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.SBData
import moe.nea.firmament.util.data.ProfileSpecificDataHolder
import moe.nea.firmament.util.item.displayNameAccordingToNbt
import moe.nea.firmament.util.item.loreAccordingToNbt
import moe.nea.firmament.util.json.DashlessUUIDSerializer
import moe.nea.firmament.util.skyblockUUID
import moe.nea.firmament.util.unformattedString

object SlotLocking : FirmamentFeature {
    override val identifier: String
        get() = "slot-locking"

    @Serializable
    data class Data(
        val lockedSlots: MutableSet<Int> = mutableSetOf(),
        val lockedSlotsRift: MutableSet<Int> = mutableSetOf(),

        val lockedUUIDs: MutableSet<UUID> = mutableSetOf(),
    )

    object TConfig : ManagedConfig(identifier) {
        val lockSlot by keyBinding("lock") { GLFW.GLFW_KEY_L }
        val lockUUID by keyBindingWithOutDefaultModifiers("lock-uuid") {
            SavedKeyBinding(GLFW.GLFW_KEY_L, shift = true)
        }
    }

    override val config: TConfig
        get() = TConfig

    object DConfig : ProfileSpecificDataHolder<Data>(serializer(), "locked-slots", ::Data)

    val lockedUUIDs get() = DConfig.data?.lockedUUIDs

    val lockedSlots
        get() = when (SBData.skyblockLocation) {
            "rift" -> DConfig.data?.lockedSlotsRift
            null -> null
            else -> DConfig.data?.lockedSlots
        }

    fun isSalvageScreen(screen: HandledScreen<*>?): Boolean {
        if (screen == null) return false
        return screen.title.unformattedString.contains("Salvage Item")
    }

    fun isTradeScreen(screen: HandledScreen<*>?): Boolean {
        if (screen == null) return false
        val handler = screen.screenHandler as? GenericContainerScreenHandler ?: return false
        if (handler.inventory.size() < 9) return false
        val middlePane = handler.inventory.getStack(handler.inventory.size() - 5)
        if (middlePane == null) return false
        return middlePane.displayNameAccordingToNbt?.unformattedString == "⇦ Your stuff"
    }

    fun isNpcShop(screen: HandledScreen<*>?): Boolean {
        if (screen == null) return false
        val handler = screen.screenHandler as? GenericContainerScreenHandler ?: return false
        if (handler.inventory.size() < 9) return false
        val sellItem = handler.inventory.getStack(handler.inventory.size() - 5)
        if (sellItem == null) return false
        if (sellItem.displayNameAccordingToNbt?.unformattedString == "Sell Item") return true
        val lore = sellItem.loreAccordingToNbt
        return (lore.lastOrNull() ?: return false).value?.unformattedString == "Click to buyback!"
    }

    override fun onLoad() {
        HandledScreenKeyPressedEvent.subscribe {
            if (!it.matches(TConfig.lockSlot)) return@subscribe
            val inventory = MC.handledScreen ?: return@subscribe
            inventory as AccessorHandledScreen

            val slot = inventory.focusedSlot_Firmament ?: return@subscribe
            val lockedSlots = lockedSlots ?: return@subscribe
            if (slot.inventory is PlayerInventory) {
                if (slot.index in lockedSlots) {
                    lockedSlots.remove(slot.index)
                } else {
                    lockedSlots.add(slot.index)
                }
                DConfig.markDirty()
                CommonSoundEffects.playSuccess()
            }
        }
        HandledScreenKeyPressedEvent.subscribe {
            if (!it.matches(TConfig.lockUUID)) return@subscribe
            val inventory = MC.handledScreen ?: return@subscribe
            inventory as AccessorHandledScreen

            val slot = inventory.focusedSlot_Firmament ?: return@subscribe
            val stack = slot.stack ?: return@subscribe
            val uuid = stack.skyblockUUID ?: return@subscribe
            val lockedUUIDs = lockedUUIDs ?: return@subscribe
            if (uuid in lockedUUIDs) {
                lockedUUIDs.remove(uuid)
            } else {
                lockedUUIDs.add(uuid)
            }
            DConfig.markDirty()
            CommonSoundEffects.playSuccess()
        }
        IsSlotProtectedEvent.subscribe {
            if (it.slot != null && it.slot.inventory is PlayerInventory && it.slot.index in (lockedSlots ?: setOf())) {
                it.protect()
            }
        }
        IsSlotProtectedEvent.subscribe { event ->
            val doesNotDeleteItem = event.actionType == SlotActionType.SWAP
                || event.actionType == SlotActionType.PICKUP
                || event.actionType == SlotActionType.QUICK_MOVE
                || event.actionType == SlotActionType.QUICK_CRAFT
                || event.actionType == SlotActionType.CLONE
                || event.actionType == SlotActionType.PICKUP_ALL
            val isSellOrTradeScreen =
                isNpcShop(MC.handledScreen) || isTradeScreen(MC.handledScreen) || isSalvageScreen(MC.handledScreen)
            if (!isSellOrTradeScreen && doesNotDeleteItem) return@subscribe
            val stack = event.itemStack ?: return@subscribe
            val uuid = stack.skyblockUUID ?: return@subscribe
            if (uuid in (lockedUUIDs ?: return@subscribe)) {
                event.protect()
            }
        }
        IsSlotProtectedEvent.subscribe { event ->
            if (event.slot == null) return@subscribe
            if (!event.slot.hasStack()) return@subscribe
            if (event.slot.stack.displayNameAccordingToNbt?.unformattedString != "Salvage Items") return@subscribe
            val inv = event.slot.inventory
            var anyBlocked = false
            for (i in 0 until event.slot.index) {
                val stack = inv.getStack(i)
                if (IsSlotProtectedEvent.shouldBlockInteraction(null, SlotActionType.THROW, stack))
                    anyBlocked = true
            }
            if (anyBlocked) {
                event.protectSilent()
            }
        }
        SlotRenderEvents.After.subscribe {
            val isSlotLocked = it.slot.inventory is PlayerInventory && it.slot.index in (lockedSlots ?: setOf())
            val isUUIDLocked = (it.slot.stack?.skyblockUUID) in (lockedUUIDs ?: setOf())
            if (isSlotLocked || isUUIDLocked) {
                RenderSystem.disableDepthTest()
                it.context.drawSprite(
                    it.slot.x, it.slot.y, 0,
                    16, 16,
                    MC.guiAtlasManager.getSprite(
                        when {
                            isSlotLocked ->
                                (Identifier("firmament:slot_locked"))

                            isUUIDLocked ->
                                (Identifier("firmament:uuid_locked"))

                            else ->
                                error("unreachable")
                        }
                    )
                )
                RenderSystem.enableDepthTest()
            }
        }
    }
}