Skip to content

Commit

Permalink
Improvement: Less spam (#2700)
Browse files Browse the repository at this point in the history
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
  • Loading branch information
hannibal002 and hannibal002 authored Oct 9, 2024
1 parent 5fc17cd commit 4f724df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/at/hannibal2/skyhanni/data/MayorAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ object MayorAPI {
ChatUtils.clickableChat(
"The Perkpocalypse Mayor is not known! Click here to update the temporary Mayor.",
onClick = { HypixelCommands.calendar() },
replaceSameMessage = true,
)
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import at.hannibal2.skyhanni.utils.chat.Text.command
import at.hannibal2.skyhanni.utils.chat.Text.hover
import at.hannibal2.skyhanni.utils.chat.Text.onClick
import at.hannibal2.skyhanni.utils.chat.Text.prefix
import at.hannibal2.skyhanni.utils.chat.Text.send
import at.hannibal2.skyhanni.utils.chat.Text.url
import at.hannibal2.skyhanni.utils.compat.getFormattedTextCompat
import net.minecraft.client.Minecraft
Expand Down Expand Up @@ -110,6 +111,7 @@ object ChatUtils {
* @param expireAt When the click action should expire, default never
* @param prefix Whether to prefix the message with the chat prefix, default true
* @param prefixColor Color that the prefix should be, default yellow (§e)
* @param replaceSameMessage Replace the old message with this new message if they are identical
*
* @see CHAT_PREFIX
*/
Expand All @@ -121,14 +123,30 @@ object ChatUtils {
prefix: Boolean = true,
prefixColor: String = "§e",
oneTimeClick: Boolean = false,
replaceSameMessage: Boolean = false,
) {
val msgPrefix = if (prefix) prefixColor + CHAT_PREFIX else ""
chat(Text.text(msgPrefix + message) {

val rawText = msgPrefix + message
val text = Text.text(rawText) {
this.onClick(expireAt, oneTimeClick, onClick)
this.hover = hover.asComponent()
})
}
if (replaceSameMessage) {
text.send(getUniqueMessageIdForString(rawText))
} else {
chat(text)
}
}

val uniqueMessageIdStorage = mutableMapOf<String, Int>()

fun getUniqueMessageIdForString(string: String) = uniqueMessageIdStorage.getOrPut(string) { getUniqueMessageId() }

var lastUniqueMessageId = 123242

fun getUniqueMessageId() = lastUniqueMessageId++

/**
* Sends a message to the user that they can click and run a command
* @param message The message to be sent
Expand Down Expand Up @@ -271,6 +289,7 @@ object ChatUtils {
},
hover = "§eClick to $actionName!\n" +
"§eShift-Click or Control-Click to disable this feature!",
replaceSameMessage = true,
)
}
}

0 comments on commit 4f724df

Please sign in to comment.