@@ -599,201 +599,139 @@ function inject (bot) {
599599 bot . emit ( 'entitySpawn' , bot . entity )
600600 } )
601601
602- bot . _client . on ( 'player_info' , ( packet ) => {
603- // player list item(s)
602+ function handlePlayerInfoBitfield ( packet ) {
603+ for ( const item of packet . data ) {
604+ let player = bot . _playerFromUUID ( item . uuid )
605+ const newPlayer = ! player
604606
605- if ( typeof packet . action !== 'number' ) {
606- // the features checks below this will be un-needed with https://github.com/PrismarineJS/minecraft-data/pull/948
607- for ( const update of packet . data ) {
608- let player = bot . uuidToUsername [ update . uuid ] ? bot . players [ bot . uuidToUsername [ update . uuid ] ] : null
609- let newPlayer = false
610-
611- const obj = {
612- uuid : update . uuid
613- }
614-
615- if ( ! player ) newPlayer = true
616-
617- player ||= obj
618-
619- if ( packet . action . add_player ) {
620- obj . username = update . player . name
621- obj . displayName = player . displayName || new ChatMessage ( { text : '' , extra : [ { text : update . player . name } ] } )
622- obj . skinData = extractSkinInformation ( update . player . properties )
623- }
624-
625- if ( packet . action . update_game_mode ) {
626- obj . gamemode = update . gamemode
627- }
628-
629- if ( packet . action . update_latency ) {
630- obj . ping = update . latency
631- }
607+ if ( newPlayer ) {
608+ player = { uuid : item . uuid }
609+ }
632610
633- if ( update . displayName ) {
634- obj . displayName = ChatMessage . fromNotch ( update . displayName )
611+ if ( packet . action . add_player ) {
612+ player . username = item . player . name
613+ player . displayName = new ChatMessage ( { text : '' , extra : [ { text : item . player . name } ] } )
614+ player . skinData = extractSkinInformation ( item . player . properties )
615+ }
616+ if ( packet . action . initialize_chat && item . chatSession ) {
617+ player . chatSession = {
618+ publicKey : item . chatSession . publicKey ,
619+ sessionUuid : item . chatSession . uuid
635620 }
621+ }
622+ if ( packet . action . update_game_mode ) {
623+ player . gamemode = item . gamemode
624+ }
625+ if ( packet . action . update_listed ) {
626+ player . listed = item . listed
627+ }
628+ if ( packet . action . update_latency ) {
629+ player . ping = item . latency
630+ }
631+ if ( packet . action . update_display_name ) {
632+ player . displayName = item . displayName ? ChatMessage . fromNotch ( item . displayName ) : new ChatMessage ( { text : '' , extra : [ { text : player . username } ] } )
633+ }
636634
637- if ( newPlayer ) {
638- if ( ! obj . username ) continue // Should be unreachable
639- player = bot . players [ obj . username ] = obj
640- bot . uuidToUsername [ obj . uuid ] = obj . username
641- } else {
642- Object . assign ( player , obj )
643- }
635+ if ( newPlayer ) {
636+ if ( ! player . username ) continue // Should be unreachable if add_player is always sent for new players
637+ bot . players [ player . username ] = player
638+ bot . uuidToUsername [ player . uuid ] = player . username
639+ }
644640
645- const playerEntity = Object . values ( bot . entities ) . find ( e => e . type === 'player' && e . username === player . username )
646- player . entity = playerEntity
641+ const playerEntity = Object . values ( bot . entities ) . find ( e => e . type === 'player' && e . username === player . username )
642+ player . entity = playerEntity
647643
648- if ( playerEntity === bot . entity ) {
649- bot . player = player
650- }
644+ if ( playerEntity === bot . entity ) {
645+ bot . player = player
646+ }
651647
652- if ( newPlayer ) {
653- bot . emit ( 'playerJoined' , player )
654- } else {
655- bot . emit ( 'playerUpdated' , player )
656- }
648+ if ( newPlayer ) {
649+ bot . emit ( 'playerJoined' , player )
650+ } else {
651+ bot . emit ( 'playerUpdated' , player )
657652 }
658- return
659653 }
654+ }
660655
661- if ( bot . supportFeature ( 'playerInfoActionIsBitfield' ) ) {
662- for ( const item of packet . data ) {
663- let player = bot . uuidToUsername [ item . uuid ] ? bot . players [ bot . uuidToUsername [ item . uuid ] ] : null
664- let newPlayer = false
665-
666- const obj = {
667- uuid : item . uuid
668- }
669-
670- if ( ! player ) newPlayer = true
671-
672- player = player || obj
673-
674- if ( packet . action & 1 ) {
675- obj . username = item . player . name
676- obj . displayName = player . displayName || new ChatMessage ( { text : '' , extra : [ { text : item . player . name } ] } )
677- obj . skinData = extractSkinInformation ( item . player . properties )
678- }
679-
680- if ( packet . action & 4 ) {
681- obj . gamemode = item . gamemode
682- }
683-
684- if ( packet . action & 16 ) {
685- obj . ping = item . latency
686- }
687-
688- if ( item . displayName ) {
689- obj . displayName = ChatMessage . fromNotch ( item . displayName )
690- } else if ( packet . action & 32 ) obj . displayName = new ChatMessage ( { text : '' , extra : [ { text : player . username || obj . username } ] } )
691-
692- if ( newPlayer ) {
693- if ( ! obj . username ) continue // Should be unreachable
694- player = bot . players [ obj . username ] = obj
695- bot . uuidToUsername [ obj . uuid ] = obj . username
696- } else {
697- Object . assign ( player , obj )
698- }
699-
700- const playerEntity = Object . values ( bot . entities ) . find ( e => e . type === 'player' && e . username === player . username )
701- player . entity = playerEntity
702-
703- if ( playerEntity === bot . entity ) {
704- bot . player = player
705- }
706-
707- if ( newPlayer ) {
708- bot . emit ( 'playerJoined' , player )
709- } else {
710- bot . emit ( 'playerUpdated' , player )
711- }
712- }
713- } else {
714- for ( const item of packet . data ) {
715- let player = bot . uuidToUsername [ item . UUID ] ? bot . players [ bot . uuidToUsername [ item . UUID ] ] : null
716- if ( packet . action === 0 ) {
717- let newPlayer = false
656+ function handlePlayerInfoLegacy ( packet ) {
657+ for ( const item of packet . data ) {
658+ let player = bot . _playerFromUUID ( item . uuid )
718659
719- // New Player
720- if ( ! player ) {
660+ switch ( packet . action ) {
661+ case 'add_player' : {
662+ const newPlayer = ! player
663+ if ( newPlayer ) {
721664 player = bot . players [ item . name ] = {
722665 username : item . name ,
723- ping : item . ping ,
724- uuid : item . UUID ,
725- displayName : new ChatMessage ( { text : '' , extra : [ { text : item . name } ] } ) ,
726- skinData : extractSkinInformation ( item . properties ) ,
727- profileKeys : item . crypto
728- ? {
729- publicKey : item . crypto . publicKey , // DER-encoded public key
730- signature : item . crypto . signature // Signature
731- }
732- : null
666+ uuid : item . uuid
733667 }
668+ bot . uuidToUsername [ item . uuid ] = item . name
669+ }
734670
735- bot . uuidToUsername [ item . UUID ] = item . name
736- bot . emit ( 'playerJoined' , player )
737- newPlayer = true
738- } else {
739- // Just an Update
740- player . gamemode = item . gamemode
741- player . ping = item . ping
671+ player . ping = item . ping
672+ player . gamemode = item . gamemode
673+ player . displayName = item . displayName ? ChatMessage . fromNotch ( item . displayName ) : new ChatMessage ( { text : '' , extra : [ { text : item . name } ] } )
674+ if ( item . properties ) {
742675 player . skinData = extractSkinInformation ( item . properties )
743- if ( item . crypto ) {
744- player . profileKeys = {
745- publicKey : item . crypto . publicKey ,
746- signature : item . crypto . signature
747- }
748- }
749676 }
750-
751- if ( item . displayName ) {
752- player . displayName = ChatMessage . fromNotch ( item . displayName )
677+ if ( item . crypto ) {
678+ player . profileKeys = {
679+ publicKey : item . crypto . publicKey ,
680+ signature : item . crypto . signature
681+ }
753682 }
754683
755684 const playerEntity = Object . values ( bot . entities ) . find ( e => e . type === 'player' && e . username === item . name )
756685 player . entity = playerEntity
757-
758686 if ( playerEntity === bot . entity ) {
759687 bot . player = player
760688 }
761689
762- if ( ! newPlayer ) {
690+ if ( newPlayer ) bot . emit ( 'playerJoined' , player )
691+ else bot . emit ( 'playerUpdated' , player )
692+ break
693+ }
694+ case 'update_gamemode' : {
695+ if ( player ) {
696+ player . gamemode = item . gamemode
763697 bot . emit ( 'playerUpdated' , player )
764698 }
765- } else if ( player ) {
766- if ( packet . action === 1 ) {
767- player . gamemode = item . gamemode
768- } else if ( packet . action === 2 ) {
699+ break
700+ }
701+ case 'update_latency' : {
702+ if ( player ) {
769703 player . ping = item . ping
770- } else if ( packet . action === 3 && ! item . displayName ) {
771- player . displayName = new ChatMessage ( { text : '' , extra : [ { text : player . username } ] } )
772- } else if ( packet . action === 3 && item . displayName ) {
773- player . displayName = ChatMessage . fromNotch ( item . displayName )
774- } else if ( packet . action === 4 ) {
704+ bot . emit ( 'playerUpdated' , player )
705+ }
706+ break
707+ }
708+ case 'update_display_name' : {
709+ if ( player ) {
710+ player . displayName = item . displayName ? ChatMessage . fromNotch ( item . displayName ) : new ChatMessage ( { text : '' , extra : [ { text : player . username } ] } )
711+ bot . emit ( 'playerUpdated' , player )
712+ }
713+ break
714+ }
715+ case 'remove_player' : {
716+ if ( player ) {
775717 if ( player . entity === bot . entity ) continue
776-
777718 player . entity = null
778719 delete bot . players [ player . username ]
779- delete bot . uuidToUsername [ item . UUID ]
720+ delete bot . uuidToUsername [ item . uuid ]
780721 bot . emit ( 'playerLeft' , player )
781- continue
782- } else {
783- continue
784722 }
785-
786- bot . emit ( 'playerUpdated' , player )
723+ break
787724 }
788725 }
789726 }
790- } )
727+ }
791728
792- // (1.19.3) player(s) leave the game
729+ bot . _client . on ( 'player_info' , bot . supportFeature ( 'playerInfoActionIsBitfield' ) ? handlePlayerInfoBitfield : handlePlayerInfoLegacy )
730+
731+ // 1.19.3+ - player(s) leave the game
793732 bot . _client . on ( 'player_remove' , ( packet ) => {
794733 for ( const uuid of packet . players ) {
795- const player = bot . uuidToUsername [ uuid ] ? bot . players [ bot . uuidToUsername [ uuid ] ] : null
796-
734+ const player = bot . _playerFromUUID ( uuid )
797735 if ( ! player || player . entity === bot . entity ) continue
798736
799737 player . entity = null
0 commit comments