@@ -50,6 +50,7 @@ import {
5050 miscUiState ,
5151 showModal ,
5252 gameAdditionalState ,
53+ maybeCleanupAfterDisconnect ,
5354} from './globalState'
5455
5556import { parseServerAddress } from './parseServerAddress'
@@ -168,6 +169,8 @@ export async function connect (connectOptions: ConnectOptions) {
168169 } )
169170 }
170171
172+ maybeCleanupAfterDisconnect ( )
173+
171174 appStatusState . showReconnect = false
172175 loadingTimerState . loading = true
173176 loadingTimerState . start = Date . now ( )
@@ -212,12 +215,13 @@ export async function connect (connectOptions: ConnectOptions) {
212215
213216 let ended = false
214217 let bot ! : typeof __type_bot
218+ let hadWorldLoaded = false
215219 let hadConnected = false
216- const destroyAll = ( wasKicked = false ) => {
220+ const handleSessionEnd = ( wasKicked = false ) => {
217221 if ( ended ) return
218222 loadingTimerState . loading = false
219223 const { alwaysReconnect } = appQueryParams
220- if ( ( ! wasKicked && miscUiState . appConfig ?. allowAutoConnect && appQueryParams . autoConnect && hadConnected ) || ( alwaysReconnect ) ) {
224+ if ( ( ! wasKicked && miscUiState . appConfig ?. allowAutoConnect && appQueryParams . autoConnect && hadWorldLoaded ) || ( alwaysReconnect ) ) {
221225 if ( alwaysReconnect === 'quick' || alwaysReconnect === 'fast' ) {
222226 quickDevReconnect ( )
223227 } else {
@@ -227,26 +231,33 @@ export async function connect (connectOptions: ConnectOptions) {
227231 errorAbortController . abort ( )
228232 ended = true
229233 progress . end ( )
230- // dont reset viewer so we can still do debugging
231- localServer = window . localServer = window . server = undefined
232- gameAdditionalState . viewerConnection = false
233-
234- if ( bot ) {
235- bot . end ( )
236- // ensure mineflayer plugins receive this event for cleanup
237- bot . emit ( 'end' , '' )
238- bot . removeAllListeners ( )
239- bot . _client . removeAllListeners ( )
240- bot . _client = {
241- //@ts -expect-error
242- write ( packetName ) {
243- console . warn ( 'Tried to write packet' , packetName , 'after bot was destroyed' )
234+ bot . end ( )
235+ // ensure mineflayer plugins receive this event for cleanup
236+ bot . emit ( 'end' , '' )
237+
238+ miscUiState . disconnectedCleanup = {
239+ callback ( ) {
240+ appViewer . resetBackend ( true )
241+ localServer = window . localServer = window . server = undefined
242+ gameAdditionalState . viewerConnection = false
243+
244+ if ( bot ) {
245+ bot . removeAllListeners ( )
246+ bot . _client . removeAllListeners ( )
247+ bot . _client = {
248+ //@ts -expect-error
249+ write ( packetName ) {
250+ console . warn ( 'Tried to write packet' , packetName , 'after bot was destroyed' )
251+ }
252+ }
253+ //@ts -expect-error
254+ window . bot = bot = undefined
244255 }
245- }
246- //@ts -expect-error
247- window . bot = bot = undefined
256+ cleanFs ( )
257+ } ,
258+ date : Date . now ( ) ,
259+ wasConnected : hadConnected
248260 }
249- cleanFs ( )
250261 }
251262 const cleanFs = ( ) => {
252263 if ( singleplayer && ! fsState . inMemorySave ) {
@@ -277,7 +288,7 @@ export async function connect (connectOptions: ConnectOptions) {
277288 setLoadingScreenStatus ( `Error encountered. ${ err } ` , true )
278289 appStatusState . showReconnect = true
279290 onPossibleErrorDisconnect ( )
280- destroyAll ( )
291+ handleSessionEnd ( )
281292 }
282293
283294 // todo(hard): remove it!
@@ -678,7 +689,7 @@ export async function connect (connectOptions: ConnectOptions) {
678689 }
679690 setLoadingScreenStatus ( `The Minecraft server kicked you. Kick reason: ${ kickReasonString } ` , true , undefined , undefined , kickReasonFormatted )
680691 appStatusState . showReconnect = true
681- destroyAll ( true )
692+ handleSessionEnd ( true )
682693 } )
683694
684695 const packetBeforePlay = ( _ , __ , ___ , fullBuffer ) => {
@@ -705,13 +716,14 @@ export async function connect (connectOptions: ConnectOptions) {
705716 setLoadingScreenStatus ( `You have been disconnected from the server. End reason:\n${ endReason } ` , true )
706717 appStatusState . showReconnect = true
707718 onPossibleErrorDisconnect ( )
708- destroyAll ( )
719+ handleSessionEnd ( )
709720 if ( isCypress ( ) ) throw new Error ( `disconnected: ${ endReason } ` )
710721 } )
711722
712723 onBotCreate ( )
713724
714725 bot . once ( 'login' , ( ) => {
726+ hadConnected = true
715727 errorAbortController . abort ( )
716728 loadingTimerState . networkOnlyStart = 0
717729 progress . setMessage ( 'Loading world' )
@@ -871,7 +883,7 @@ export async function connect (connectOptions: ConnectOptions) {
871883 } catch ( err ) {
872884 handleError ( err )
873885 }
874- hadConnected = true
886+ hadWorldLoaded = true
875887 }
876888 // don't use spawn event, player can be dead
877889 bot . once ( spawnEarlier ? 'forcedMove' : 'health' , displayWorld )
0 commit comments