Skip to content

Commit

Permalink
Add Java API to receive events (Fix #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Aug 19, 2023
1 parent f4f3263 commit 2853cdc
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 13 deletions.
11 changes: 10 additions & 1 deletion buildSrc/src/main/kotlin/lavalink-jvm-module.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
kotlin("jvm")
id("org.jetbrains.dokka")
Expand All @@ -13,7 +15,6 @@ repositories {

kotlin {
explicitApi()
jvmToolchain(8)

sourceSets {
all {
Expand All @@ -27,6 +28,14 @@ kotlin {
}
}
}

compilerOptions {
jvmTarget = JvmTarget.JVM_1_8
}
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
}

tasks {
Expand Down
15 changes: 11 additions & 4 deletions buildSrc/src/main/kotlin/lavalink-module.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
kotlin("multiplatform")
id("org.jetbrains.dokka")
Expand All @@ -14,11 +16,10 @@ repositories {
kotlin {
explicitApi()

jvmToolchain(8)
jvm {
tasks {
withType<Test> {
useJUnitPlatform()
compilations.all {
compilerOptions.configure {
jvmTarget = JvmTarget.JVM_1_8
}
}
}
Expand All @@ -43,3 +44,9 @@ kotlin {
}
}
}

tasks {
withType<Test> {
useJUnitPlatform()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlinx.coroutines.launch
*
* @see EventSource.on
*/
public interface EventSource<T> {
public interface EventSource<T : Any> {
public val events: Flow<T>
public val coroutineScope: CoroutineScope
}
Expand All @@ -31,7 +31,7 @@ public interface EventSource<T> {
* The returned [Job] is a reference to the created coroutine, call [Job.cancel] to cancel the processing of any further
* events.
*/
public inline fun <T, reified E : T> EventSource<T>.on(
public inline fun <T : Any, reified E : T> EventSource<T>.on(
scope: CoroutineScope = coroutineScope,
noinline consumer: suspend E.() -> Unit
): Job =
Expand Down
13 changes: 9 additions & 4 deletions example/src/commonMain/kotlin/Lavakord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import dev.kord.core.on
import dev.kord.gateway.Intent
import dev.kord.gateway.PrivilegedIntent
import dev.kord.rest.builder.interaction.string
import dev.schlaubi.lavakord.audio.Event
import dev.schlaubi.lavakord.plugins.lavasrc.lavaSrcInfo
import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.audio.TrackEvent
import dev.schlaubi.lavakord.audio.on
import dev.schlaubi.lavakord.kord.getLink
import dev.schlaubi.lavakord.kord.lavakord
Expand Down Expand Up @@ -49,7 +50,7 @@ suspend fun main() {
}
}

lavalink.addNode("ws://localhost:2333", "youshallnotpass")
lavalink.addNode("wss://joachim.lava-hosts.schlaubi.net", "jcdrNb7Y8D8TTo6D64x&msiA3CzD")

kord.on<GuildChatInputCommandInteractionCreateEvent> {
val ack = interaction.deferPublicResponse()
Expand All @@ -60,8 +61,12 @@ suspend fun main() {
val followUpCreator = FollowupPermittingInteractionResponseBehavior(
interaction.applicationId, interaction.token, interaction.kord, interaction.supplier
)
player.on<Event> {
followUpCreator.createEphemeralFollowup { content = "Event: $this" }
player.on<TrackEvent> {
try {
followUpCreator.createEphemeralFollowup { content = "Event: ${this@on.track.lavaSrcInfo}" }
} catch (e: Exception) {
e.printStackTrace()
}
}
listenedGuilds.add(interaction.guildId)
}
Expand Down
3 changes: 3 additions & 0 deletions example/src/jvmMain/java/dev/schlaubi/lavakord/Javakord.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package dev.schlaubi.lavakord;

import dev.arbjerg.lavalink.protocol.v4.LoadResult;
import dev.schlaubi.lavakord.audio.TrackEndEvent;
import dev.schlaubi.lavakord.interop.JavaInterop;
import dev.schlaubi.lavakord.interop.JavaLavakord;
import dev.schlaubi.lavakord.interop.TrackUtil;
Expand Down
13 changes: 13 additions & 0 deletions java/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.vanniktech.maven.publish.JavadocJar
import com.vanniktech.maven.publish.KotlinJvm
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
`lavalink-jvm-module`
Expand All @@ -12,6 +13,18 @@ dependencies {
exclude(module = "opus-java")
}
implementation(libs.kotlinx.coroutines.jdk8)
implementation(libs.kotlinx.coroutines.jdk9)
}


kotlin {
compilerOptions {
jvmTarget = JvmTarget.JVM_11
}
}

java {
sourceCompatibility = JavaVersion.VERSION_11
}

mavenPublishing {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package dev.schlaubi.lavakord.interop

import dev.schlaubi.lavakord.audio.EventSource
import kotlinx.coroutines.jdk9.asPublisher
import mu.KotlinLogging
import java.util.concurrent.Flow
import java.util.concurrent.Flow.Subscriber
import java.util.function.Consumer

private val LOG = KotlinLogging.logger { }

/**
* Java equivalent of [EventSource].
*
* @param T the base event type
*
* @property suspendingEventSource the underlying delegate [EventSource]
* @property events All events received using a [Flow.Publisher]
*/
public interface JavaEventSource<T : Any> {
public val suspendingEventSource: EventSource<T>

public val events: Flow.Publisher<T>
get() = suspendingEventSource.events.asPublisher()

/**
* Creates an event handler which executes [Consumer] for every event of [eventType].
*/
public fun <E : T> on(eventType: Class<E>, handler: Consumer<E>) {
events.subscribe(object : Subscriber<T> {
override fun onSubscribe(subscription: Flow.Subscription) = Unit

override fun onError(throwable: Throwable) {
// This in theory should never happen
LOG.error(throwable) { "An error occurred whilst subscribing to events" }
}

override fun onComplete() = Unit

override fun onNext(item: T) {
if (eventType.isInstance(item)) {
handler.accept(eventType.cast(item))
}
}
})
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dev.schlaubi.lavakord.interop

import dev.schlaubi.lavakord.InsufficientPermissionException
import dev.schlaubi.lavakord.LavaKord
import dev.schlaubi.lavakord.audio.Event
import dev.schlaubi.lavakord.audio.EventSource
import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.audio.Link.State
import dev.schlaubi.lavakord.audio.Node
Expand All @@ -20,9 +22,11 @@ import kotlin.coroutines.CoroutineContext
* @property guildId the id of the Guild this [Link] is connected to
* @property lastChannelId the id of the last channel this Link is connected to
*/
public class JavaLink(internal val suspendingLink: Link) : CoroutineScope {
public class JavaLink(internal val suspendingLink: Link) : CoroutineScope, JavaEventSource<Event> {
override val coroutineContext: CoroutineContext
get() = suspendingLink.lavakord.coroutineContext
override val suspendingEventSource: EventSource<Event>
get() = suspendingLink.node
public val node: Node
get() = suspendingLink.node
public val player: JavaPlayer by lazy { JavaPlayer(suspendingLink.player) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.schlaubi.lavakord.interop

import dev.arbjerg.lavalink.protocol.v4.Track
import dev.schlaubi.lavakord.audio.Event
import dev.schlaubi.lavakord.audio.EventSource
import dev.schlaubi.lavakord.audio.player.Player
import kotlinx.coroutines.CoroutineScope
import java.time.Duration
Expand All @@ -16,9 +18,11 @@ import kotlin.coroutines.CoroutineContext
* @property volume the current volume of this player
* @property position the position of the current song the player is at (-1 if [playingTrack] is null)
*/
public class JavaPlayer(internal val suspendingPlayer: Player) : CoroutineScope {
public class JavaPlayer(internal val suspendingPlayer: Player) : CoroutineScope, JavaEventSource<Event> {
override val coroutineContext: CoroutineContext
get() = suspendingPlayer.coroutineScope.coroutineContext
override val suspendingEventSource: EventSource<Event>
get() = suspendingPlayer
public val playingTrack: Track?
get() = suspendingPlayer.playingTrack
public val paused: Boolean
Expand Down
11 changes: 11 additions & 0 deletions jda-java/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.vanniktech.maven.publish.JavadocJar
import com.vanniktech.maven.publish.KotlinJvm
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
`lavalink-jvm-module`
Expand All @@ -11,6 +12,16 @@ dependencies {
api(projects.java)
}

kotlin {
compilerOptions {
jvmTarget = JvmTarget.JVM_11
}
}

java {
sourceCompatibility = JavaVersion.VERSION_11
}

mavenPublishing {
configure(KotlinJvm(JavadocJar.Dokka("dokkaHtml")))
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fun VersionCatalogBuilder.kotlinx() {
val coroutines = version("coroutines", "1.7.1")
library("kotlinx-coroutines-core", "org.jetbrains.kotlinx", "kotlinx-coroutines-core").versionRef(coroutines)
library("kotlinx-coroutines-jdk8", "org.jetbrains.kotlinx", "kotlinx-coroutines-jdk8").versionRef(coroutines)
library("kotlinx-coroutines-jdk9", "org.jetbrains.kotlinx", "kotlinx-coroutines-jdk9").versionRef(coroutines)
library("kotlinx-coroutines-test", "org.jetbrains.kotlinx", "kotlinx-coroutines-test").versionRef(coroutines)
library("kotlinx-serialization-json", "org.jetbrains.kotlinx", "kotlinx-serialization-json").version("1.5.0")
library("kotlinx-datetime", "org.jetbrains.kotlinx", "kotlinx-datetime").version("0.4.0")
Expand Down

0 comments on commit 2853cdc

Please sign in to comment.