Skip to content

Commit a863267

Browse files
committed
Discord and Network refactor
1 parent d78be35 commit a863267

File tree

20 files changed

+330
-245
lines changed

20 files changed

+330
-245
lines changed

build.gradle.kts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ subprojects {
8585

8686
if (path == ":common") return@subprojects
8787

88+
loom.runs {
89+
all {
90+
property("lambda.dev", "youtu.be/RYnFIRc0k6E")
91+
}
92+
}
93+
8894
tasks {
8995
register<Exec>("renderDoc") {
9096
val javaHome = Jvm.current().javaHome
@@ -144,7 +150,7 @@ allprojects {
144150
tasks {
145151
compileKotlin {
146152
compilerOptions {
147-
jvmTarget.set(JvmTarget.JVM_17)
153+
jvmTarget = JvmTarget.JVM_17
148154
}
149155
}
150156
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ object Lambda {
5252
@JvmStatic
5353
val mc: MinecraftClient by lazy { MinecraftClient.getInstance() }
5454

55+
val isDebug = System.getProperty("lambda.dev") != null
56+
5557
val gson: Gson = GsonBuilder()
5658
.setPrettyPrinting()
5759
.registerTypeAdapter(ModuleTag::class.java, ModuleTagSerializer)

common/src/main/kotlin/com/lambda/command/commands/RpcCommand.kt renamed to common/src/main/kotlin/com/lambda/command/commands/DiscordCommand.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ import com.lambda.brigadier.argument.word
2323
import com.lambda.brigadier.execute
2424
import com.lambda.brigadier.required
2525
import com.lambda.command.LambdaCommand
26-
import com.lambda.module.modules.client.DiscordRPC
26+
import com.lambda.module.modules.client.Discord
2727
import com.lambda.util.extension.CommandBuilder
2828

29-
object RpcCommand : LambdaCommand(
30-
name = "rpc",
31-
description = "Discord Rich Presence commands.",
32-
usage = "rpc <join [id] | accept>"
29+
object DiscordCommand : LambdaCommand(
30+
name = "discord",
31+
description = "Discord Rich Presence commands",
32+
usage = "rpc <join [id]>"
3333
) {
3434
override fun CommandBuilder.create() {
3535
required(literal("join")) {
3636
required(word("id")) { id ->
3737
execute {
38-
DiscordRPC.join(id().value())
38+
Discord.join(id().value())
3939
}
4040
}
4141
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ class SafeListener<T : Event>(
132132

133133
/**
134134
* This function registers a new [SafeListener] for a generic [Event] type [T].
135-
* The [transform] is executed on the same thread where the [Event] was dispatched.
136-
* The [transform] will only be executed when the context satisfies certain safety conditions.
135+
* The [predicate] is executed on the same thread where the [Event] was dispatched.
136+
* The [predicate] will only be executed when the context satisfies certain safety conditions.
137137
* These conditions are met when none of the following [SafeContext] properties are null:
138138
* - [SafeContext.world]
139139
* - [SafeContext.player]
@@ -142,7 +142,7 @@ class SafeListener<T : Event>(
142142
*
143143
* This typically occurs when the user is in-game.
144144
*
145-
* After the [transform] is executed once, the [SafeListener] will be automatically unsubscribed.
145+
* After the [predicate] is executed once, the [SafeListener] will be automatically unsubscribed.
146146
*
147147
* Usage:
148148
* ```kotlin
@@ -156,24 +156,20 @@ class SafeListener<T : Event>(
156156
* @param T The type of the event to listen for. This should be a subclass of Event.
157157
* @param priority The priority of the listener. Listeners with higher priority will be executed first. The Default value is 0.
158158
* @param alwaysListen If true, the listener will be executed even if it is muted. The Default value is false.
159-
* @param transform The function used to transform the event into a value.
160159
* @return The newly created and registered [SafeListener].
161160
*/
162-
inline fun <reified T : Event, reified E> Any.listenOnce(
161+
inline fun <reified T : Event> Any.listenOnce(
163162
priority: Int = 0,
164163
alwaysListen: Boolean = false,
165164
noinline predicate: SafeContext.(T) -> Boolean = { true },
166-
noinline transform: SafeContext.(T) -> E? = { null },
167-
): ReadWriteProperty<Any?, E?> {
168-
val pointer = Pointer<E>()
165+
): ReadWriteProperty<Any?, T?> {
166+
val pointer = Pointer<T>()
169167

170168
val destroyable by selfReference<SafeListener<T>> {
171169
SafeListener(priority, this@listenOnce, alwaysListen) { event ->
172-
pointer.value = transform(event)
170+
pointer.value = event
173171

174-
if (predicate(event) &&
175-
pointer.value != null
176-
) {
172+
if (predicate(event)) {
177173
val self by this@selfReference
178174
EventFlow.syncListeners.unsubscribe(self)
179175
}

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ import com.lambda.context.SafeContext
2121
import com.lambda.event.Event
2222
import com.lambda.event.EventFlow
2323
import com.lambda.event.Muteable
24+
import com.lambda.threading.runConcurrent
2425
import com.lambda.util.Pointer
2526
import com.lambda.util.selfReference
27+
import kotlinx.coroutines.CoroutineDispatcher
28+
import kotlinx.coroutines.Dispatchers
2629
import kotlin.properties.ReadOnlyProperty
2730
import kotlin.properties.ReadWriteProperty
2831
import kotlin.reflect.KProperty
@@ -136,24 +139,20 @@ class UnsafeListener<T : Event>(
136139
* @param T The type of the event to listen for. This should be a subclass of Event.
137140
* @param priority The priority of the listener. Listeners with higher priority will be executed first.
138141
* @param alwaysListen If true, the listener will be executed even if it is muted.
139-
* @param transform The function used to transform the event into a value.
140142
* @return The newly created and registered [UnsafeListener].
141143
*/
142-
inline fun <reified T : Event, reified E> Any.listenOnceUnsafe(
144+
inline fun <reified T : Event> Any.listenOnceUnsafe(
143145
priority: Int = 0,
144146
alwaysListen: Boolean = false,
145-
noinline transform: (T) -> E? = { null },
146-
noinline predicate: (T) -> Boolean = { true },
147-
): ReadWriteProperty<Any?, E?> {
148-
val pointer = Pointer<E>()
147+
noinline function: (T) -> Boolean = { true },
148+
): ReadWriteProperty<Any?, T?> {
149+
val pointer = Pointer<T>()
149150

150151
val destroyable by selfReference<UnsafeListener<T>> {
151152
UnsafeListener(priority, this@listenOnceUnsafe, alwaysListen) { event ->
152-
pointer.value = transform(event)
153+
pointer.value = event
153154

154-
if (predicate(event) &&
155-
pointer.value != null
156-
) {
155+
if (function(event)) {
157156
val self by this@selfReference
158157
EventFlow.syncListeners.unsubscribe(self)
159158
}
@@ -194,10 +193,13 @@ class UnsafeListener<T : Event>(
194193
inline fun <reified T : Event> Any.listenUnsafeConcurrently(
195194
priority: Int = 0,
196195
alwaysListen: Boolean = false,
197-
noinline function: (T) -> Unit = {},
196+
scheduler: CoroutineDispatcher = Dispatchers.Default,
197+
noinline function: suspend (T) -> Unit = {},
198198
): UnsafeListener<T> {
199199
val listener = UnsafeListener<T>(priority, this, alwaysListen) { event ->
200-
function(event)
200+
runConcurrent(scheduler) {
201+
function(event)
202+
}
201203
}
202204

203205
EventFlow.concurrentListeners.subscribe<T>(listener)

common/src/main/kotlin/com/lambda/http/api/rpc/v1/endpoints/Login.kt

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

0 commit comments

Comments
 (0)