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
+1
View File
@@ -3,3 +3,4 @@
/.regolith /.regolith
docs/ docs/
.claude .claude
node_modules/
+36
View File
@@ -0,0 +1,36 @@
{
"name": "Construct",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"dependencies": {
"@minecraft/server": "^2.8.0-beta.1.26.21-stable"
}
},
"node_modules/@minecraft/common": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/@minecraft/common/-/common-1.3.0.tgz",
"integrity": "sha512-GLT8USFhvEyeTTFHZAgszbrnoT007hmXmK+aO4l+2A1up9/zwZ+4e8R8F0KcKrCWDjLEqkOJtPow0hQCcNJ++Q==",
"license": "MIT",
"peer": true
},
"node_modules/@minecraft/server": {
"version": "2.8.0-beta.1.26.21-stable",
"resolved": "https://registry.npmjs.org/@minecraft/server/-/server-2.8.0-beta.1.26.21-stable.tgz",
"integrity": "sha512-HdR2EjmleJBBuaXH4iEobdXh7dcuWtV8AIZn2Kd+UkvlBPc4iIPSqXaEt7nqOk0tyhbKfRs/X3aoVtAMHW+kmQ==",
"license": "MIT",
"peerDependencies": {
"@minecraft/common": "^1.2.0",
"@minecraft/vanilla-data": ">=1.20.70"
}
},
"node_modules/@minecraft/vanilla-data": {
"version": "1.26.21",
"resolved": "https://registry.npmjs.org/@minecraft/vanilla-data/-/vanilla-data-1.26.21.tgz",
"integrity": "sha512-bDmqSIjZBoaChpAdK2H3SVzhIod4/kXwY+viEA3AgftAbda1X1dkjMiBbxZcmwzRrVwT9embIB3zsEkYn1rSNA==",
"license": "MIT",
"peer": true
}
}
}
+5
View File
@@ -0,0 +1,5 @@
{
"dependencies": {
"@minecraft/server": "^2.8.0-beta.1.26.21-stable"
}
}
+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 { Commands } from "./Commands.js";
import { BlockCommandOrigin } from "./BlockCommandOrigin"; import { BlockCommandOrigin } from "./BlockCommandOrigin";
import { EntityCommandOrigin } from "./EntityCommandOrigin"; import { EntityCommandOrigin } from "./EntityCommandOrigin";
@@ -60,8 +60,15 @@ export class Command {
this.callback = (origin, ...args) => { this.callback = (origin, ...args) => {
const source = Command.resolveCommandOrigin(origin); const source = Command.resolveCommandOrigin(origin);
if (this.#commandSourceIsNotAllowed(source)) if (this.#commandSourceIsNotAllowed(source))
return { status: CustomCommandStatus.Failure, message: 'commands.generic.invalidsource' }; return { status: CustomCommandStatus.Failure, message: 'construct.error.invalidCommandSource' };
return this.customCommand.callback(source, ...args); 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 []; return [];
for (const parameter of parameters) { for (const parameter of parameters) {
if (parameter.name) if (parameter.name)
parameter.name = `${parameter.name}`;
if (parameter.type === CustomCommandParamType.Enum)
parameter.name = `${PACK_IDENTIFIER}:${parameter.name}`; parameter.name = `${PACK_IDENTIFIER}:${parameter.name}`;
} }
return parameters; 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.') { constructor(message = 'Command requires a player source.') {
super(message); super(message);
this.name = 'NotAPlayerError'; 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.instanceName = instanceName;
this.structureId = structureId; this.structureId = structureId;
this.load(); this.load();
this.save();
} }
save() { save() {
@@ -1,5 +1,6 @@
import { ItemStack, system } from "@minecraft/server"; import { ItemStack, system } from "@minecraft/server";
import { Vector } from "../../lib/Vector"; import { Vector } from "../../lib/Vector";
import { InstanceNotPlacedError } from "../Errors/InstanceNotPlacedError";
class StructureMaterials { class StructureMaterials {
instance; instance;
@@ -21,11 +22,11 @@ class StructureMaterials {
system.runJob(this.populateActive()); system.runJob(this.populateActive());
else else
system.runJob(this.populateAll()); system.runJob(this.populateAll());
} catch (e) { } catch (error) {
if (e.name === 'InstanceNotPlacedError') if (error instanceof InstanceNotPlacedError)
this.clear(); this.clear();
else else
throw e; throw error;
} }
} }
+9 -11
View File
@@ -3,6 +3,8 @@ import { structureCollection } from './Structure/StructureCollection';
import { MenuFormBuilder } from './MenuFormBuilder'; import { MenuFormBuilder } from './MenuFormBuilder';
import { InstanceForm } from './Instance/InstanceForm'; import { InstanceForm } from './Instance/InstanceForm';
import { BuilderForm } from './Builder/BuilderForm'; import { BuilderForm } from './Builder/BuilderForm';
import { InstanceExistsError } from './Errors/InstanceExistsError';
import { StructureNotFoundError } from './Errors/StructureNotFoundError';
export class MenuForm { export class MenuForm {
constructor(player, { jumpToInstance = false, instanceName = void 0 } = {}) { constructor(player, { jumpToInstance = false, instanceName = void 0 } = {}) {
@@ -43,12 +45,12 @@ export class MenuForm {
return selectedInstanceName || this.createNewInstance(); return selectedInstanceName || this.createNewInstance();
} }
}); });
} catch (e) { } catch (error) {
if (e.message === 'Menu timed out.') { if (error.message === 'Menu timed out.') {
this.player.sendMessage({ translate: 'construct.menu.open.timeout' }); this.player.sendMessage({ translate: 'construct.menu.open.timeout' });
return void 0; return void 0;
} }
throw e; throw error;
} }
} }
@@ -64,16 +66,12 @@ export class MenuForm {
return void 0; return void 0;
try { try {
structureCollection.add(instanceName, structureId); structureCollection.add(instanceName, structureId);
} catch (e) { } catch (error) {
if (e.name === 'InvalidInstanceError') { if (error instanceof InstanceExistsError || error instanceof StructureNotFoundError) {
this.player.sendMessage({ translate: 'construct.mainmenu.instance.exists', with: [instanceName] }); error.sendTo(this.player);
return void 0; return void 0;
} }
if (e.name === 'InvalidStructureError') { throw error;
this.player.sendMessage({ translate: 'construct.mainmenu.instance.notfound', with: [structureId] });
return void 0;
}
throw e;
} }
return instanceName; return instanceName;
}); });
@@ -1,3 +1,4 @@
import { StructureNotFoundError } from '../Errors/StructureNotFoundError';
import { Outliner } from '../Outliner'; import { Outliner } from '../Outliner';
export class StructureOutliner { export class StructureOutliner {
@@ -13,11 +14,11 @@ export class StructureOutliner {
this.bounds = this.instance.getBounds(); this.bounds = this.instance.getBounds();
this.bounds.min = this.instance.toGlobalCoords(this.bounds.min); this.bounds.min = this.instance.toGlobalCoords(this.bounds.min);
this.bounds.max = this.instance.toGlobalCoords(this.bounds.max); this.bounds.max = this.instance.toGlobalCoords(this.bounds.max);
} catch (e) { } catch (error) {
if (e.name === 'InvalidStructureError') if (error instanceof StructureNotFoundError)
this.outliner.stopDraw(); this.outliner.stopDraw();
else else
throw e; throw error;
} }
} }
@@ -1,6 +1,6 @@
import { world } from "@minecraft/server"; import { world } from "@minecraft/server";
import { Vector } from "../../lib/Vector"; import { Vector } from "../../lib/Vector";
import { InvalidStructureError } from "../Errors/InvalidStructureError"; import { StructureNotFoundError } from "../Errors/StructureNotFoundError";
export class Structure { export class Structure {
structureId; structureId;
@@ -10,7 +10,7 @@ export class Structure {
this.structureId = structureId; this.structureId = structureId;
this.#structure = world.structureManager.get(structureId); this.#structure = world.structureManager.get(structureId);
if (!this.#structure) if (!this.#structure)
throw new InvalidStructureError(`[Construct] Structure '${structureId}' not found on world.`); throw new StructureNotFoundError(structureId);
this.#structure.saveToWorld(); 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 { InstanceOptions } from '../Instance/InstanceOptions';
import { StructureInstance } from '../Instance/StructureInstance'; import { StructureInstance } from '../Instance/StructureInstance';
import { world } from '@minecraft/server'; import { InvalidStructureError, world } from '@minecraft/server';
class StructureCollection { class StructureCollection {
structures; structures;
@@ -27,7 +29,7 @@ class StructureCollection {
add(instanceName, structureId) { add(instanceName, structureId) {
if (this.structures[instanceName]) if (this.structures[instanceName])
throw new InvalidInstanceError(`Instance ${instanceName} already exists.`); throw new InstanceExistsError(instanceName);
const structure = new StructureInstance(instanceName, structureId); const structure = new StructureInstance(instanceName, structureId);
this.structures[instanceName] = structure; this.structures[instanceName] = structure;
return structure; return structure;
@@ -36,7 +38,7 @@ class StructureCollection {
get(instanceName) { get(instanceName) {
const structure = this.structures[instanceName]; const structure = this.structures[instanceName];
if (!structure) if (!structure)
throw new InvalidInstanceError(`Instance ${instanceName} not found.`); throw new InstanceNotFoundError(instanceName);
return structure; return structure;
} }
@@ -58,12 +60,12 @@ class StructureCollection {
return Object.values(this.structures).filter(structure => { return Object.values(this.structures).filter(structure => {
try { try {
return structure.isLocationActive(dimensionId, structure.toStructureCoords(location), options) return structure.isLocationActive(dimensionId, structure.toStructureCoords(location), options)
} catch (e) { } catch (error) {
if (e.name === 'InvalidStructureError') { if (error instanceof StructureNotFoundError || error instanceof InvalidStructureError) {
structureCollection.delete(structure.name); this.delete(structure.name);
return false; return false;
} else { } else {
throw e; throw error;
} }
} }
}); });
@@ -101,7 +103,7 @@ class StructureCollection {
rename(instanceName, newName) { rename(instanceName, newName) {
const structure = this.get(instanceName); const structure = this.get(instanceName);
if (this.structures[newName]) if (this.structures[newName])
throw new Error(`Instance '${newName}' already exists.`); throw new InstanceExistsError(newName);
structure.rename(newName); structure.rename(newName);
this.structures[newName] = structure; this.structures[newName] = structure;
delete this.structures[instanceName]; delete this.structures[instanceName];
+19 -23
View File
@@ -1,7 +1,6 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { findInstance } from '../classes/Commands/lib/findInstance'; import { CustomCommandParamType, CustomCommandStatus, CommandPermissionLevel, system } from '@minecraft/server';
import { commandError } from '../classes/Commands/lib/commandError'; import { structureCollection } from '../classes/Structure/StructureCollection';
export class ActiveCommand extends Command { export class ActiveCommand extends Command {
constructor() { constructor() {
@@ -12,32 +11,29 @@ export class ActiveCommand extends Command {
{ name: 'instanceName', type: CustomCommandParamType.String }, { name: 'instanceName', type: CustomCommandParamType.String },
{ name: 'state', type: CustomCommandParamType.Boolean } { name: 'state', type: CustomCommandParamType.Boolean }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName, state) => this.run(source, instanceName, state) callback: (source, instanceName, state) => this.run(source, instanceName, state)
}); });
} }
run(source, instanceName, state) { run(source, instanceName, state) {
try { const instance = structureCollection.get(instanceName);
const instance = findInstance(source, instanceName); if (state && !instance.hasLocation()) {
if (!instance) return { status: CustomCommandStatus.Failure }; source.sendMessage({ translate: 'construct.commands.error.noLocation', with: [instanceName] });
if (state && !instance.hasLocation()) { return void 0;
system.run(() => source.sendMessage({
rawtext: [{ translate: 'construct.commands.error.noLocation', with: [instanceName] }]
}));
return { status: CustomCommandStatus.Failure };
}
system.run(() => {
if (state) instance.enable();
else instance.disable();
source.sendMessage({
rawtext: [{ translate: 'construct.commands.active.success',
with: [instanceName, String(state)] }]
});
});
return { status: CustomCommandStatus.Success };
} catch (err) {
return commandError(source, err);
} }
system.run(() => {
if (state)
instance.enable();
else
instance.disable();
this.sendFeedback(source, instanceName, state);
});
return { status: CustomCommandStatus.Success };
}
sendFeedback(source, instanceName, state) {
source.sendMessage({ translate: state ? 'construct.commands.active.true' : 'construct.commands.active.false', with: [instanceName] });
} }
} }
+16 -17
View File
@@ -1,8 +1,6 @@
import { CommandPermissionLevel, CustomCommandStatus, EntityComponentTypes, ItemStack, system } from '@minecraft/server'; import { CommandPermissionLevel, CustomCommandStatus, EntityComponentTypes, ItemStack, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin'; import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin';
import { requirePlayer } from '../classes/Commands/lib/requirePlayer';
import { commandError } from '../classes/Commands/lib/commandError';
import { MENU_ITEM } from '../consts'; import { MENU_ITEM } from '../consts';
export class ConstructCommand extends Command { export class ConstructCommand extends Command {
@@ -10,28 +8,29 @@ export class ConstructCommand extends Command {
super({ super({
name: 'construct', name: 'construct',
description: 'construct.commands.construct', description: 'construct.commands.construct',
permissionLevel: CommandPermissionLevel.Any,
cheatsRequired: false, cheatsRequired: false,
allowedSources: [PlayerCommandOrigin], allowedSources: [PlayerCommandOrigin],
permissionLevel: CommandPermissionLevel.Any,
callback: (source) => this.run(source) callback: (source) => this.run(source)
}); });
} }
run(source) { run(source) {
try { const player = source.getSource();
const player = requirePlayer(source); system.run(() => {
system.run(() => { this.giveMenuItem(player);
const remaining = player.getComponent(EntityComponentTypes.Inventory) });
?.container?.addItem(new ItemStack(MENU_ITEM)); return { status: CustomCommandStatus.Success };
if (remaining) }
player.sendMessage({ translate: 'construct.commands.construct.fail' });
else giveMenuItem(player) {
player.sendMessage({ translate: 'construct.commands.construct.success' }); const inventoryComponent = player.getComponent(EntityComponentTypes.Inventory);
}); const inventoryContainer = inventoryComponent?.container;
return { status: CustomCommandStatus.Success }; const remaining = inventoryContainer?.addItem(new ItemStack(MENU_ITEM));
} catch (err) { if (remaining)
return commandError(source, err); player.sendMessage({ translate: 'construct.commands.construct.fail' });
} else
player.sendMessage({ translate: 'construct.commands.construct.success' });
} }
} }
+8 -16
View File
@@ -1,8 +1,6 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { CustomCommandParamType, CustomCommandStatus, CommandPermissionLevel, system } from '@minecraft/server';
import { structureCollection } from '../classes/Structure/StructureCollection'; import { structureCollection } from '../classes/Structure/StructureCollection';
import { findInstance } from '../classes/Commands/lib/findInstance';
import { commandError } from '../classes/Commands/lib/commandError';
export class DeleteCommand extends Command { export class DeleteCommand extends Command {
constructor() { constructor() {
@@ -12,24 +10,18 @@ export class DeleteCommand extends Command {
mandatoryParameters: [ mandatoryParameters: [
{ name: 'instanceName', type: CustomCommandParamType.String } { name: 'instanceName', type: CustomCommandParamType.String }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName) => this.run(source, instanceName) callback: (source, instanceName) => this.run(source, instanceName)
}); });
} }
run(source, instanceName) { run(source, instanceName) {
try { const instance = structureCollection.get(instanceName);
const instance = findInstance(source, instanceName); system.run(() => {
if (!instance) return { status: CustomCommandStatus.Failure }; structureCollection.delete(instanceName);
system.run(() => { source.sendMessage({ translate: 'construct.commands.delete.success', with: [instanceName] });
structureCollection.delete(instanceName); });
source.sendMessage({ return { status: CustomCommandStatus.Success };
rawtext: [{ translate: 'construct.commands.delete.success', with: [instanceName] }]
});
});
return { status: CustomCommandStatus.Success };
} catch (err) {
return commandError(source, err);
}
} }
} }
+55 -38
View File
@@ -1,7 +1,7 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server'; import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { findInstance } from '../classes/Commands/lib/findInstance'; import { structureCollection } from '../classes/Structure/StructureCollection';
import { commandError } from '../classes/Commands/lib/commandError'; import { Vector } from '../lib/Vector';
export class InfoCommand extends Command { export class InfoCommand extends Command {
constructor() { constructor() {
@@ -11,46 +11,63 @@ export class InfoCommand extends Command {
mandatoryParameters: [ mandatoryParameters: [
{ name: 'instanceName', type: CustomCommandParamType.String } { name: 'instanceName', type: CustomCommandParamType.String }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName) => this.run(source, instanceName) callback: (source, instanceName) => this.run(source, instanceName)
}); });
} }
run(source, instanceName) { run(source, instanceName) {
try { const instance = structureCollection.get(instanceName);
const instance = findInstance(source, instanceName); const message = { rawtext: [
if (!instance) return { status: CustomCommandStatus.Failure }; this.getHeaderText(instance),
const rawtext = [ { text: '\n' },
{ translate: 'construct.commands.info.header', with: [instance.getName()] }, this.getStructureIdText(instance),
{ text: '\n' }, { text: '\n' },
{ translate: 'construct.commands.info.structure', with: [instance.getStructureId()] }, this.getLocationText(instance),
{ text: '\n' }, { text: '\n' },
{ translate: 'construct.commands.info.enabled', with: [String(instance.isEnabled())] }, this.getEnabledText(instance),
{ text: '\n' } { text: '\n' },
]; this.getLayerText(instance),
if (instance.hasLocation()) { { text: '\n' },
const { dimensionId, location } = instance.getLocation(); this.getVerifierText(instance),
rawtext.push({ translate: 'construct.commands.info.location', { text: '\n' },
with: [String(location.x), String(location.y), String(location.z), this.getSizeText(instance)
dimensionId.replace('minecraft:', '')] }); ]};
} else { source.sendMessage(message);
rawtext.push({ translate: 'construct.commands.info.noLocation' }); return { status: CustomCommandStatus.Success };
} }
rawtext.push({ text: '\n' });
rawtext.push({ translate: 'construct.commands.info.layer', getHeaderText(instance) {
with: [String(instance.getLayer()), String(instance.getMaxLayer())] }); return { translate: 'construct.commands.info.header', with: [instance.getName()] };
rawtext.push({ text: '\n' }); }
rawtext.push({ translate: 'construct.commands.info.verifier',
with: [String(instance.options.verifier.isEnabled)] }); getStructureIdText(instance) {
rawtext.push({ text: '\n' }); return { translate: 'construct.commands.info.structure', with: [instance.getStructureId()] };
const bounds = instance.getBounds(); }
rawtext.push({ translate: 'construct.commands.info.bounds',
with: [String(bounds.min.x), String(bounds.min.y), String(bounds.min.z), getLocationText(instance) {
String(bounds.max.x), String(bounds.max.y), String(bounds.max.z)] }); if (!instance.hasLocation())
system.run(() => source.sendMessage({ rawtext })); return { translate: 'construct.commands.info.noLocation' };
return { status: CustomCommandStatus.Success }; const { dimensionId, location } = instance.getLocation();
} catch (err) { return { translate: 'construct.commands.info.location', with: [location.toString(), dimensionId.replace('minecraft:', '')] };
return commandError(source, err); }
}
getEnabledText(instance) {
return { translate: 'construct.commands.info.enabled', with: [String(instance.isEnabled())] };
}
getLayerText(instance) {
return { translate: 'construct.commands.info.layer', with: [String(instance.getLayer()), String(instance.getMaxLayer())] };
}
getVerifierText(instance) {
const verifier = instance.options.verifier;
return { translate: 'construct.commands.info.verifier', with: [String(verifier.isEnabled)] };
}
getSizeText(instance) {
const bounds = instance.getBounds();
return { translate: 'construct.commands.info.size', with: [bounds.max.toString(), Vector.volume(bounds.min, bounds.max).toString()] };
} }
} }
+11 -24
View File
@@ -1,7 +1,6 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { findInstance } from '../classes/Commands/lib/findInstance'; import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { commandError } from '../classes/Commands/lib/commandError'; import { structureCollection } from '../classes/Structure/StructureCollection';
export class LayerCommand extends Command { export class LayerCommand extends Command {
constructor() { constructor() {
@@ -12,33 +11,21 @@ export class LayerCommand extends Command {
{ name: 'instanceName', type: CustomCommandParamType.String }, { name: 'instanceName', type: CustomCommandParamType.String },
{ name: 'layer', type: CustomCommandParamType.Integer } { name: 'layer', type: CustomCommandParamType.Integer }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName, layer) => this.run(source, instanceName, layer) callback: (source, instanceName, layer) => this.run(source, instanceName, layer)
}); });
} }
run(source, instanceName, layer) { run(source, instanceName, layer) {
try { const instance = structureCollection.get(instanceName);
const instance = findInstance(source, instanceName); const max = instance.getMaxLayer();
if (!instance) return { status: CustomCommandStatus.Failure }; if (layer < 0 || layer > max) {
const max = instance.getMaxLayer(); source.sendMessage({ translate: 'construct.commands.layer.outOfBounds', with: [String(layer), instanceName, String(max)] });
if (layer < 0 || layer > max) { return void 0;
system.run(() => source.sendMessage({
rawtext: [{ translate: 'construct.commands.layer.outOfBounds',
with: [String(layer), instanceName, String(max)] }]
}));
return { status: CustomCommandStatus.Failure };
}
system.run(() => {
instance.setLayer(layer);
source.sendMessage({
rawtext: [{ translate: 'construct.commands.layer.success',
with: [instanceName, String(layer)] }]
});
});
return { status: CustomCommandStatus.Success };
} catch (err) {
return commandError(source, err);
} }
instance.setLayer(layer);
source.sendMessage({ translate: 'construct.commands.layer.success', with: [instanceName, String(layer)] });
return { status: CustomCommandStatus.Success };
} }
} }
+30 -32
View File
@@ -1,53 +1,51 @@
import { CustomCommandStatus, system } from '@minecraft/server'; import { CommandPermissionLevel, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { structureCollection } from '../classes/Structure/StructureCollection'; import { structureCollection } from '../classes/Structure/StructureCollection';
import { commandError } from '../classes/Commands/lib/commandError';
export class ListCommand extends Command { export class ListCommand extends Command {
constructor() { constructor() {
super({ super({
name: 'list', name: 'list',
description: 'construct.commands.list', description: 'construct.commands.list',
permissionLevel: CommandPermissionLevel.Any,
callback: (source) => this.run(source) callback: (source) => this.run(source)
}); });
} }
run(source) { run(source) {
try { const names = structureCollection.getInstanceNames();
const names = structureCollection.getInstanceNames(); if (names.length === 0)
if (names.length === 0) { return { status: CustomCommandStatus.Success, message: 'construct.commands.list.empty' };
return { status: CustomCommandStatus.Success, message: 'construct.commands.list.empty' }; const rawtext = [
} { translate: 'construct.commands.list.header', with: [String(names.length)] },
system.run(() => { { text: '\n' }
const rawtext = [ ];
{ translate: 'construct.commands.list.header', with: [String(names.length)] }, for (const name of names) {
{ text: '\n' } const instance = structureCollection.get(name);
]; const status = this.formatStatus(instance);
for (const name of names) { rawtext.push({
const instance = structureCollection.get(name); translate: 'construct.commands.list.row',
const status = this.formatStatus(instance); with: { rawtext: [{ text: name }, { text: instance.getStructureId() }, status] }
rawtext.push({
translate: 'construct.commands.list.row',
with: [name, instance.getStructureId(), status]
});
rawtext.push({ text: '\n' });
}
source.sendMessage({ rawtext });
}); });
return { status: CustomCommandStatus.Success }; rawtext.push({ text: '\n' });
} catch (err) {
return commandError(source, err);
} }
source.sendMessage({ rawtext });
return { status: CustomCommandStatus.Success };
} }
formatStatus(instance) { formatStatus(instance) {
if (!instance.hasLocation()) const statusMessage = { rawtext: [] };
return 'no location'; if (instance.isEnabled())
if (!instance.isEnabled()) statusMessage.rawtext.push({ translate: 'construct.commands.list.row.enabled' });
return 'disabled'; else
const { dimensionId, location } = instance.getLocation(); statusMessage.rawtext.push({ translate: 'construct.commands.list.row.disabled' });
const dim = dimensionId.replace('minecraft:', ''); if (instance.hasLocation()) {
return `enabled @ ${location.x} ${location.y} ${location.z} (${dim})`; const { dimensionId, location } = instance.getLocation();
statusMessage.rawtext.push({ translate: 'construct.commands.list.row.location', with: [location.toString(), dimensionId.replace('minecraft:', '')] });
} else {
statusMessage.rawtext.push({ translate: 'construct.commands.list.row.nolocation' });
}
return statusMessage;
} }
} }
+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 { Command } from '../classes/Commands/Command';
import { findInstance } from '../classes/Commands/lib/findInstance'; import { structureCollection } from '../classes/Structure/StructureCollection';
import { requirePlayer } from '../classes/Commands/lib/requirePlayer'; import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin';
import { commandError } from '../classes/Commands/lib/commandError'; import { NotAPlayerError } from '../classes/Errors/NotAPlayerError';
export class MaterialsCommand extends Command { export class MaterialsCommand extends Command {
constructor() { constructor() {
@@ -15,44 +15,45 @@ export class MaterialsCommand extends Command {
optionalParameters: [ optionalParameters: [
{ name: 'missing', type: CustomCommandParamType.Boolean } { name: 'missing', type: CustomCommandParamType.Boolean }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName, missing) => this.run(source, instanceName, missing) callback: (source, instanceName, missing) => this.run(source, instanceName, missing)
}); });
} }
run(source, instanceName, missing) { run(source, instanceName, missing) {
try { const instance = structureCollection.get(instanceName);
const instance = findInstance(source, instanceName); const onlyMissing = missing === true;
if (!instance) return { status: CustomCommandStatus.Failure }; if (onlyMissing)
const onlyMissing = missing === true; this.assertIsPlayer(source);
let container; const headerKey = onlyMissing ? 'construct.commands.materials.headerMissing' : 'construct.commands.materials.headerAll';
let headerKey; const rawtext = [
if (onlyMissing) { { translate: headerKey, with: [instanceName] },
const player = requirePlayer(source); { text: '\n' }
container = player.getComponent(EntityComponentTypes.Inventory)?.container; ];
headerKey = 'construct.commands.materials.headerMissing'; const list = this.getMaterialList(source, instance, onlyMissing);
} else { if (!list.rawtext || list.rawtext.length === 0)
headerKey = 'construct.commands.materials.headerAll'; rawtext.push({ translate: 'construct.commands.materials.empty' });
} else
system.run(() => { rawtext.push(list);
const materials = instance.getActiveMaterials(); source.sendMessage({ rawtext });
const materialsMap = onlyMissing return { status: CustomCommandStatus.Success };
? materials.getMaterialsDifference(container) }
: undefined;
const list = materials.formatString(materialsMap); assertIsPlayer(source) {
const rawtext = [ if (!(source instanceof PlayerCommandOrigin))
{ translate: headerKey, with: [instanceName] }, throw new NotAPlayerError();
{ text: '\n' } }
];
if (!list.rawtext || list.rawtext.length === 0) getMaterialList(source, instance, onlyMissing) {
rawtext.push({ translate: 'construct.commands.materials.empty' }); const materials = instance.getActiveMaterials();
else let container;
rawtext.push(list); if (onlyMissing) {
source.sendMessage({ rawtext }); const player = source.getSource();
}); const inventoryComponent = player?.getComponent(EntityComponentTypes.Inventory);
return { status: CustomCommandStatus.Success }; container = inventoryComponent?.container;
} catch (err) {
return commandError(source, err);
} }
const materialsMap = onlyMissing ? materials.getMaterialsDifference(container) : void 0;
return materials.formatString(materialsMap);
} }
} }
+24 -39
View File
@@ -1,10 +1,8 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { findInstance } from '../classes/Commands/lib/findInstance'; import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, system, world } from '@minecraft/server';
import { commandError } from '../classes/Commands/lib/commandError'; import { Vector } from '../lib/Vector';
import { structureCollection } from '../classes/Structure/StructureCollection';
import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin'; import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin';
import { BlockCommandOrigin } from '../classes/Commands/BlockCommandOrigin';
import { EntityCommandOrigin } from '../classes/Commands/EntityCommandOrigin';
export class MoveCommand extends Command { export class MoveCommand extends Command {
constructor() { constructor() {
@@ -15,47 +13,34 @@ export class MoveCommand extends Command {
{ name: 'instanceName', type: CustomCommandParamType.String } { name: 'instanceName', type: CustomCommandParamType.String }
], ],
optionalParameters: [ optionalParameters: [
{ name: 'pos', type: CustomCommandParamType.Location } { name: 'dimensionId', type: CustomCommandParamType.Enum }, // Enum defined in PlaceCommand.js
{ name: 'location', type: CustomCommandParamType.Location }
], ],
callback: (source, instanceName, pos) => this.run(source, instanceName, pos) permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName, dimensionId, location) => this.run(source, instanceName, dimensionId, location)
}); });
} }
run(source, instanceName, pos) { run(source, instanceName, dimensionId, location) {
try { const instance = structureCollection.get(instanceName);
const instance = findInstance(source, instanceName); if (dimensionId === void 0 || location === void 0) {
if (!instance) return { status: CustomCommandStatus.Failure }; if (!(source instanceof PlayerCommandOrigin))
let dimensionId; return { status: CustomCommandStatus.Failure, message: 'construct.commands.move.locationRequired' };
let location = pos; const player = source.getSource();
if (location === undefined) { location = player.location;
if (!(source instanceof PlayerCommandOrigin)) dimensionId = player.dimension.id;
return { status: CustomCommandStatus.Failure, message: 'construct.commands.move.posRequired' };
const player = source.getSource();
location = player.location;
dimensionId = player.dimension.id;
} else {
dimensionId = this.resolveDimensionId(source);
}
system.run(() => {
instance.move(dimensionId, location);
source.sendMessage({
rawtext: [{ translate: 'construct.commands.move.success',
with: [instanceName, String(Math.floor(location.x)), String(Math.floor(location.y)),
String(Math.floor(location.z)), dimensionId.replace('minecraft:', '')] }]
});
});
return { status: CustomCommandStatus.Success };
} catch (err) {
return commandError(source, err);
} }
this.assertDimensionExists(dimensionId);
const flooredLocation = Vector.from(location).floor();
system.run(() => {
instance.move(dimensionId, flooredLocation);
source.sendMessage({ translate: 'construct.commands.move.success', with: [instanceName, flooredLocation.toString(), dimensionId.replace('minecraft:', '')] });
});
return { status: CustomCommandStatus.Success };
} }
resolveDimensionId(source) { assertDimensionExists(dimensionId) {
if (source instanceof PlayerCommandOrigin || source instanceof EntityCommandOrigin) return world.getDimension(dimensionId) !== void 0;
return source.getSource().dimension.id;
if (source instanceof BlockCommandOrigin)
return source.getSource().dimension.id;
return 'minecraft:overworld';
} }
} }
+29 -25
View File
@@ -1,7 +1,8 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { structureCollection } from '../classes/Structure/StructureCollection'; import { structureCollection } from '../classes/Structure/StructureCollection';
import { commandError } from '../classes/Commands/lib/commandError'; import { InstanceExistsError } from '../classes/Errors/InstanceExistsError';
import { StructureNotFoundError } from '../classes/Errors/StructureNotFoundError';
export class NewCommand extends Command { export class NewCommand extends Command {
constructor() { constructor() {
@@ -12,35 +13,38 @@ export class NewCommand extends Command {
{ name: 'instanceName', type: CustomCommandParamType.String }, { name: 'instanceName', type: CustomCommandParamType.String },
{ name: 'structureId', type: CustomCommandParamType.String } { name: 'structureId', type: CustomCommandParamType.String }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName, structureId) => this.run(source, instanceName, structureId) callback: (source, instanceName, structureId) => this.run(source, instanceName, structureId)
}); });
} }
run(source, instanceName, structureId) { run(source, instanceName, structureId) {
try { this.tryAddStructure(source, instanceName, structureId);
if (structureCollection.has(instanceName)) { return { status: CustomCommandStatus.Success };
system.run(() => source.sendMessage({
rawtext: [{ translate: 'construct.commands.new.duplicateName', with: [instanceName] }]
}));
return { status: CustomCommandStatus.Failure };
}
if (!structureCollection.getWorldStructureIds().includes(structureId)) {
system.run(() => source.sendMessage({
rawtext: [{ translate: 'construct.commands.new.unknownStructure', with: [structureId] }]
}));
return { status: CustomCommandStatus.Failure };
}
system.run(() => {
structureCollection.add(instanceName, structureId);
source.sendMessage({
rawtext: [{ translate: 'construct.commands.new.success', with: [instanceName, structureId] }]
});
});
return { status: CustomCommandStatus.Success };
} catch (err) {
return commandError(source, err);
}
} }
tryAddStructure(source, instanceName, structureId) {
system.run(() => {
try {
this.addStructure(source, instanceName, structureId);
} catch (error) {
this.handleStructureAdditionErrors(source, error);
}
});
}
addStructure(source, instanceName, structureId) {
structureCollection.add(instanceName, structureId);
source.sendMessage({ translate: 'construct.commands.new.success', with: [instanceName, structureId] });
}
handleStructureAdditionErrors(source, error) {
if (error instanceof InstanceExistsError || error instanceof StructureNotFoundError)
error.sendTo(source);
else
throw error;
}
} }
export const newCommand = new NewCommand(); export const newCommand = new NewCommand();
+7 -17
View File
@@ -1,7 +1,6 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server'; import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { findInstance } from '../classes/Commands/lib/findInstance'; import { structureCollection } from '../classes/Structure/StructureCollection';
import { commandError } from '../classes/Commands/lib/commandError';
export class NextLayerCommand extends Command { export class NextLayerCommand extends Command {
constructor() { constructor() {
@@ -11,25 +10,16 @@ export class NextLayerCommand extends Command {
mandatoryParameters: [ mandatoryParameters: [
{ name: 'instanceName', type: CustomCommandParamType.String } { name: 'instanceName', type: CustomCommandParamType.String }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName) => this.run(source, instanceName) callback: (source, instanceName) => this.run(source, instanceName)
}); });
} }
run(source, instanceName) { run(source, instanceName) {
try { const instance = structureCollection.get(instanceName);
const instance = findInstance(source, instanceName); instance.increaseLayer();
if (!instance) return { status: CustomCommandStatus.Failure }; source.sendMessage({ translate: 'construct.commands.nextlayer.success', with: [instanceName, String(instance.getLayer())] });
system.run(() => { return { status: CustomCommandStatus.Success };
instance.increaseLayer();
source.sendMessage({
rawtext: [{ translate: 'construct.commands.nextlayer.success',
with: [instanceName, String(instance.getLayer())] }]
});
});
return { status: CustomCommandStatus.Success };
} catch (err) {
return commandError(source, err);
}
} }
} }
+11 -20
View File
@@ -1,9 +1,7 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server'; import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin'; import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin';
import { BuilderOptions } from '../classes/Builder/BuilderOptions'; import { BuilderOptions } from '../classes/Builder/BuilderOptions';
import { requirePlayer } from '../classes/Commands/lib/requirePlayer';
import { commandError } from '../classes/Commands/lib/commandError';
export class OptionCommand extends Command { export class OptionCommand extends Command {
constructor() { constructor() {
@@ -18,29 +16,22 @@ export class OptionCommand extends Command {
enums: [ enums: [
{ name: 'optionId', values: ['easyPlace', 'fastEasyPlace', 'materialGrabber'] } { name: 'optionId', values: ['easyPlace', 'fastEasyPlace', 'materialGrabber'] }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, optionId, state) => this.run(source, optionId, state) callback: (source, optionId, state) => this.run(source, optionId, state)
}); });
} }
run(source, optionId, state) { run(source, optionId, state) {
try { if (!BuilderOptions.get(optionId)) {
const player = requirePlayer(source); source.sendMessage({ translate: 'construct.commands.option.unknownOption', with: [optionId] });
if (!BuilderOptions.get(optionId)) { return void 0;
system.run(() => source.sendMessage({
rawtext: [{ translate: 'construct.commands.option.unknownOption', with: [optionId] }]
}));
return { status: CustomCommandStatus.Failure };
}
system.run(() => {
BuilderOptions.setValue(optionId, player.id, state);
source.sendMessage({
rawtext: [{ translate: 'construct.commands.option.success', with: [optionId, String(state)] }]
});
});
return { status: CustomCommandStatus.Success };
} catch (err) {
return commandError(source, err);
} }
system.run(() => {
const player = source.getSource();
BuilderOptions.setValue(optionId, player.id, state);
source.sendMessage({ translate: 'construct.commands.option.success', with: [optionId, String(state)] });
});
return { status: CustomCommandStatus.Success };
} }
} }
+20 -31
View File
@@ -1,49 +1,38 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { findInstance } from '../classes/Commands/lib/findInstance'; import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, DimensionTypes, system, world } from '@minecraft/server';
import { commandError } from '../classes/Commands/lib/commandError'; import { structureCollection } from '../classes/Structure/StructureCollection';
import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin'; import { InstanceExistsError } from '../classes/Errors/InstanceExistsError';
import { BlockCommandOrigin } from '../classes/Commands/BlockCommandOrigin'; import { Vector } from '../lib/Vector';
import { EntityCommandOrigin } from '../classes/Commands/EntityCommandOrigin';
export class PlaceCommand extends Command { export class PlaceCommand extends Command {
constructor() { constructor() {
super({ super({
name: 'place', name: 'place',
description: 'construct.commands.place', description: 'construct.commands.place',
enums: [ { name: 'dimensionId', values: Object.values(DimensionTypes.getAll().map(d => d.typeId)) } ],
mandatoryParameters: [ mandatoryParameters: [
{ name: 'instanceName', type: CustomCommandParamType.String }, { name: 'instanceName', type: CustomCommandParamType.String },
{ name: 'pos', type: CustomCommandParamType.Location } { name: 'dimensionId', type: CustomCommandParamType.Enum },
{ name: 'location', type: CustomCommandParamType.Location }
], ],
callback: (source, instanceName, pos) => this.run(source, instanceName, pos) permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName, dimensionId, location) => this.run(source, instanceName, dimensionId, location)
}); });
} }
run(source, instanceName, pos) { run(source, instanceName, dimensionId, location) {
try { const instance = structureCollection.get(instanceName);
const instance = findInstance(source, instanceName); const flooredLocation = Vector.from(location).floor();
if (!instance) return { status: CustomCommandStatus.Failure }; this.assertDimensionExists(dimensionId);
const dimensionId = this.resolveDimensionId(source); system.run(() => {
system.run(() => { instance.place(dimensionId, flooredLocation);
instance.place(dimensionId, pos); source.sendMessage({ translate: 'construct.commands.place.success', with: [instanceName, flooredLocation.toString(), dimensionId.replace('minecraft:', '')] });
source.sendMessage({ });
rawtext: [{ translate: 'construct.commands.place.success', return { status: CustomCommandStatus.Success };
with: [instanceName, String(pos.x), String(pos.y), String(pos.z),
dimensionId.replace('minecraft:', '')] }]
});
});
return { status: CustomCommandStatus.Success };
} catch (err) {
return commandError(source, err);
}
} }
resolveDimensionId(source) { assertDimensionExists(dimensionId) {
if (source instanceof PlayerCommandOrigin || source instanceof EntityCommandOrigin) return world.getDimension(dimensionId) !== void 0;
return source.getSource().dimension.id;
if (source instanceof BlockCommandOrigin)
return source.getSource().dimension.id;
return 'minecraft:overworld';
} }
} }
+7 -17
View File
@@ -1,7 +1,6 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server'; import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { findInstance } from '../classes/Commands/lib/findInstance'; import { structureCollection } from '../classes/Structure/StructureCollection';
import { commandError } from '../classes/Commands/lib/commandError';
export class PrevLayerCommand extends Command { export class PrevLayerCommand extends Command {
constructor() { constructor() {
@@ -11,25 +10,16 @@ export class PrevLayerCommand extends Command {
mandatoryParameters: [ mandatoryParameters: [
{ name: 'instanceName', type: CustomCommandParamType.String } { name: 'instanceName', type: CustomCommandParamType.String }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName) => this.run(source, instanceName) callback: (source, instanceName) => this.run(source, instanceName)
}); });
} }
run(source, instanceName) { run(source, instanceName) {
try { const instance = structureCollection.get(instanceName);
const instance = findInstance(source, instanceName); instance.decreaseLayer();
if (!instance) return { status: CustomCommandStatus.Failure }; source.sendMessage({ translate: 'construct.commands.prevlayer.success', with: [instanceName, String(instance.getLayer())] });
system.run(() => { return { status: CustomCommandStatus.Success };
instance.decreaseLayer();
source.sendMessage({
rawtext: [{ translate: 'construct.commands.prevlayer.success',
with: [instanceName, String(instance.getLayer())] }]
});
});
return { status: CustomCommandStatus.Success };
} catch (err) {
return commandError(source, err);
}
} }
} }
+9 -21
View File
@@ -1,8 +1,6 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { structureCollection } from '../classes/Structure/StructureCollection'; 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 { export class RenameCommand extends Command {
constructor() { constructor() {
@@ -13,30 +11,20 @@ export class RenameCommand extends Command {
{ name: 'instanceName', type: CustomCommandParamType.String }, { name: 'instanceName', type: CustomCommandParamType.String },
{ name: 'newName', type: CustomCommandParamType.String } { name: 'newName', type: CustomCommandParamType.String }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName, newName) => this.run(source, instanceName, newName) callback: (source, instanceName, newName) => this.run(source, instanceName, newName)
}); });
} }
run(source, instanceName, newName) { run(source, instanceName, newName) {
try { const instance = structureCollection.get(instanceName);
const instance = findInstance(source, instanceName); if (structureCollection.has(newName)) {
if (!instance) return { status: CustomCommandStatus.Failure }; source.sendMessage({ translate: 'construct.error.instanceExists', with: [newName] });
if (structureCollection.has(newName)) { return void 0;
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);
} }
structureCollection.rename(instanceName, newName);
source.sendMessage({ translate: 'construct.commands.rename.success', with: [instanceName, newName] });
return { status: CustomCommandStatus.Success };
} }
} }
+22 -18
View File
@@ -1,8 +1,9 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { findInstance } from '../classes/Commands/lib/findInstance'; import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, system, TicksPerSecond } from '@minecraft/server';
import { commandError } from '../classes/Commands/lib/commandError';
import { InstanceFormBuilder } from '../classes/Instance/InstanceFormBuilder'; import { InstanceFormBuilder } from '../classes/Instance/InstanceFormBuilder';
import { structureCollection } from '../classes/Structure/StructureCollection';
import { StructureVerifier } from '../classes/Verifier/StructureVerifier';
import { StructureStatistics } from '../classes/Structure/StructureStatistics';
export class StatsCommand extends Command { export class StatsCommand extends Command {
constructor() { constructor() {
@@ -12,26 +13,29 @@ export class StatsCommand extends Command {
mandatoryParameters: [ mandatoryParameters: [
{ name: 'instanceName', type: CustomCommandParamType.String } { name: 'instanceName', type: CustomCommandParamType.String }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName) => this.run(source, instanceName) callback: (source, instanceName) => this.run(source, instanceName)
}); });
} }
run(source, instanceName) { run(source, instanceName) {
try { const instance = structureCollection.get(instanceName);
const instance = findInstance(source, instanceName); if (this.structureVerifier)
if (!instance) return { status: CustomCommandStatus.Failure }; return { status: CustomCommandStatus.Failure, error: 'construct.commands.stats.alreadyRunning' };
system.run(async () => { system.run(async () => {
try { source.sendMessage(await this.getStatsMessage(instance));
const { stats } = await InstanceFormBuilder.buildStatistics(instance); });
source.sendMessage(stats); return { status: CustomCommandStatus.Success };
} catch (err) { }
source.sendMessage({ translate: 'construct.commands.stats.alreadyRunning' });
} async getStatsMessage(instance) {
}); const verifierOptions = { isEnabled: true, particleLifetime: 1*TicksPerSecond, isStandalone: true };
return { status: CustomCommandStatus.Success }; this.structureVerifier = new StructureVerifier(instance, verifierOptions);
} catch (err) { const verification = await this.structureVerifier.verifyStructure(true);
return commandError(source, err); const statistics = new StructureStatistics(instance, verification);
} const statsMessage = statistics.getMessage();
this.structureVerifier = void 0;
return statsMessage;
} }
} }
+15 -25
View File
@@ -1,10 +1,8 @@
import { CustomCommandParamType, CustomCommandStatus, EntityComponentTypes, EquipmentSlot, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, EntityComponentTypes, EquipmentSlot, system } from '@minecraft/server';
import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin'; import { PlayerCommandOrigin } from '../classes/Commands/PlayerCommandOrigin';
import { findInstance } from '../classes/Commands/lib/findInstance';
import { requirePlayer } from '../classes/Commands/lib/requirePlayer';
import { commandError } from '../classes/Commands/lib/commandError';
import { MENU_ITEM } from '../consts'; import { MENU_ITEM } from '../consts';
import { structureCollection } from '../classes/Structure/StructureCollection';
export class TagCommand extends Command { export class TagCommand extends Command {
constructor() { constructor() {
@@ -15,32 +13,24 @@ export class TagCommand extends Command {
mandatoryParameters: [ mandatoryParameters: [
{ name: 'instanceName', type: CustomCommandParamType.String } { name: 'instanceName', type: CustomCommandParamType.String }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName) => this.run(source, instanceName) callback: (source, instanceName) => this.run(source, instanceName)
}); });
} }
run(source, instanceName) { run(source, instanceName) {
try { const instance = structureCollection.get(instanceName);
const player = requirePlayer(source); const player = source.getSource();
const instance = findInstance(source, instanceName); const equipment = player.getComponent(EntityComponentTypes.Equippable);
if (!instance) return { status: CustomCommandStatus.Failure }; const itemStack = equipment?.getEquipment(EquipmentSlot.Mainhand);
const equipment = player.getComponent(EntityComponentTypes.Equippable); if (itemStack?.typeId !== MENU_ITEM)
const itemStack = equipment?.getEquipment(EquipmentSlot.Mainhand); return { status: CustomCommandStatus.Failure, message: 'construct.commands.tag.notHoldingItem' };
if (itemStack?.typeId !== MENU_ITEM) { system.run(() => {
system.run(() => source.sendMessage({ translate: 'construct.commands.tag.notHoldingItem' })); itemStack.nameTag = instanceName;
return { status: CustomCommandStatus.Failure }; equipment.setEquipment(EquipmentSlot.Mainhand, itemStack);
} source.sendMessage({ translate: 'construct.commands.tag.success', with: [instanceName] });
system.run(() => { });
itemStack.nameTag = instanceName; return { status: CustomCommandStatus.Success };
equipment.setEquipment(EquipmentSlot.Mainhand, itemStack);
source.sendMessage({
rawtext: [{ translate: 'construct.commands.tag.success', with: [instanceName] }]
});
});
return { status: CustomCommandStatus.Success };
} catch (err) {
return commandError(source, err);
}
} }
} }
+14 -17
View File
@@ -1,7 +1,6 @@
import { CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server'; import { CommandPermissionLevel, CustomCommandParamType, CustomCommandStatus, system } from '@minecraft/server';
import { Command } from '../classes/Commands/Command'; import { Command } from '../classes/Commands/Command';
import { findInstance } from '../classes/Commands/lib/findInstance'; import { structureCollection } from '../classes/Structure/StructureCollection';
import { commandError } from '../classes/Commands/lib/commandError';
export class VerifierCommand extends Command { export class VerifierCommand extends Command {
constructor() { constructor() {
@@ -12,25 +11,23 @@ export class VerifierCommand extends Command {
{ name: 'instanceName', type: CustomCommandParamType.String }, { name: 'instanceName', type: CustomCommandParamType.String },
{ name: 'state', type: CustomCommandParamType.Boolean } { name: 'state', type: CustomCommandParamType.Boolean }
], ],
permissionLevel: CommandPermissionLevel.Any,
callback: (source, instanceName, state) => this.run(source, instanceName, state) callback: (source, instanceName, state) => this.run(source, instanceName, state)
}); });
} }
run(source, instanceName, state) { run(source, instanceName, state) {
try { const instance = structureCollection.get(instanceName);
const instance = findInstance(source, instanceName); if (state)
if (!instance) return { status: CustomCommandStatus.Failure }; instance.setVerifierEnabled(true);
system.run(() => { else
instance.setVerifierEnabled(state); instance.setVerifierEnabled(false);
source.sendMessage({ this.sendFeedback(source, instanceName, state);
rawtext: [{ translate: 'construct.commands.verifier.success', return { status: CustomCommandStatus.Success };
with: [instanceName, String(state)] }] }
});
}); sendFeedback(source, instanceName, state) {
return { status: CustomCommandStatus.Success }; source.sendMessage({ translate: state ? 'construct.commands.verifier.enabled' : 'construct.commands.verifier.disabled', with: [instanceName] });
} catch (err) {
return commandError(source, err);
}
} }
} }
+40 -34
View File
@@ -57,15 +57,6 @@ construct.structure.statistics.stateincorrect=§7Block State Incorrect: §e%s ##
construct.structure.statistics.incorrect=§7Incorrect: §c%s ## Insert string: number of incorrectly placed blocks construct.structure.statistics.incorrect=§7Incorrect: §c%s ## Insert string: number of incorrectly placed blocks
construct.structure.statistics.missing=§7Missing: §3%s ## Insert string: number of missing blocks construct.structure.statistics.missing=§7Missing: §3%s ## Insert string: number of missing blocks
## Structure Block Info Display
construct.blockinfo.header=Structure:
construct.blockinfo.none=§7None
construct.blockinfo.nosupply= §c[No Supply] ## When the targeted block is not in the player's inventory
construct.blockinfo.unknown=§7Unknown
construct.blockinfo.waterlogged=§7isWaterlogged: §3true
construct.mainmenu.instance.exists=§cInstance '%s' already exists. Try again with a new name. ## Insert string: instance name
construct.mainmenu.instance.notfound=§cStructure ID '%s' not found. If you're looking for a structure that you put in the structures folder, please restart your world and try again. ## Insert string: structure name
construct.mainmenu.title=§l§2Construct ## This is the name of the pack. construct.mainmenu.title=§l§2Construct ## This is the name of the pack.
construct.mainmenu.selectinstance=Select an instance: construct.mainmenu.selectinstance=Select an instance:
construct.mainmenu.howto=How to Add/Remove Structures construct.mainmenu.howto=How to Add/Remove Structures
@@ -84,12 +75,14 @@ construct.mainmenu.howto.add.mcstructure=§7To transfer structures §fbetween wo
construct.mainmenu.howto.remove.header=§cHow to Remove Structures: construct.mainmenu.howto.remove.header=§cHow to Remove Structures:
construct.mainmenu.howto.remove.body=§7- Use the §f/structure delete§7 command to remove a structure from the world. construct.mainmenu.howto.remove.body=§7- Use the §f/structure delete§7 command to remove a structure from the world.
## Commands ## Structure Block Info Display
construct.commands.construct=Gives you the Construct item. Use it to open the Construct menu. construct.blockinfo.header=Structure:
construct.commands.construct.fail=§cFailed to give you the Construct item. construct.blockinfo.none=§7None
construct.commands.construct.success=§aYou recieved the Construct item! Use it to open the Construct menu. construct.blockinfo.nosupply= §c[No Supply] ## When the targeted block is not in the player's inventory
construct.blockinfo.unknown=§7Unknown
construct.blockinfo.waterlogged=§7isWaterlogged: §3true
## Options ## Builder Options
construct.option.enabled= is now enabled! construct.option.enabled= is now enabled!
construct.option.disabled= is now disabled. construct.option.disabled= is now disabled.
construct.option.easyplace.name=Easy Place construct.option.easyplace.name=Easy Place
@@ -106,14 +99,14 @@ construct.option.materialgrabber.grabbed.zero=§7Grabbed 0 items.
construct.option.materialgrabber.grabbed.one=§aGrabbed 1 item. construct.option.materialgrabber.grabbed.one=§aGrabbed 1 item.
construct.option.materialgrabber.grabbed.many=§aGrabbed %s items. ## Insert string: number of items transferred to the player construct.option.materialgrabber.grabbed.many=§aGrabbed %s items. ## Insert string: number of items transferred to the player
## CLI shared errors ## Commands
construct.commands.error.instanceNotFound=§cInstance "%1" not found. construct.commands.construct=Gives you the Construct item. Use it to open the Construct menu.
construct.commands.error.notAPlayer=§cThis command can only be used by players. construct.commands.construct.fail=§cFailed to give you the Construct item.
construct.commands.construct.success=§aYou recieved the Construct item! Use it to open the Construct menu.
## construct:new ## construct:new
construct.commands.new=Create a new instance bound to a structure. construct.commands.new=Create a new instance bound to a structure.
construct.commands.new.success=§aCreated instance "%1" bound to structure "%2". construct.commands.new.success=§aCreated instance "%1" bound to structure "%2".
construct.commands.new.duplicateName=§cAn instance named "%1" already exists.
construct.commands.new.unknownStructure=§cNo structure with id "%1" found in the world. construct.commands.new.unknownStructure=§cNo structure with id "%1" found in the world.
## construct:delete ## construct:delete
@@ -123,30 +116,34 @@ construct.commands.delete.success=§aDeleted instance "%1".
## construct:rename ## construct:rename
construct.commands.rename=Rename an existing instance. construct.commands.rename=Rename an existing instance.
construct.commands.rename.success=§aRenamed instance "%1" to "%2". construct.commands.rename.success=§aRenamed instance "%1" to "%2".
construct.commands.rename.duplicateName=§cAn instance named "%1" already exists.
## construct:list ## construct:list
construct.commands.list=List all registered instances. construct.commands.list=List all registered instances.
construct.commands.list.empty=§7No instances registered. construct.commands.list.empty=§7No instances registered.
construct.commands.list.header=§eRegistered instances (%1):§r construct.commands.list.header=§aRegistered instances (%1):§r
construct.commands.list.row=§a%1§r §8[§r%2§8]§r §7%3§r construct.commands.list.row=§a%1§r §8[§r%2§8]§r §7%3§r
construct.commands.list.row.enabled=enabled
construct.commands.list.row.disabled=disabled
construct.commands.list.row.location= at %1 in %2
construct.commands.list.row.nolocation= (no location)
## construct:place ## construct:place
construct.commands.place=Place an instance at world coordinates (enables it). construct.commands.place=Place an instance at world coordinates (enables it).
construct.commands.place.success=§aPlaced instance "%1" at %2 %3 %4 in %5. construct.commands.place.success=§aPlaced instance "%1" at %2 in %3.
## construct:move ## construct:move
construct.commands.move=Reposition an instance without toggling its enabled state. construct.commands.move=Reposition an instance without toggling its enabled state.
construct.commands.move.success=§aMoved instance "%1" to %2 %3 %4 in %5. construct.commands.move.success=§aMoved instance "%1" to %2 in %3.
construct.commands.move.posRequired=§cMust provide coordinates when not running as a player. construct.commands.move.locationRequired=§cMust provide a dimension and coordinates when not running as a player.
## construct:active ## construct:active
construct.commands.active=Enable or disable an instance. construct.commands.active=Enable or disable an instance.
construct.commands.active.success=§aSet instance "%1" active=%2. construct.commands.active.true=§aEnabled instance "%1".
construct.commands.active.false=§aDisabled instance "%1".
construct.commands.error.noLocation=§cInstance "%1" has no saved location. construct.commands.error.noLocation=§cInstance "%1" has no saved location.
## construct:layer ## construct:layer
construct.commands.layer=Set the active layer of an instance (0 = whole structure). construct.commands.layer=Set the active layer of an instance (Use 0 for the whole structure).
construct.commands.layer.success=§aSet instance "%1" layer to %2. construct.commands.layer.success=§aSet instance "%1" layer to %2.
construct.commands.layer.outOfBounds=§cLayer %1 is out of bounds for instance "%2" (max %3). construct.commands.layer.outOfBounds=§cLayer %1 is out of bounds for instance "%2" (max %3).
@@ -160,7 +157,8 @@ construct.commands.prevlayer.success=§aInstance "%1" stepped back to layer %2.
## construct:verifier ## construct:verifier
construct.commands.verifier=Toggle block validation overlay for an instance. construct.commands.verifier=Toggle block validation overlay for an instance.
construct.commands.verifier.success=§aSet instance "%1" verifier=%2. construct.commands.verifier.enabled=§aEnabled verifier for instance "%1".
construct.commands.verifier.disabled=§aDisabled verifier for instance "%1".
## construct:option ## construct:option
construct.commands.option=Toggle a per-player builder option. construct.commands.option=Toggle a per-player builder option.
@@ -169,26 +167,34 @@ construct.commands.option.unknownOption=§cUnknown option "%1".
## construct:info ## construct:info
construct.commands.info=Print instance details to chat. construct.commands.info=Print instance details to chat.
construct.commands.info.header=§e=== Instance "%1" ===§r construct.commands.info.header=§a=== Instance Info for "%1" ===§r
construct.commands.info.structure=§7Structure:§r %1 construct.commands.info.structure=§7Structure:§r %1
construct.commands.info.enabled=§7Enabled:§r %1 construct.commands.info.location=§7Location:§r %1 in %2
construct.commands.info.location=§7Location:§r %1 %2 %3 in %4
construct.commands.info.noLocation=§7Location:§r (none) construct.commands.info.noLocation=§7Location:§r (none)
construct.commands.info.enabled=§7Enabled:§r %1
construct.commands.info.layer=§7Layer:§r %1 / %2 construct.commands.info.layer=§7Layer:§r %1 / %2
construct.commands.info.verifier=§7Verifier:§r %1 construct.commands.info.verifier=§7Verifier running:§r %1
construct.commands.info.bounds=§7Bounds:§r (%1, %2, %3) -> (%4, %5, %6) construct.commands.info.size=§7Size:§r %1 (%2 blocks)
## construct:stats ## construct:stats
construct.commands.stats=Run the structure verifier and print statistics. construct.commands.stats=Run the structure verifier and print statistics.
construct.commands.stats.alreadyRunning=§cA verification is already in progress. construct.commands.stats.alreadyRunning=§cA verification is already in progress. Please wait until it finishes.
## construct:materials ## construct:materials
construct.commands.materials=Print the material list for an instance. construct.commands.materials=Print the material list for an instance.
construct.commands.materials.headerAll=§eMaterials for "%1":§r construct.commands.materials.headerAll=§aMaterials for "%1":§r
construct.commands.materials.headerMissing=§eMissing materials for "%1":§r construct.commands.materials.headerMissing=§aMissing materials for "%1":§r
construct.commands.materials.empty=§7(no materials) construct.commands.materials.empty=§7(no materials)
## construct:tag ## construct:tag
construct.commands.tag=Rename the held Construct item to an instance name for quick-open. construct.commands.tag=Rename the held Construct item to an instance name for quick-open.
construct.commands.tag.success=§aTagged held Construct item with instance "%1". construct.commands.tag.success=§aTagged held Construct item with instance "%1".
construct.commands.tag.notHoldingItem=§cYou must be holding a Construct item. construct.commands.tag.notHoldingItem=§cYou must be holding a Construct item.
## Errors
construct.error.invalidCommandSource=§cThis command cannot be run from this source.
construct.error.instanceNotFound=§cInstance "%1" not found. ## Insert string: instance name
construct.error.instanceExists=§cInstance "%1" already exists. Try again with a new name. ## Insert string: instance name
construct.error.structureNotFound=§cStructure ID "%1" not found. If you're looking for a structure that you put in the structures folder, please restart your world and try again. ## Insert string: structure name
construct.error.dimensionNotFound=§cDimension "%1" not found. ## Insert string: dimension name
construct.error.notAPlayer=§cThis command can only be used by players.