UNPKG

@levimc-lse/interface-api

Version:

API for the logic tree development framework that supports cross-plugin UI queues at runtime in Minecraft Legacy Script Engine.

138 lines (137 loc) 6.62 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomFormNode = void 0; const CustomFormLayerType_1 = require("../enums/CustomFormLayerType"); const LevelType_1 = require("../LevelType"); class CustomFormNode { constructor(title, preload) { this.father = null; this.preload = preload; this.title = title; this.controls = new Array(); this.multiFactorTriggers = new Array(); } setFather(father) { this.father = father; } addLabel(text) { this.controls.push({ type: CustomFormLayerType_1.CustomFormLayerType.LABEL, options: { text: text } }); return this; } addInput(title, placeholder, defaultValue, callback) { this.controls.push({ type: CustomFormLayerType_1.CustomFormLayerType.INPUT, options: { title: title, placeholder: placeholder, default: defaultValue }, callback: callback }); return this; } addSwitch(title, defaultValue, callback) { this.controls.push({ type: CustomFormLayerType_1.CustomFormLayerType.SWITCH, options: { title: title, default: defaultValue }, callback: callback }); return this; } addDropdown(title, items, defaultValue, callback) { this.controls.push({ type: CustomFormLayerType_1.CustomFormLayerType.DROPDOWN, options: { title: title, items: items, default: defaultValue }, callback: callback }); return this; } addSlider(title, min, max, step, defaultValue, callback) { this.controls.push({ type: CustomFormLayerType_1.CustomFormLayerType.SLIDER, options: { title: title, min: min, max: max, step: step, default: defaultValue }, callback: callback }); return this; } addMultiFactorTrigger(trigger, callback) { this.multiFactorTriggers.push({ trigger: trigger, callback: callback }); return this; } render(playerIdentifier) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; if (this.preload) { yield this.preload(this); } const form = mc.newCustomForm(); if (this.title) { form.setTitle(this.title); } if (this.controls.length > 0) { for (const control of this.controls) { if (control.type === CustomFormLayerType_1.CustomFormLayerType.LABEL) { const options = control.options; form.addLabel(options.text); } else if (control.type === CustomFormLayerType_1.CustomFormLayerType.INPUT) { const options = control.options; form.addInput(options.title, (_a = options.placeholder) !== null && _a !== void 0 ? _a : "", (_b = options.default) !== null && _b !== void 0 ? _b : ""); } else if (control.type === CustomFormLayerType_1.CustomFormLayerType.SWITCH) { const options = control.options; form.addSwitch(options.title, (_c = options.default) !== null && _c !== void 0 ? _c : true); } else if (control.type === CustomFormLayerType_1.CustomFormLayerType.DROPDOWN) { const options = control.options; form.addDropdown(options.title, options.items, (_d = options.default) !== null && _d !== void 0 ? _d : 0); } else if (control.type === CustomFormLayerType_1.CustomFormLayerType.SLIDER) { const options = control.options; form.addSlider(options.title, options.min, options.max, options.step, (_e = options.default) !== null && _e !== void 0 ? _e : options.min); } else if (control.type === CustomFormLayerType_1.CustomFormLayerType.STEP_SLIDER) { const options = control.options; form.addStepSlider(options.title, options.items, (_f = options.default) !== null && _f !== void 0 ? _f : 0); } } } mc.getPlayer(playerIdentifier).sendForm(form, (player, data) => { setTimeout(() => __awaiter(this, void 0, void 0, function* () { if (this.father && !data) { yield this.father.render(playerIdentifier); } else if (data) { let dataIndex = 0; for (const control of this.controls) { if (control.callback) { yield control.callback(this, playerIdentifier, data[dataIndex]); } dataIndex++; } for (const trigger of this.multiFactorTriggers) { if (trigger.trigger == data) { yield trigger.callback(this, playerIdentifier); } } } resolve(); })); }); })); }); } getType() { return LevelType_1.LevelType.FORM; } getOccupiedTicks() { return 0; } } exports.CustomFormNode = CustomFormNode;