@@ -121,13 +121,60 @@ class SafeListener<T : Event>(
121121 alwaysListen : Boolean = false,
122122 noinline function : SafeContext .(T ) -> Unit = {},
123123 ): SafeListener <T > {
124- val listener = SafeListener <T >(priority, this , alwaysListen) { event -> function(event) }
124+ val listener = SafeListener <T >(priority, this , alwaysListen) { event ->
125+ function(event)
126+ }
125127
126128 EventFlow .syncListeners.subscribe(listener)
127129
128130 return listener
129131 }
130132
133+ /* *
134+ * Registers a new [SafeListener] for a generic [Event] type [T] within the context of a [Task].
135+ * The [function] is executed on the same thread where the [Event] was dispatched.
136+ * The [function] will only be executed when the context satisfies certain safety conditions.
137+ * These conditions are met when none of the following [SafeContext] properties are null:
138+ * - [SafeContext.world]
139+ * - [SafeContext.player]
140+ * - [SafeContext.interaction]
141+ * - [SafeContext.connection]
142+ *
143+ * Usage:
144+ * ```kotlin
145+ * myTask.listen<MyEvent> { event ->
146+ * player.sendMessage("Event received: $event")
147+ * }
148+ *
149+ * myTask.listen<MyEvent>(priority = 1) { event ->
150+ * player.sendMessage("Event received before the previous listener: $event")
151+ * }
152+ * ```
153+ *
154+ * @param T The type of the event to listen for.
155+ * This should be a subclass of Event.
156+ * @param priority The priority of the listener.
157+ * Listeners with higher priority will be executed first.
158+ * The Default value is 0.
159+ * @param alwaysListen If true, the listener will be executed even if it is muted. The Default value is false.
160+ * @param function The function to be executed when the event is posted.
161+ * This function should take a SafeContext and an event of type T as parameters.
162+ * @return The newly created and registered [SafeListener].
163+ */
164+ inline fun <reified T : Event > Task <* >.listen (
165+ priority : Int = 0,
166+ alwaysListen : Boolean = false,
167+ noinline function : SafeContext .(T ) -> Unit = {},
168+ ): SafeListener <T > {
169+ val listener = SafeListener <T >(priority, this , alwaysListen) { event ->
170+ function(event) // ToDo: run function always on game thread
171+ }
172+
173+ syncListeners.subscribe<T >(listener)
174+
175+ return listener
176+ }
177+
131178 /* *
132179 * This function registers a new [SafeListener] for a generic [Event] type [T].
133180 * The [transform] is executed on the same thread where the [Event] was dispatched.
@@ -183,51 +230,6 @@ class SafeListener<T : Event>(
183230 return pointer
184231 }
185232
186- /* *
187- * Registers a new [SafeListener] for a generic [Event] type [T] within the context of a [Task].
188- * The [function] is executed on the same thread where the [Event] was dispatched.
189- * The [function] will only be executed when the context satisfies certain safety conditions.
190- * These conditions are met when none of the following [SafeContext] properties are null:
191- * - [SafeContext.world]
192- * - [SafeContext.player]
193- * - [SafeContext.interaction]
194- * - [SafeContext.connection]
195- *
196- * Usage:
197- * ```kotlin
198- * myTask.listen<MyEvent> { event ->
199- * player.sendMessage("Event received: $event")
200- * }
201- *
202- * myTask.listen<MyEvent>(priority = 1) { event ->
203- * player.sendMessage("Event received before the previous listener: $event")
204- * }
205- * ```
206- *
207- * @param T The type of the event to listen for.
208- * This should be a subclass of Event.
209- * @param priority The priority of the listener.
210- * Listeners with higher priority will be executed first.
211- * The Default value is 0.
212- * @param alwaysListen If true, the listener will be executed even if it is muted. The Default value is false.
213- * @param function The function to be executed when the event is posted.
214- * This function should take a SafeContext and an event of type T as parameters.
215- * @return The newly created and registered [SafeListener].
216- */
217- inline fun <reified T : Event > Task <* >.listen (
218- priority : Int = 0,
219- alwaysListen : Boolean = false,
220- noinline function : SafeContext .(T ) -> Unit = {},
221- ): SafeListener <T > {
222- val listener = SafeListener <T >(priority, this , alwaysListen) { event ->
223- function(event) // ToDo: run function always on game thread
224- }
225-
226- syncListeners.subscribe<T >(listener)
227-
228- return listener
229- }
230-
231233 /* *
232234 * Registers a new [SafeListener] for a generic [Event] type [T].
233235 * The [function] is executed on a new thread running asynchronously to the game thread.
0 commit comments