Skip to content

Commit

Permalink
feat: Make CustomNotifications toggleable
Browse files Browse the repository at this point in the history
  • Loading branch information
My-Name-Is-Jeff committed Feb 25, 2025
1 parent c2c0956 commit cdfa787
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import gg.skytils.skytilsmod.core.PersistentSave
import gg.skytils.skytilsmod.utils.RegexAsString
import gg.skytils.skytilsmod.utils.Utils
import kotlinx.coroutines.launch
import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.*
import net.minecraftforge.client.event.ClientChatReceivedEvent
Expand All @@ -43,7 +43,8 @@ object CustomNotifications : PersistentSave(File(Skytils.modDir, "customnotifica
if (!Utils.inSkyblock || event.type != 0.toByte() || notifications.isEmpty()) return
Skytils.launch {
val formatted = event.message.formattedText
for ((regex, text, displayTicks) in notifications) {
for ((regex, text, displayTicks, enabled) in notifications) {
if (!enabled) continue
val match = regex.find(formatted) ?: continue
var title = text
match.groupValues.forEachIndexed { i, s -> title = title.replace("%%${i}%%", s) }
Expand Down Expand Up @@ -79,6 +80,7 @@ object CustomNotifications : PersistentSave(File(Skytils.modDir, "customnotifica
@Serializable(with = RegexAsString::class)
val regex: Regex,
val text: String,
@SerialName("ticks") val displayTicks: Int
@SerialName("ticks") val displayTicks: Int,
@EncodeDefault val enabled: Boolean = true
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import gg.essential.elementa.constraints.SiblingConstraint
import gg.essential.elementa.dsl.*
import gg.essential.elementa.effects.OutlineEffect
import gg.essential.universal.UKeyboard
import gg.essential.vigilance.gui.settings.CheckboxComponent
import gg.essential.vigilance.utils.onLeftClick
import gg.skytils.skytilsmod.core.PersistentSave
import gg.skytils.skytilsmod.features.impl.handlers.CustomNotifications
Expand All @@ -49,7 +50,8 @@ class CustomNotificationsGui : WindowScreen(ElementaVersion.V2, newGuiScale = 2)
val container: UIContainer,
val regex: UITextInput,
val displayText: UITextInput,
val ticks: UITextInput
val ticks: UITextInput,
val toggle: CheckboxComponent
)

init {
Expand Down Expand Up @@ -95,21 +97,26 @@ class CustomNotificationsGui : WindowScreen(ElementaVersion.V2, newGuiScale = 2)
)

for (notif in CustomNotifications.notifications.sortedBy { it.text }) {
addNewNotification(notif.regex.pattern, notif.text, notif.displayTicks)
addNewNotification(notif.regex.pattern, notif.text, notif.displayTicks, notif.enabled)
}
}

private fun addNewNotification(regex: String = "", text: String = "", ticks: Int = 20) {
private fun addNewNotification(regex: String = "", text: String = "", ticks: Int = 20, enabled: Boolean = true) {
val container = UIContainer().childOf(scrollComponent).constrain {
x = CenterConstraint()
y = SiblingConstraint(5f)
width = 80.percent()
height = 9.5.percent()
}.effect(OutlineEffect(Color(0, 243, 255), 1f))

val triggerMessage = UITextInput("Trigger Regex").childOf(container).constrain {
val toggle = CheckboxComponent(enabled).childOf(container).constrain {
x = 5.pixels()
y = CenterConstraint()
}

val triggerMessage = UITextInput("Trigger Regex").childOf(container).constrain {
x = SiblingConstraint(5f)
y = CenterConstraint()
width = 40.percent()
}.apply {
onLeftClick {
Expand Down Expand Up @@ -164,14 +171,14 @@ class CustomNotificationsGui : WindowScreen(ElementaVersion.V2, newGuiScale = 2)
components.remove(container)
}

components[container] = Entry(container, triggerMessage, displayText, displayTicks)
components[container] = Entry(container, triggerMessage, displayText, displayTicks, toggle)
}

override fun onScreenClose() {
super.onScreenClose()
CustomNotifications.notifications.clear()

for ((_, triggerRegex, displayText, displayTicks) in components.values) {
for ((_, triggerRegex, displayText, displayTicks, toggle) in components.values) {
if (triggerRegex.getText().isBlank() || displayText.getText().isBlank() || displayTicks.getText()
.isBlank()
) continue
Expand All @@ -180,7 +187,8 @@ class CustomNotificationsGui : WindowScreen(ElementaVersion.V2, newGuiScale = 2)
CustomNotifications.Notification(
triggerRegex.getText().replace("%%MC_IGN%%", mc.session.username).toRegex(),
displayText.getText(),
displayTicks.getText().toInt()
displayTicks.getText().toInt(),
toggle.checked
)
)
}.onFailure {
Expand Down

0 comments on commit cdfa787

Please sign in to comment.