Skip to content

Commit

Permalink
fix some bugs arising from translations
Browse files Browse the repository at this point in the history
  • Loading branch information
ForestOfLight committed Oct 16, 2024
1 parent a48528b commit 7f25bcb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Canopy [BP]/scripts/lib/canopy/Command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
51 changes: 37 additions & 14 deletions Canopy [BP]/scripts/lib/canopy/help/HelpBook.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HelpPage } from "./HelpPage";
import Utils from "stickycore/utils";
import { CommandHelpEntry } from "./HelpEntry";

class HelpBook {
constructor() {
Expand Down Expand Up @@ -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;
}
}

Expand Down
6 changes: 6 additions & 0 deletions Canopy [BP]/scripts/lib/canopy/registry/CommandRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']});
2 changes: 2 additions & 0 deletions Canopy [BP]/scripts/lib/canopy/registry/RuleRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']});
2 changes: 1 addition & 1 deletion Canopy [BP]/scripts/src/commands/changedimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
14 changes: 11 additions & 3 deletions Canopy [BP]/scripts/src/commands/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}` });
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7f25bcb

Please sign in to comment.