players plugin with vanish finally

This commit is contained in:
ChomeNS 2022-12-07 17:28:40 +07:00
parent 22a94a6daa
commit 08d07b23e2

View file

@ -1,3 +1,4 @@
const { EventEmitter } = require('events')
class PlayerList {
list = []
@ -38,50 +39,22 @@ class PlayerList {
}
}
class TabCompletePlayerRequester {
id = 0
queue = {}
bot
constructor (bot) {
this.bot = bot
bot._client.on('target_packet', (name, data) => {
if (name !== 'tab_complete') return
const players = data.matches
.filter((match) => !match.tooltip)
.map((match) => match.match)
this.returnPlayerList(data.transactionId, players)
})
}
getPlayerList () {
return new Promise((resolve) => {
this.id++
this.id %= 256
this.queue[this.id] = resolve
setTimeout(() => this.returnPlayerList(this.id, this.getPlayerList()), 1000 * 5)
this.bot.write('tab_complete', { transactionId: this.id, text: '/scoreboard players add ' })
})
}
returnPlayerList (id, players) {
if (!this.queue[id]) return
this.queue[id](players)
delete this.queue[id]
}
}
function inject (bot, dcclient, config) {
bot.players = new PlayerList()
bot.requester = new TabCompletePlayerRequester(bot)
const tabCompleteRequester = {
async getPlayerList () {
bot.write('tab_complete', {
text: '/scoreboard players add '
})
const [packet] = await EventEmitter.once(bot._client, 'tab_complete')
return packet.matches
.filter((match) => !match.tooltip)
.map((match /* what should i name this instead of "match"? */) => match.match)
}
}
bot._client.on('player_info', (packet) => {
for (const player of packet.data) {
@ -106,8 +79,8 @@ function inject (bot, dcclient, config) {
})
function addPlayer (player, packet) {
// if (bot.players.getPlayer(player)) bot.emit('player_unvanished', player, packet);
/* else */bot.emit('player_added', player, packet)
if (bot.players.getPlayer(player)) bot.emit('player_unvanished', player, packet)
else bot.emit('player_added', player, packet)
bot.players.addPlayer(player)
}
@ -143,11 +116,13 @@ function inject (bot, dcclient, config) {
}
async function removePlayer (player, packet) {
// 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);
/* else */bot.emit('player_removed', player, packet)
const fullPlayer = bot.players.getPlayer(player)
const players = await tabCompleteRequester.getPlayerList()
if (fullPlayer && players.some((name) => name === fullPlayer.name)) {
bot.emit('player_vanished', player)
return
} else bot.emit('player_removed', player, packet)
bot.players.removePlayer(player)
}