Skip to content

Commit fe6e990

Browse files
committed
Muteable module listening
1 parent 17ef8c0 commit fe6e990

File tree

15 files changed

+209
-89
lines changed

15 files changed

+209
-89
lines changed

common/src/main/java/com/lambda/mixin/MinecraftClientMixin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.lambda.mixin;
22

33
import com.lambda.event.EventFlow;
4-
import com.lambda.event.events.GameEvent;
4+
import com.lambda.event.events.ClientEvent;
55
import com.lambda.event.events.TickEvent;
66
import net.minecraft.client.MinecraftClient;
77
import org.spongepowered.asm.mixin.Mixin;
@@ -23,14 +23,14 @@ void onTickPost(CallbackInfo ci) {
2323

2424
@Inject(at = @At(value = "INVOKE", target = "Lorg/slf4j/Logger;info(Ljava/lang/String;)V", shift = At.Shift.AFTER, remap = false), method = "stop")
2525
private void onShutdown(CallbackInfo ci) {
26-
EventFlow.post(GameEvent.Shutdown.INSTANCE);
26+
EventFlow.post(ClientEvent.Shutdown.INSTANCE);
2727
}
2828

2929
/**
3030
* Inject after the thread field is set so `ThreadExecutor#getThread` is available
3131
*/
3232
@Inject(at = @At(value = "FIELD", target = "Lnet/minecraft/client/MinecraftClient;thread:Ljava/lang/Thread;", shift = At.Shift.AFTER, ordinal = 0), method = "run")
3333
private void onStartup(CallbackInfo ci) {
34-
EventFlow.post(GameEvent.Startup.INSTANCE);
34+
EventFlow.post(ClientEvent.Startup.INSTANCE);
3535
}
3636
}

common/src/main/kotlin/com/lambda/Lambda.kt

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@ import com.google.gson.Gson
44
import com.google.gson.GsonBuilder
55
import com.lambda.config.serializer.BlockPosSerializer
66
import com.lambda.config.serializer.BlockSerializer
7-
import com.lambda.event.events.KeyPressEvent
8-
import com.lambda.event.listener.SafeListener.Companion.listener
97
import com.lambda.module.modules.BoringModule
10-
import com.lambda.module.modules.BoringModule2
11-
import com.lambda.task.tasks.HelloWorldTask
12-
import com.lambda.threading.taskContext
138
import net.minecraft.block.Block
149
import net.minecraft.client.MinecraftClient
1510
import net.minecraft.util.math.BlockPos
1611
import org.apache.logging.log4j.LogManager
1712
import org.apache.logging.log4j.Logger
18-
import org.lwjgl.glfw.GLFW
1913

2014
object Lambda {
2115
private const val MOD_NAME = "Lambda"
@@ -33,33 +27,32 @@ object Lambda {
3327

3428
init {
3529
BoringModule
36-
BoringModule2
3730

38-
listener<KeyPressEvent> {
39-
if (it.key != GLFW.GLFW_KEY_Z) {
40-
return@listener
41-
}
42-
43-
taskContext {
44-
HelloWorldTask()
45-
.withDelay(500L)
46-
.withTimeout(200L)
47-
.withRepeats(10)
48-
.withMaxAttempts(5)
49-
.onSuccess {
50-
LOG.info("Hello, World! Task completed")
51-
}.onRepeat { repeats ->
52-
LOG.info("Hello, World! Task $name repeated $repeats times")
53-
}.onTimeout {
54-
LOG.warn("Hello, World! Task $name timed out")
55-
HelloWorldTask().execute()
56-
}.onRetry {
57-
LOG.warn("Hello, World! Task $name retrying")
58-
}.onFailure { error ->
59-
LOG.error("Hello, World! Task failed", error)
60-
}.execute()
61-
}
62-
}
31+
// listener<KeyPressEvent> {
32+
// if (it.key != GLFW.GLFW_KEY_Z) {
33+
// return@listener
34+
// }
35+
//
36+
// taskContext {
37+
// HelloWorldTask()
38+
// .withDelay(500L)
39+
// .withTimeout(200L)
40+
// .withRepeats(10)
41+
// .withMaxAttempts(5)
42+
// .onSuccess {
43+
// LOG.info("Hello, World! Task completed")
44+
// }.onRepeat { repeats ->
45+
// LOG.info("Hello, World! Task $name repeated $repeats times")
46+
// }.onTimeout {
47+
// LOG.warn("Hello, World! Task $name timed out")
48+
// HelloWorldTask().execute()
49+
// }.onRetry {
50+
// LOG.warn("Hello, World! Task $name retrying")
51+
// }.onFailure { error ->
52+
// LOG.error("Hello, World! Task failed", error)
53+
// }.execute()
54+
// }
55+
// }
6356
}
6457

6558
fun initialize() {

common/src/main/kotlin/com/lambda/config/Configurable.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ import com.lambda.config.settings.comparable.BooleanSetting
1111
import com.lambda.config.settings.comparable.EnumSetting
1212
import com.lambda.config.settings.complex.BlockPosSetting
1313
import com.lambda.config.settings.complex.BlockSetting
14+
import com.lambda.config.settings.complex.KeyBindSetting
1415
import com.lambda.config.settings.numeric.*
16+
import com.lambda.util.KeyCode
1517
import com.lambda.util.Nameable
1618
import net.minecraft.block.Block
19+
import net.minecraft.client.option.KeyBinding
1720
import net.minecraft.util.math.BlockPos
1821

1922
/**
@@ -161,6 +164,15 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
161164
settings.add(it)
162165
}
163166

167+
fun setting(
168+
name: String,
169+
defaultValue: KeyCode,
170+
visibility: () -> Boolean = { true },
171+
description: String = ""
172+
) = KeyBindSetting(name, defaultValue, visibility, description).also {
173+
settings.add(it)
174+
}
175+
164176
fun setting(
165177
name: String,
166178
defaultValue: BlockPos,

common/src/main/kotlin/com/lambda/config/Configuration.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package com.lambda.config
33
import com.google.gson.JsonElement
44
import com.google.gson.JsonObject
55
import com.google.gson.JsonParser
6-
import com.lambda.Lambda
76
import com.lambda.Lambda.LOG
87
import com.lambda.Lambda.gson
8+
import com.lambda.event.EventFlow
99
import com.lambda.event.EventFlow.lambdaScope
10-
import com.lambda.event.events.GameEvent
10+
import com.lambda.event.events.ClientEvent
1111
import com.lambda.event.listener.UnsafeListener.Companion.unsafeListener
1212
import kotlinx.coroutines.Dispatchers
1313
import kotlinx.coroutines.launch
@@ -22,9 +22,9 @@ abstract class Configuration : Jsonable {
2222
get() = File("${primary.parent}/${primary.nameWithoutExtension}-backup.${primary.extension}")
2323

2424
init {
25-
unsafeListener<GameEvent.Startup> { tryLoad() }
25+
unsafeListener<ClientEvent.Startup> { tryLoad() }
2626

27-
unsafeListener<GameEvent.Shutdown> { trySave() }
27+
unsafeListener<ClientEvent.Shutdown> { trySave() }
2828
}
2929

3030
override fun toJson() =
@@ -63,24 +63,27 @@ abstract class Configuration : Jsonable {
6363
private fun tryLoad() {
6464
lambdaScope.launch(Dispatchers.IO) {
6565
runCatching { load(primary) }
66-
.onSuccess { LOG.info("$configName config loaded") }
66+
.onSuccess {
67+
LOG.info("$configName config loaded")
68+
EventFlow.post(ClientEvent.ConfigLoaded(this@Configuration))
69+
}
6770
.onFailure { LOG.error("Failed to load $configName config, loading backup", it) }
6871
.recoverCatching {
6972
runCatching { load(backup) }
7073
.onSuccess { LOG.info("$configName config loaded from backup") }
7174
.onFailure { LOG.error("Failed to load $configName config from backup, unrecoverable error", it) }
72-
.isSuccess
7375
}
74-
.isSuccess
7576
}
7677
}
7778

7879
private fun trySave() {
7980
lambdaScope.launch(Dispatchers.IO) {
8081
runCatching { save() }
81-
.onSuccess { LOG.info("$configName config saved") }
82+
.onSuccess {
83+
LOG.info("$configName config saved")
84+
EventFlow.post(ClientEvent.ConfigSaved(this@Configuration))
85+
}
8286
.onFailure { LOG.error("Failed to save $configName config", it) }
83-
.isSuccess
8487
}
8588
}
8689

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.lambda.config.settings.complex
2+
3+
import com.lambda.config.AbstractSetting
4+
import com.lambda.util.KeyCode
5+
6+
class KeyBindSetting(
7+
override val name: String,
8+
defaultValue: KeyCode,
9+
visibility: () -> Boolean,
10+
description: String
11+
) : AbstractSetting<KeyCode>(
12+
defaultValue,
13+
visibility,
14+
description
15+
)

common/src/main/kotlin/com/lambda/event/EventFlow.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,19 @@ object EventFlow {
7575

7676
private fun Event.executeListenerSynchronous() {
7777
syncListeners[this::class]?.forEach { listener ->
78+
if (listener.owner is Muteable
79+
&& (listener.owner as Muteable).isMuted
80+
&& !listener.alwaysListen) return
7881
if (this is ICancellable && this.isCanceled()) return
7982
listener.execute(this@executeListenerSynchronous)
8083
}
8184
}
8285

8386
private fun Event.executeListenerConcurrently() {
8487
concurrentListeners[this::class]?.forEach { listener ->
88+
if (listener.owner is Muteable
89+
&& (listener.owner as Muteable).isMuted
90+
&& !listener.alwaysListen) return
8591
if (this is ICancellable && this.isCanceled()) return
8692
runConcurrent {
8793
listener.execute(this@executeListenerConcurrently)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.lambda.event
2+
3+
interface Muteable {
4+
val isMuted: Boolean
5+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.lambda.event.events
2+
3+
import com.lambda.config.Configuration
4+
import com.lambda.event.Event
5+
import com.lambda.module.Module
6+
7+
8+
abstract class ClientEvent : Event {
9+
data object Shutdown : ClientEvent()
10+
data object Startup : ClientEvent()
11+
data class ModuleToggle(val module: Module) : ClientEvent()
12+
data class ModuleEnable(val module: Module) : ClientEvent()
13+
data class ModuleDisable(val module: Module) : ClientEvent()
14+
data class ConfigLoaded(val configuration: Configuration) : ClientEvent()
15+
data class ConfigSaved(val configuration: Configuration) : ClientEvent()
16+
}

common/src/main/kotlin/com/lambda/event/events/GameEvent.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

common/src/main/kotlin/com/lambda/event/listener/Listener.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.lambda.event.Event
55
abstract class Listener : Comparable<Listener> {
66
abstract val priority: Int
77
abstract val owner: Any // ToDo: Evaluate if this is even needed
8+
abstract val alwaysListen: Boolean
89

910
abstract fun execute(event: Event)
1011

0 commit comments

Comments
 (0)