feat: rework commands and fix bugs

This commit is contained in:
ForestOfLight
2026-05-20 17:08:03 -07:00
Unverified
parent b1370b5d28
commit 7ea64d1e6a
38 changed files with 527 additions and 525 deletions
+37 -36
View File
@@ -1,8 +1,8 @@
import { CustomCommandParamType, CustomCommandStatus, EntityComponentTypes, system } from '@minecraft/server';
import { CommandPermissionLevel, 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';
import { structureCollection } from '../classes/Structure/StructureCollection';
import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin';
import { NotAPlayerError } from '../classes/Errors/NotAPlayerError';
export class MaterialsCommand extends Command {
constructor() {
@@ -15,44 +15,45 @@ export class MaterialsCommand extends Command {
optionalParameters: [
{ name: 'missing', type: CustomCommandParamType.Boolean }
],
permissionLevel: CommandPermissionLevel.Any,
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);
const instance = structureCollection.get(instanceName);
const onlyMissing = missing === true;
if (onlyMissing)
this.assertIsPlayer(source);
const headerKey = onlyMissing ? 'construct.commands.materials.headerMissing' : 'construct.commands.materials.headerAll';
const rawtext = [
{ translate: headerKey, with: [instanceName] },
{ text: '\n' }
];
const list = this.getMaterialList(source, instance, onlyMissing);
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 };
}
assertIsPlayer(source) {
if (!(source instanceof PlayerCommandOrigin))
throw new NotAPlayerError();
}
getMaterialList(source, instance, onlyMissing) {
const materials = instance.getActiveMaterials();
let container;
if (onlyMissing) {
const player = source.getSource();
const inventoryComponent = player?.getComponent(EntityComponentTypes.Inventory);
container = inventoryComponent?.container;
}
const materialsMap = onlyMissing ? materials.getMaterialsDifference(container) : void 0;
return materials.formatString(materialsMap);
}
}