From dd5cb3d095019595058b1be8016f99eb4f801265 Mon Sep 17 00:00:00 2001 From: Ic3Tank <61137113+IceTank@users.noreply.github.com> Date: Sat, 7 Mar 2026 22:36:49 +0100 Subject: [PATCH 1/2] Add ModuleNotifier to get feedback in chat when modules toggle --- src/main/kotlin/com/lambda/module/Module.kt | 23 ++++++++++++ .../module/modules/client/ModuleNotifier.kt | 36 +++++++++++++++++++ .../kotlin/com/lambda/util/Communication.kt | 27 +++++++++++++- .../kotlin/com/lambda/util/text/TextDsl.kt | 16 +++++++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/lambda/module/modules/client/ModuleNotifier.kt diff --git a/src/main/kotlin/com/lambda/module/Module.kt b/src/main/kotlin/com/lambda/module/Module.kt index 3599ef30c..deca5ee31 100644 --- a/src/main/kotlin/com/lambda/module/Module.kt +++ b/src/main/kotlin/com/lambda/module/Module.kt @@ -37,11 +37,18 @@ import com.lambda.event.listener.Listener import com.lambda.event.listener.SafeListener import com.lambda.event.listener.SafeListener.Companion.listen import com.lambda.event.listener.UnsafeListener +import com.lambda.module.modules.client.ModuleNotifier +import com.lambda.module.modules.client.ModuleNotifier.NotifyTarget +import com.lambda.module.modules.client.ModuleNotifier.notifyTarget import com.lambda.module.tag.ModuleTag import com.lambda.sound.LambdaSound import com.lambda.sound.SoundManager.play +import com.lambda.threading.runSafe +import com.lambda.util.Communication.log import com.lambda.util.KeyCode import com.lambda.util.Nameable +import net.minecraft.text.Text +import net.minecraft.util.Colors /** * A [Module] is a feature or tool for the utility mod. @@ -171,6 +178,22 @@ abstract class Module( fun toggle() { isEnabled = !isEnabled + + if (ModuleNotifier.isEnabled) { + runSafe { + val message = if (isEnabled) { + Text.literal("on").withColor(Colors.GREEN) + } else { + Text.literal("off").withColor(Colors.RED) + } + if (notifyTarget.contains(NotifyTarget.Chat)) { + log(message, source = name) + } + if (notifyTarget.contains(NotifyTarget.ActionBar)) { + log(message, source = name, inGameOverlay = true) + } + } + } } fun onEnable(block: SafeContext.() -> Unit) { diff --git a/src/main/kotlin/com/lambda/module/modules/client/ModuleNotifier.kt b/src/main/kotlin/com/lambda/module/modules/client/ModuleNotifier.kt new file mode 100644 index 000000000..ae0717b95 --- /dev/null +++ b/src/main/kotlin/com/lambda/module/modules/client/ModuleNotifier.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2026 Lambda + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lambda.module.modules.client + +import com.lambda.module.Module +import com.lambda.util.Describable +import com.lambda.util.NamedEnum + +@Suppress("unused") +object ModuleNotifier : Module( + name = "ModuleNotifier", + description = "Notifies you when a module is enabled or disabled", + tag = com.lambda.module.tag.ModuleTag.CLIENT +) { + var notifyTarget by setting("Notify Target", setOf(NotifyTarget.ActionBar), NotifyTarget.entries.toSet(), description = "Where to send notifications when modules are toggled") + + enum class NotifyTarget(override val displayName: String, override val description: String) : Describable, NamedEnum { + Chat("Chat", "Sends a message to chat when a module is toggled"), + ActionBar("Action Bar", "Sends a message to the action bar when a module is toggled"),; + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/lambda/util/Communication.kt b/src/main/kotlin/com/lambda/util/Communication.kt index 914db8d66..d1fb1fcbf 100644 --- a/src/main/kotlin/com/lambda/util/Communication.kt +++ b/src/main/kotlin/com/lambda/util/Communication.kt @@ -39,6 +39,7 @@ import com.lambda.util.text.literal import com.lambda.util.text.styled import com.lambda.util.text.text import net.minecraft.client.toast.SystemToast +import net.minecraft.text.MutableText import net.minecraft.text.Text import java.awt.Color import java.time.LocalDateTime @@ -96,18 +97,42 @@ object Communication { } } + /** + * Logs messages to the in game chat if available while stripping the message styles + */ fun Any.log( message: Text, logLevel: LogLevel = LogLevel.Info, source: String = "", textSource: Text = Text.empty(), + inGameOverlay: Boolean = false ) { buildText { text(this@log.source(logLevel, source, textSource)) text(message) }.let { log -> runSafeGameScheduled { - player.sendMessage(log, false) + player.sendMessage(log, inGameOverlay) + } + } + } + + /** + * Logs messages to the in game chat if available while keeping the message styles + */ + fun Any.log( + message: MutableText, + logLevel: LogLevel = LogLevel.Info, + source: String = "", + textSource: Text = Text.empty(), + inGameOverlay: Boolean = false + ) { + buildText { + text(this@log.source(logLevel, source, textSource)) + literal(message) + }.let { log -> + runSafeGameScheduled { + player.sendMessage(log, inGameOverlay) } } } diff --git a/src/main/kotlin/com/lambda/util/text/TextDsl.kt b/src/main/kotlin/com/lambda/util/text/TextDsl.kt index 6a8f3892e..c097058ec 100644 --- a/src/main/kotlin/com/lambda/util/text/TextDsl.kt +++ b/src/main/kotlin/com/lambda/util/text/TextDsl.kt @@ -80,6 +80,11 @@ class TextBuilder { this.text.append(style.applyTo(text)) } + @TextDsl + fun append(text: MutableText) { + this.text.append(text) + } + /** * Returns the [Text] result of this builder. */ @@ -120,6 +125,17 @@ fun TextBuilder.literal(value: String) { styleAndAppend(Text.literal(value)) } +/** + * Adds a literal text that is not re-styled by the builder's current style. + * + * @param value The text. + * @see StyleBuilder for action + */ +@TextDsl +fun TextBuilder.literal(value: MutableText) { + append(value) +} + /** * Adds a literal text. * From eeb3b6658f293aab668deab9e4521e8e5b70d55e Mon Sep 17 00:00:00 2001 From: Ic3Tank <61137113+IceTank@users.noreply.github.com> Date: Fri, 13 Mar 2026 21:24:47 +0100 Subject: [PATCH 2/2] Refactor ModuleNotifier to listen to emitted Events by the Module when toggling --- .../com/lambda/event/events/ModuleEvent.kt | 55 +++++++++++++++++++ src/main/kotlin/com/lambda/module/Module.kt | 42 ++++++-------- .../module/modules/client/ModuleNotifier.kt | 39 ++++++++++++- .../kotlin/com/lambda/util/Communication.kt | 20 ------- .../kotlin/com/lambda/util/text/TextDsl.kt | 16 ------ 5 files changed, 110 insertions(+), 62 deletions(-) create mode 100644 src/main/kotlin/com/lambda/event/events/ModuleEvent.kt diff --git a/src/main/kotlin/com/lambda/event/events/ModuleEvent.kt b/src/main/kotlin/com/lambda/event/events/ModuleEvent.kt new file mode 100644 index 000000000..516b195af --- /dev/null +++ b/src/main/kotlin/com/lambda/event/events/ModuleEvent.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2026 Lambda + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lambda.event.events + +import com.lambda.event.Event +import com.lambda.event.callback.Cancellable +import com.lambda.event.callback.ICancellable +import com.lambda.module.Module + +/** + * Represents events related to toggling, enabling, and disabling of [Module]s. + * + * @see Toggle + * @see Enabled + * @see Disabled + */ +sealed class ModuleEvent { + /** + * Event that fires before a [Module] is toggled, allowing listeners to cancel the toggle. + * @property module The module that is being toggled. + * @property newValue The value that the module is being toggled to (true for enabling, false for disabling). + * @see Enabled + * @see Disabled + */ + data class Toggle(val module: Module, val newValue: Boolean) : Event, ICancellable by Cancellable() + + /** + * Event that fires before a [Module] is enabled, allowing listeners to cancel the enable. + * @property module The module that is being enabled. + * @see Toggle + */ + data class Enabled(val module: Module) : Event, ICancellable by Cancellable() + + /** + * Event that fires before a [Module] is disabled, allowing listeners to cancel the disable. + * @property module The module that is being disabled. + * @see Toggle + */ + data class Disabled(val module: Module) : Event, ICancellable by Cancellable() +} \ No newline at end of file diff --git a/src/main/kotlin/com/lambda/module/Module.kt b/src/main/kotlin/com/lambda/module/Module.kt index deca5ee31..58c6071b9 100644 --- a/src/main/kotlin/com/lambda/module/Module.kt +++ b/src/main/kotlin/com/lambda/module/Module.kt @@ -28,27 +28,22 @@ import com.lambda.config.settings.complex.Bind import com.lambda.config.settings.complex.KeybindSetting.Companion.onPress import com.lambda.config.settings.complex.KeybindSetting.Companion.onRelease import com.lambda.context.SafeContext +import com.lambda.event.EventFlow.post import com.lambda.event.EventFlow.updateListenerSorting import com.lambda.event.Muteable import com.lambda.event.OwnerPriority import com.lambda.event.events.ClientEvent import com.lambda.event.events.ConnectionEvent +import com.lambda.event.events.ModuleEvent import com.lambda.event.listener.Listener import com.lambda.event.listener.SafeListener import com.lambda.event.listener.SafeListener.Companion.listen import com.lambda.event.listener.UnsafeListener -import com.lambda.module.modules.client.ModuleNotifier -import com.lambda.module.modules.client.ModuleNotifier.NotifyTarget -import com.lambda.module.modules.client.ModuleNotifier.notifyTarget import com.lambda.module.tag.ModuleTag import com.lambda.sound.LambdaSound import com.lambda.sound.SoundManager.play -import com.lambda.threading.runSafe -import com.lambda.util.Communication.log import com.lambda.util.KeyCode import com.lambda.util.Nameable -import net.minecraft.text.Text -import net.minecraft.util.Colors /** * A [Module] is a feature or tool for the utility mod. @@ -157,8 +152,12 @@ abstract class Module( get() = !isEnabled && !alwaysListening init { - onEnable { LambdaSound.ModuleOn.play() } - onDisable { LambdaSound.ModuleOff.play() } + onEnable { + LambdaSound.ModuleOn.play() + } + onDisable { + LambdaSound.ModuleOff.play() + } onEnableUnsafe { LambdaSound.ModuleOn.play() } onDisableUnsafe { LambdaSound.ModuleOff.play() } @@ -169,31 +168,24 @@ abstract class Module( } fun enable() { + if (ModuleEvent.Enabled(this@Module).post().isCanceled()) { + return + } isEnabled = true } fun disable() { + if (ModuleEvent.Disabled(this@Module).post().isCanceled()) { + return + } isEnabled = false } fun toggle() { - isEnabled = !isEnabled - - if (ModuleNotifier.isEnabled) { - runSafe { - val message = if (isEnabled) { - Text.literal("on").withColor(Colors.GREEN) - } else { - Text.literal("off").withColor(Colors.RED) - } - if (notifyTarget.contains(NotifyTarget.Chat)) { - log(message, source = name) - } - if (notifyTarget.contains(NotifyTarget.ActionBar)) { - log(message, source = name, inGameOverlay = true) - } - } + if (ModuleEvent.Toggle(this@Module, !isEnabled).post().isCanceled()) { + return } + isEnabled = !isEnabled } fun onEnable(block: SafeContext.() -> Unit) { diff --git a/src/main/kotlin/com/lambda/module/modules/client/ModuleNotifier.kt b/src/main/kotlin/com/lambda/module/modules/client/ModuleNotifier.kt index ae0717b95..26106b517 100644 --- a/src/main/kotlin/com/lambda/module/modules/client/ModuleNotifier.kt +++ b/src/main/kotlin/com/lambda/module/modules/client/ModuleNotifier.kt @@ -17,11 +17,16 @@ package com.lambda.module.modules.client +import com.lambda.event.events.ModuleEvent +import com.lambda.event.listener.SafeListener.Companion.listen import com.lambda.module.Module +import com.lambda.threading.runSafe +import com.lambda.util.Communication.log import com.lambda.util.Describable import com.lambda.util.NamedEnum +import net.minecraft.text.Text +import net.minecraft.util.Colors -@Suppress("unused") object ModuleNotifier : Module( name = "ModuleNotifier", description = "Notifies you when a module is enabled or disabled", @@ -33,4 +38,36 @@ object ModuleNotifier : Module( Chat("Chat", "Sends a message to chat when a module is toggled"), ActionBar("Action Bar", "Sends a message to the action bar when a module is toggled"),; } + + init { + listen { + runSafe { + logToTargets(Text.literal("on").withColor(Colors.GREEN)) + } + } + + listen { + runSafe { + logToTargets(Text.literal("off").withColor(Colors.RED)) + } + } + + listen { + runSafe { + val newState = if (it.newValue) "on" else "off" + val color = if (it.newValue) Colors.GREEN else Colors.RED + val message = Text.literal(newState).withColor(color) + logToTargets(message) + } + } + } + + private fun logToTargets(message: Text) { + if (notifyTarget.contains(NotifyTarget.Chat)) { + log(message, source = name) + } + if (notifyTarget.contains(NotifyTarget.ActionBar)) { + log(message, source = name, inGameOverlay = true) + } + } } \ No newline at end of file diff --git a/src/main/kotlin/com/lambda/util/Communication.kt b/src/main/kotlin/com/lambda/util/Communication.kt index d1fb1fcbf..04ba66ac2 100644 --- a/src/main/kotlin/com/lambda/util/Communication.kt +++ b/src/main/kotlin/com/lambda/util/Communication.kt @@ -117,26 +117,6 @@ object Communication { } } - /** - * Logs messages to the in game chat if available while keeping the message styles - */ - fun Any.log( - message: MutableText, - logLevel: LogLevel = LogLevel.Info, - source: String = "", - textSource: Text = Text.empty(), - inGameOverlay: Boolean = false - ) { - buildText { - text(this@log.source(logLevel, source, textSource)) - literal(message) - }.let { log -> - runSafeGameScheduled { - player.sendMessage(log, inGameOverlay) - } - } - } - private fun Any.source( logLevel: LogLevel, source: String = "", diff --git a/src/main/kotlin/com/lambda/util/text/TextDsl.kt b/src/main/kotlin/com/lambda/util/text/TextDsl.kt index c097058ec..6a8f3892e 100644 --- a/src/main/kotlin/com/lambda/util/text/TextDsl.kt +++ b/src/main/kotlin/com/lambda/util/text/TextDsl.kt @@ -80,11 +80,6 @@ class TextBuilder { this.text.append(style.applyTo(text)) } - @TextDsl - fun append(text: MutableText) { - this.text.append(text) - } - /** * Returns the [Text] result of this builder. */ @@ -125,17 +120,6 @@ fun TextBuilder.literal(value: String) { styleAndAppend(Text.literal(value)) } -/** - * Adds a literal text that is not re-styled by the builder's current style. - * - * @param value The text. - * @see StyleBuilder for action - */ -@TextDsl -fun TextBuilder.literal(value: MutableText) { - append(value) -} - /** * Adds a literal text. *