This repository was archived by the owner on Dec 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathAbstractModule.kt
More file actions
151 lines (125 loc) · 5.06 KB
/
AbstractModule.kt
File metadata and controls
151 lines (125 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.lambda.client.module
import com.lambda.client.commons.interfaces.Alias
import com.lambda.client.commons.interfaces.Nameable
import com.lambda.client.event.LambdaEventBus
import com.lambda.client.event.events.ModuleToggleEvent
import com.lambda.client.gui.clickgui.LambdaClickGui
import com.lambda.client.manager.managers.NotificationManager
import com.lambda.client.module.modules.client.ClickGUI
import com.lambda.client.setting.configs.NameableConfig
import com.lambda.client.setting.settings.AbstractSetting
import com.lambda.client.setting.settings.SettingRegister
import com.lambda.client.setting.settings.impl.number.IntegerSetting
import com.lambda.client.setting.settings.impl.other.BindSetting
import com.lambda.client.setting.settings.impl.primitive.BooleanSetting
import com.lambda.client.util.Bind
import com.lambda.client.util.notifications.NotificationType
import net.minecraft.client.Minecraft
@Suppress("UNCHECKED_CAST")
abstract class AbstractModule(
override val name: String,
override val alias: Array<String> = emptyArray(),
val category: Category,
val description: String,
val modulePriority: Int = -1,
var alwaysListening: Boolean = false,
val showOnArray: Boolean = true,
val alwaysEnabled: Boolean = false,
val enabledByDefault: Boolean = false,
private val config: NameableConfig<out Nameable>
) : Nameable, Alias, SettingRegister<Nameable> by config as NameableConfig<Nameable> {
val bind = BindSetting("Bind", Bind(), { !alwaysEnabled }).also(::addSetting)
private val enabled = BooleanSetting("Enabled", false, { false }).also(::addSetting)
private val visible = BooleanSetting("Visible", showOnArray).also(::addSetting)
private val default = BooleanSetting("Default", false, { settingList.isNotEmpty() }).also(::addSetting)
val priorityForGui = IntegerSetting("Priority In GUI", 0, 0..1000, 50, { ClickGUI.sortBy.value == ClickGUI.SortByOptions.CUSTOM }, fineStep = 1).also(::addSetting)
val clicks = IntegerSetting("Clicks", 0, 0..Int.MAX_VALUE, 1, { false }).also(::addSetting) // Not nice, however easiest way to save it.
val fullSettingList get() = (config as NameableConfig<Nameable>).getSettings(this)
val settingList: List<AbstractSetting<*>> get() = fullSettingList.filter { it != bind && it != enabled && it != visible && it != default && it != clicks }
val isEnabled: Boolean get() = enabled.value || alwaysEnabled
val isDisabled: Boolean get() = !isEnabled
var isPaused = false
val chatName: String get() = "[${name}]"
val isVisible: Boolean get() = visible.value
private fun addSetting(setting: AbstractSetting<*>) {
(config as NameableConfig<Nameable>).addSettingToConfig(this, setting)
}
internal fun postInit() {
enabled.value = enabledByDefault || alwaysEnabled
if (alwaysListening) LambdaEventBus.subscribe(this)
}
fun toggle() {
enabled.value = !enabled.value
isPaused = false
if (enabled.value) clicks.value++
if (this.category != Category.CLIENT) {
NotificationManager.registerNotification("$name ${if (enabled.value) "Enabled" else "Disabled"}", NotificationType.INFO)
}
}
fun enable() {
clicks.value++
enabled.value = true
}
fun disable() {
enabled.value = false
}
fun pause() {
isPaused = true
LambdaEventBus.unsubscribe(this)
}
fun unpause() {
isPaused = false
LambdaEventBus.subscribe(this)
}
open fun isActive(): Boolean {
return isEnabled || alwaysListening
}
open fun getHudInfo(): String {
return ""
}
protected fun onEnable(block: (Boolean) -> Unit) {
enabled.valueListeners.add { _, input ->
if (input) {
block(true)
}
}
}
protected fun onDisable(block: (Boolean) -> Unit) {
enabled.valueListeners.add { _, input ->
if (!input) {
block(false)
}
}
}
protected fun onToggle(block: (Boolean) -> Unit) {
enabled.valueListeners.add { _, input ->
block(input)
}
}
init {
enabled.consumers.add { prev, input ->
val enabled = alwaysEnabled || input
if (prev != input && !alwaysEnabled) {
LambdaEventBus.post(ModuleToggleEvent(this))
}
if (enabled || alwaysListening) {
LambdaEventBus.subscribe(this)
} else {
LambdaEventBus.unsubscribe(this)
}
enabled
}
default.valueListeners.add { _, it ->
if (it) {
settingList.forEach { it.resetValue() }
default.value = false
NotificationManager.registerNotification("$chatName Set to defaults!")
}
}
priorityForGui.listeners.add { LambdaClickGui.reorderModules() }
// clicks is deliberately not re-organised when changed.
}
protected companion object {
val mc: Minecraft = Minecraft.getMinecraft()
}
}