Skip to content

Commit 3f2fd6d

Browse files
extremeheatrom1504
andauthored
Update player_info handling (PrismarineJS#3689)
* Update player_info handling * update sound packet for holder * internal test fixes * add logging * cleanup * revert last change * Update ci.yml * Update package.json --------- Co-authored-by: Romain Beaumont <romain.rom1@gmail.com>
1 parent 12e50a2 commit 3f2fd6d

File tree

7 files changed

+194
-371
lines changed

7 files changed

+194
-371
lines changed

lib/plugins/chat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function inject (bot, options) {
118118
target: data.targetName ? JSON.parse(data.targetName) : undefined,
119119
content: message ? JSON.parse(message) : { text: data.plainMessage }
120120
}
121-
const registryIndex = data.type.registryIndex ? data.type.registryIndex : data.type
121+
const registryIndex = data.type.chatType != null ? data.type.chatType : data.type
122122
msg = ChatMessage.fromNetwork(registryIndex, parameters)
123123

124124
if (data.unsignedContent) {

lib/plugins/entities.js

Lines changed: 94 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/plugins/physics.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -354,38 +354,36 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) {
354354
// e.g. when crouching/crawling/swimming. Can someone confirm?
355355
bot.entity.height = 1.8
356356

357-
// Velocity is reset if the x, y, z flags are not set
358357
const vel = bot.entity.velocity
359-
// If the x, y, z flags are not set, the position is absolute
360358
const pos = bot.entity.position
361-
362-
// TODO: this current mineflayer logic maybe incorrect. New (maybe even older) versions of minecraft have flag values for
363-
// dx/dy/dz but mineflayer is assuming that 0b111 applies for both velocity and position.
364-
365359
let newYaw, newPitch
366360

367-
if (bot.registry.version['>=']('1.21.3')) {
368-
// flags is now an object with keys
369-
// "flags": ["x", "y", "z", "yaw", "pitch", "dx", "dy", "dz", "yawDelta"]
370-
const flags = packet.flags
361+
// Note: 1.20.5+ uses a bitflags object, older versions use a bitmask number
362+
if (typeof packet.flags === 'object') {
363+
// Modern path with bitflags object
364+
// Velocity is only set to 0 if the flag is not set, otherwise keep current velocity
371365
vel.set(
372-
flags.x ? vel.x : 0,
373-
flags.y ? vel.y : 0,
374-
flags.z ? vel.z : 0
366+
packet.flags.x ? vel.x : 0,
367+
packet.flags.y ? vel.y : 0,
368+
packet.flags.z ? vel.z : 0
375369
)
370+
// If flag is set, then the corresponding value is relative, else it is absolute
376371
pos.set(
377-
flags.x ? (pos.x + packet.x) : packet.x,
378-
flags.y ? (pos.y + packet.y) : packet.y,
379-
flags.z ? (pos.z + packet.z) : packet.z
372+
packet.flags.x ? (pos.x + packet.x) : packet.x,
373+
packet.flags.y ? (pos.y + packet.y) : packet.y,
374+
packet.flags.z ? (pos.z + packet.z) : packet.z
380375
)
381-
newYaw = (flags.yaw ? conv.toNotchianYaw(bot.entity.yaw) : 0) + packet.yaw
382-
newPitch = (flags.pitch ? conv.toNotchianPitch(bot.entity.pitch) : 0) + packet.pitch
376+
newYaw = (packet.flags.yaw ? conv.toNotchianYaw(bot.entity.yaw) : 0) + packet.yaw
377+
newPitch = (packet.flags.pitch ? conv.toNotchianPitch(bot.entity.pitch) : 0) + packet.pitch
383378
} else {
379+
// Legacy path with bitmask number
380+
// Velocity is only set to 0 if the flag is not set, otherwise keep current velocity
384381
vel.set(
385382
packet.flags & 1 ? vel.x : 0,
386383
packet.flags & 2 ? vel.y : 0,
387384
packet.flags & 4 ? vel.z : 0
388385
)
386+
// If flag is set, then the corresponding value is relative, else it is absolute
389387
pos.set(
390388
packet.flags & 1 ? (pos.x + packet.x) : packet.x,
391389
packet.flags & 2 ? (pos.y + packet.y) : packet.y,

lib/plugins/ray_trace.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module.exports = (bot) => {
1111
}
1212

1313
bot.blockInSight = (maxSteps = 256, vectorLength = 5 / 16) => {
14-
console.warn('[deprecated] use bot.blockAtCursor instead')
1514
const block = bot.blockAtCursor(maxSteps * vectorLength)
1615
if (block) return block
1716
}

0 commit comments

Comments
 (0)