aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils
diff options
context:
space:
mode:
authorhannibal2 <24389977+hannibal002@users.noreply.github.com>2024-04-15 18:53:53 +0200
committerGitHub <noreply@github.com>2024-04-15 18:53:53 +0200
commitc78914d5e8bfca12eef8f6c9ff976aed26269345 (patch)
tree7aa494224edff67087b01570e866a817418e0caf /src/main/java/at/hannibal2/skyhanni/utils
parent53f2cfd71a6e97fe59e2ced18045e8eed0434f4e (diff)
downloadskyhanni-c78914d5e8bfca12eef8f6c9ff976aed26269345.tar.gz
skyhanni-c78914d5e8bfca12eef8f6c9ff976aed26269345.tar.bz2
skyhanni-c78914d5e8bfca12eef8f6c9ff976aed26269345.zip
Improvement: Sack Display Update (#1381)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Co-authored-by: Cal <cwolfson58@gmail.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt77
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt3
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt7
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt24
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt1
5 files changed, 104 insertions, 8 deletions
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 <T> List<T?>.takeIfAllNotNull(): List<T>? =
takeIf { null !in this } as? List<T>
+
+ // TODO add cache
+ fun MutableList<Renderable>.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<Renderable>.addItemStack(itemStack: ItemStack) {
+ add(Renderable.itemStack(itemStack))
+ }
+
+ fun MutableList<Renderable>.addItemStack(internalName: NEUInternalName) {
+ addItemStack(internalName.getItemStack())
+ }
+
+ inline fun <reified T : Enum<T>> MutableList<Renderable>.addSelector(
+ prefix: String,
+ getName: (T) -> String,
+ isCurrent: (T) -> Boolean,
+ crossinline onChange: (T) -> Unit,
+ ) {
+ add(Renderable.horizontalContainer(buildSelector<T>(prefix, getName, isCurrent, onChange)))
+ }
+
+ inline fun <reified T : Enum<T>> buildSelector(
+ prefix: String,
+ getName: (T) -> String,
+ isCurrent: (T) -> Boolean,
+ crossinline onChange: (T) -> Unit,
+ ) = buildList {
+ addString(prefix)
+ for (entry in enumValues<T>()) {
+ 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<Renderable>.addButton(
+ prefix: String,
+ getName: String,
+ crossinline onChange: () -> Unit,
+ tips: List<String> = 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<Any>, use List<Renderable> instead", ReplaceWith(""))
inline fun <reified T : Enum<T>> MutableList<List<Any>>.addSelector(
prefix: String,
getName: (T) -> String,
@@ -226,6 +227,7 @@ object LorenzUtils {
add(buildSelector<T>(prefix, getName, isCurrent, onChange))
}
+ @Deprecated("do not use List<Any>, use List<Renderable> instead", ReplaceWith(""))
inline fun <reified T : Enum<T>> buildSelector(
prefix: String,
getName: (T) -> String,
@@ -248,6 +250,7 @@ object LorenzUtils {
}
}
+ @Deprecated("do not use List<Any>, use List<Renderable> instead", ReplaceWith(""))
inline fun MutableList<List<Any>>.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 <T> List<String>.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<Int> = 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<Int> = 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<Int> = 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)
}
-
}