Skip to content

Commit 9f5cdb1

Browse files
committed
Add cachedPlayer getter on the link
1 parent 4ca714f commit 9f5cdb1

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,27 +141,6 @@ class LavalinkNode(
141141
}
142142
}
143143

144-
/**
145-
* Gets a player from the local cache. If the player is not in the local cache, it will be created.
146-
*
147-
* @param guildId The guild id of the player.
148-
*
149-
* @return The local player. This player may not exist on the [LavalinkNode] yet.
150-
*/
151-
fun getOrAssumePlayer(guildId: Long): LavalinkPlayer {
152-
val cachedPlayer = playerCache[guildId]
153-
154-
if (cachedPlayer == null) {
155-
val newPlayer = newPlayer(this, guildId.toString())
156-
157-
playerCache[guildId] = newPlayer
158-
159-
return newPlayer
160-
}
161-
162-
return cachedPlayer
163-
}
164-
165144
fun updatePlayer(guildId: Long, updateConsumer: Consumer<PlayerUpdateBuilder>): Mono<LavalinkPlayer> {
166145
val update = createOrUpdatePlayer(guildId)
167146

@@ -411,11 +390,30 @@ class LavalinkNode(
411390
* Get a [LavalinkPlayer] from the player cache.
412391
*
413392
* @return The cached player, or null if not yet cached.
414-
*
415-
* @see [getOrAssumePlayer]
416393
*/
417394
fun getCachedPlayer(guildId: Long): LavalinkPlayer? = playerCache[guildId]
418395

396+
/**
397+
* Gets a player from the local cache. If the player is not in the local cache, it will be created.
398+
*
399+
* @param guildId The guild id of the player.
400+
*
401+
* @return The local player. This player may not exist on the [LavalinkNode] yet.
402+
*/
403+
internal fun getOrAssumePlayer(guildId: Long): LavalinkPlayer {
404+
val cachedPlayer = playerCache[guildId]
405+
406+
if (cachedPlayer == null) {
407+
val newPlayer = newPlayer(this, guildId.toString())
408+
409+
playerCache[guildId] = newPlayer
410+
411+
return newPlayer
412+
}
413+
414+
return cachedPlayer
415+
}
416+
419417
override fun equals(other: Any?): Boolean {
420418
if (this === other) return true
421419
if (javaClass != other?.javaClass) return false

src/main/kotlin/dev/arbjerg/lavalink/client/Link.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@ class Link(
2222
internal set
2323

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

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

3835
/**
3936
* Destroys the player for this link. This will also remove the link from the client.

testbot/src/main/java/me/duncte123/testbot/JDAListener.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent even
7878
break;
7979
case "now-playing": {
8080
final var link = this.client.getLink(event.getGuild().getIdLong());
81-
final var player = link.getOrAssumePlayer();
81+
final var player = link.getCachedPlayer();
82+
83+
if (player == null) {
84+
event.reply("Not connected or no player available!").queue();
85+
break;
86+
}
87+
8288
final var track = player.getTrack();
8389

8490
if (track == null) {

0 commit comments

Comments
 (0)