diff --git a/packs/BP/scripts/commands/MaterialsCommand.js b/packs/BP/scripts/commands/MaterialsCommand.js new file mode 100644 index 0000000..2638e56 --- /dev/null +++ b/packs/BP/scripts/commands/MaterialsCommand.js @@ -0,0 +1,59 @@ +import { CustomCommandParamType, CustomCommandStatus, EntityComponentTypes, system } from '@minecraft/server'; +import { Command } from '../classes/Commands/Command'; +import { findInstance } from '../classes/Commands/lib/findInstance'; +import { requirePlayer } from '../classes/Commands/lib/requirePlayer'; +import { commandError } from '../classes/Commands/lib/commandError'; + +export class MaterialsCommand extends Command { + constructor() { + super({ + name: 'materials', + description: 'construct.commands.materials', + mandatoryParameters: [ + { name: 'instanceName', type: CustomCommandParamType.String } + ], + optionalParameters: [ + { name: 'missing', type: CustomCommandParamType.Boolean } + ], + callback: (source, instanceName, missing) => this.run(source, instanceName, missing) + }); + } + + run(source, instanceName, missing) { + try { + const instance = findInstance(source, instanceName); + if (!instance) return { status: CustomCommandStatus.Failure }; + const onlyMissing = missing === true; + let container; + let headerKey; + if (onlyMissing) { + const player = requirePlayer(source); + container = player.getComponent(EntityComponentTypes.Inventory)?.container; + headerKey = 'construct.commands.materials.headerMissing'; + } else { + headerKey = 'construct.commands.materials.headerAll'; + } + system.run(() => { + const materials = instance.getActiveMaterials(); + const materialsMap = onlyMissing + ? materials.getMaterialsDifference(container) + : undefined; + const list = materials.formatString(materialsMap); + const rawtext = [ + { translate: headerKey, with: [instanceName] }, + { text: '\n' } + ]; + if (!list.rawtext || list.rawtext.length === 0) + rawtext.push({ translate: 'construct.commands.materials.empty' }); + else + rawtext.push(list); + source.sendMessage({ rawtext }); + }); + return { status: CustomCommandStatus.Success }; + } catch (err) { + return commandError(source, err); + } + } +} + +export const materialsCommand = new MaterialsCommand(); diff --git a/packs/BP/scripts/main.js b/packs/BP/scripts/main.js index cee0a1e..b8a94bb 100644 --- a/packs/BP/scripts/main.js +++ b/packs/BP/scripts/main.js @@ -25,6 +25,7 @@ import './commands/VerifierCommand'; import './commands/OptionCommand'; import './commands/InfoCommand'; import './commands/StatsCommand'; +import './commands/MaterialsCommand'; // Other import './classes/BlockInfo'; diff --git a/packs/RP/texts/en_US.lang b/packs/RP/texts/en_US.lang index 3db5aa0..389655a 100644 --- a/packs/RP/texts/en_US.lang +++ b/packs/RP/texts/en_US.lang @@ -182,3 +182,9 @@ construct.commands.info.bounds=§7Bounds:§r (%1, %2, %3) -> (%4, %5, %6) ## construct:stats construct.commands.stats=Run the structure verifier and print statistics. construct.commands.stats.alreadyRunning=§cA verification is already in progress. + +## construct:materials +construct.commands.materials=Print the material list for an instance. +construct.commands.materials.headerAll=§eMaterials for "%1":§r +construct.commands.materials.headerMissing=§eMissing materials for "%1":§r +construct.commands.materials.empty=§7(no materials) diff --git a/packs/RP/texts/zh_CN.lang b/packs/RP/texts/zh_CN.lang index 4af099d..1c009c7 100644 --- a/packs/RP/texts/zh_CN.lang +++ b/packs/RP/texts/zh_CN.lang @@ -182,3 +182,9 @@ construct.commands.info.bounds=§7Bounds:§r (%1, %2, %3) -> (%4, %5, %6) ## construct:stats construct.commands.stats=Run the structure verifier and print statistics. construct.commands.stats.alreadyRunning=§cA verification is already in progress. + +## construct:materials +construct.commands.materials=Print the material list for an instance. +construct.commands.materials.headerAll=§eMaterials for "%1":§r +construct.commands.materials.headerMissing=§eMissing materials for "%1":§r +construct.commands.materials.empty=§7(no materials)