aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/inventory/storageoverlay/StorageBackingHandle.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-08-28 19:04:24 +0200
committerLinnea Gräf <nea@nea.moe>2024-08-28 19:04:24 +0200
commitd2f240ff0ca0d27f417f837e706c781a98c31311 (patch)
tree0db7aff6cc14deaf36eed83889d59fd6b3a6f599 /src/main/kotlin/features/inventory/storageoverlay/StorageBackingHandle.kt
parenta6906308163aa3b2d18fa1dc1aa71ac9bbcc83ab (diff)
downloadfirmament-d2f240ff0ca0d27f417f837e706c781a98c31311.tar.gz
firmament-d2f240ff0ca0d27f417f837e706c781a98c31311.tar.bz2
firmament-d2f240ff0ca0d27f417f837e706c781a98c31311.zip
Refactor source layout
Introduce compat source sets and move all kotlin sources to the main directory [no changelog]
Diffstat (limited to 'src/main/kotlin/features/inventory/storageoverlay/StorageBackingHandle.kt')
-rw-r--r--src/main/kotlin/features/inventory/storageoverlay/StorageBackingHandle.kt53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/main/kotlin/features/inventory/storageoverlay/StorageBackingHandle.kt b/src/main/kotlin/features/inventory/storageoverlay/StorageBackingHandle.kt
new file mode 100644
index 0000000..1015578
--- /dev/null
+++ b/src/main/kotlin/features/inventory/storageoverlay/StorageBackingHandle.kt
@@ -0,0 +1,53 @@
+
+
+package moe.nea.firmament.features.inventory.storageoverlay
+
+import net.minecraft.client.gui.screen.Screen
+import net.minecraft.client.gui.screen.ingame.GenericContainerScreen
+import net.minecraft.screen.GenericContainerScreenHandler
+import moe.nea.firmament.util.ifMatches
+import moe.nea.firmament.util.unformattedString
+
+/**
+ * A handle representing the state of the "server side" screens.
+ */
+sealed interface StorageBackingHandle {
+
+ sealed interface HasBackingScreen {
+ val handler: GenericContainerScreenHandler
+ }
+
+ /**
+ * The main storage overview is open. Clicking on a slot will open that page. This page is accessible via `/storage`
+ */
+ data class Overview(override val handler: GenericContainerScreenHandler) : StorageBackingHandle, HasBackingScreen
+
+ /**
+ * An individual storage page is open. This may be a backpack or an enderchest page. This page is accessible via
+ * the [Overview] or via `/ec <index + 1>` for enderchest pages.
+ */
+ data class Page(override val handler: GenericContainerScreenHandler, val storagePageSlot: StoragePageSlot) :
+ StorageBackingHandle, HasBackingScreen
+
+ companion object {
+ private val enderChestName = "^Ender Chest \\(([1-9])/[1-9]\\)$".toRegex()
+ private val backPackName = "^.+Backpack \\(Slot #([0-9]+)\\)$".toRegex()
+
+ /**
+ * Parse a screen into a [StorageBackingHandle]. If this returns null it means that the screen is not
+ * representable as a [StorageBackingHandle], meaning another screen is open, for example the enderchest icon
+ * selection screen.
+ */
+ fun fromScreen(screen: Screen?): StorageBackingHandle? {
+ if (screen == null) return null
+ if (screen !is GenericContainerScreen) return null
+ val title = screen.title.unformattedString
+ if (title == "Storage") return Overview(screen.screenHandler)
+ return title.ifMatches(enderChestName) {
+ Page(screen.screenHandler, StoragePageSlot.ofEnderChestPage(it.groupValues[1].toInt()))
+ } ?: title.ifMatches(backPackName) {
+ Page(screen.screenHandler, StoragePageSlot.ofBackPackPage(it.groupValues[1].toInt()))
+ }
+ }
+ }
+}