From d2166651beeeef3ad2eeff9fb5acddbfb30782e1 Mon Sep 17 00:00:00 2001 From: Chipmunk <65827213+ChipmunkMC@users.noreply.github.com> Date: Mon, 30 Sep 2024 20:56:05 -0400 Subject: [PATCH] Include ports in netmsg and logs, and make mail fancier --- bot.js | 3 ++- commands/mail.js | 5 ++++- commands/netmsg.js | 3 +-- plugins/!console.js | 2 +- plugins/mail.js | 21 +++++++++++++++++---- util/player_data.js | 4 ++++ 6 files changed, 29 insertions(+), 9 deletions(-) diff --git a/bot.js b/bot.js index f8cb8a6..6d901ec 100644 --- a/bot.js +++ b/bot.js @@ -48,7 +48,8 @@ function createBot (options = {}) { // properties bot.host = options.host - bot.port = options.port + bot.port = options.port ?? 25565 + bot.hostPortPair = bot.port !== 25565 ? bot.host + ':' + bot.port : bot.host bot.brand = options.brand bot.styles = options.styles diff --git a/commands/mail.js b/commands/mail.js index 2b29b58..592b491 100644 --- a/commands/mail.js +++ b/commands/mail.js @@ -1,7 +1,7 @@ const { literal, argument, string, greedyString, DynamicCommandExceptionType } = require('brigadier-commands') const { createUuidSelector } = require('../util/command/utility') -const UNABLE_TO_LOAD_PLAYER_DATA_ERROR = new DynamicCommandExceptionType(error => new TextMessage([{ text: 'An unexpected error occurred trying to send that message', hoverEvent: { action: 'show_text', contents: error.stack } }])) +const UNABLE_TO_SEND_MAIL_ERROR = new DynamicCommandExceptionType(error => new TextMessage([{ text: 'An unexpected error occurred trying to send that message', hoverEvent: { action: 'show_text', contents: error.stack } }])) module.exports = { register (dispatcher) { @@ -48,6 +48,7 @@ module.exports = { try { await bot.sendMail(player.username, username, message) } catch (error) { + bot.console.error(error.stack) throw UNABLE_TO_SEND_MAIL_ERROR.create(error) } @@ -79,6 +80,8 @@ module.exports = { msg[msg.length - 1].text = msg[msg.length - 1].text.slice(0, -1) bot.tellraw(msg, createUuidSelector(player.uuid)) + + delete playerData.data.mailUnread }, clearCommand (context) { diff --git a/commands/netmsg.js b/commands/netmsg.js index db236fb..ae85b93 100755 --- a/commands/netmsg.js +++ b/commands/netmsg.js @@ -19,7 +19,6 @@ module.exports = { const bot = source.bot const message = context.getArgument('message') - const host = bot.host - bot.bots.forEach((bot) => bot.fancyMsg(host, source.displayName, message)) + bot.bots.forEach((bot2) => bot2.fancyMsg(bot.hostPortPair, source.displayName, message)) } } diff --git a/plugins/!console.js b/plugins/!console.js index 749966b..e22c729 100644 --- a/plugins/!console.js +++ b/plugins/!console.js @@ -4,7 +4,7 @@ const CommandSource = require('../util/command/command_source') const Console = require('../util/console') function inject (bot, options) { - bot.console = options.console ? options.console.fork(bot.host) : new Console({ namespace: bot.host }) + bot.console = options.console ? options.console.fork(bot.hostPortPair) : new Console({ namespace: bot.hostPortPair }) bot.on('registry_loaded', () => (bot.console.language = bot.registry.language)) diff --git a/plugins/mail.js b/plugins/mail.js index e11415e..d061ffd 100644 --- a/plugins/mail.js +++ b/plugins/mail.js @@ -17,15 +17,28 @@ function inject (bot) { playerData.data.mail ??= [] playerData.data.mail.push({ sender: sender, message, host: bot.host, port: bot.port }) + const previouslyUnread = playerData.data.mailUnread + playerData.data.mailUnread = true + + if (!loadedManually && !previouslyUnread) { + for (const bot2 of bot.bots) { + const player = bot2.players.find(player => player.username === receiver) + if (player) sendMailNotification(player.uuid) + } + } + if (loadedManually) await playerData.save() } bot.on('player_data_loaded', (player, playerData) => { - if (!playerData.data.mail?.length) return - - const msg = [{ text: "You\'ve got mail!\nRun ", ...bot.styles.primary }, { text: bot.commands.prefixes[0] + 'mail list', ...bot.styles.secondary }, ' to read it'] - bot.tellraw(msg, createUuidSelector(player.uuid)) + if (!playerData.data.mailUnread) return + sendMailNotification(player.uuid) }) + + function sendMailNotification (uuid) { + const msg = [{ text: "You've got mail!\nRun ", ...bot.styles.primary }, { text: bot.commands.prefixes[0] + 'mail list', ...bot.styles.secondary }, ' to read it'] + bot.tellraw(msg, createUuidSelector(uuid)) + } } module.exports = inject diff --git a/util/player_data.js b/util/player_data.js index 5140b09..4a24734 100644 --- a/util/player_data.js +++ b/util/player_data.js @@ -20,6 +20,8 @@ class PlayerData extends PersistentData { parsed.mail = data.mail.value.value.map(this.#parseMail) } + parsed.mailUnread = !!data.mailUnread?.value + return parsed } @@ -50,6 +52,8 @@ class PlayerData extends PersistentData { data.mail = nbt.list(nbt.comp(parsed.mail.map(this.#unparseMail))) } + if (parsed.mailUnread) data.mailUnread = nbt.byte(+parsed.mailUnread) + return nbt.comp(data) }