UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

200 lines (199 loc) 6.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionType = void 0; const Location_1 = __importDefault(require("../minecraft/Location")); const BlockLocation_1 = __importDefault(require("../minecraft/BlockLocation")); const ste_events_1 = require("ste-events"); const Utilities_1 = __importDefault(require("../core/Utilities")); const Log_1 = __importDefault(require("../core/Log")); var ActionType; (function (ActionType) { ActionType["simulatedPlayerSpawn"] = "test_simulated_player_spawn"; ActionType["simulatedPlayerMove"] = "test_simulated_player_move"; ActionType["simulatedPlayerInteract"] = "test_simulated_player_interact"; ActionType["entitySpawn"] = "entity_spawn"; ActionType["idle"] = "test_idle"; })(ActionType || (exports.ActionType = ActionType = {})); class Action { data; group; typeForm; actionSet; get id() { return this.data.id; } set id(newId) { this.data.id = newId; } _onPropertyChanged = new ste_events_1.EventDispatcher(); get onPropertyChanged() { return this._onPropertyChanged.asEvent(); } get typeId() { return "action"; } get shortSubjectId() { return "obj"; } get withId() { const withId = this.data.withId; if (withId) { return withId; } return this.shortSubjectId; } get setId() { const setId = this.data.setId; if (setId) { return setId; } return this.shortSubjectId; } get type() { return this.data.type; } getDataCopy() { const result = {}; if (this.data) { for (const propName in this.data) { let val = this.data[propName]; if (val) { result[propName] = val; } } } return result; } constructor(group, data) { this.group = group; if (!this.group._actionSet) { throw new Error("Cannot add action to unattached group"); } this.actionSet = this.group._actionSet; this.data = data; } run(scope) { } getProperty(id) { if (!Utilities_1.default.isUsableAsObjectKey(id)) { Log_1.default.unsupportedToken(id); throw new Error(); } return this.data[id]; } setProperty(id, value) { if (!Utilities_1.default.isUsableAsObjectKey(id)) { Log_1.default.unsupportedToken(id); throw new Error(); } this.data[id] = value; } getBaseValue() { return this.data; } setBaseValue(value) { this.data = value; } getArgumentAsLocation(name) { const val = this.data[name]; if (val instanceof Array && val.length >= 3) { const loc = new Location_1.default(val[0], val[1], val[2]); return loc; } return undefined; } getArgumentAsBlockLocation(name) { const val = this.data[name]; if (val instanceof Array && val.length >= 3) { const loc = new BlockLocation_1.default(val[0], val[1], val[2]); return loc; } return undefined; } _notifyPropertyChanged(propertyName) { this._onPropertyChanged.dispatch(this, propertyName); } getScriptTest() { return "test"; } getScriptWith() { if (!this.data.withId) { return "obj"; } return this.data.withId; } getScriptSet() { if (!this.data.setId) { return "obj"; } return this.data.setId; } getCommandWith() { if (!this.data.withId) { return "obj"; } return this.data.withId; } getCommandSet() { if (!this.data.setId) { return "obj"; } return this.data.setId; } getArgumentAsNumber(name) { const val = this.data[name]; if (typeof val === "number") { return val; } else if (typeof val === "string") { return parseFloat(val); } return 0; } getArgumentAsString(name) { const val = this.data[name]; return val; } validateArgumentIsEntityType(name) { const val = this.data[name]; if (!val || typeof val !== "string") { throw new Error("Argument '" + name + "' is not defined."); } return true; } absolutizeLocation(location) { if (!this.actionSet.locationRoot) { return location; } return new Location_1.default(this.actionSet.locationRoot.x + location.x, this.actionSet.locationRoot.y + location.y, this.actionSet.locationRoot.z + location.z); } absolutizeBlockLocation(location) { if (!this.actionSet.locationRoot) { return location; } return new BlockLocation_1.default(Math.round(this.actionSet.locationRoot.x + location.x), Math.round(this.actionSet.locationRoot.y + location.y), Math.round(this.actionSet.locationRoot.z + location.z)); } validateArgumentIsType(name, type) { const val = this.data[name]; const typestr = typeof val; switch (type) { case "BlockLocation": case "Location": if (!(val instanceof Array) || val.length !== 3) { throw new Error("Expected an array of 3 numbers for parameter '" + name + "'"); } break; case "boolean": case "number": case "string": if (typestr !== type) { throw new Error("Unexpected type mismatch: " + name + " is " + typestr + " (expected " + type + ")"); } break; } return true; } } exports.default = Action;