Skip to content

Commit b069ac3

Browse files
committed
add another unsafeListener builder which takes a KClass. Update Velocity to use regular listen, and update listener documentation
1 parent b4b5877 commit b069ac3

3 files changed

Lines changed: 53 additions & 14 deletions

File tree

src/main/kotlin/com/lambda/event/listener/SafeListener.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import com.lambda.event.Event
2222
import com.lambda.event.EventFlow
2323
import com.lambda.event.Muteable
2424
import com.lambda.threading.runConcurrent
25-
import com.lambda.threading.runGameScheduled
2625
import com.lambda.threading.runSafe
2726
import com.lambda.util.Pointer
2827
import com.lambda.util.collections.updatable
@@ -109,13 +108,13 @@ class SafeListener<T : Event>(
109108
* player.sendMessage("Event received: $event")
110109
* }
111110
*
112-
* listen<MyEvent>(priority = 1) { event ->
111+
* listen<MyEvent>(priority = { 1 }) { event ->
113112
* player.sendMessage("Event received before the previous listener: $event")
114113
* }
115114
* ```
116115
*
117116
* @param T The type of the event to listen for. This should be a subclass of Event.
118-
* @param priority The priority of the listener. Listeners with higher priority will be executed first. The Default value is 0.
117+
* @param priority The priority of the listener. Listeners with higher priority will be executed first. The Default value is { 0 }.
119118
* @param alwaysListen If true, the listener will be executed even if it is muted. The Default value is false.
120119
* @param function The function to be executed when the event is posted. This function should take a SafeContext and an event of type T as parameters.
121120
* @return The newly created and registered [SafeListener].
@@ -150,14 +149,14 @@ class SafeListener<T : Event>(
150149
* player.sendMessage("Event received: $event")
151150
* }
152151
*
153-
* listen(MyEvent::class, priority = 1) { event ->
152+
* listen(MyEvent::class, priority = { 1 }) { event ->
154153
* player.sendMessage("Event received before the previous listener: $event")
155154
* }
156155
* ```
157156
*
158157
* @param kClass The KClass instance of covariant type [T] used to circumvent type erasure.
159158
* @param T The type of the event to listen for. This should be a subclass of Event.
160-
* @param priority The priority of the listener. Listeners with higher priority will be executed first. The Default value is 0.
159+
* @param priority The priority of the listener. Listeners with higher priority will be executed first. The Default value is { 0 }.
161160
* @param alwaysListen If true, the listener will be executed even if it is muted. The Default value is false.
162161
* @param function The function to be executed when the event is posted. This function should take a SafeContext and an event of type T as parameters.
163162
* @return The newly created and registered [SafeListener].
@@ -199,7 +198,7 @@ class SafeListener<T : Event>(
199198
* ```
200199
*
201200
* @param T The type of the event to listen for. This should be a subclass of Event.
202-
* @param priority The priority of the listener. Listeners with higher priority will be executed first. The Default value is 0.
201+
* @param priority The priority of the listener. Listeners with higher priority will be executed first. The Default value is { 0 }.
203202
* @param alwaysListen If true, the listener will be executed even if it is muted. The Default value is false.
204203
* @return The newly created and registered [SafeListener].
205204
*/
@@ -241,12 +240,12 @@ class SafeListener<T : Event>(
241240
* // no safe access to player or world
242241
* }
243242
*
244-
* listenConcurrently<MyEvent>(priority = 1) { event ->
243+
* listenConcurrently<MyEvent>(priority = { 1 }) { event ->
245244
* println("Concurrent event received before the previous listener: $event")
246245
* }
247246
* ```
248247
* @param T The type of the event to listen for. This should be a subclass of Event.
249-
* @param priority The priority of the listener. Listeners with higher priority will be executed first. The Default value is 0.
248+
* @param priority The priority of the listener. Listeners with higher priority will be executed first. The Default value is { 0 }.
250249
* @param alwaysListen If true, the listener will be executed even if it is muted. The Default value is false.
251250
* @param function The function to be executed when the event is posted. This function should take a SafeContext and an event of type T as parameters.
252251
* @return The newly created and registered [SafeListener].

src/main/kotlin/com/lambda/event/listener/UnsafeListener.kt

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.lambda.event.listener
1919

20+
import com.lambda.context.SafeContext
2021
import com.lambda.event.Event
2122
import com.lambda.event.EventFlow
2223
import com.lambda.event.Muteable
@@ -31,6 +32,7 @@ import kotlinx.coroutines.CoroutineDispatcher
3132
import kotlinx.coroutines.Dispatchers
3233
import kotlin.properties.ReadOnlyProperty
3334
import kotlin.properties.ReadWriteProperty
35+
import kotlin.reflect.KClass
3436
import kotlin.reflect.KProperty
3537

3638
/**
@@ -98,7 +100,7 @@ class UnsafeListener<T : Event>(
98100
* // no safe access to player or world
99101
* }
100102
*
101-
* listenUnsafe<MyEvent>(priority = 1) { event ->
103+
* listenUnsafe<MyEvent>(priority = { 1 }) { event ->
102104
* println("Unsafe event received before the previous listener: $event")
103105
* }
104106
* ```
@@ -121,6 +123,45 @@ class UnsafeListener<T : Event>(
121123
return listener
122124
}
123125

126+
/**
127+
* This function registers a new [UnsafeListener] for a generic [Event] type [T] with the given [KClass] instance to circumvent type erasure.
128+
* The [function] is executed on the same thread where the [Event] was dispatched.
129+
* The execution of the [function] is independent of the safety conditions of the context.
130+
* Use this function when you need to listen to an [Event] in a context that is not in-game.
131+
* For only in-game related contexts, use the [SafeListener.listen] function instead.
132+
*
133+
* Usage:
134+
* ```kotlin
135+
* listenUnsafe(MyEvent::class) { event ->
136+
* println("Unsafe event received: $event")
137+
* // no safe access to player or world
138+
* }
139+
*
140+
* listenUnsafe(MyEvent::class, priority = { 1 }) { event ->
141+
* println("Unsafe event received before the previous listener: $event")
142+
* }
143+
* ```
144+
*
145+
* @param kClass The KClass instance of covariant type [T] used to circumvent type erasure.
146+
* @param T The type of the event to listen for. This should be a subclass of Event.
147+
* @param priority The priority of the listener. Listeners with higher priority will be executed first.
148+
* @param alwaysListen If true, the listener will be executed even if it is muted.
149+
* @param function The function to be executed when the event is posted. This function should take an event of type T as a parameter.
150+
* @return The newly created and registered [UnsafeListener].
151+
*/
152+
fun <T : Event> Any.listenUnsafe(
153+
kClass: KClass<out T>,
154+
priority: () -> Int = ownerPriorityOr0Getter,
155+
alwaysListen: Boolean = false,
156+
function: (T) -> Unit = {},
157+
): UnsafeListener<T> {
158+
val listener = UnsafeListener<T>(priority, this, alwaysListen) { function(it) }
159+
160+
EventFlow.syncListeners.subscribe(kClass, listener)
161+
162+
return listener
163+
}
164+
124165
/**
125166
* Registers a new [UnsafeListener] for a generic [Event] type [T].
126167
* The [function] is executed only once when the [Event] is dispatched.
@@ -183,12 +224,12 @@ class UnsafeListener<T : Event>(
183224
* // no safe access to player or world
184225
* }
185226
*
186-
* listenUnsafeConcurrently<MyEvent>(priority = 1) { event ->
227+
* listenUnsafeConcurrently<MyEvent>(priority = { 1 }) { event ->
187228
* println("Concurrent event received before the previous listener: $event")
188229
* }
189230
* ```
190231
* @param T The type of the event to listen for. This should be a subclass of Event.
191-
* @param priority The priority of the listener. Listeners with higher priority will be executed first. The Default value is 0.
232+
* @param priority The priority of the listener. Listeners with higher priority will be executed first. The Default value is { 0 }.
192233
* @param alwaysListen If true, the listener will be executed even if it is muted. The Default value is false.
193234
* @param function The function to be executed when the event is posted. This function should take a SafeContext and an event of type T as parameters.
194235
* @return The newly created and registered [UnsafeListener].

src/main/kotlin/com/lambda/module/modules/movement/Velocity.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717

1818
package com.lambda.module.modules.movement
1919

20-
import com.lambda.Lambda.mc
2120
import com.lambda.event.events.PacketEvent
22-
import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe
21+
import com.lambda.event.listener.SafeListener.Companion.listen
2322
import com.lambda.module.Module
2423
import com.lambda.module.tag.ModuleTag
2524
import net.minecraft.network.packet.s2c.play.EntityVelocityUpdateS2CPacket
@@ -34,7 +33,7 @@ object Velocity : Module(
3433
@JvmStatic val explosion by setting("Explosion", true, "Prevents the player from taking knockback from explosions")
3534

3635
init {
37-
listenUnsafe <PacketEvent.Receive.Pre> { event ->
36+
listen<PacketEvent.Receive.Pre> { event ->
3837
when (event.packet) {
3938
is EntityVelocityUpdateS2CPacket if (knockback && event.packet.entityId == mc.player?.id) -> event.cancel()
4039
}

0 commit comments

Comments
 (0)