Skip to content

Commit 846ecdb

Browse files
committed
Removed Returnables and cleaner event posing
1 parent d91b12f commit 846ecdb

File tree

16 files changed

+81
-77
lines changed

16 files changed

+81
-77
lines changed

common/src/main/java/com/lambda/mixin/entity/PlayerEntityMixin.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class PlayerEntityMixin {
1313
@Inject(method = "clipAtLedge", at = @At(value = "HEAD"), cancellable = true)
1414
private void injectSafeWalk(CallbackInfoReturnable<Boolean> cir) {
1515
MovementEvent.ClipAtLedge event = new MovementEvent.ClipAtLedge(cir.getReturnValueZ());
16-
EventFlow.post(event);
17-
cir.setReturnValue(event.getReturnValue());
16+
cir.setReturnValue(EventFlow.post(event).getClip());
1817
}
1918
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.lambda
33
import com.google.gson.Gson
44
import com.google.gson.GsonBuilder
55
import com.lambda.config.serializer.*
6+
import com.lambda.core.Loader
67
import com.lambda.module.tag.ModuleTag
78
import com.lambda.util.KeyCode
89
import net.minecraft.block.Block

common/src/main/kotlin/com/lambda/command/CommandManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.lambda.command
22

3-
import com.lambda.Loadable
3+
import com.lambda.core.Loadable
44
import com.lambda.brigadier.CommandException
55
import com.lambda.brigadier.register
66
import com.lambda.config.Configurable

common/src/main/kotlin/com/lambda/command/commands/ModuleCommand.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ object ModuleCommand : LambdaCommand {
106106
literal("$name already ")
107107
literal(if (module.isEnabled) "enabled" else "disabled")
108108
}
109-
})
109+
}, "")
110110
return@runSafe success()
111111
}
112112

common/src/main/kotlin/com/lambda/Loadable.kt renamed to common/src/main/kotlin/com/lambda/core/Loadable.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.lambda
1+
package com.lambda.core
22

33
interface Loadable {
44
fun load() = this::class.simpleName?.let { "Loaded $it" } ?: "Loaded"

common/src/main/kotlin/com/lambda/Loader.kt renamed to common/src/main/kotlin/com/lambda/core/Loader.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package com.lambda
1+
package com.lambda.core
22

3+
import com.lambda.Lambda
34
import com.lambda.Lambda.LOG
45
import com.lambda.command.CommandManager
6+
import com.lambda.interaction.PlayerPacketManager
57
import com.lambda.interaction.RotationManager
68
import com.lambda.module.ModuleRegistry
79
import kotlin.system.measureTimeMillis
@@ -10,7 +12,8 @@ object Loader {
1012
private val loadables = listOf(
1113
ModuleRegistry,
1214
CommandManager,
13-
RotationManager
15+
RotationManager,
16+
PlayerPacketManager
1417
)
1518

1619
fun initialize() {

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

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.lambda.event
22

33
import com.lambda.event.callback.ICancellable
4-
import com.lambda.event.callback.Returnable
54
import com.lambda.event.listener.Listener
65
import com.lambda.threading.runConcurrent
76
import kotlinx.coroutines.CoroutineScope
@@ -13,6 +12,13 @@ import kotlinx.coroutines.flow.MutableSharedFlow
1312
import kotlinx.coroutines.flow.filter
1413
import kotlinx.coroutines.flow.filterNot
1514

15+
16+
/**
17+
* [EventFlow] is an object that manages the flow ([MutableSharedFlow]) of [Event]s in the application.
18+
* It provides methods to post [Event]s to both synchronous and asynchronous [Listener]s.
19+
*
20+
* The [EventFlow] also provides methods to unsubscribe from event flows for a specific event type.
21+
*/
1622
object EventFlow {
1723
/**
1824
* [lambdaScope] is a [CoroutineScope] which is used to launch coroutines.
@@ -56,52 +62,46 @@ object EventFlow {
5662
* An instant callback ([CallbackEvent]) can only be achieved by synchronous listening objects
5763
* as the concurrent listener will be executed "later".
5864
*
59-
* @param event The [Event] to be posted to the event flow.
65+
* CAUTION: The returned [Event] may have not yet been processed by concurrent listeners.
66+
*
67+
* @param E The type of the event to be posted. This should be a subclass of Event.
68+
* @receiver The [Event] to be posted to the event flow.
6069
*/
61-
@JvmStatic fun post(event: Event) {
62-
concurrentFlow.tryEmit(event)
63-
event.executeListenerSynchronous()
70+
@JvmStatic fun <E : Event> E.post(): E {
71+
concurrentFlow.tryEmit(this)
72+
executeListenerSynchronous()
73+
return this@post
6474
}
6575

6676
/**
67-
* Posts a [cancellable] [Event] to the event flow and returns the [Event].
77+
* Posts an [Event] to the event flow and then applies the given [process] function to the event.
6878
*
69-
* This function is a variant of the [post] function specifically for [ICancellable] [Event]s.
70-
* It posts the [Event] to the event flow,
71-
* runs it through the synchronous set of listeners ([syncListeners]), and then returns the [Event].
72-
* This is useful as [ICancellable] [Event]s are often checked after being processed by the [Listener]s.
79+
* This function first posts the event to the event flow by calling the [post] function.
80+
* After the event has been posted, it applies the [process] function to the event.
7381
*
74-
* The returned event is guaranteed to be of the same type as the input,
75-
* thanks to the type parameter [E] which is a subtype of both [Event] and [ICancellable].
82+
* CAUTION: The processed [Event] may have not yet been processed by concurrent listeners.
7683
*
77-
* @param cancellable The cancellable [Event] to be posted to the event flow.
78-
* @return The same [ICancellable] [Event] after being processed by the [Listener]s.
84+
* @param E The type of the event to be posted. This should be a subclass of Event.
85+
* @param process A function to be applied to the event after it has been posted.
7986
*/
80-
@JvmStatic fun <E> post(cancellable: E) : E where E : Event, E : ICancellable {
81-
post(cancellable as Event)
82-
return cancellable
83-
}
84-
85-
@JvmStatic fun <E> post(cancellable: E, process: E.() -> Unit) where E : Event, E : ICancellable {
86-
post(cancellable)
87-
cancellable.process()
88-
}
89-
90-
@JvmStatic fun <E> postChecked(cancellable: E, process: E.() -> Unit) where E : Event, E : ICancellable {
91-
post(cancellable)
92-
if (!cancellable.isCanceled()) cancellable.process()
87+
@JvmStatic fun <E : Event> E.post(process: E.() -> Unit) {
88+
post()
89+
process()
9390
}
9491

95-
@JvmStatic fun <E, R> postR(returnable: E): E where E : Returnable<R>, E : Event {
96-
post(returnable as Event)
97-
return returnable
98-
}
99-
100-
@JvmStatic fun <R, E> post(
101-
returnable: E, process: E.() -> Unit
102-
) where E : Returnable<R>, E : Event {
103-
post(returnable as Event)
104-
returnable.process()
92+
/**
93+
* Posts an [Event] to the event flow and then applies the given [process] function to the event
94+
* if it is not canceled.
95+
* If not it applies the [process] function to the event.
96+
*
97+
* CAUTION: The processed [Event] may have not yet been processed by concurrent listeners.
98+
*
99+
* @param E The type of the event to be posted. This should be a subclass of [Event] and implement [ICancellable].
100+
* @param process A function to be applied to the event after it has been posted if the [Event] is not canceled.
101+
*/
102+
@JvmStatic fun <E> E.postChecked(process: E.() -> Unit) where E : Event, E : ICancellable {
103+
post()
104+
if (!isCanceled()) process()
105105
}
106106

107107
/**

common/src/main/kotlin/com/lambda/event/callback/IReturnable.kt

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

common/src/main/kotlin/com/lambda/event/callback/Returnable.kt

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

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package com.lambda.event.events
33
import com.lambda.event.Event
44
import com.lambda.event.callback.Cancellable
55
import com.lambda.event.callback.ICancellable
6-
import com.lambda.event.callback.IReturnable
7-
import com.lambda.event.callback.Returnable
86
import net.minecraft.client.input.Input
97

108
abstract class MovementEvent : Event {
@@ -17,8 +15,8 @@ abstract class MovementEvent : Event {
1715
) : MovementEvent(), ICancellable by Cancellable()
1816

1917
class ClipAtLedge(
20-
var defaultValue: Boolean,
21-
) : MovementEvent(), IReturnable<Boolean> by Returnable(defaultValue)
18+
var clip: Boolean,
19+
) : MovementEvent()
2220

2321
class Jump(var height: Double) : MovementEvent(), ICancellable by Cancellable()
2422
class SlowDown : Event, ICancellable by Cancellable()

0 commit comments

Comments
 (0)