Chipmunk
5f3560910b
not sure if it's better or worse now. also, i have tried to optimize color codes, but this optimization seems unreliable (i might fix or remove it in the future)
95 lines
2.6 KiB
JavaScript
95 lines
2.6 KiB
JavaScript
const { parseNbtText } = require('../util/chat/utility')
|
|
|
|
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 = parseNbtText(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
|