@@ -31,7 +31,7 @@ import './controls'
3131import './dragndrop'
3232import './browserfs'
3333import './eruda'
34- import downloadAndOpenWorld from './downloadAndOpenWorld'
34+ import downloadAndOpenWorld , { hasMapUrl } from './downloadAndOpenWorld'
3535
3636import net from 'net'
3737import Stats from 'stats.js'
@@ -63,7 +63,8 @@ import {
6363 isCypress ,
6464 loadScript ,
6565 toMajorVersion ,
66- setLoadingScreenStatus
66+ setLoadingScreenStatus ,
67+ resolveTimeout
6768} from './utils'
6869
6970import {
@@ -74,13 +75,14 @@ import {
7475
7576import { startLocalServer , unsupportedLocalServerFeatures } from './createLocalServer'
7677import serverOptions from './defaultLocalServerOptions'
77- import { customCommunication } from './customServer'
78+ import { clientDuplex , customCommunication } from './customServer'
7879import updateTime from './updateTime'
7980import { options } from './optionsStorage'
8081import { subscribeKey } from 'valtio/utils'
8182import _ from 'lodash'
8283import { contro } from './controls'
8384import { genTexturePackTextures , watchTexturepackInViewer } from './texturePack'
85+ import { connectToPeer } from './localServerMultiplayer'
8486
8587//@ts -ignore
8688window . THREE = THREE
@@ -263,16 +265,17 @@ const removeAllListeners = () => {
263265 disposables = [ ]
264266}
265267
266- /**
267- * @param {{ server: any; port?: string; singleplayer: any; username: any; password: any; proxy: any; botVersion?: any; serverOverrides? } } connectOptions
268- */
269- async function connect ( connectOptions ) {
268+ async function connect ( connectOptions : {
269+ server : any ; port ?: string ; singleplayer ?: any ; username : any ; password : any ; proxy : any ; botVersion ?: any ; serverOverrides ?; peerId ?: string
270+ } ) {
270271 const menu = document . getElementById ( 'play-screen' )
271272 menu . style = 'display: none;'
272273 removePanorama ( )
273274
274275 const singeplayer = connectOptions . singleplayer
276+ const p2pMultiplayer = ! ! connectOptions . peerId
275277 miscUiState . singleplayer = singeplayer
278+ miscUiState . flyingSquid = singeplayer || p2pMultiplayer
276279 const oldSetInterval = window . setInterval
277280 // @ts -ignore
278281 window . setInterval = ( callback , ms ) => {
@@ -403,9 +406,9 @@ async function connect(connectOptions) {
403406 await loadScript ( `./mc-data/${ toMajorVersion ( version ) } .js` )
404407 }
405408
406- const version = connectOptions . botVersion ?? serverOptions . version
407- if ( version ) {
408- await downloadMcData ( version )
409+ const downloadVersion = connectOptions . botVersion || singeplayer ? serverOptions . version : undefined
410+ if ( downloadVersion ) {
411+ await downloadMcData ( downloadVersion )
409412 }
410413
411414 if ( singeplayer ) {
@@ -421,7 +424,6 @@ async function connect(connectOptions) {
421424 // flying-squid: 'login' -> player.login -> now sends 'login' event to the client (handled in many plugins in mineflayer) -> then 'update_health' is sent which emits 'spawn' in mineflayer
422425
423426 setLoadingScreenStatus ( 'Starting local server' )
424- window . serverDataChannel ??= { }
425427 localServer = window . localServer = startLocalServer ( )
426428 // todo need just to call quit if started
427429 // loadingScreen.maybeRecoverable = false
@@ -433,16 +435,23 @@ async function connect(connectOptions) {
433435 }
434436 }
435437
438+ const usingCustomCommunication = true
439+
440+ const botDuplex = ! p2pMultiplayer ? undefined /* clientDuplex */ : await connectToPeer ( connectOptions . peerId ) ;
441+
436442 setLoadingScreenStatus ( 'Creating mineflayer bot' )
437443 bot = mineflayer . createBot ( {
438444 host,
439445 port,
440- version : connectOptions . botVersion === '' ? false : connectOptions . botVersion ,
446+ version : ! connectOptions . botVersion ? false : connectOptions . botVersion ,
447+ ...singeplayer || p2pMultiplayer ? {
448+ keepAlive : false ,
449+ stream : botDuplex ,
450+ } : { } ,
441451 ...singeplayer ? {
442452 version : serverOptions . version ,
443453 connect ( ) { } ,
444- keepAlive : false ,
445- customCommunication
454+ customCommunication : usingCustomCommunication ? customCommunication : undefined ,
446455 } : { } ,
447456 username,
448457 password,
@@ -454,7 +463,8 @@ async function connect(connectOptions) {
454463 await downloadMcData ( client . version )
455464 }
456465 } )
457- if ( singeplayer ) {
466+ if ( singeplayer || p2pMultiplayer ) {
467+ // p2pMultiplayer still uses the same flying-squid server
458468 const _supportFeature = bot . supportFeature
459469 bot . supportFeature = ( feature ) => {
460470 if ( unsupportedLocalServerFeatures . includes ( feature ) ) {
@@ -463,13 +473,16 @@ async function connect(connectOptions) {
463473 return _supportFeature ( feature )
464474 }
465475
466- bot . emit ( 'inject_allowed' )
467- bot . _client . emit ( 'connect' )
476+ if ( usingCustomCommunication ) {
477+ bot . emit ( 'inject_allowed' )
478+ bot . _client . emit ( 'connect' )
479+ }
468480 }
469481 } catch ( err ) {
470482 handleError ( err )
471483 }
472484 if ( ! bot ) return
485+ let p2pConnectTimeout = p2pMultiplayer ? setTimeout ( ( ) => { throw new Error ( 'Spawn timeout. There might be error on other side, check console.' ) } , 20_000 ) : undefined
473486 hud . preload ( bot )
474487
475488 // bot.on('inject_allowed', () => {
@@ -500,6 +513,7 @@ async function connect(connectOptions) {
500513 } )
501514
502515 bot . once ( 'spawn' , ( ) => {
516+ if ( p2pConnectTimeout ) clearTimeout ( p2pConnectTimeout )
503517 // todo display notification if not critical
504518 const mcData = require ( 'minecraft-data' ) ( bot . version )
505519
@@ -702,6 +716,7 @@ async function connect(connectOptions) {
702716 errorAbortController . abort ( )
703717 if ( loadingScreen . hasError ) return
704718 // remove loading screen, wait a second to make sure a frame has properly rendered
719+ setLoadingScreenStatus ( undefined )
705720 hideCurrentScreens ( )
706721 } , singeplayer ? 0 : 2500 )
707722 } )
@@ -744,4 +759,24 @@ window.addEventListener('keydown', (e) => {
744759addPanoramaCubeMap ( )
745760showModal ( document . getElementById ( 'title-screen' ) )
746761main ( )
747- downloadAndOpenWorld ( )
762+ if ( hasMapUrl ( ) ) {
763+ downloadAndOpenWorld ( )
764+ } else {
765+ window . addEventListener ( 'hud-ready' , ( e ) => {
766+ // try to connect to peer
767+ const qs = new URLSearchParams ( window . location . search )
768+ const peerId = qs . get ( 'connectPeer' )
769+ const version = qs . get ( 'peerVersion' )
770+ if ( peerId ) {
771+ let username = options . guestUsername
772+ if ( ! options . askGuestName ) username = prompt ( 'Enter your username' , username )
773+ options . guestUsername = username
774+ connect ( {
775+ server : '' , port : '' , proxy : '' , password : '' ,
776+ username,
777+ botVersion : version || undefined ,
778+ peerId
779+ } )
780+ }
781+ } )
782+ }
0 commit comments