@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
310 lines (309 loc) • 10.3 kB
JavaScript
;
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandScope = void 0;
const Log_1 = __importDefault(require("../core/Log"));
const Utilities_1 = __importDefault(require("../core/Utilities"));
const CommandStructure_1 = __importDefault(require("./CommandStructure"));
const ICommand_1 = require("./ICommand");
const toolcommands_1 = require("./toolcommands");
var CommandScope;
(function (CommandScope) {
CommandScope[CommandScope["any"] = 1] = "any";
CommandScope[CommandScope["project"] = 2] = "project";
CommandScope[CommandScope["minecraft"] = 3] = "minecraft";
CommandScope[CommandScope["carto"] = 4] = "carto";
CommandScope[CommandScope["host"] = 5] = "host";
CommandScope[CommandScope["debug"] = 10] = "debug";
CommandScope[CommandScope["debugProject"] = 11] = "debugProject";
CommandScope[CommandScope["debugMinecraft"] = 12] = "debugMinecraft";
CommandScope[CommandScope["debugHost"] = 14] = "debugHost";
})(CommandScope || (exports.CommandScope = CommandScope = {}));
const MinecraftCommands = [
"aimassist",
"allowlist",
"alwaysday",
"camera",
"camerashake",
"changesetting",
"clear",
"clearspawnpoint",
"clone",
"commandbuilder",
"connect",
"controlscheme",
"damage",
"daylock",
"deop",
"dialogue",
"difficulty",
"effect",
"enchant",
"event",
"execute",
"fill",
"fog",
"function",
"gamemode",
"gamerule",
"gametest",
"give",
"help",
"hud",
"inputpermission",
"kick",
"kill",
"list",
"locate",
"loot",
"me",
"msg",
"mobevent",
"music",
"op",
"ops",
"particle",
"permission",
"place",
"playanimation",
"playsound",
"project",
"recipe",
"reload",
"reloadconfig",
"reloadpacketlimitconfig",
"replaceitem",
"ride",
"save",
"say",
"schedule",
"scoreboard",
"script",
"scriptevent",
"sendshowstoreoffer",
"setblock",
"setmaxplayers",
"setworldspawn",
"simulationtype",
"spawnpoint",
"spreadplayers",
"stop",
"stopsound",
"structure",
"summon",
"tag",
"teleport",
"tell",
"tellraw",
"testfor",
"testforblock",
"testforblocks",
"tickingarea",
"tp",
"time",
"title",
"titleraw",
"toggledownfall",
"transfer",
"volumearea",
"weather",
"wsserver",
"whitelist",
"?",
"w",
"xp",
];
const MinecraftAddOnBlockedCommands = [
"allowlist",
"alwaysday",
"changesetting",
"connect",
"daylock",
"deop",
"difficulty",
"gamemode",
"gamerule",
"gametest",
"help",
"kick",
"list",
"locate",
"op",
"ops",
"permission",
"project",
"reload",
"reloadconfig",
"save",
"script",
"setmaxplayers",
"setworldspawn",
"simulationtype",
"stop",
"tickingarea",
"time",
"transfer",
"wsserver",
"whitelist",
"?",
];
class CommandRegistry {
static _registry;
_commandsByName = {};
_commandsByScope = {};
static get main() {
if (!this._registry) {
this._registry = new CommandRegistry();
this._registry.registerCommand("sleep", CommandScope.any, async (context, name, args) => {
if (args.length === 1) {
let delay = 0;
try {
delay = parseInt(args[0]);
}
catch (e) { }
if (delay > 0) {
context.creatorTools.notifyStatusUpdate("(Delaying commands for " + delay + "ms).");
await Utilities_1.default.sleep(delay);
}
}
return { status: ICommand_1.CommandStatus.completed };
});
}
return this._registry;
}
registerCommand(commandName, commandScope, command) {
commandName = commandName.toLowerCase();
if (Utilities_1.default.isUsableAsObjectKey(commandName)) {
this._commandsByName[commandName] = command;
this._commandsByScope[commandName] = commandScope;
}
else {
Log_1.default.unsupportedToken(commandName);
}
}
logHelp() {
let commandArr = [];
for (const commandName in this._commandsByName) {
commandArr.push(commandName);
}
commandArr = commandArr.sort();
Log_1.default.message("Use help <command name> for more detailed help on a command.");
for (let i = 0; i < commandArr.length; i++) {
Log_1.default.message(commandArr[i]);
}
}
static isMinecraftBuiltInCommand(name) {
return MinecraftCommands.includes(name) || name === "#";
}
static isAddOnBlockedCommand(name) {
return MinecraftAddOnBlockedCommands.includes(name);
}
/**
* Create an IToolCommandOutput from IContext for ToolCommand execution.
*/
createToolCommandOutput(context) {
return {
info: (msg) => {
Log_1.default.message(msg);
context.creatorTools.notifyStatusUpdate(msg);
},
success: (msg) => {
Log_1.default.message("✓ " + msg);
context.creatorTools.notifyStatusUpdate(msg);
},
warn: (msg) => Log_1.default.debug(msg),
error: (msg) => Log_1.default.error(msg),
debug: (msg) => Log_1.default.verbose(msg),
progress: (current, total, msg) => {
const pct = Math.round((current / total) * 100);
context.creatorTools.notifyStatusUpdate(`[${pct}%] ${msg || ""}`);
},
};
}
/**
* Create an IToolCommandContext from the app's IContext.
*/
createToolCommandContext(context) {
return {
creatorTools: context.creatorTools,
project: context.project,
minecraft: context.minecraft,
output: this.createToolCommandOutput(context),
scope: "ui",
};
}
async runCommand(context, commandText) {
const command = CommandStructure_1.default.parse(commandText);
if (!command || !command.fullName || !command.commandArguments) {
return undefined;
}
// Initialize ToolCommands if not already done
(0, toolcommands_1.initializeToolCommands)();
// Check ToolCommandRegistry first (except for Minecraft built-in commands other than "help")
// The "help" command is special - we want our unified help to handle it
const toolRegistry = toolcommands_1.ToolCommandRegistry.instance;
if (command.fullName === "help" || !CommandRegistry.isMinecraftBuiltInCommand(command.fullName)) {
if (toolRegistry.has(command.fullName)) {
const toolContext = this.createToolCommandContext(context);
const result = await toolRegistry.execute(commandText, toolContext);
if (result) {
if (result.success) {
return { status: ICommand_1.CommandStatus.completed, data: result.data };
}
else {
Log_1.default.error(result.error?.message || "Command failed");
return { status: ICommand_1.CommandStatus.invalidArguments };
}
}
}
}
const scope = this._commandsByScope[command.fullName];
const commandName = this._commandsByName[command.fullName];
// Pass through Minecraft built-in commands (except "help" which is handled above)
if (CommandRegistry.isMinecraftBuiltInCommand(command.fullName) &&
command.fullName !== "help" &&
context.minecraft) {
Log_1.default.debug("Sending '" + commandText + "' to Minecraft.");
let result = await context.minecraft.runCommand(commandText);
return {
data: result,
status: ICommand_1.CommandStatus.completed,
};
}
else if (scope && commandName) {
if ((scope === CommandScope.debug ||
scope === CommandScope.debugProject ||
scope === CommandScope.debugMinecraft) &&
!Utilities_1.default.isDebug) {
return undefined;
}
if ((scope === CommandScope.project || scope === CommandScope.debugProject) && !context.project) {
Log_1.default.message("Could not run command '" + command.fullName + "'; no active project.");
return undefined;
}
if ((scope === CommandScope.minecraft || scope === CommandScope.debugMinecraft) && !context.minecraft) {
Log_1.default.message("Could not run command '" + command.fullName + "'; no active Minecraft deploy target was set.");
return undefined;
}
if ((scope === CommandScope.host || scope === CommandScope.debugHost) && !context.host) {
Log_1.default.message("Could not run command '" + command.fullName + "'; no host was set.");
return undefined;
}
const result = await commandName(context, command.fullName, command.commandArguments);
if (result.status === ICommand_1.CommandStatus.invalidEnvironment) {
Log_1.default.error("'" + command.fullName + "' was not set up properly.");
}
else if (result.status === ICommand_1.CommandStatus.invalidArguments) {
Log_1.default.error("'" + command.fullName + "' command arguments were not set up.");
}
return result;
}
context.creatorTools.notifyStatusUpdate("Could not find a command '" + command.fullName + "'.");
return undefined;
}
}
exports.default = CommandRegistry;