From 7f25bcb712934cb09cd40ebfe906dd70e598b542 Mon Sep 17 00:00:00 2001 From: ForestOfLight Date: Tue, 15 Oct 2024 23:18:57 -0700 Subject: [PATCH] fix some bugs arising from translations --- Canopy [BP]/scripts/lib/canopy/Command.js | 2 +- .../scripts/lib/canopy/help/HelpBook.js | 51 ++++++++++++++----- .../lib/canopy/registry/CommandRegistry.js | 6 +++ .../lib/canopy/registry/RuleRegistry.js | 2 + .../scripts/src/commands/changedimension.js | 2 +- Canopy [BP]/scripts/src/commands/counter.js | 14 +++-- 6 files changed, 58 insertions(+), 19 deletions(-) diff --git a/Canopy [BP]/scripts/lib/canopy/Command.js b/Canopy [BP]/scripts/lib/canopy/Command.js index 9c73327..1ba868e 100644 --- a/Canopy [BP]/scripts/lib/canopy/Command.js +++ b/Canopy [BP]/scripts/lib/canopy/Command.js @@ -17,7 +17,7 @@ class Command { #extensionName; static prefix = './'; - constructor({ name, description = '', usage, callback, args = [], contingentRules = [], adminOnly = false, helpEntries = [], helpHidden = false, extensionName = false }) { + constructor({ name, description = { text: '' }, usage, callback, args = [], contingentRules = [], adminOnly = false, helpEntries = [], helpHidden = false, extensionName = false }) { this.#name = name; this.#description = description; this.#usage = usage; diff --git a/Canopy [BP]/scripts/lib/canopy/help/HelpBook.js b/Canopy [BP]/scripts/lib/canopy/help/HelpBook.js index 51c1edd..b9c8d52 100644 --- a/Canopy [BP]/scripts/lib/canopy/help/HelpBook.js +++ b/Canopy [BP]/scripts/lib/canopy/help/HelpBook.js @@ -1,5 +1,6 @@ import { HelpPage } from "./HelpPage"; import Utils from "stickycore/utils"; +import { CommandHelpEntry } from "./HelpEntry"; class HelpBook { constructor() { @@ -69,22 +70,44 @@ class HelpBook { } } - if (results.length === 0) { + if (results.length === 0) player.sendMessage({ translate: 'commands.help.search.noresult', with: [searchTerm] }); - } else { - const message = { rawtext: [{ translate: 'commands.help.search.results', with: [searchTerm] }] }; - for (let entry of results) { - const entryRawMessage = await entry.toRawMessage(); - const newEntryTitle = Utils.recolor(entryRawMessage.rawtext[0].text, searchTerm, '§a'); - message.rawtext.push({ - rawtext: [ - { text: '\n ' }, { text: newEntryTitle }, - { translate: entryRawMessage.rawtext[1].translate, with: entryRawMessage.rawtext[1].with } - ] - }); - } - player.sendMessage(message); + else + player.sendMessage(await this.processSearchResults(searchTerm, results)); + } + + async processSearchResults(searchTerm, results) { + let message = { rawtext: [{ translate: 'commands.help.search.results', with: [searchTerm] }] }; + for (const entry of results) { + const entryRawMessage = await this.formatEntryHeader(entry, searchTerm, message); + message = this.formatEntryHelp(entryRawMessage, searchTerm, message); + } + return message; + } + + async formatEntryHeader(entry, searchTerm, message) { + const entryRawMessage = await entry.toRawMessage(); + const newEntryTitle = Utils.recolor(entryRawMessage.rawtext[0].text, searchTerm, '§a'); + message.rawtext.push({ + rawtext: [ + { text: '\n ' }, { text: newEntryTitle }, + { translate: entryRawMessage.rawtext[1].translate, with: entryRawMessage.rawtext[1].with } + ] + }); + return entryRawMessage; + } + + formatEntryHelp(entryRawMessage, searchTerm, message) { + for (let i = 2; i < entryRawMessage.rawtext.length; i++) { + const newEntryText = Utils.recolor(entryRawMessage.rawtext[i].rawtext[0].text, searchTerm, '§a'); + message.rawtext.push({ + rawtext: [ + { text: newEntryText }, + { translate: entryRawMessage.rawtext[i].rawtext[1].translate, with: entryRawMessage.rawtext[i].rawtext[1].with } + ] + }); } + return message; } } diff --git a/Canopy [BP]/scripts/lib/canopy/registry/CommandRegistry.js b/Canopy [BP]/scripts/lib/canopy/registry/CommandRegistry.js index 927b019..1f04e13 100644 --- a/Canopy [BP]/scripts/lib/canopy/registry/CommandRegistry.js +++ b/Canopy [BP]/scripts/lib/canopy/registry/CommandRegistry.js @@ -12,6 +12,12 @@ system.afterEvents.scriptEventReceive.subscribe((event) => { console.warn(`[CommandRegistry] Failed to parse command data: ${error}, ${event.message}`); } if (!cmdData) return; + if (typeof cmdData.description === 'string') + cmdData.description = { text: cmdData.description }; + for (const helpEntry of cmdData.helpEntries) { + if (typeof helpEntry.description === 'string') + helpEntry.description = { text: helpEntry.description }; + } new Command(cmdData); // console.warn(`[Canopy] Registered command: ${cmdData.extensionName}:${cmdData.name}`); }, { namespaces: ['canopyExtension']}); \ No newline at end of file diff --git a/Canopy [BP]/scripts/lib/canopy/registry/RuleRegistry.js b/Canopy [BP]/scripts/lib/canopy/registry/RuleRegistry.js index 76aed18..9eaadf5 100644 --- a/Canopy [BP]/scripts/lib/canopy/registry/RuleRegistry.js +++ b/Canopy [BP]/scripts/lib/canopy/registry/RuleRegistry.js @@ -12,6 +12,8 @@ system.afterEvents.scriptEventReceive.subscribe((event) => { console.warn(`[RuleRegistry] Failed to parse rule data: ${error}, ${event.message}`); } if (!ruleData) return; + if (typeof ruleData.description === 'string') + ruleData.description = { text: ruleData.description }; new Rule(ruleData); // console.warn(`[Canopy] Registered rule: ${ruleData.extensionName}:${ruleData.identifier}`); }, { namespaces: ['canopyExtension']}); \ No newline at end of file diff --git a/Canopy [BP]/scripts/src/commands/changedimension.js b/Canopy [BP]/scripts/src/commands/changedimension.js index 59bc3ac..75ead22 100644 --- a/Canopy [BP]/scripts/src/commands/changedimension.js +++ b/Canopy [BP]/scripts/src/commands/changedimension.js @@ -38,7 +38,7 @@ function changedimensionCommand(player, args) { return cmd.sendUsage(player); const validDimensionId = validDimensions[dimension.toLowerCase()]; if (!validDimensionId) - return player.sendMessage({ translate: 'commands.changedimension.notfound', with: Object.keys(validDimensions).join(', ')}); + return player.sendMessage({ translate: 'commands.changedimension.notfound', with: [Object.keys(validDimensions).join(', ')] }); const validDimension = world.getDimension(validDimensionId); if ((x !== null && y !== null && z !== null) && (Utils.isNumeric(x) && Utils.isNumeric(y) && Utils.isNumeric(z))) { diff --git a/Canopy [BP]/scripts/src/commands/counter.js b/Canopy [BP]/scripts/src/commands/counter.js index b4e5002..5346240 100644 --- a/Canopy [BP]/scripts/src/commands/counter.js +++ b/Canopy [BP]/scripts/src/commands/counter.js @@ -160,7 +160,14 @@ class CounterChannelMap { getQueryOutput(channel) { let realtimeText = this.realtime ? 'realtime: ' : ''; - let message = { rawtext: [{ translate: 'commands.counter.query.channel', with: [formatColor(channel.color), realtimeText, this.getMinutesSinceStart(channel), channel.totalCount, Utils.calculatePerTime(channel.totalCount, this.getDeltaTime(channel), channel.mode)] }] }; + let message = { rawtext: [ + { translate: 'commands.counter.query.channel', with: [ + formatColor(channel.color), + realtimeText, + String(this.getMinutesSinceStart(channel)), + String(channel.totalCount), + Utils.calculatePerTime(channel.totalCount ,this.getDeltaTime(channel), channel.mode) ] + }] }; for (const item of Object.keys(channel.itemMap)) { message.rawtext.push({ text: `\n §7- ${item}: ${getAllModeOutput(channel, item)}` }); } @@ -304,8 +311,9 @@ function queryAll(sender) { message.rawtext.push({ rawtext: [channelMap.getQueryOutput(channel), { text: '\n' }] }); }); - if (output === '') output = { translate: 'commands.counter.query.empty' }; - sender?.sendMessage(output); + if (message == { rawtext: [] }) + message = { translate: 'commands.counter.query.empty' }; + sender?.sendMessage(message); } function setMode(sender, color, mode) {