From df3a7f7d5006306466e55d66067926464ed041b8 Mon Sep 17 00:00:00 2001 From: Empa <42304516+ItsEmpa@users.noreply.github.com> Date: Wed, 24 Apr 2024 14:23:59 +0200 Subject: [PATCH 1/5] improved performance by like 10x --- .../inventory/wardrobe/WardrobeOverlay.kt | 106 ++++++++++-------- 1 file changed, 59 insertions(+), 47 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt index c2e40581ca1e..6322181ea472 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt @@ -5,6 +5,7 @@ import at.hannibal2.skyhanni.config.core.config.Position import at.hannibal2.skyhanni.events.GuiContainerEvent import at.hannibal2.skyhanni.events.GuiRenderEvent import at.hannibal2.skyhanni.events.InventoryCloseEvent +import at.hannibal2.skyhanni.events.InventoryUpdatedEvent import at.hannibal2.skyhanni.features.inventory.wardrobe.WardrobeAPI.currentPage import at.hannibal2.skyhanni.features.inventory.wardrobe.WardrobeAPI.favorite import at.hannibal2.skyhanni.features.inventory.wardrobe.WardrobeAPI.getArmor @@ -17,7 +18,6 @@ import at.hannibal2.skyhanni.mixins.hooks.RenderLivingEntityHelper import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.ColorUtils.withAlpha import at.hannibal2.skyhanni.utils.DelayedRun -import at.hannibal2.skyhanni.utils.GuiRenderUtils import at.hannibal2.skyhanni.utils.InventoryUtils.clickSlot import at.hannibal2.skyhanni.utils.InventoryUtils.getWindowId import at.hannibal2.skyhanni.utils.ItemUtils.name @@ -44,6 +44,7 @@ class WardrobeOverlay { private val config get() = SkyHanniMod.feature.inventory.wardrobeOverlay private var display = emptyList>() + private var renderablesCache = emptyList>() private var tempToggleShowOverlay = true private var favoriteToggle = false @@ -73,7 +74,7 @@ class WardrobeOverlay { val verticalSpacing = 20 val rows = ceil(totalPlayers.toDouble() / maxPlayersPerRow).toInt() - val totalHeight = rows * playerHeight + (rows - 1) * verticalSpacing + val totalHeight = rows * playerHeight + (rows - 1) * (verticalSpacing + 1) val startY = centerY + playerHeight - totalHeight / 2 @@ -83,7 +84,7 @@ class WardrobeOverlay { listOf( Renderable.drawInsideRoundedRect( Renderable.clickable( - Renderable.emptyContainer(30, 30), + Renderable.placeholder(30, 30), bypassChecks = true, onClick = { ChatUtils.chat("Clicked on wardrobe toggle") @@ -101,7 +102,7 @@ class WardrobeOverlay { listOf( Renderable.drawInsideRoundedRect( Renderable.clickable( - Renderable.emptyContainer(30, 30), + Renderable.placeholder(30, 30), bypassChecks = true, onClick = { ChatUtils.chat("Clicked on favorite toggle") @@ -114,6 +115,7 @@ class WardrobeOverlay { ) display += favoriteTogglePos to favoriteToggleRenderable + val isRenderableCacheEmpty = renderablesCache.isEmpty() GlStateManager.pushMatrix() GlStateManager.color(1f, 1f, 1f, 1f) @@ -122,22 +124,17 @@ class WardrobeOverlay { for (row in 0 until rows) { val playersInRow = if (row != rows - 1 || totalPlayers % maxPlayersPerRow == 0) maxPlayersPerRow else totalPlayers % maxPlayersPerRow - val totalWidth = playersInRow * playerWidth + (playersInRow - 1) * horizontalSpacing + val totalWidth = playersInRow * playerWidth + (playersInRow - 1) * (horizontalSpacing + 1) val startX = centerX - (totalWidth - playerWidth) / 2 - val playerY = startY + row * (playerHeight + verticalSpacing) + val playerY = startY + row * ((playerHeight + verticalSpacing) + 1) for (i in 0 until playersInRow) { - val playerX = startX + i * (playerWidth + horizontalSpacing) + val playerX = startX + i * ((playerWidth + horizontalSpacing) + 1) var scale = playerWidth.toDouble() val wardrobeSlot = list[slot] - val padding = 10 - val pos = Position(playerX - padding - playerWidth / 2, playerY - playerHeight - padding) - val containerWidth = playerWidth + 2 * padding - val containerHeight = playerHeight + 2 * padding - val fakePlayer = wardrobeSlot.getFakePlayer() fakePlayer.inventory.armorInventory = @@ -152,46 +149,49 @@ class WardrobeOverlay { ) } - val isHovered = GuiRenderUtils.isPointInRect( - event.mouseX, - event.mouseY, - pos.rawX, - pos.rawY, - containerWidth, - containerHeight - ) - - val hoverRenderable = { - val lore = mutableListOf() - lore.add("§aEstimated Armor Value:") - - var totalPrice = 0.0 - for (item in wardrobeSlot.getArmor().filter { it != WardrobeAPI.AIR }) { - val price = item.getPrice() - totalPrice += price - lore.add(" §7- ${item.name}: §6${NumberUtil.format(price)}") - } + if (isRenderableCacheEmpty) { + val padding = 10 + val pos = Position(playerX - padding - playerWidth / 2, playerY - playerHeight - padding) + val containerWidth = playerWidth + 2 * padding + val containerHeight = playerHeight + 2 * padding + + val hoverRenderable = { + val lore = mutableListOf() + lore.add("§aEstimated Armor Value:") + + var totalPrice = 0.0 + for (item in wardrobeSlot.getArmor().filter { it != WardrobeAPI.AIR }) { + val price = item.getPrice() + totalPrice += price + lore.add(" §7- ${item.name}: §6${NumberUtil.format(price)}") + } - if (wardrobeSlot.getArmor().any { it != WardrobeAPI.AIR }) { - lore.add(" §aTotal Value: §6§l${NumberUtil.format(totalPrice)} coins") + if (wardrobeSlot.getArmor().any { it != WardrobeAPI.AIR }) { + lore.add(" §aTotal Value: §6§l${NumberUtil.format(totalPrice)} coins") - Renderable.toolTipContainer(lore, containerWidth, containerHeight) - } else { - Renderable.emptyContainer(containerWidth, containerHeight) + Renderable.toolTipContainer(lore, containerWidth, containerHeight) + } else { + Renderable.placeholder(containerWidth, containerHeight) + } } - } - val renderable = Renderable.drawInsideRoundedRect( - Renderable.clickAndHoverable( - hoverRenderable.invoke(), - Renderable.emptyContainer(containerWidth, containerHeight), + val renderable = Renderable.clickAndHoverable( + Renderable.drawInsideRoundedRect( + hoverRenderable.invoke(), + getWardrobeSlotColor(wardrobeSlot, true), + padding = 0 + ), + Renderable.drawInsideRoundedRect( + Renderable.placeholder(containerWidth, containerHeight), + getWardrobeSlotColor(wardrobeSlot, false), + padding = 0 + ), onClick = { clickWardrobeSlot(wardrobeSlot) - } - ), - getWardrobeSlotColor(wardrobeSlot, isHovered), - 0, - ) + }, + ) + renderablesCache += pos to renderable + } // Calculate the new mouse position relative to the player val mouseXRelativeToPlayer = (playerX - event.mouseX).toFloat() @@ -201,12 +201,12 @@ class WardrobeOverlay { val eyesY = if (config.eyesFollowMouse) mouseYRelativeToPlayer else 0f display += Position(playerX, playerY) to Renderable.player(fakePlayer, eyesX, eyesY, scale.toInt()) - display += pos to renderable RenderLivingEntityHelper.removeEntityColor(fakePlayer) slot++ } } + renderablesCache.forEach { display += it.first to it.second } GlStateManager.popMatrix() @@ -221,10 +221,15 @@ class WardrobeOverlay { favoriteToggle = false itemPriceCache = mutableMapOf() fakePlayerCache = mutableMapOf() + renderablesCache = mutableListOf() } } } + private fun update() { + renderablesCache = mutableListOf() + } + private fun ItemStack.getPrice(): Double = itemPriceCache.getOrPut(this.getItemUuid()) { EstimatedItemValueCalculator.calculate(this).first } @@ -258,12 +263,19 @@ class WardrobeOverlay { GlStateManager.popMatrix() } + @SubscribeEvent + fun onInventoryUpdate(event: InventoryUpdatedEvent) { + if (!inWardrobe()) return + update() + } + private fun clickWardrobeSlot(wardrobeSlot: WardrobeAPI.WardrobeSlot) { val previousPageSlot = 45 val nextPageSlot = 53 val wardrobePage = currentPage ?: return if (favoriteToggle) { wardrobeSlot.favorite = !wardrobeSlot.favorite + update() return } if (wardrobeSlot.isInCurrentPage()) { From 6c4138d706c37b37a0bc11a55b1511755236e53d Mon Sep 17 00:00:00 2001 From: Empa <42304516+ItsEmpa@users.noreply.github.com> Date: Wed, 24 Apr 2024 15:50:39 +0200 Subject: [PATCH 2/5] fixed issues --- .../inventory/wardrobe/WardrobeOverlay.kt | 53 +++++++++---------- .../skyhanni/utils/renderables/Renderable.kt | 8 ++- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt index 237325654bc1..f93be7406323 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt @@ -14,11 +14,8 @@ import at.hannibal2.skyhanni.features.inventory.wardrobe.WardrobeAPI.isCurrentSl import at.hannibal2.skyhanni.features.inventory.wardrobe.WardrobeAPI.isInCurrentPage import at.hannibal2.skyhanni.features.inventory.wardrobe.WardrobeAPI.locked import at.hannibal2.skyhanni.features.misc.items.EstimatedItemValueCalculator -import at.hannibal2.skyhanni.mixins.hooks.RenderLivingEntityHelper -import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.ColorUtils.withAlpha import at.hannibal2.skyhanni.utils.DelayedRun -import at.hannibal2.skyhanni.utils.GuiRenderUtils import at.hannibal2.skyhanni.utils.InventoryUtils.clickSlot import at.hannibal2.skyhanni.utils.InventoryUtils.getWindowId import at.hannibal2.skyhanni.utils.ItemUtils.name @@ -26,7 +23,6 @@ import at.hannibal2.skyhanni.utils.ItemUtils.removeEnchants import at.hannibal2.skyhanni.utils.LorenzColor import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.NumberUtil -import at.hannibal2.skyhanni.utils.RenderUtils import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getItemUuid import at.hannibal2.skyhanni.utils.renderables.Renderable @@ -79,6 +75,8 @@ class WardrobeOverlay { display += addButtons(gui.width, gui.height, totalHeight) + val isRenderableCacheEmpty = renderablesCache.isEmpty() + GlStateManager.pushMatrix() GlStateManager.color(1f, 1f, 1f, 1f) @@ -102,15 +100,6 @@ class WardrobeOverlay { fakePlayer.inventory.armorInventory = wardrobeSlot.getArmor().map { it?.copy()?.removeEnchants() }.reversed().toTypedArray() - if (!wardrobeSlot.isInCurrentPage()) { - scale *= 0.9 - RenderLivingEntityHelper.setEntityColor( - fakePlayer, - Color.GRAY.withAlpha(100), - ::isEnabled - ) - } - if (isRenderableCacheEmpty) { val padding = 10 val pos = Position(playerX - padding - playerWidth / 2, playerY - playerHeight - padding) @@ -118,18 +107,19 @@ class WardrobeOverlay { val containerHeight = playerHeight + 2 * padding val hoverRenderable = { - val lore = mutableListOf() - lore.add("§aEstimated Armor Value:") - - var totalPrice = 0.0 - for (item in wardrobeSlot.getArmor().filterNotNull()) { - val price = item.getPrice() - totalPrice += price - lore.add(" §7- ${item.name}: §6${NumberUtil.format(price)}") - } - if (wardrobeSlot.getArmor().any { it != null }) { + val lore = mutableListOf() + lore.add("§aEstimated Armor Value:") + + var totalPrice = 0.0 + for (item in wardrobeSlot.getArmor().filterNotNull()) { + val price = item.getPrice() + totalPrice += price + lore.add(" §7- ${item.name}: §6${NumberUtil.format(price)}") + } + lore.add(" §aTotal Value: §6§l${NumberUtil.format(totalPrice)} coins") + Renderable.toolTipContainer(lore, containerWidth, containerHeight) } else { Renderable.placeholder(containerWidth, containerHeight) @@ -161,9 +151,18 @@ class WardrobeOverlay { val eyesX = if (config.eyesFollowMouse) mouseXRelativeToPlayer else 0f val eyesY = if (config.eyesFollowMouse) mouseYRelativeToPlayer else 0f - display += Position(playerX, playerY) to Renderable.entity(fakePlayer, eyesX, eyesY, scale.toInt()) - RenderLivingEntityHelper.removeEntityColor(fakePlayer) - + val color = if (!wardrobeSlot.isInCurrentPage()) { + scale *= 0.9 + Color.GRAY.withAlpha(100) + } else null + + display += Position(playerX, playerY) to Renderable.entity( + fakePlayer, + eyesX, + eyesY, + scale.toInt(), + color + ) slot++ } } @@ -189,6 +188,7 @@ class WardrobeOverlay { private fun update() { renderablesCache = mutableListOf() + fakePlayerCache = mutableMapOf() } private fun addButtons(screenWidth: Int, screenHeight: Int, playerHeight: Int) = buildList { @@ -312,5 +312,4 @@ class WardrobeOverlay { } fun isEnabled() = LorenzUtils.inSkyBlock && config.enabled && inWardrobe() - } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt index 9aaf52c96f00..2a188cc225b9 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt @@ -6,6 +6,7 @@ import at.hannibal2.skyhanni.data.HighlightOnHoverSlot import at.hannibal2.skyhanni.data.ToolTipData import at.hannibal2.skyhanni.features.chroma.ChromaShaderManager import at.hannibal2.skyhanni.features.chroma.ChromaType +import at.hannibal2.skyhanni.mixins.hooks.RenderLivingEntityHelper import at.hannibal2.skyhanni.utils.ColorUtils import at.hannibal2.skyhanni.utils.ColorUtils.darker import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyClicked @@ -685,10 +686,12 @@ interface Renderable { * Don't ask me, ask Mojang. */ fun entity( - player: EntityLivingBase, + entity: EntityLivingBase, eyesX: Float = 0f, eyesY: Float = 0f, scale: Int = 30, + color: Int? = null, + condition: () -> Boolean = { true } ) = object : Renderable { override val width = scale override val height = scale * 2 @@ -696,7 +699,8 @@ interface Renderable { override val verticalAlign = VerticalAlignment.TOP override fun render(posX: Int, posY: Int) { - drawEntityOnScreen(posX, posY, scale, eyesX, eyesY, player) + if (color != null) RenderLivingEntityHelper.setEntityColor(entity, color, condition) + drawEntityOnScreen(posX, posY, scale, eyesX, eyesY, entity) } } } From b3ec38d65653d66e8107e4a909f6e376b6bff4ad Mon Sep 17 00:00:00 2001 From: J10a1n15 <45315647+j10a1n15@users.noreply.github.com> Date: Wed, 24 Apr 2024 18:10:51 +0200 Subject: [PATCH 3/5] Update src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt --- .../skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt index f93be7406323..f7be924078ef 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt @@ -186,7 +186,7 @@ class WardrobeOverlay { } } - private fun update() { + private fun reset() { renderablesCache = mutableListOf() fakePlayerCache = mutableMapOf() } From d541bf9169095e1eda373ec6087d58c84f2db85c Mon Sep 17 00:00:00 2001 From: J10a1n15 <45315647+j10a1n15@users.noreply.github.com> Date: Wed, 24 Apr 2024 18:10:55 +0200 Subject: [PATCH 4/5] Update src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt --- .../skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt index f7be924078ef..a82bc033b5d4 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt @@ -276,7 +276,7 @@ class WardrobeOverlay { @SubscribeEvent fun onInventoryUpdate(event: InventoryUpdatedEvent) { if (!inWardrobe()) return - update() + reset() } private fun clickWardrobeSlot(wardrobeSlot: WardrobeAPI.WardrobeSlot) { From 1329a589f446069ab0a3306312de981adf706740 Mon Sep 17 00:00:00 2001 From: J10a1n15 <45315647+j10a1n15@users.noreply.github.com> Date: Wed, 24 Apr 2024 18:10:59 +0200 Subject: [PATCH 5/5] Update src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt --- .../skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt index a82bc033b5d4..b4867a86655a 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/wardrobe/WardrobeOverlay.kt @@ -285,7 +285,7 @@ class WardrobeOverlay { val wardrobePage = currentPage ?: return if (favoriteToggle) { wardrobeSlot.favorite = !wardrobeSlot.favorite - update() + reset() return } if (wardrobeSlot.isInCurrentPage()) {