Skip to content

Commit 950f9e2

Browse files
Wait for configuration phase to finish before adding the player to their game
1 parent 57e433c commit 950f9e2

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

Diff for: src/main/kotlin/com/bluedragonmc/server/bootstrap/prod/InitialInstanceRouter.kt

+20-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ import kotlinx.coroutines.withTimeout
1515
import net.kyori.adventure.text.Component
1616
import net.kyori.adventure.text.format.NamedTextColor
1717
import net.minestom.server.MinecraftServer
18+
import net.minestom.server.ServerFlag
1819
import net.minestom.server.event.Event
1920
import net.minestom.server.event.EventNode
2021
import net.minestom.server.event.player.AsyncPlayerConfigurationEvent
22+
import net.minestom.server.network.ConnectionState
23+
import net.minestom.server.timer.TaskSchedule
2124

2225
object InitialInstanceRouter : Bootstrap(EnvType.PRODUCTION) {
2326

@@ -27,6 +30,8 @@ object InitialInstanceRouter : Bootstrap(EnvType.PRODUCTION) {
2730
Component.text("Couldn't find which world to put you in! (Destination not ready)", NamedTextColor.RED)
2831
private val HANDSHAKE_FAILED =
2932
Component.text("Couldn't find which world to put you in! (Handshake failed)", NamedTextColor.RED)
33+
private val LOAD_TIMED_OUT =
34+
Component.text("There was a problem joining the server! (Configuration timed out)", NamedTextColor.RED)
3035
internal val DATA_LOAD_FAILED =
3136
Component.text("Failed to load your player data!", NamedTextColor.RED)
3237

@@ -104,8 +109,21 @@ object InitialInstanceRouter : Bootstrap(EnvType.PRODUCTION) {
104109
game.getModule<SpawnpointModule>().spawnpointProvider.getSpawnpoint(event.player)
105110
}
106111

107-
MinecraftServer.getSchedulerManager().scheduleNextTick {
108-
game.addPlayer(event.player, sendPlayer = false)
112+
113+
var ticks = 0
114+
115+
// Wait up to 10 seconds for the player to enter the PLAY phase and then add them to the game.
116+
MinecraftServer.getSchedulerManager().submitTask {
117+
ticks ++
118+
if (event.player.playerConnection.connectionState == ConnectionState.PLAY) {
119+
game.addPlayer(event.player, sendPlayer = false)
120+
return@submitTask TaskSchedule.stop()
121+
} else if (ticks < ServerFlag.SERVER_TICKS_PER_SECOND * 10) {
122+
return@submitTask TaskSchedule.nextTick()
123+
} else {
124+
event.player.kick(LOAD_TIMED_OUT)
125+
return@submitTask TaskSchedule.stop()
126+
}
109127
}
110128

111129
Messaging.IO.launch {

0 commit comments

Comments
 (0)