chomens-bot-js/plugins/players.js

157 lines
3.5 KiB
JavaScript
Raw Normal View History

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
}
}
class TabCompletePlayerRequester {
2022-11-27 02:35:28 -05:00
id = 0
queue = {}
bot
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
constructor (bot) {
this.bot = bot
2022-08-14 05:51:45 -04:00
2022-11-11 06:17:50 -05:00
bot._client.on('target_packet', (name, data) => {
2022-11-27 02:35:28 -05:00
if (name !== 'tab_complete') return
2022-08-14 05:51:45 -04:00
const players = data.matches
2022-11-27 02:35:28 -05:00
.filter((match) => !match.tooltip)
.map((match) => match.match)
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
this.returnPlayerList(data.transactionId, players)
})
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
getPlayerList () {
2022-08-14 05:51:45 -04:00
return new Promise((resolve) => {
2022-11-27 02:35:28 -05:00
this.id++
this.id %= 256
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
this.queue[this.id] = resolve
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
setTimeout(() => this.returnPlayerList(this.id, this.getPlayerList()), 1000 * 5)
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
this.bot.write('tab_complete', { transactionId: this.id, text: '/scoreboard players add ' })
})
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
returnPlayerList (id, players) {
if (!this.queue[id]) return
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
this.queue[id](players)
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
delete this.queue[id]
2022-08-14 05:51:45 -04:00
}
}
2022-11-27 02:35:28 -05:00
function inject (bot, dcclient, config) {
bot.players = new PlayerList()
bot.requester = new TabCompletePlayerRequester(bot)
2022-08-14 05:51:45 -04:00
bot._client.on('player_info', (packet) => {
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-11-11 06:17:50 -05:00
// if (bot.players.getPlayer(player)) bot.emit('player_unvanished', player, packet);
2022-11-27 02:35:28 -05:00
/* 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
}
2022-11-27 02:35:28 -05:00
async function removePlayer (player, packet) {
2022-11-11 06:17:50 -05:00
// const fullPlayer = bot.players.getPlayer(player);
// const players = await bot.requester.getPlayerList();
//
// if (fullPlayer && players.some((name) => name === fullPlayer.name)) bot.emit('player_vanished', player);
2022-11-27 02:35:28 -05:00
/* else */bot.emit('player_removed', player, packet)
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
bot.players.removePlayer(player)
2022-08-14 05:51:45 -04:00
}
}
2022-11-27 02:35:28 -05:00
module.exports = { inject }