Skip to content

Commit 63a1bd6

Browse files
committed
Create a local player when a link is created
1 parent 07e99f0 commit 63a1bd6

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class LavalinkClient(val userId: Long) : Closeable, Disposable {
114114
if (!linkMap.containsKey(guildId)) {
115115
val bestNode = loadBalancer.selectNode(region)
116116
linkMap[guildId] = Link(guildId, bestNode)
117+
bestNode.playerCache[guildId] = newPlayer(bestNode, guildId.toString())
117118
}
118119

119120
return linkMap[guildId]!!

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import reactor.kotlin.core.publisher.toMono
2828
import java.io.Closeable
2929
import java.io.IOException
3030
import java.net.URI
31+
import java.util.concurrent.ConcurrentHashMap
3132
import java.util.function.Consumer
3233
import java.util.function.UnaryOperator
3334

@@ -66,7 +67,7 @@ class LavalinkNode(
6667
/**
6768
* A local player cache, allows us to not call the rest client every time we need a player.
6869
*/
69-
internal val playerCache = mutableMapOf<Long, LavalinkPlayer>()
70+
internal val playerCache = ConcurrentHashMap<Long, LavalinkPlayer>()
7071

7172
override fun dispose() {
7273
close()
@@ -127,7 +128,7 @@ class LavalinkNode(
127128
fun getPlayer(guildId: Long): Mono<LavalinkPlayer> {
128129
if (!available) return Mono.error(IllegalStateException("Node is not available"))
129130

130-
if (guildId in playerCache) {
131+
if (playerCache.containsKey(guildId)) {
131132
return playerCache[guildId].toMono()
132133
}
133134

@@ -168,12 +169,12 @@ class LavalinkNode(
168169
return rest.destroyPlayer(guildId)
169170
.doOnSuccess {
170171
removeCachedPlayer(guildId)
172+
lavalink.removeDestroyedLink(guildId)
171173
}
172174
}
173175

174176
internal fun removeCachedPlayer(guildId: Long) {
175177
playerCache.remove(guildId)
176-
lavalink.removeDestroyedLink(guildId)
177178
}
178179

179180
/**
@@ -385,13 +386,7 @@ class LavalinkNode(
385386
}
386387
}
387388

388-
internal fun getCachedPlayer(guildId: Long): LavalinkPlayer? {
389-
if (guildId in playerCache) {
390-
return playerCache[guildId]
391-
}
392-
393-
return null
394-
}
389+
internal fun getCachedPlayer(guildId: Long): LavalinkPlayer? = playerCache[guildId]
395390

396391
override fun equals(other: Any?): Boolean {
397392
if (this === other) return true

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ import dev.arbjerg.lavalink.client.protocol.toCustom
55
import dev.arbjerg.lavalink.protocol.v4.*
66
import kotlin.math.min
77

8+
internal fun newPlayer(node: LavalinkNode, guildId: String) = LavalinkPlayer(node, Player(
9+
guildId = guildId,
10+
track = null,
11+
volume = 100,
12+
paused = false,
13+
state = PlayerState(0, 0, false, -1),
14+
voice = VoiceState("", "", ""),
15+
filters = Filters()
16+
))
17+
818
/**
919
* Represents a player that is tied to a guild.
1020
*/

src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,11 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos
7070
val update = event as Message.PlayerUpdateEvent
7171
val idLong = update.guildId.toLong()
7272

73-
if (idLong in node.playerCache) {
74-
node.playerCache[idLong]!!.state = update.state
75-
node.lavalink.getLinkIfCached(idLong)?.state = if (update.state.connected) {
76-
LinkState.CONNECTED
77-
} else {
78-
LinkState.DISCONNECTED
79-
}
73+
node.getCachedPlayer(idLong)?.state = update.state
74+
node.lavalink.getLinkIfCached(idLong)?.state = if (update.state.connected) {
75+
LinkState.CONNECTED
76+
} else {
77+
LinkState.DISCONNECTED
8078
}
8179
}
8280

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ private static void registerLavalinkNodes(LavalinkClient client) {
4646
),*/
4747

4848
client.addNode(
49-
"Mac-mini",
50-
URI.create("ws://mac-mini.local.duncte123.lgbt:2333"),
49+
"Pi-local",
50+
URI.create("ws://pi.local.duncte123.lgbt:2333"),
5151
"youshallnotpass",
5252
RegionGroup.US
5353
)
@@ -79,10 +79,11 @@ private static void registerLavalinkListeners(LavalinkClient client) {
7979
final LavalinkNode node = event.getNode();
8080

8181
LOG.info(
82-
"Node '{}' has stats, current players: {}/{}",
82+
"Node '{}' has stats, current players: {}/{} (link count {})",
8383
node.getName(),
8484
event.getPlayingPlayers(),
85-
event.getPlayers()
85+
event.getPlayers(),
86+
client.getLinks().size()
8687
);
8788
});
8889

0 commit comments

Comments
 (0)