diff --git a/packs/BP/scripts/commands/ListCommand.js b/packs/BP/scripts/commands/ListCommand.js new file mode 100644 index 0000000..c6b2ab8 --- /dev/null +++ b/packs/BP/scripts/commands/ListCommand.js @@ -0,0 +1,54 @@ +import { CustomCommandStatus, system, world } from '@minecraft/server'; +import { Command } from '../classes/Commands/Command'; +import { structureCollection } from '../classes/Structure/StructureCollection'; +import { commandError } from '../classes/Commands/lib/commandError'; + +export class ListCommand extends Command { + constructor() { + super({ + name: 'list', + description: 'construct.commands.list', + callback: (source) => this.run(source) + }); + } + + run(source) { + try { + const names = structureCollection.getInstanceNames(); + if (names.length === 0) { + return { status: CustomCommandStatus.Success, message: 'construct.commands.list.empty' }; + } + system.run(() => { + const rawtext = [ + { translate: 'construct.commands.list.header', with: [String(names.length)] }, + { text: '\n' } + ]; + for (const name of names) { + const instance = structureCollection.get(name); + const status = this.formatStatus(instance); + rawtext.push({ + translate: 'construct.commands.list.row', + with: [name, instance.getStructureId(), status] + }); + rawtext.push({ text: '\n' }); + } + source.sendMessage({ rawtext }); + }); + return { status: CustomCommandStatus.Success }; + } catch (err) { + return commandError(source, err); + } + } + + formatStatus(instance) { + if (!instance.hasLocation()) + return 'no location'; + if (!instance.isEnabled()) + return 'disabled'; + const { dimensionId, location } = instance.getLocation(); + const dim = dimensionId.replace('minecraft:', ''); + return `enabled @ ${location.x} ${location.y} ${location.z} (${dim})`; + } +} + +export const listCommand = new ListCommand(); diff --git a/packs/BP/scripts/main.js b/packs/BP/scripts/main.js index 93e8159..6b9ff4b 100644 --- a/packs/BP/scripts/main.js +++ b/packs/BP/scripts/main.js @@ -14,6 +14,7 @@ import './commands/ConstructCommand'; import './commands/NewCommand'; import './commands/DeleteCommand'; import './commands/RenameCommand'; +import './commands/ListCommand'; // Other import './classes/BlockInfo'; diff --git a/packs/RP/texts/en_US.lang b/packs/RP/texts/en_US.lang index 3647230..7d36c7b 100644 --- a/packs/RP/texts/en_US.lang +++ b/packs/RP/texts/en_US.lang @@ -125,3 +125,9 @@ construct.commands.delete.success=§aDeleted instance "%1". construct.commands.rename=Rename an existing instance. construct.commands.rename.success=§aRenamed instance "%1" to "%2". construct.commands.rename.duplicateName=§cAn instance named "%1" already exists. + +## construct:list +construct.commands.list=List all registered instances. +construct.commands.list.empty=§7No instances registered. +construct.commands.list.header=§eRegistered instances (%1):§r +construct.commands.list.row=§a%1§r §8[§r%2§8]§r §7%3§r diff --git a/packs/RP/texts/zh_CN.lang b/packs/RP/texts/zh_CN.lang index f327f62..9299cfc 100644 --- a/packs/RP/texts/zh_CN.lang +++ b/packs/RP/texts/zh_CN.lang @@ -125,3 +125,9 @@ construct.commands.delete.success=§aDeleted instance "%1". construct.commands.rename=Rename an existing instance. construct.commands.rename.success=§aRenamed instance "%1" to "%2". construct.commands.rename.duplicateName=§cAn instance named "%1" already exists. + +## construct:list +construct.commands.list=List all registered instances. +construct.commands.list.empty=§7No instances registered. +construct.commands.list.header=§eRegistered instances (%1):§r +construct.commands.list.row=§a%1§r §8[§r%2§8]§r §7%3§r