diff --git a/packs/BP/scripts/commands/RenameCommand.js b/packs/BP/scripts/commands/RenameCommand.js new file mode 100644 index 0000000..8758760 --- /dev/null +++ b/packs/BP/scripts/commands/RenameCommand.js @@ -0,0 +1,43 @@ +import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server'; +import { Command } from '../classes/Commands/Command'; +import { structureCollection } from '../classes/Structure/StructureCollection'; +import { findInstance } from '../classes/Commands/lib/findInstance'; +import { commandError } from '../classes/Commands/lib/commandError'; + +export class RenameCommand extends Command { + constructor() { + super({ + name: 'rename', + description: 'construct.commands.rename', + mandatoryParameters: [ + { name: 'instanceName', type: CustomCommandParamType.String }, + { name: 'newName', type: CustomCommandParamType.String } + ], + callback: (source, instanceName, newName) => this.run(source, instanceName, newName) + }); + } + + run(source, instanceName, newName) { + try { + const instance = findInstance(source, instanceName); + if (!instance) return { status: CustomCommandStatus.Failure }; + if (structureCollection.has(newName)) { + system.run(() => source.sendMessage({ + rawtext: [{ translate: 'construct.commands.rename.duplicateName', with: [newName] }] + })); + return { status: CustomCommandStatus.Failure }; + } + system.run(() => { + structureCollection.rename(instanceName, newName); + source.sendMessage({ + rawtext: [{ translate: 'construct.commands.rename.success', with: [instanceName, newName] }] + }); + }); + return { status: CustomCommandStatus.Success }; + } catch (err) { + return commandError(source, err); + } + } +} + +export const renameCommand = new RenameCommand(); diff --git a/packs/BP/scripts/main.js b/packs/BP/scripts/main.js index 18e60b0..93e8159 100644 --- a/packs/BP/scripts/main.js +++ b/packs/BP/scripts/main.js @@ -13,6 +13,7 @@ import './classes/MenuItemHandler'; import './commands/ConstructCommand'; import './commands/NewCommand'; import './commands/DeleteCommand'; +import './commands/RenameCommand'; // Other import './classes/BlockInfo'; diff --git a/packs/RP/texts/en_US.lang b/packs/RP/texts/en_US.lang index aee85a1..3647230 100644 --- a/packs/RP/texts/en_US.lang +++ b/packs/RP/texts/en_US.lang @@ -120,3 +120,8 @@ construct.commands.new.unknownStructure=§cNo structure with id "%1" found in th ## construct:delete construct.commands.delete=Permanently delete an instance. construct.commands.delete.success=§aDeleted instance "%1". + +## construct:rename +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. diff --git a/packs/RP/texts/zh_CN.lang b/packs/RP/texts/zh_CN.lang index 246b4ed..f327f62 100644 --- a/packs/RP/texts/zh_CN.lang +++ b/packs/RP/texts/zh_CN.lang @@ -120,3 +120,8 @@ construct.commands.new.unknownStructure=§cNo structure with id "%1" found in th ## construct:delete construct.commands.delete=Permanently delete an instance. construct.commands.delete.success=§aDeleted instance "%1". + +## construct:rename +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.