@@ -15,6 +15,8 @@ import com.bluedragonmc.server.service.Messaging
15
15
import com.bluedragonmc.server.utils.listen
16
16
import com.bluedragonmc.server.utils.listenSuspend
17
17
import com.bluedragonmc.server.utils.miniMessage
18
+ import com.github.benmanes.caffeine.cache.Caffeine
19
+ import io.grpc.ManagedChannel
18
20
import io.grpc.ManagedChannelBuilder
19
21
import kotlinx.coroutines.launch
20
22
import kotlinx.coroutines.runBlocking
@@ -28,6 +30,7 @@ import net.minestom.server.event.instance.AddEntityToInstanceEvent
28
30
import net.minestom.server.event.player.PlayerDisconnectEvent
29
31
import net.minestom.server.event.player.PlayerSpawnEvent
30
32
import net.minestom.server.instance.Instance
33
+ import java.time.Duration
31
34
import java.util.*
32
35
import java.util.concurrent.TimeUnit
33
36
@@ -111,6 +114,27 @@ class OutgoingRPCHandlerImpl(serverAddress: String) : OutgoingRPCHandler {
111
114
private val queueStub = QueueServiceGrpcKt .QueueServiceCoroutineStub (channel)
112
115
private val partyStub = PartyServiceGrpcKt .PartyServiceCoroutineStub (channel)
113
116
117
+ private val proxyChannelCache = Caffeine
118
+ .newBuilder()
119
+ .expireAfterAccess(Duration .ofMinutes(10 ))
120
+ .evictionListener<String , ManagedChannel > { _, value, _ ->
121
+ value?.shutdown()?.awaitTermination(10 , TimeUnit .SECONDS )
122
+ }
123
+ .build<String , ManagedChannel >()
124
+
125
+ private fun getChannelToProxyOf (player : Player ): ManagedChannel {
126
+ val address = player.playerConnection.remoteAddress.toString()
127
+ return proxyChannelCache.get(address) { _ ->
128
+ ManagedChannelBuilder .forAddress(address, 50051 )
129
+ .defaultLoadBalancingPolicy(" round_robin" )
130
+ .usePlaintext()
131
+ .enableRetry()
132
+ .build()
133
+ }
134
+ }
135
+
136
+ private fun getJukeboxStubOf (player : Player ) = JukeboxGrpcKt .JukeboxCoroutineStub (getChannelToProxyOf(player))
137
+
114
138
override fun isConnected (): Boolean {
115
139
return ! channel.isShutdown && ! channel.isTerminated && ::serverName.isInitialized
116
140
}
@@ -299,4 +323,46 @@ class OutgoingRPCHandlerImpl(serverAddress: String) : OutgoingRPCHandler {
299
323
.build()
300
324
)
301
325
}
302
- }
326
+
327
+ override suspend fun getSongInfo (player : Player ): JukeboxOuterClass .PlayerSongQueue {
328
+ return getJukeboxStubOf(player).getSongInfo(songInfoRequest {
329
+ playerUuid = player.uuid.toString()
330
+ })
331
+ }
332
+
333
+ override suspend fun playSong (
334
+ player : Player ,
335
+ songName : String ,
336
+ queuePosition : Int ,
337
+ startTimeInTicks : Int ,
338
+ tags : List <String >,
339
+ ): Boolean {
340
+ return getJukeboxStubOf(player).playSong(playSongRequest {
341
+ this .playerUuid = player.uuid.toString()
342
+ this .songName = songName
343
+ this .queuePosition = queuePosition
344
+ this .startTimeTicks = startTimeInTicks
345
+ tags.forEach { this .tags.add(it) }
346
+ }).startedPlaying
347
+ }
348
+
349
+ override suspend fun removeSongByName (player : Player , songName : String ) {
350
+ getJukeboxStubOf(player).removeSong(songRemoveRequest {
351
+ this .playerUuid = player.uuid.toString()
352
+ this .songName = songName
353
+ })
354
+ }
355
+
356
+ override suspend fun removeSongByTag (player : Player , matchTags : List <String >) {
357
+ getJukeboxStubOf(player).removeSongs(batchSongRemoveRequest {
358
+ this .playerUuid = player.uuid.toString()
359
+ matchTags.forEach { this .matchTags.add(it) }
360
+ })
361
+ }
362
+
363
+ override suspend fun stopSongAndClearQueue (player : Player ) {
364
+ getJukeboxStubOf(player).stopSong(stopSongRequest {
365
+ this .playerUuid = player.uuid.toString()
366
+ })
367
+ }
368
+ }
0 commit comments