asajs
Version:
Make your Minecraft JsonUI with ScriptingAPI
396 lines (395 loc) • 15.6 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var Modify_exports = {};
__export(Modify_exports, {
Modify: () => Modify
});
module.exports = __toCommonJS(Modify_exports);
var import_JsonBuilder = require("../compilers/generator/JsonBuilder");
var import_Log = require("../compilers/generator/Log");
var import_CurrentLine = require("../compilers/reader/CurrentLine");
var import_Object = require("../compilers/reader/Object");
var import_ReadBinding = require("../compilers/reader/ReadBinding");
var import_ReadProperties = require("../compilers/reader/ReadProperties");
var import_MappingTypes = require("../types/enums/MappingTypes");
var import_Class = require("./Class");
var import_Random = require("./Random");
var import_UI = require("./UI");
class Modify extends import_Class.Class {
constructor(properties, identifier) {
var _a, _b;
super();
__publicField(this, "properties", {});
__publicField(this, "controls");
__publicField(this, "bindings");
__publicField(this, "button_mappings");
__publicField(this, "variables");
__publicField(this, "anims");
__publicField(this, "isValidPath");
__publicField(this, "name", "");
__publicField(this, "namespace", "");
__publicField(this, "modifyBindings");
__publicField(this, "removeModifyBindings");
__publicField(this, "modifyControls", {
remove: [],
replace: [],
insertBack: [],
insertFront: [],
insertAfter: [],
insertBefore: [],
moveAfter: [],
moveBack: [],
moveBefore: [],
moveFront: []
});
__publicField(this, "override", {
sourceBindings: {},
setProperties: (properties) => {
this.properties = {
...this.properties,
...properties
};
return this.override;
},
addChild: (element, properties, name, callback) => {
if (!element) {
this.controls || (this.controls = []);
return this.override;
}
if (!this.controls) this.controls = [];
name || (name = import_Random.Random.getName());
this.controls.push({
["".concat(name, "@").concat(typeof element === "string" ? element.startsWith("@") ? element.slice(1) : element : element.getPath())]: properties ? (0, import_ReadProperties.ReadProperties)(properties) : {}
});
callback == null ? void 0 : callback(this, name);
return this.override;
},
addBindings: (bindings) => {
if (!bindings) {
this.bindings || (this.bindings = []);
return this.override;
}
if (Array.isArray(bindings))
for (const binding of bindings) this.override.addBindings(binding);
else (this.bindings || (this.bindings = [])).push((0, import_ReadBinding.ReadBinding)(bindings, this.override));
return this.override;
},
addMapping: (mapping) => {
this.button_mappings || (this.button_mappings = []);
if (!mapping) return this.override;
if (Array.isArray(mapping)) mapping.forEach((v) => this.override.addMapping(v));
else {
mapping.mapping_type || (mapping.mapping_type = import_MappingTypes.MappingType.Global);
this.button_mappings.push(mapping);
}
return this.override;
},
addVariables: (variables) => {
this.variables || (this.variables = {});
if (variables)
import_Object.Obj.forEach(variables, (key, value) => {
this.variables[key] = {
...import_Object.Obj.map(value, (k, v) => {
return { key: k, value: (0, import_ReadProperties.ReadValue)(v) };
})
};
});
return this.override;
},
addAnimation: (animation, startIndex) => {
this.anims || (this.anims = []);
if (animation) this.anims.push(animation.getKeyIndex(startIndex || 0));
return this.override;
},
searchBinding: (bindingName, controlName, targetBindingName) => {
var _a, _b;
for (let index = 0; index < (((_a = this.bindings) == null ? void 0 : _a.length) || 0); index++) {
const binding = (_b = this.bindings) == null ? void 0 : _b[index];
if (controlName) {
if ((binding == null ? void 0 : binding.source_control_name) === controlName && binding.source_property_name === bindingName) {
if (targetBindingName) {
if (binding.target_property_name === targetBindingName) {
return targetBindingName;
} else return void 0;
} else return binding.target_property_name;
}
} else {
if ((binding == null ? void 0 : binding.source_property_name) === bindingName) {
if (targetBindingName) {
if (binding.target_property_name === targetBindingName) {
return targetBindingName;
} else return void 0;
} else return binding.target_property_name;
}
}
}
return void 0;
}
});
__publicField(this, "modify", {
bindings: {
remove: (bindings) => {
var _a, _b;
if (Array.isArray(bindings)) {
(_a = this.removeModifyBindings || (this.removeModifyBindings = [])) == null ? void 0 : _a.push(...bindings);
} else (_b = this.removeModifyBindings || (this.removeModifyBindings = [])) == null ? void 0 : _b.push(bindings);
return this.modify.bindings;
},
addBindings: (bindings) => {
if (Array.isArray(bindings))
bindings.forEach((binding) => this.modify.bindings.addBindings(binding));
else {
(this.modifyBindings || (this.modifyBindings = [])).push(
(0, import_ReadBinding.ReadBinding)(bindings, this.modify.bindings)
);
}
return this.modify.bindings;
}
},
controls: {
remove: (childName) => {
if (Array.isArray(childName)) this.modifyControls.remove.push(...childName);
else this.modifyControls.remove.push(childName);
return this.modify.controls;
},
moveAfter: (childName) => {
if (Array.isArray(childName)) this.modifyControls.moveAfter.push(...childName);
else this.modifyControls.moveAfter.push(childName);
return this.modify.controls;
},
moveBack: (childName) => {
if (Array.isArray(childName)) this.modifyControls.moveBack.push(...childName);
else this.modifyControls.moveBack.push(childName);
return this.modify.controls;
},
moveFront: (childName) => {
if (Array.isArray(childName)) this.modifyControls.moveFront.push(...childName);
else this.modifyControls.moveFront.push(childName);
return this.modify.controls;
},
moveBefore: (childName) => {
if (Array.isArray(childName)) this.modifyControls.moveBefore.push(...childName);
else this.modifyControls.moveBefore.push(childName);
return this.modify.controls;
},
replace: (childName, ui, properties, elementName) => {
this.modifyControls.replace.push([
childName,
{
["".concat(elementName || import_Random.Random.getName(), "@").concat(typeof ui === "string" ? ui : ui.getPath())]: (0, import_ReadProperties.ReadProperties)(properties || {})
}
]);
return this.modify.controls;
},
insertAfter: (childName, ui, properties, elementName) => {
this.modifyControls.insertAfter.push([
childName,
{
["".concat(elementName || import_Random.Random.getName(), "@").concat(typeof ui === "string" ? ui : ui.getPath())]: (0, import_ReadProperties.ReadProperties)(properties || {})
}
]);
return this.modify.controls;
},
insertBefore: (childName, ui, properties, elementName) => {
this.modifyControls.insertBefore.push([
childName,
{
["".concat(elementName || import_Random.Random.getName(), "@").concat(typeof ui === "string" ? ui : ui.getPath())]: (0, import_ReadProperties.ReadProperties)(properties || {})
}
]);
return this.modify.controls;
},
insertBack: (ui, properties, elementName) => {
this.modifyControls.insertBack.push({
["".concat(elementName || import_Random.Random.getName(), "@").concat(typeof ui === "string" ? ui : ui.getPath())]: (0, import_ReadProperties.ReadProperties)(properties || {})
});
return this.modify.controls;
},
insertFront: (ui, properties, elementName) => {
this.modifyControls.insertFront.push({
["".concat(elementName || import_Random.Random.getName(), "@").concat(typeof ui === "string" ? ui : ui.getPath())]: (0, import_ReadProperties.ReadProperties)(properties || {})
});
return this.modify.controls;
}
}
});
if (properties) this.override.setProperties(properties);
if (identifier && ((_b = (_a = identifier.name) == null ? void 0 : _a.match(/\w+/g)) == null ? void 0 : _b.length) === 1 && identifier.namespace)
this.isValidPath = true;
else this.isValidPath = false;
this.name = (identifier == null ? void 0 : identifier.name) || "";
this.namespace = (identifier == null ? void 0 : identifier.namespace) || "";
}
getPath() {
if (this.isValidPath) return "".concat(this.namespace, ".").concat(this.name);
else {
import_Log.Log.error("".concat((0, import_CurrentLine.CurrentLine)(), " Cannot use this element for extend or addChild!"));
return "";
}
}
getElement() {
return "@".concat(this.getPath());
}
extend(identifier, properties) {
return import_UI.UI.extend(this, properties, identifier);
}
getUI() {
const code = (0, import_ReadProperties.ReadProperties)(this.properties);
const modifications = [];
for (const key of ["type", "controls", "bindings", "button_mappings", "anims"])
if (this[key]) code[key] = this[key];
if (this.variables) code.variables;
if (this.variables && Object(this.variables).length !== 0)
import_Object.Obj.forEach(this.variables, (k, v) => {
code.variables.push({
requires: k,
...v
});
});
{
if (this.modifyBindings) {
modifications.push({
array_name: "bindings",
operation: "insert_front",
value: this.modifyBindings
});
}
if (this.removeModifyBindings) {
modifications.push(
...this.removeModifyBindings.map((v) => ({
array_name: "bindings",
operation: "remove",
where: v
}))
);
}
}
{
modifications.push(
...this.modifyControls.remove.map((controlName) => ({
array_name: "controls",
operation: "remove",
control_name: controlName
}))
);
modifications.push(
...this.modifyControls.moveAfter.map((controlName) => ({
array_name: "controls",
operation: "move_after",
control_name: controlName
}))
);
modifications.push(
...this.modifyControls.moveBack.map((controlName) => ({
array_name: "controls",
operation: "move_back",
control_name: controlName
}))
);
modifications.push(
...this.modifyControls.moveBefore.map((controlName) => ({
array_name: "controls",
operation: "move_before",
control_name: controlName
}))
);
modifications.push(
...this.modifyControls.moveFront.map((controlName) => ({
array_name: "controls",
operation: "move_front",
control_name: controlName
}))
);
modifications.push(
...this.modifyControls.replace.map(([childName, element]) => ({
array_name: "controls",
operation: "replace",
control_name: childName,
value: element
}))
);
modifications.push(
...this.modifyControls.insertAfter.map(([childName, element]) => ({
array_name: "controls",
operation: "insert_after",
control_name: childName,
value: [element]
}))
);
modifications.push(
...this.modifyControls.insertBefore.map(([childName, element]) => ({
array_name: "controls",
operation: "insert_before",
control_name: childName,
value: [element]
}))
);
if (this.modifyControls.insertBack.length)
modifications.push({
array_name: "controls",
operation: "insert_back",
value: this.modifyControls.insertBack
});
if (this.modifyControls.insertFront.length)
modifications.push({
array_name: "controls",
operation: "insert_front",
value: this.modifyControls.insertFront
});
}
if (modifications.length > 0) code["modifications"] = modifications;
for (const bindingKey in this.override.sourceBindings) {
const targetBinding = this.override.sourceBindings[bindingKey];
const [sourceBinding, sourceControl] = bindingKey.split(":");
code.bindings.push({
binding_type: "view",
source_control_name: sourceControl,
source_property_name: sourceBinding,
target_property_name: targetBinding
});
}
return Object.keys(code).length > 0 ? code : void 0;
}
addChild(element, properties, elementName) {
this.modify.controls.insertFront(element, properties, elementName);
return this;
}
static register(filePath, elementPath, properties) {
return this.registerWithNamespace(filePath, elementPath, "", properties);
}
static registerWithNamespace(filePath, elementPath, namespace, properties) {
var _a;
const modify = import_JsonBuilder.JsonBuilder.getModify(filePath, elementPath);
(_a = modify == null ? void 0 : modify.override) == null ? void 0 : _a.setProperties(properties || {});
return modify || import_JsonBuilder.JsonBuilder.registerModify(
filePath,
elementPath,
new Modify(properties, {
name: elementPath,
namespace
})
);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Modify
});