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
+12 -3
View File
@@ -1,4 +1,4 @@
import { CustomCommandSource, CustomCommandStatus, Player, system } from "@minecraft/server";
import { CustomCommandParamType, CustomCommandSource, CustomCommandStatus, Player, RawMessageError, system } from "@minecraft/server";
import { Commands } from "./Commands.js";
import { BlockCommandOrigin } from "./BlockCommandOrigin";
import { EntityCommandOrigin } from "./EntityCommandOrigin";
@@ -60,8 +60,15 @@ export class Command {
this.callback = (origin, ...args) => {
const source = Command.resolveCommandOrigin(origin);
if (this.#commandSourceIsNotAllowed(source))
return { status: CustomCommandStatus.Failure, message: 'commands.generic.invalidsource' };
return this.customCommand.callback(source, ...args);
return { status: CustomCommandStatus.Failure, message: 'construct.error.invalidCommandSource' };
try {
return this.customCommand.callback(source, ...args);
} catch (error) {
if (error instanceof RawMessageError)
error.sendTo(source);
else
throw error;
}
}
}
@@ -88,6 +95,8 @@ export class Command {
return [];
for (const parameter of parameters) {
if (parameter.name)
parameter.name = `${parameter.name}`;
if (parameter.type === CustomCommandParamType.Enum)
parameter.name = `${PACK_IDENTIFIER}:${parameter.name}`;
}
return parameters;
@@ -1,9 +0,0 @@
import { CustomCommandStatus } from '@minecraft/server';
import { NotAPlayerError } from '../../Errors/NotAPlayerError';
export function commandError(source, err) {
if (err instanceof NotAPlayerError) {
return { status: CustomCommandStatus.Failure, message: 'construct.commands.error.notAPlayer' };
}
throw err;
}
@@ -1,12 +0,0 @@
import { system } from '@minecraft/server';
import { structureCollection } from '../../Structure/StructureCollection';
export function findInstance(source, name) {
if (!structureCollection.has(name)) {
system.run(() => source.sendMessage({
rawtext: [{ translate: 'construct.commands.error.instanceNotFound', with: [name] }]
}));
return null;
}
return structureCollection.get(name);
}
@@ -1,8 +0,0 @@
import { PlayerCommandOrigin } from '../PlayerCommandOrigin';
import { NotAPlayerError } from '../../Errors/NotAPlayerError';
export function requirePlayer(source) {
if (!(source instanceof PlayerCommandOrigin))
throw new NotAPlayerError();
return source.getSource();
}
@@ -0,0 +1,14 @@
export class CommandResponseError extends Error {
constructor(message) {
super(message);
this.name = 'CommandResponseError';
}
getRawMessage() {
throw new Error('getRawMessage() must be implemented by subclasses of CommandResponseError');
}
sendTo(source) {
source.sendMessage(this.getRawMessage());
}
}
@@ -0,0 +1,15 @@
import { CommandResponseError } from "./CommandResponseError";
export class InstanceExistsError extends CommandResponseError {
instanceName;
constructor(instanceName) {
super(`An instance with the name "${instanceName}" already exists.`);
this.name = 'InstanceExistsError';
this.instanceName = instanceName;
}
getRawMessage() {
return { translate: 'construct.error.instanceExists', with: [this.instanceName] };
}
}
@@ -0,0 +1,15 @@
import { CommandResponseError } from "./CommandResponseError";
export class InstanceNotFoundError extends CommandResponseError {
instanceName;
constructor(instanceName) {
super(`§cInstance "${instanceName}" not found.`);
this.name = 'InstanceNotFoundError';
this.instanceName = instanceName;
}
getRawMessage() {
return { translate: 'construct.error.instanceNotFound', with: [this.instanceName] };
}
}
@@ -1,6 +0,0 @@
export class InvalidInstanceError extends Error {
constructor(message) {
super(message);
this.name = 'InvalidInstanceError';
}
}
@@ -1,6 +0,0 @@
export class InvalidStructureError extends Error {
constructor(message) {
super(message);
this.name = 'InvalidStructureError';
}
}
@@ -1,6 +1,12 @@
export class NotAPlayerError extends Error {
import { CommandResponseError } from "./CommandResponseError";
export class NotAPlayerError extends CommandResponseError {
constructor(message = 'Command requires a player source.') {
super(message);
this.name = 'NotAPlayerError';
}
getRawMessage() {
return { translate: 'construct.commands.error.notAPlayer' };
}
}
@@ -0,0 +1,15 @@
import { CommandResponseError } from "./CommandResponseError";
export class StructureNotFoundError extends CommandResponseError {
structureId;
constructor(structureId) {
super(`Structure with ID "${structureId}" not found.`);
this.name = 'StructureNotFoundError';
this.structureId = structureId;
}
getRawMessage() {
return { translate: 'construct.error.structureNotFound', with: [this.structureId] };
}
}
@@ -26,6 +26,7 @@ export class InstanceOptions extends Option {
this.instanceName = instanceName;
this.structureId = structureId;
this.load();
this.save();
}
save() {
@@ -1,5 +1,6 @@
import { ItemStack, system } from "@minecraft/server";
import { Vector } from "../../lib/Vector";
import { InstanceNotPlacedError } from "../Errors/InstanceNotPlacedError";
class StructureMaterials {
instance;
@@ -21,11 +22,11 @@ class StructureMaterials {
system.runJob(this.populateActive());
else
system.runJob(this.populateAll());
} catch (e) {
if (e.name === 'InstanceNotPlacedError')
} catch (error) {
if (error instanceof InstanceNotPlacedError)
this.clear();
else
throw e;
throw error;
}
}
+9 -11
View File
@@ -3,6 +3,8 @@ import { structureCollection } from './Structure/StructureCollection';
import { MenuFormBuilder } from './MenuFormBuilder';
import { InstanceForm } from './Instance/InstanceForm';
import { BuilderForm } from './Builder/BuilderForm';
import { InstanceExistsError } from './Errors/InstanceExistsError';
import { StructureNotFoundError } from './Errors/StructureNotFoundError';
export class MenuForm {
constructor(player, { jumpToInstance = false, instanceName = void 0 } = {}) {
@@ -43,12 +45,12 @@ export class MenuForm {
return selectedInstanceName || this.createNewInstance();
}
});
} catch (e) {
if (e.message === 'Menu timed out.') {
} catch (error) {
if (error.message === 'Menu timed out.') {
this.player.sendMessage({ translate: 'construct.menu.open.timeout' });
return void 0;
}
throw e;
throw error;
}
}
@@ -64,16 +66,12 @@ export class MenuForm {
return void 0;
try {
structureCollection.add(instanceName, structureId);
} catch (e) {
if (e.name === 'InvalidInstanceError') {
this.player.sendMessage({ translate: 'construct.mainmenu.instance.exists', with: [instanceName] });
} catch (error) {
if (error instanceof InstanceExistsError || error instanceof StructureNotFoundError) {
error.sendTo(this.player);
return void 0;
}
if (e.name === 'InvalidStructureError') {
this.player.sendMessage({ translate: 'construct.mainmenu.instance.notfound', with: [structureId] });
return void 0;
}
throw e;
throw error;
}
return instanceName;
});
@@ -1,3 +1,4 @@
import { StructureNotFoundError } from '../Errors/StructureNotFoundError';
import { Outliner } from '../Outliner';
export class StructureOutliner {
@@ -13,11 +14,11 @@ export class StructureOutliner {
this.bounds = this.instance.getBounds();
this.bounds.min = this.instance.toGlobalCoords(this.bounds.min);
this.bounds.max = this.instance.toGlobalCoords(this.bounds.max);
} catch (e) {
if (e.name === 'InvalidStructureError')
} catch (error) {
if (error instanceof StructureNotFoundError)
this.outliner.stopDraw();
else
throw e;
throw error;
}
}
@@ -1,6 +1,6 @@
import { world } from "@minecraft/server";
import { Vector } from "../../lib/Vector";
import { InvalidStructureError } from "../Errors/InvalidStructureError";
import { StructureNotFoundError } from "../Errors/StructureNotFoundError";
export class Structure {
structureId;
@@ -10,7 +10,7 @@ export class Structure {
this.structureId = structureId;
this.#structure = world.structureManager.get(structureId);
if (!this.#structure)
throw new InvalidStructureError(`[Construct] Structure '${structureId}' not found on world.`);
throw new StructureNotFoundError(structureId);
this.#structure.saveToWorld();
}
@@ -1,7 +1,9 @@
import { InvalidInstanceError } from '../Errors/InvalidInstanceError';
import { InstanceExistsError } from '../Errors/InstanceExistsError';
import { InstanceNotFoundError } from '../Errors/InstanceNotFoundError';
import { StructureNotFoundError } from '../Errors/StructureNotFoundError';
import { InstanceOptions } from '../Instance/InstanceOptions';
import { StructureInstance } from '../Instance/StructureInstance';
import { world } from '@minecraft/server';
import { InvalidStructureError, world } from '@minecraft/server';
class StructureCollection {
structures;
@@ -27,7 +29,7 @@ class StructureCollection {
add(instanceName, structureId) {
if (this.structures[instanceName])
throw new InvalidInstanceError(`Instance ${instanceName} already exists.`);
throw new InstanceExistsError(instanceName);
const structure = new StructureInstance(instanceName, structureId);
this.structures[instanceName] = structure;
return structure;
@@ -36,7 +38,7 @@ class StructureCollection {
get(instanceName) {
const structure = this.structures[instanceName];
if (!structure)
throw new InvalidInstanceError(`Instance ${instanceName} not found.`);
throw new InstanceNotFoundError(instanceName);
return structure;
}
@@ -58,12 +60,12 @@ class StructureCollection {
return Object.values(this.structures).filter(structure => {
try {
return structure.isLocationActive(dimensionId, structure.toStructureCoords(location), options)
} catch (e) {
if (e.name === 'InvalidStructureError') {
structureCollection.delete(structure.name);
} catch (error) {
if (error instanceof StructureNotFoundError || error instanceof InvalidStructureError) {
this.delete(structure.name);
return false;
} else {
throw e;
throw error;
}
}
});
@@ -101,7 +103,7 @@ class StructureCollection {
rename(instanceName, newName) {
const structure = this.get(instanceName);
if (this.structures[newName])
throw new Error(`Instance '${newName}' already exists.`);
throw new InstanceExistsError(newName);
structure.rename(newName);
this.structures[newName] = structure;
delete this.structures[instanceName];