Skip to content

Commit

Permalink
Add cachedPlayer getter on the link
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Feb 1, 2024
1 parent 4ca714f commit 9f5cdb1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
44 changes: 21 additions & 23 deletions src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,27 +141,6 @@ class LavalinkNode(
}
}

/**
* Gets a player from the local cache. If the player is not in the local cache, it will be created.
*
* @param guildId The guild id of the player.
*
* @return The local player. This player may not exist on the [LavalinkNode] yet.
*/
fun getOrAssumePlayer(guildId: Long): LavalinkPlayer {
val cachedPlayer = playerCache[guildId]

if (cachedPlayer == null) {
val newPlayer = newPlayer(this, guildId.toString())

playerCache[guildId] = newPlayer

return newPlayer
}

return cachedPlayer
}

fun updatePlayer(guildId: Long, updateConsumer: Consumer<PlayerUpdateBuilder>): Mono<LavalinkPlayer> {
val update = createOrUpdatePlayer(guildId)

Expand Down Expand Up @@ -411,11 +390,30 @@ class LavalinkNode(
* Get a [LavalinkPlayer] from the player cache.
*
* @return The cached player, or null if not yet cached.
*
* @see [getOrAssumePlayer]
*/
fun getCachedPlayer(guildId: Long): LavalinkPlayer? = playerCache[guildId]

/**
* Gets a player from the local cache. If the player is not in the local cache, it will be created.
*
* @param guildId The guild id of the player.
*
* @return The local player. This player may not exist on the [LavalinkNode] yet.
*/
internal fun getOrAssumePlayer(guildId: Long): LavalinkPlayer {
val cachedPlayer = playerCache[guildId]

if (cachedPlayer == null) {
val newPlayer = newPlayer(this, guildId.toString())

playerCache[guildId] = newPlayer

return newPlayer
}

return cachedPlayer
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down
13 changes: 5 additions & 8 deletions src/main/kotlin/dev/arbjerg/lavalink/client/Link.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ class Link(
internal set

/**
* Gets the player for this link. If the player is not cached, it will be retrieved or created on the server.
*
* If you just want a local player instead, you can use [getOrAssumePlayer]
* Gets the player associated with this link. Returns null if it's not cached.
*/
fun getPlayer() = node.getPlayer(guildId)
val cachedPlayer: LavalinkPlayer?
get() = node.getCachedPlayer(guildId)

/**
* Gets the cached player for this link. If the player is not cached it will be created locally.
*
* To ensure a player also exist on the node, you can use [getPlayer]
* Gets the player for this link. If the player is not cached, it will be retrieved or created on the server.
*/
fun getOrAssumePlayer() = node.getOrAssumePlayer(guildId)
fun getPlayer() = node.getPlayer(guildId)

/**
* Destroys the player for this link. This will also remove the link from the client.
Expand Down
8 changes: 7 additions & 1 deletion testbot/src/main/java/me/duncte123/testbot/JDAListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
break;
case "now-playing": {
final var link = this.client.getLink(event.getGuild().getIdLong());
final var player = link.getOrAssumePlayer();
final var player = link.getCachedPlayer();

if (player == null) {
event.reply("Not connected or no player available!").queue();
break;
}

final var track = player.getTrack();

if (track == null) {
Expand Down

0 comments on commit 9f5cdb1

Please sign in to comment.