95 lines
2.5 KiB
JavaScript
95 lines
2.5 KiB
JavaScript
const nbt = require('prismarine-nbt')
|
|
|
|
const gamemodes = ['survival', 'creative', 'adventure', 'spectator']
|
|
|
|
function inject (bot) {
|
|
bot.players = []
|
|
|
|
bot.on('packet.player_info', packet => {
|
|
for (const player of packet.data) {
|
|
if (packet.action & 1) addPlayer(player)
|
|
if (packet.action & 2) initializeChat(player)
|
|
if (packet.action & 4) updateGamemode(player)
|
|
if (packet.action & 8) updateListed(player)
|
|
if (packet.action & 16) updateLatency(player)
|
|
if (packet.action & 32) updateDisplayName(player)
|
|
}
|
|
})
|
|
|
|
bot.on('packet.player_remove', packet => {
|
|
for (const uuid of packet.players) {
|
|
removePlayer(uuid)
|
|
}
|
|
})
|
|
|
|
function addPlayer (player) {
|
|
bot.players.filter(_player => _player.uuid !== player.uuid) // Remove duplicates
|
|
|
|
const target = {
|
|
uuid: player.uuid,
|
|
username: player.player.name,
|
|
properties: player.player.properties
|
|
}
|
|
bot.players.push(target)
|
|
|
|
bot.emit('player_added', target)
|
|
}
|
|
|
|
function initializeChat (player) {
|
|
const target = bot.players.find(_player => _player.uuid === player.uuid)
|
|
if (target == null) return
|
|
|
|
target.chatSession = player.chatSession
|
|
}
|
|
|
|
function updateGamemode (player) {
|
|
const target = bot.players.find(_player => _player.uuid === player.uuid)
|
|
if (target == null) return
|
|
|
|
target.gamemode = gamemodes[player.gamemode]
|
|
}
|
|
|
|
function updateListed (player) {
|
|
const target = bot.players.find(_player => _player.uuid === player.uuid)
|
|
if (target == null) return
|
|
|
|
target.listed = player.listed
|
|
}
|
|
|
|
function updateLatency (player) {
|
|
const target = bot.players.find(_player => _player.uuid === player.uuid)
|
|
if (target == null) return
|
|
|
|
target.latency = player.latency
|
|
}
|
|
|
|
function updateDisplayName (player) {
|
|
const target = bot.players.find(_player => _player.uuid === player.uuid)
|
|
if (target == null) return
|
|
|
|
target.displayName = nbt.simplify(player.displayName)
|
|
}
|
|
|
|
async function removePlayer (uuid) {
|
|
const target = bot.players.find(_player => _player.uuid === uuid)
|
|
if (target == null) return
|
|
|
|
// Check that the player actually left
|
|
const completions = await bot.tabComplete('scoreboard players add ')
|
|
for (const match of completions.matches) {
|
|
if (match.tooltip) continue
|
|
|
|
if (match.match === target.username) {
|
|
target.listed = false
|
|
return
|
|
}
|
|
}
|
|
|
|
bot.players = bot.players.filter(player => player.uuid !== uuid)
|
|
bot.emit('player_removed', target)
|
|
}
|
|
|
|
bot.on('end', () => (bot.players = []))
|
|
}
|
|
|
|
module.exports = inject
|