-
Notifications
You must be signed in to change notification settings - Fork 22
feat(ModuleNotifier): Add Module to get feedback in chat when modules toggle #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 1.21.11
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| 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() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,11 +28,13 @@ 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 | ||
|
|
@@ -150,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() } | ||
|
|
@@ -162,14 +168,23 @@ 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() { | ||
| if (ModuleEvent.Toggle(this@Module, !isEnabled).post().isCanceled()) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enabled and Disabled dont seem to get called when the module is toggled. The toggle event should be posted first, then the corresponding event for the new value |
||
| return | ||
| } | ||
| isEnabled = !isEnabled | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * 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 <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| 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 | ||
|
|
||
| object ModuleNotifier : Module( | ||
| name = "ModuleNotifier", | ||
| description = "Notifies you when a module is enabled or disabled", | ||
| tag = com.lambda.module.tag.ModuleTag.CLIENT | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use an import instead of the full qualified reference |
||
| ) { | ||
| var notifyTarget by setting("Notify Target", setOf<NotifyTarget>(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"),; | ||
| } | ||
|
|
||
| init { | ||
| listen<ModuleEvent.Enabled> { | ||
| runSafe { | ||
| logToTargets(Text.literal("on").withColor(Colors.GREEN)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have a text dsl builder for stuff like this |
||
| } | ||
| } | ||
|
|
||
| listen<ModuleEvent.Disabled> { | ||
| runSafe { | ||
| logToTargets(Text.literal("off").withColor(Colors.RED)) | ||
| } | ||
| } | ||
|
|
||
| listen<ModuleEvent.Toggle> { | ||
| 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)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could use NotifyTarget.Chat in notifyTarget |
||
| log(message, source = name) | ||
| } | ||
| if (notifyTarget.contains(NotifyTarget.ActionBar)) { | ||
| log(message, source = name, inGameOverlay = true) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused import |
||
| import net.minecraft.text.Text | ||
| import java.awt.Color | ||
| import java.time.LocalDateTime | ||
|
|
@@ -96,18 +97,22 @@ object Communication { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Logs messages to the in game chat if available while stripping the message styles | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems to be outdated now |
||
| */ | ||
| 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) | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a styling choice but i typically omit the brackets for early returns and small one-liner if blocks. The return can just be placed at the end of the if statement without the brackets, or just on the next line