feat: inventory buttons rendering in other guys toggle

This commit is contained in:
Jacob
2025-07-01 19:46:19 +08:00
committed by GitHub
parent 89d16bf3f3
commit 073a62c2e3
2 changed files with 14 additions and 8 deletions

View File

@@ -6,14 +6,13 @@ import me.shedaniel.math.Rectangle
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer import kotlinx.serialization.serializer
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
import net.minecraft.client.MinecraftClient import net.minecraft.client.gui.screen.ingame.HandledScreen
import net.minecraft.client.gui.screen.ingame.InventoryScreen
import net.minecraft.text.Text import net.minecraft.text.Text
import moe.nea.firmament.annotations.Subscribe import moe.nea.firmament.annotations.Subscribe
import moe.nea.firmament.events.HandledScreenClickEvent import moe.nea.firmament.events.HandledScreenClickEvent
import moe.nea.firmament.events.HandledScreenForegroundEvent import moe.nea.firmament.events.HandledScreenForegroundEvent
import moe.nea.firmament.events.HandledScreenPushREIEvent import moe.nea.firmament.events.HandledScreenPushREIEvent
import moe.nea.firmament.features.FirmamentFeature
import moe.nea.firmament.gui.FirmHoverComponent
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.ScreenUtil import moe.nea.firmament.util.ScreenUtil
@@ -29,6 +28,7 @@ object InventoryButtons {
openEditor() openEditor()
} }
val hoverText by toggle("hover-text") { true } val hoverText by toggle("hover-text") { true }
val onlyInv by toggle("only-inv") { false }
} }
object DConfig : DataHolder<Data>(serializer(), "inventory-buttons", ::Data) object DConfig : DataHolder<Data>(serializer(), "inventory-buttons", ::Data)
@@ -38,13 +38,17 @@ object InventoryButtons {
var buttons: MutableList<InventoryButton> = mutableListOf() var buttons: MutableList<InventoryButton> = mutableListOf()
) )
fun getValidButtons(screen: HandledScreen<*>): Sequence<InventoryButton> {
return DConfig.data.buttons.asSequence().filter { button ->
button.isValid() && (!TConfig.onlyInv || screen is InventoryScreen)
}
}
fun getValidButtons() = DConfig.data.buttons.asSequence().filter { it.isValid() }
@Subscribe @Subscribe
fun onRectangles(it: HandledScreenPushREIEvent) { fun onRectangles(it: HandledScreenPushREIEvent) {
val bounds = it.screen.getRectangle() val bounds = it.screen.getRectangle()
for (button in getValidButtons()) { for (button in getValidButtons(it.screen)) {
val buttonBounds = button.getBounds(bounds) val buttonBounds = button.getBounds(bounds)
it.block(buttonBounds) it.block(buttonBounds)
} }
@@ -53,7 +57,7 @@ object InventoryButtons {
@Subscribe @Subscribe
fun onClickScreen(it: HandledScreenClickEvent) { fun onClickScreen(it: HandledScreenClickEvent) {
val bounds = it.screen.getRectangle() val bounds = it.screen.getRectangle()
for (button in getValidButtons()) { for (button in getValidButtons(it.screen)) {
val buttonBounds = button.getBounds(bounds) val buttonBounds = button.getBounds(bounds)
if (buttonBounds.contains(it.mouseX, it.mouseY)) { if (buttonBounds.contains(it.mouseX, it.mouseY)) {
MC.sendCommand(button.command!! /* non null invariant covered by getValidButtons */) MC.sendCommand(button.command!! /* non null invariant covered by getValidButtons */)
@@ -67,10 +71,10 @@ object InventoryButtons {
@Subscribe @Subscribe
fun onRenderForeground(it: HandledScreenForegroundEvent) { fun onRenderForeground(it: HandledScreenForegroundEvent) {
val bounds = it.screen.getRectangle() val bounds = it.screen.getRectangle()
var hoveredComponent: InventoryButton? = null var hoveredComponent: InventoryButton? = null
for (button in getValidButtons()) { for (button in getValidButtons(it.screen)) {
val buttonBounds = button.getBounds(bounds) val buttonBounds = button.getBounds(bounds)
it.context.matrices.push() it.context.matrices.push()
it.context.matrices.translate(buttonBounds.minX.toFloat(), buttonBounds.minY.toFloat(), 0F) it.context.matrices.translate(buttonBounds.minX.toFloat(), buttonBounds.minY.toFloat(), 0F)

View File

@@ -160,6 +160,8 @@
"firmament.config.inventory-buttons-config": "Inventory Buttons", "firmament.config.inventory-buttons-config": "Inventory Buttons",
"firmament.config.inventory-buttons-config.hover-text": "Hover Tooltip", "firmament.config.inventory-buttons-config.hover-text": "Hover Tooltip",
"firmament.config.inventory-buttons-config.hover-text.description": "Hovering over inventory buttons will show the command they run.", "firmament.config.inventory-buttons-config.hover-text.description": "Hovering over inventory buttons will show the command they run.",
"firmament.config.inventory-buttons-config.only-inv": "Inventory Only",
"firmament.config.inventory-buttons-config.only-inv.description": "Only shows buttons while in the inventory",
"firmament.config.inventory-buttons-config.open-editor": "Open Editor", "firmament.config.inventory-buttons-config.open-editor": "Open Editor",
"firmament.config.inventory-buttons-config.open-editor.description": "Click anywhere to create a new inventory button or to edit one. Hold SHIFT to grid align.", "firmament.config.inventory-buttons-config.open-editor.description": "Click anywhere to create a new inventory button or to edit one. Hold SHIFT to grid align.",
"firmament.config.item-hotkeys": "Item Hotkeys", "firmament.config.item-hotkeys": "Item Hotkeys",