import { forceShow } from '../utils'; import { structureCollection } from './Structure/StructureCollection'; import { MenuFormBuilder } from './MenuFormBuilder'; import { InstanceForm } from './Instance/InstanceForm'; import { BuilderForm } from './Builder/BuilderForm'; export class MenuForm { constructor(player, { jumpToInstance = false, instanceName = void 0 } = {}) { this.player = player; this.show(jumpToInstance, instanceName); } async show(jumpToInstance = false, instanceName = void 0) { if (jumpToInstance) { if (!instanceName) instanceName = structureCollection.getStructure(this.player.dimension.id, this.player.location, { useActiveLayer: false })?.getName(); if (instanceName) { new InstanceForm(this.player, instanceName); return; } } instanceName = await this.getInstanceNameFromForm(); if (!instanceName) return; new InstanceForm(this.player, instanceName); } async getInstanceNameFromForm() { try { return forceShow(this.player, MenuFormBuilder.buildAllInstanceName()).then((response) => { if (response.canceled) return; let selection = response.selection; if (selection === 0) { new BuilderForm(this.player); return void 0; } else if (selection == structureCollection.getInstanceNames().length + 2) { MenuFormBuilder.buildHowTo().show(this.player); return void 0; } else { selection--; const selectedInstanceName = structureCollection.getInstanceNames()[selection]; return selectedInstanceName || this.createNewInstance(); } }); } catch (e) { if (e.message === 'Menu timed out.') { this.player.sendMessage({ translate: 'construct.menu.open.timeout' }); return; } throw e; } } async createNewInstance() { return MenuFormBuilder.buildNewInstance().show(this.player).then(async (response) => { if (response.canceled) return void 0; const instanceName = response.formValues[0]; if (instanceName === '') return void 0; const structureId = await this.getStructureId(); if (!structureId) return void 0; try { structureCollection.add(instanceName, structureId); } catch (e) { if (e.name === 'InvalidInstanceError') { this.player.sendMessage({ translate: 'construct.mainmenu.instance.exists', with: [instanceName] }); return void 0; } if (e.name === 'InvalidStructureError') { this.player.sendMessage({ translate: 'construct.mainmenu.instance.notfound', with: [structureId] }); return void 0; } throw e; } return instanceName; }); } async getStructureId() { return MenuFormBuilder.buildAllStructures().show(this.player).then((response) => { if (response.canceled) return void 0; const selectedStructureId = structureCollection.getWorldStructureIds()[response.selection]; return selectedStructureId || this.getOtherStructureId(); }); } getOtherStructureId() { return MenuFormBuilder.buildOtherStructure().show(this.player).then((response) => { if (response.canceled) return void 0; const structureId = response.formValues[0]; if (structureId === '') return void 0; return structureId; }); } }