From 0cc753e0354006eb29c8c58b7961ccbaf41eb146 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 24 Oct 2022 18:04:01 +0700 Subject: [PATCH] Re-opening GUIs no longer evaporate scrollbar due to already scrolled slots Fixes #150 --- .../mc/otm/client/ClientEventHandler.kt | 56 +++++++++++++------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/ClientEventHandler.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/ClientEventHandler.kt index f3f4624ba..6f96736ad 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/ClientEventHandler.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/ClientEventHandler.kt @@ -31,6 +31,7 @@ import ru.dbotthepony.mc.otm.core.integerDivisionUp import ru.dbotthepony.mc.otm.menu.MatteryMenu import ru.dbotthepony.mc.otm.network.MenuNetworkChannel import ru.dbotthepony.mc.otm.registry.AndroidFeatures +import java.util.WeakHashMap import java.util.function.Consumer fun onMovementInputUpdate(event: MovementInputUpdateEvent) { @@ -115,6 +116,9 @@ private class InventoryScrollbarPanel(screen: S, matteryPlayer: Matt }, isSlim = true ) +private data class ScreenScrollbarConfig(val x: Float, val y: Float, val height: Float) +private val foundScreenConfigurations = WeakHashMap, ScreenScrollbarConfig>() + private fun exosuitInventoryLogic(screen: Screen, addListener: (GuiEventListener) -> Unit) { val player = minecraft.player ?: return val matteryPlayer = player.matteryPlayer ?: return @@ -127,6 +131,7 @@ private fun exosuitInventoryLogic(screen: Screen, addListener: (GuiEventListener return } + val existingConfig = foundScreenConfigurations[screen] val foundInventorySlots = Int2ObjectArrayMap() for (slot in screen.menu.slots) { @@ -135,34 +140,51 @@ private fun exosuitInventoryLogic(screen: Screen, addListener: (GuiEventListener } } + var passed = true + for (i in 9 .. 35) { if (i !in foundInventorySlots) { - return + if (existingConfig == null) { + return + } else { + passed = false + break + } } } - val mostTop = foundInventorySlots.values - .stream() - .min { o1, o2 -> o1.y.compareTo(o2.y) } - .get().y.toFloat() + val config: ScreenScrollbarConfig - val mostBottom = foundInventorySlots.values - .stream() - .max { o1, o2 -> o1.y.compareTo(o2.y) } - .get().y.toFloat() + if (passed) { + val mostTop = foundInventorySlots.values + .stream() + .min { o1, o2 -> o1.y.compareTo(o2.y) } + .get().y.toFloat() - val mostLeft = foundInventorySlots.values - .stream() - .max { o1, o2 -> o1.x.compareTo(o2.x) } - .get().x.toFloat() + 26f + val mostBottom = foundInventorySlots.values + .stream() + .max { o1, o2 -> o1.y.compareTo(o2.y) } + .get().y.toFloat() + + val mostLeft = foundInventorySlots.values + .stream() + .max { o1, o2 -> o1.x.compareTo(o2.x) } + .get().x.toFloat() + 26f + + config = ScreenScrollbarConfig(mostLeft + screen.guiLeft - 9f, mostTop + screen.guiTop - 1f, mostBottom - mostTop + AbstractSlotPanel.SIZE) + foundScreenConfigurations[screen] = config + } else if (existingConfig != null) { + config = existingConfig + } else { + return + } val findScrollbar = (screen.renderables.firstOrNull { it is Panel2Widget<*, *> && it.panel is InventoryScrollbarPanel } as Panel2Widget<*, *>?)?.panel as InventoryScrollbarPanel? val scrollbar = findScrollbar ?: InventoryScrollbarPanel(screen, matteryPlayer) - // HOW - scrollbar.x = mostLeft + screen.guiLeft - 9f - scrollbar.y = mostTop + screen.guiTop - 1f - scrollbar.height = mostBottom - mostTop + AbstractSlotPanel.SIZE + scrollbar.x = config.x + scrollbar.y = config.y + scrollbar.height = config.height if (findScrollbar == null) { val widget = Panel2Widget(scrollbar)