chomens-bot-js/plugins/players.js

137 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2022-12-07 05:28:40 -05:00
const { EventEmitter } = require('events')
2022-08-14 05:51:45 -04:00
class PlayerList {
2022-11-27 02:35:28 -05:00
list = []
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
addPlayer (player) {
this.removePlayer(player)
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
this.list.push(player)
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
hasPlayer (player) {
return this.getPlayer(player) !== undefined
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
getPlayer (player) {
let identifier
2022-08-14 05:51:45 -04:00
switch (typeof player) {
case 'object':
2022-11-27 02:35:28 -05:00
identifier = player.UUID
break
2022-08-14 05:51:45 -04:00
case 'string':
2022-11-27 02:35:28 -05:00
identifier = player
break
2022-08-14 05:51:45 -04:00
default:
2022-11-27 02:35:28 -05:00
throw new Error(`Get player called with ${player}`)
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
return this.list.find((player) => [player.UUID, player.name].some((item) => item === identifier))
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
getPlayers () {
return Array.from(this.list)
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
removePlayer (player) {
this.list = this.list.filter(({ UUID }) => UUID !== player.UUID)
2022-08-14 05:51:45 -04:00
}
}
2022-12-07 05:28:40 -05:00
function inject (bot, dcclient, config) {
bot.players = new PlayerList()
2022-08-14 05:51:45 -04:00
const tabCompletePlayerList = {
list: [],
interval: setInterval(async () => {
2022-12-07 05:28:40 -05:00
bot.write('tab_complete', {
text: '/scoreboard players add '
})
2022-08-14 05:51:45 -04:00
2022-12-07 05:28:40 -05:00
const [packet] = await EventEmitter.once(bot._client, 'tab_complete')
2022-08-14 05:51:45 -04:00
2022-12-07 05:28:40 -05:00
return packet.matches
2022-11-27 02:35:28 -05:00
.filter((match) => !match.tooltip)
.map(({ match }) => match)
}, 1000 * 3)
2022-08-14 05:51:45 -04:00
}
bot._client.on('player_info', async (packet) => {
2022-08-14 05:51:45 -04:00
for (const player of packet.data) {
switch (packet.action) {
case 0:
2022-11-27 02:35:28 -05:00
addPlayer(player, packet)
break
2022-08-14 05:51:45 -04:00
case 1:
2022-11-27 02:35:28 -05:00
updateGamemode(player, packet)
break
2022-08-14 05:51:45 -04:00
case 2:
2022-11-27 02:35:28 -05:00
updatePing(player, packet)
break
2022-08-14 05:51:45 -04:00
case 3:
2022-11-27 02:35:28 -05:00
updateDisplayName(player, packet)
break
2022-08-14 05:51:45 -04:00
case 4:
2022-11-27 02:35:28 -05:00
removePlayer(player, packet)
break
2022-08-14 05:51:45 -04:00
}
}
2022-11-27 02:35:28 -05:00
})
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
function addPlayer (player, packet) {
2022-12-07 05:28:40 -05:00
if (bot.players.getPlayer(player)) bot.emit('player_unvanished', player, packet)
else bot.emit('player_added', player, packet)
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
bot.players.addPlayer(player)
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
function updateGamemode (player, packet) {
const fullPlayer = bot.players.getPlayer(player)
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
bot.emit('onPlayerGamemodeUpdate', player, packet)
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
if (fullPlayer === undefined) return
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
fullPlayer.gamemode = player.gamemode
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
function updatePing (player, packet) {
const fullPlayer = bot.players.getPlayer(player)
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
bot.emit('player_ping_updated', player, packet)
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
if (fullPlayer === undefined) return
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
fullPlayer.ping = player.ping
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
function updateDisplayName (player, packet) {
const fullPlayer = bot.players.getPlayer(player)
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
bot.emit('player_display_name_updated', player, packet)
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
if (fullPlayer === undefined) return
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
fullPlayer.displayName = player.displayName
2022-08-14 05:51:45 -04:00
}
function removePlayer (player, packet) {
2022-12-07 05:28:40 -05:00
const fullPlayer = bot.players.getPlayer(player)
const players = tabCompletePlayerList.list
2022-12-07 05:28:40 -05:00
if (fullPlayer && players.some((name) => name === fullPlayer.name)) {
bot.emit('player_vanished', player)
2022-12-28 07:08:55 -05:00
} else {
bot.emit('player_removed', player, packet)
bot.players.removePlayer(player)
}
2022-08-14 05:51:45 -04:00
}
bot.on('end', () => {
clearInterval(tabCompletePlayerList.interval)
})
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
module.exports = { inject }