eros: play: check if server type is active

This commit is contained in:
Bixilon 2021-07-30 15:15:15 +02:00
parent 49491141fe
commit e257313205
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 18 additions and 3 deletions

View File

@ -51,7 +51,14 @@ class PlayController : EmbeddedJavaFXController<Pane>() {
override fun init() {
playTypeListViewFX.setCellFactory { ServerTypeCardController.build() }
playTypeListViewFX.items += ServerTypes.VALUES
for (type in ServerTypes.VALUES) {
if (!type.active) {
continue
}
playTypeListViewFX.items += type
}
// ToDo
check(playTypeListViewFX.items.size > 0)
playTypeListViewFX.selectionModel.selectedItemProperty().addListener { _, _, new ->

View File

@ -25,10 +25,11 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
enum class ServerTypes(
val icon: Ikon,
private val activeSupplier: (() -> Boolean)? = null,
private val countSupplier: () -> Int,
) : Translatable {
CUSTOM(FontAwesomeSolid.SERVER, { Minosoft.config.config.server.entries.size }),
LAN(FontAwesomeSolid.NETWORK_WIRED, { LANServerListener.SERVERS.size }),
CUSTOM(FontAwesomeSolid.SERVER, null, { Minosoft.config.config.server.entries.size }),
LAN(FontAwesomeSolid.NETWORK_WIRED, { LANServerListener.active }, { LANServerListener.SERVERS.size }),
;
override val translationKey: ResourceLocation = "minosoft:server_type.${name.lowercase()}".asResourceLocation()
@ -36,6 +37,9 @@ enum class ServerTypes(
val count: Int
get() = countSupplier()
val active: Boolean
get() = activeSupplier?.invoke() ?: true
companion object : ValuesEnum<ServerTypes> {
override val VALUES: Array<ServerTypes> = values()
override val NAME_MAP: Map<String, ServerTypes> = KUtil.getEnumValues(VALUES)

View File

@ -28,6 +28,8 @@ import java.nio.charset.StandardCharsets
import java.util.concurrent.CountDownLatch
object LANServerListener {
var active: Boolean = false
private set
val SERVERS: HashBiMap<InetAddress, Server> = HashBiMap.create()
private const val MOTD_START_STRING = "[MOTD]"
private const val MOTD_END_STRING = "[/MOTD]"
@ -44,6 +46,7 @@ object LANServerListener {
val buffer = ByteArray(256) // this should be enough, if the packet is longer, it is probably invalid
Log.log(LogMessageType.NETWORK_STATUS, LogLevels.INFO) { "Listening for LAN servers..." }
latch.countDown()
active = true
while (true) {
try {
val packet = DatagramPacket(buffer, buffer.size)
@ -75,6 +78,7 @@ object LANServerListener {
latch.countDown()
}
SERVERS.clear()
active = false
Log.log(LogMessageType.NETWORK_STATUS, LogLevels.INFO) { "Stop listening for LAN servers..." }
}, "LAN Server Listener").start()
latch.await()