Files
Construct-CN/packs/BP/scripts/commands/MoveCommand.js
T
2026-05-29 23:26:49 +02:00

48 lines
2.1 KiB
JavaScript

import { Command } from '../classes/Commands/Command';
import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, system, world } from '@minecraft/server';
import { Vector } from '../lib/Vector';
import { structureCollection } from '../classes/Structure/StructureCollection';
import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin';
export class MoveCommand extends Command {
constructor() {
super({
name: 'move',
description: 'construct.commands.move',
mandatoryParameters: [
{ name: 'instanceName', type: CustomCommandParamType.String }
],
optionalParameters: [
{ name: 'dimensionId', type: CustomCommandParamType.Enum }, // Enum defined in PlaceCommand.js
{ name: 'location', type: CustomCommandParamType.Location }
],
permissionLevel: CommandPermissionLevel.Any,
callback: (origin, instanceName, dimensionId, location) => this.run(origin, instanceName, dimensionId, location)
});
}
run(origin, instanceName, dimensionId, location) {
const instance = structureCollection.get(instanceName);
if (dimensionId === void 0 || location === void 0) {
if (!(origin instanceof PlayerCommandOrigin))
return { status: CustomCommandStatus.Failure, message: 'construct.commands.move.locationRequired' };
const player = origin.getSource();
location = player.location;
dimensionId = player.dimension.id;
}
this.assertDimensionExists(dimensionId);
const flooredLocation = Vector.from(location).floor();
system.run(() => {
instance.move(dimensionId, flooredLocation);
origin.sendMessage({ translate: 'construct.commands.move.success', with: [instanceName, flooredLocation.toString(), dimensionId.replace('minecraft:', '')] });
});
return { status: CustomCommandStatus.Success };
}
assertDimensionExists(dimensionId) {
return world.getDimension(dimensionId) !== void 0;
}
}
export const moveCommand = new MoveCommand();