From c78914d5e8bfca12eef8f6c9ff976aed26269345 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal002@users.noreply.github.com> Date: Mon, 15 Apr 2024 18:53:53 +0200 Subject: Improvement: Sack Display Update (#1381) Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Co-authored-by: Cal --- .../at/hannibal2/skyhanni/utils/CollectionUtils.kt | 77 ++++++++++++++++++++++ .../at/hannibal2/skyhanni/utils/LorenzUtils.kt | 3 + .../at/hannibal2/skyhanni/utils/StringUtils.kt | 7 ++ .../skyhanni/utils/renderables/Renderable.kt | 24 +++++-- .../skyhanni/utils/renderables/RenderableUtils.kt | 1 - 5 files changed, 104 insertions(+), 8 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt index a9f5ec6c1..3082112ce 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt @@ -1,5 +1,8 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.utils.NEUItems.getItemStack +import at.hannibal2.skyhanni.utils.renderables.Renderable +import net.minecraft.item.ItemStack import java.util.Collections import java.util.WeakHashMap import java.util.concurrent.ConcurrentLinkedQueue @@ -171,4 +174,78 @@ object CollectionUtils { @Suppress("UNCHECKED_CAST") fun List.takeIfAllNotNull(): List? = takeIf { null !in this } as? List + + // TODO add cache + fun MutableList.addString( + text: String, + horizontalAlign: RenderUtils.HorizontalAlignment = RenderUtils.HorizontalAlignment.LEFT, + verticalAlign: RenderUtils.VerticalAlignment = RenderUtils.VerticalAlignment.CENTER, + ) { + add(Renderable.string(text, horizontalAlign = horizontalAlign, verticalAlign = verticalAlign)) + } + + // TODO add internal name support, and caching + fun MutableList.addItemStack(itemStack: ItemStack) { + add(Renderable.itemStack(itemStack)) + } + + fun MutableList.addItemStack(internalName: NEUInternalName) { + addItemStack(internalName.getItemStack()) + } + + inline fun > MutableList.addSelector( + prefix: String, + getName: (T) -> String, + isCurrent: (T) -> Boolean, + crossinline onChange: (T) -> Unit, + ) { + add(Renderable.horizontalContainer(buildSelector(prefix, getName, isCurrent, onChange))) + } + + inline fun > buildSelector( + prefix: String, + getName: (T) -> String, + isCurrent: (T) -> Boolean, + crossinline onChange: (T) -> Unit, + ) = buildList { + addString(prefix) + for (entry in enumValues()) { + val display = getName(entry) + if (isCurrent(entry)) { + addString("§a[$display]") + } else { + addString("§e[") + add(Renderable.link("§e$display") { + onChange(entry) + }) + addString("§e]") + } + addString(" ") + } + } + + inline fun MutableList.addButton( + prefix: String, + getName: String, + crossinline onChange: () -> Unit, + tips: List = emptyList(), + ) { + val onClick = { + if ((System.currentTimeMillis() - ChatUtils.lastButtonClicked) > 150) { // funny thing happen if I don't do that + onChange() + SoundUtils.playClickSound() + ChatUtils.lastButtonClicked = System.currentTimeMillis() + } + } + add(Renderable.horizontalContainer(buildList { + addString(prefix) + addString("§a[") + if (tips.isEmpty()) { + add(Renderable.link("§e$getName", false, onClick)) + } else { + add(Renderable.clickAndHover("§e$getName", tips, false, onClick)) + } + addString("§a]") + })) + } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt index 7e349aa9f..815bc13aa 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt @@ -217,6 +217,7 @@ object LorenzUtils { } } + @Deprecated("do not use List, use List instead", ReplaceWith("")) inline fun > MutableList>.addSelector( prefix: String, getName: (T) -> String, @@ -226,6 +227,7 @@ object LorenzUtils { add(buildSelector(prefix, getName, isCurrent, onChange)) } + @Deprecated("do not use List, use List instead", ReplaceWith("")) inline fun > buildSelector( prefix: String, getName: (T) -> String, @@ -248,6 +250,7 @@ object LorenzUtils { } } + @Deprecated("do not use List, use List instead", ReplaceWith("")) inline fun MutableList>.addButton( prefix: String, getName: String, diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt index 5079aa7fe..1057b5c0a 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt @@ -106,6 +106,13 @@ object StringUtils { return null } + inline fun List.matchAll(pattern: Pattern, consumer: Matcher.() -> T): T? { + for (line in this) { + pattern.matcher(line).let { if (it.find()) consumer(it) } + } + return null + } + private fun String.internalCleanPlayerName(): String { val split = trim().split(" ") return if (split.size > 1) { 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 f3961660d..128cd4482 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt @@ -81,17 +81,23 @@ interface Renderable { text: String, onClick: () -> Unit, bypassChecks: Boolean = false, + highlightsOnHoverSlots: List = emptyList(), condition: () -> Boolean = { true }, - ): Renderable = link(string(text), onClick, bypassChecks, condition) + ): Renderable = link(string(text), onClick, bypassChecks, highlightsOnHoverSlots = highlightsOnHoverSlots, condition) fun link( renderable: Renderable, onClick: () -> Unit, bypassChecks: Boolean = false, + highlightsOnHoverSlots: List = emptyList(), condition: () -> Boolean = { true }, ): Renderable { return clickable( - hoverable(underlined(renderable), renderable, bypassChecks, condition = condition), + hoverable( + underlined(renderable), renderable, bypassChecks, + condition = condition, + highlightsOnHoverSlots = highlightsOnHoverSlots + ), onClick, 0, bypassChecks, @@ -245,6 +251,7 @@ interface Renderable { unhovered: Renderable, bypassChecks: Boolean = false, condition: () -> Boolean = { true }, + highlightsOnHoverSlots: List = emptyList(), ) = object : Renderable { override val width: Int get() = max(hovered.width, unhovered.width) @@ -255,11 +262,14 @@ interface Renderable { var isHovered = false override fun render(posX: Int, posY: Int) { - isHovered = if (isHovered(posX, posY) && condition() && shouldAllowLink(true, bypassChecks)) { - hovered.render(posX, posY) - true - } else { - unhovered.render(posX, posY) + val pair = Pair(posX, posY) + isHovered = if (isHovered(posX, posY) && condition() && shouldAllowLink(true, bypassChecks)) { + hovered.render(posX, posY) + HighlightOnHoverSlot.currentSlots[pair] = highlightsOnHoverSlots + true + } else { + unhovered.render(posX, posY) + HighlightOnHoverSlot.currentSlots.remove(pair) false } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt index 70969d8fd..4af54b09f 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt @@ -64,5 +64,4 @@ internal object RenderableUtils { this.render(posX, posY + yOffset) GlStateManager.translate(0f, -yOffset.toFloat(), 0f) } - } -- cgit