@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.
101 lines (100 loc) • 4.23 kB
JavaScript
"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.SimpleFormNode = void 0;
const SimpleFormLayerType_1 = require("../enums/SimpleFormLayerType");
const LevelType_1 = require("../LevelType");
class SimpleFormNode {
constructor(title, content, preload) {
this.father = null;
this.preload = preload;
this.title = title;
this.content = content;
this.controls = new Array();
}
setFather(father) {
this.father = father;
}
setContent(content) {
this.content = content;
}
addButton(text, image, callback) {
this.controls.push({
type: SimpleFormLayerType_1.SimpleFormLayerType.BUTTON,
options: { text: text, image: image },
callback: callback
});
return this;
}
addLabel(text) {
this.controls.push({ type: SimpleFormLayerType_1.SimpleFormLayerType.LABEL, options: { text: text } });
return this;
}
addDivider() {
this.controls.push({ type: SimpleFormLayerType_1.SimpleFormLayerType.DIVIDER });
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;
if (this.preload) {
yield this.preload(this);
}
const form = mc.newSimpleForm();
if (this.title) {
form.setTitle(this.title);
}
if (this.content) {
form.setContent(this.content);
}
if (this.father) {
form.addButton("返回");
}
for (const control of this.controls) {
if (control.type === SimpleFormLayerType_1.SimpleFormLayerType.BUTTON) {
const options = control.options;
form.addButton(options.text, (_a = options.image) !== null && _a !== void 0 ? _a : "");
}
else if (control.type === SimpleFormLayerType_1.SimpleFormLayerType.LABEL) {
const options = control.options;
form.addLabel(options.text);
}
else if (control.type === SimpleFormLayerType_1.SimpleFormLayerType.DIVIDER) {
form.addDivider();
}
}
(_b = mc.getPlayer(playerIdentifier)) === null || _b === void 0 ? void 0 : _b.sendForm(form, (player, data) => {
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
if (this.father && data === 0) {
yield this.father.render(playerIdentifier);
}
else if (data || data === 0) {
let controlIndex = data;
if (this.father) {
controlIndex -= 1;
}
yield this.controls[controlIndex].callback(this, playerIdentifier);
}
resolve();
}));
});
}));
});
}
getType() {
return LevelType_1.LevelType.FORM;
}
getOccupiedTicks() {
return 0;
}
}
exports.SimpleFormNode = SimpleFormNode;