From 08d07b23e26f2a7edb6c57b0f9bcae94ccf5b953 Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Wed, 7 Dec 2022 17:28:40 +0700 Subject: [PATCH] players plugin with vanish finally --- plugins/players.js | 73 +++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 49 deletions(-) diff --git a/plugins/players.js b/plugins/players.js index d5ed871..241715a 100644 --- a/plugins/players.js +++ b/plugins/players.js @@ -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) }