Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ org.gradle.configuration-cache=false
kotlin.code.style=official
kotlin.stdlib.default.dependency=false
org.gradle.parallel=true
version=1.1.3
version=1.2.0

authors=twisti,SLNE-Development
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.slne.surf.queue.api

import dev.slne.surf.core.api.common.server.SurfServer

/**
* Provides the number of currently available slots for a given [SurfServer].
*/
interface SurfQueueAvailableSlotsProvider {

/**
* Returns the number of currently available slots on the given [server].
*
* @param server the server whose available slots should be resolved
* @return the amount of free slots on the server
*/
suspend fun getAvailableSlots(server: SurfServer): Int

/**
* Holds global access to the currently configured [SurfQueueAvailableSlotsProvider].
*/
companion object {
private lateinit var instance: SurfQueueAvailableSlotsProvider

/**
* Sets the global [SurfQueueAvailableSlotsProvider] instance.
*
* @param provider the provider instance to register
*/
fun set(provider: SurfQueueAvailableSlotsProvider) {
instance = provider
}

/**
* Returns the globally configured [SurfQueueAvailableSlotsProvider] instance.
*
* @return the registered provider instance
* @throws UninitializedPropertyAccessException if no provider has been set yet
*/
fun get() = instance
}
Comment thread
twisti-dev marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.slne.surf.queue.paper
import com.google.auto.service.AutoService
import dev.slne.surf.api.paper.event.register
import dev.slne.surf.core.api.common.server.SurfServer
import dev.slne.surf.queue.api.SurfQueueAvailableSlotsProvider
import dev.slne.surf.queue.common.QueueInstance
import dev.slne.surf.queue.common.queue.AbstractQueue
import dev.slne.surf.queue.common.queue.tick.QueueScheduler
Expand All @@ -11,6 +12,7 @@ import dev.slne.surf.queue.paper.config.SurfQueueConfig
import dev.slne.surf.queue.paper.hook.startup.QueueStartHook
import dev.slne.surf.queue.paper.listener.PlayerKickedDueToFullServerListener
import dev.slne.surf.queue.paper.metrics.QueueMetricsLogger
import dev.slne.surf.queue.paper.queue.DefaultSurfQueueAvailableSlotsProvider
import dev.slne.surf.queue.paper.queue.PaperOwnedQueueImpl
import dev.slne.surf.queue.paper.queue.PaperQueueImpl

Expand All @@ -25,6 +27,7 @@ class PaperSurfQueueInstance : QueueInstance() {

override suspend fun load() {
SurfQueueConfig.init()
SurfQueueAvailableSlotsProvider.set(DefaultSurfQueueAvailableSlotsProvider)
super.load()
QueueStartHook.get().onServerReady {
isLoaded = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.slne.surf.queue.paper.queue

import dev.slne.surf.core.api.common.server.SurfServer
import dev.slne.surf.queue.api.SurfQueueAvailableSlotsProvider
import org.bukkit.Bukkit

object DefaultSurfQueueAvailableSlotsProvider : SurfQueueAvailableSlotsProvider {
override suspend fun getAvailableSlots(server: SurfServer): Int {
return if (SurfServer.current().name == server.name) {
Bukkit.getMaxPlayers() - Bukkit.getOnlinePlayers().size
} else {
server.maxPlayers - server.getPlayers().size
}
}
Comment thread
twisti-dev marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.slne.surf.core.api.common.server.connection.SurfServerConnectResult
import dev.slne.surf.queue.paper.config.SurfQueueConfig
import dev.slne.surf.queue.paper.listener.PlayerKickedDueToFullServerListener
import dev.slne.surf.api.core.util.logger
import dev.slne.surf.queue.api.SurfQueueAvailableSlotsProvider
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.withTimeout
import net.kyori.adventure.text.Component
Expand All @@ -26,10 +27,10 @@ class PaperQueueTransfer(
}

suspend fun tryTransfer(): Int {
val availableSlots = Bukkit.getMaxPlayers() - Bukkit.getOnlinePlayers().size
val coreServer = SurfServer[serverName] ?: return 0
val availableSlots = SurfQueueAvailableSlotsProvider.get().getAvailableSlots(coreServer)

if (availableSlots <= 0) return 0
val coreServer = SurfServer[serverName] ?: return 0
val maxTransfers = min(availableSlots, SurfQueueConfig.getConfig().maxTransfersPerSecond)

return processor.processTransfers(maxTransfers) { (uuid) ->
Expand Down