fix: wardrobe cleanup (cancel old click, use array for keys, use library methods)

This commit is contained in:
Linnea Gräf
2025-06-26 16:37:57 +02:00
parent e4490f23bd
commit 2de6cfbe5e
2 changed files with 37 additions and 27 deletions

View File

@@ -8,6 +8,8 @@ import moe.nea.firmament.events.HandledScreenKeyPressedEvent
import moe.nea.firmament.features.FirmamentFeature import moe.nea.firmament.features.FirmamentFeature
import moe.nea.firmament.gui.config.ManagedConfig import moe.nea.firmament.gui.config.ManagedConfig
import moe.nea.firmament.util.MC import moe.nea.firmament.util.MC
import moe.nea.firmament.util.mc.SlotUtils.clickLeftMouseButton
import moe.nea.firmament.util.mc.SlotUtils.clickMiddleMouseButton
object WardrobeKeybinds : FirmamentFeature { object WardrobeKeybinds : FirmamentFeature {
override val identifier: String override val identifier: String
@@ -15,44 +17,40 @@ object WardrobeKeybinds : FirmamentFeature {
object TConfig : ManagedConfig(identifier, Category.INVENTORY) { object TConfig : ManagedConfig(identifier, Category.INVENTORY) {
val wardrobeKeybinds by toggle("wardrobe-keybinds") { false } val wardrobeKeybinds by toggle("wardrobe-keybinds") { false }
val slot1 by keyBinding("slot-1") { GLFW.GLFW_KEY_1 } val slotKeybinds = (1..9).map {
val slot2 by keyBinding("slot-2") { GLFW.GLFW_KEY_2 } keyBinding("slot-$it") { GLFW.GLFW_KEY_0 + it }
val slot3 by keyBinding("slot-3") { GLFW.GLFW_KEY_3 } }
val slot4 by keyBinding("slot-4") { GLFW.GLFW_KEY_4 }
val slot5 by keyBinding("slot-5") { GLFW.GLFW_KEY_5 }
val slot6 by keyBinding("slot-6") { GLFW.GLFW_KEY_6 }
val slot7 by keyBinding("slot-7") { GLFW.GLFW_KEY_7 }
val slot8 by keyBinding("slot-8") { GLFW.GLFW_KEY_8 }
val slot9 by keyBinding("slot-9") { GLFW.GLFW_KEY_9 }
} }
override val config: ManagedConfig? override val config: ManagedConfig?
get() = TConfig get() = TConfig
val slotKeybindsWithSlot = TConfig.slotKeybinds.withIndex().map { (index, keybinding) ->
index + 36 to keybinding
}
@Subscribe @Subscribe
fun switchSlot(it: HandledScreenKeyPressedEvent) { fun switchSlot(event: HandledScreenKeyPressedEvent) {
if (MC.player == null || MC.world == null || MC.interactionManager == null) return if (MC.player == null || MC.world == null || MC.interactionManager == null) return
val regex = Regex("Wardrobe \\([12]/2\\)") val regex = Regex("Wardrobe \\([12]/2\\)")
if (!regex.matches(it.screen.title.string)) return if (!regex.matches(event.screen.title.string)) return
if (!TConfig.wardrobeKeybinds) return if (!TConfig.wardrobeKeybinds) return
var slot: Int? = null val slot =
if (it.matches(TConfig.slot1)) slot = 36 slotKeybindsWithSlot
if (it.matches(TConfig.slot2)) slot = 37 .find { event.matches(it.second.get()) }
if (it.matches(TConfig.slot3)) slot = 38 ?.first ?: return
if (it.matches(TConfig.slot4)) slot = 39
if (it.matches(TConfig.slot5)) slot = 40
if (it.matches(TConfig.slot6)) slot = 41
if (it.matches(TConfig.slot7)) slot = 42
if (it.matches(TConfig.slot8)) slot = 43
if (it.matches(TConfig.slot9)) slot = 44
if (slot == null) return
val itemStack = it.screen.getScreenHandler().getSlot(slot).stack event.cancel()
val handler = event.screen.screenHandler
val invSlot = handler.getSlot(slot)
val itemStack = invSlot.stack
if (itemStack.item != Items.PINK_DYE && itemStack.item != Items.LIME_DYE) return if (itemStack.item != Items.PINK_DYE && itemStack.item != Items.LIME_DYE) return
MC.interactionManager!!.clickSlot(it.screen.getScreenHandler().syncId, slot, GLFW.GLFW_MOUSE_BUTTON_1, SlotActionType.PICKUP, MC.player); invSlot.clickLeftMouseButton(handler)
} }
} }

View File

@@ -1,5 +1,6 @@
package moe.nea.firmament.util.mc package moe.nea.firmament.util.mc
import org.lwjgl.glfw.GLFW
import net.minecraft.screen.ScreenHandler import net.minecraft.screen.ScreenHandler
import net.minecraft.screen.slot.Slot import net.minecraft.screen.slot.Slot
import net.minecraft.screen.slot.SlotActionType import net.minecraft.screen.slot.SlotActionType
@@ -10,7 +11,7 @@ object SlotUtils {
MC.interactionManager?.clickSlot( MC.interactionManager?.clickSlot(
handler.syncId, handler.syncId,
this.id, this.id,
2, GLFW.GLFW_MOUSE_BUTTON_MIDDLE,
SlotActionType.CLONE, SlotActionType.CLONE,
MC.player MC.player
) )
@@ -20,14 +21,25 @@ object SlotUtils {
MC.interactionManager?.clickSlot( MC.interactionManager?.clickSlot(
handler.syncId, this.id, handler.syncId, this.id,
hotbarIndex, SlotActionType.SWAP, hotbarIndex, SlotActionType.SWAP,
MC.player) MC.player
)
} }
fun Slot.clickRightMouseButton(handler: ScreenHandler) { fun Slot.clickRightMouseButton(handler: ScreenHandler) {
MC.interactionManager?.clickSlot( MC.interactionManager?.clickSlot(
handler.syncId, handler.syncId,
this.id, this.id,
1, GLFW.GLFW_MOUSE_BUTTON_RIGHT,
SlotActionType.PICKUP,
MC.player
)
}
fun Slot.clickLeftMouseButton(handler: ScreenHandler) {
MC.interactionManager?.clickSlot(
handler.syncId,
this.id,
GLFW.GLFW_MOUSE_BUTTON_LEFT,
SlotActionType.PICKUP, SlotActionType.PICKUP,
MC.player MC.player
) )