complete rename and init builder options
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
import { structureCollection } from './StructureCollection';
|
||||
import { MenuForm } from '../MenuForm';
|
||||
import { forceShow } from '../../utils';
|
||||
import { InstanceButtons } from '../enums/InstanceButtons';
|
||||
import { InstanceFormBuilder } from './InstanceFormBuilder';
|
||||
import { FormCancelationReason } from '@minecraft/server-ui';
|
||||
|
||||
export class InstanceForm {
|
||||
instanceName;
|
||||
#buttons = {
|
||||
isEnabled: [
|
||||
InstanceButtons.NextLayer,
|
||||
InstanceButtons.PreviousLayer,
|
||||
InstanceButtons.SetLayer,
|
||||
InstanceButtons.Move,
|
||||
InstanceButtons.Statistics,
|
||||
InstanceButtons.Settings,
|
||||
InstanceButtons.Rename,
|
||||
InstanceButtons.Disable,
|
||||
],
|
||||
isNotEnabledAndIsNotPlaced: [
|
||||
InstanceButtons.Place,
|
||||
InstanceButtons.Rename
|
||||
],
|
||||
isNotEnabledButIsPlaced: [
|
||||
InstanceButtons.Enable,
|
||||
InstanceButtons.Rename
|
||||
],
|
||||
common: [
|
||||
InstanceButtons.Delete,
|
||||
InstanceButtons.MainMenu
|
||||
]
|
||||
}
|
||||
|
||||
constructor(player, instanceName) {
|
||||
this.player = player;
|
||||
this.instanceName = instanceName;
|
||||
this.instance = structureCollection.get(this.instanceName);
|
||||
this.show();
|
||||
}
|
||||
|
||||
show() {
|
||||
const currentOptions = this.getActiveOptions();
|
||||
forceShow(this.player, InstanceFormBuilder.buildInstance(this.instance, currentOptions)).then((response) => {
|
||||
if (response.canceled) return;
|
||||
this.handleOption(currentOptions[response.selection]);
|
||||
});
|
||||
}
|
||||
|
||||
getActiveOptions() {
|
||||
let currentOptions = [];
|
||||
if (this.instance.isEnabled())
|
||||
currentOptions = this.#buttons.isEnabled;
|
||||
else if (this.instance.hasLocation())
|
||||
currentOptions = this.#buttons.isNotEnabledButIsPlaced;
|
||||
else
|
||||
currentOptions = this.#buttons.isNotEnabledAndIsNotPlaced;
|
||||
currentOptions = currentOptions.concat(this.#buttons.common);
|
||||
|
||||
if (!this.instance.hasLayers())
|
||||
currentOptions = currentOptions.filter(option =>
|
||||
option !== InstanceButtons.SetLayer
|
||||
&& option !== InstanceButtons.NextLayer
|
||||
&& option !== InstanceButtons.PreviousLayer
|
||||
);
|
||||
return currentOptions;
|
||||
}
|
||||
|
||||
handleOption(option) {
|
||||
switch (option) {
|
||||
case InstanceButtons.Enable:
|
||||
this.instance.enable();
|
||||
break;
|
||||
case InstanceButtons.Disable:
|
||||
this.instance.disable();
|
||||
break;
|
||||
case InstanceButtons.Place:
|
||||
this.instance.place(this.player.dimension.id, this.player.location);
|
||||
break;
|
||||
case InstanceButtons.Rename:
|
||||
this.renameInstanceForm();
|
||||
break;
|
||||
case InstanceButtons.Delete:
|
||||
structureCollection.delete(this.instanceName);
|
||||
break;
|
||||
case InstanceButtons.NextLayer:
|
||||
this.instance.increaseLayer();
|
||||
new InstanceForm(this.player, this.instanceName);
|
||||
break;
|
||||
case InstanceButtons.PreviousLayer:
|
||||
this.instance.decreaseLayer();
|
||||
new InstanceForm(this.player, this.instanceName);
|
||||
break;
|
||||
case InstanceButtons.Settings:
|
||||
this.settingsForm();
|
||||
break;
|
||||
case InstanceButtons.Move:
|
||||
this.instance.move(this.player.dimension.id, this.player.location);
|
||||
break;
|
||||
case InstanceButtons.Statistics:
|
||||
this.statisticsForm();
|
||||
break;
|
||||
case InstanceButtons.MainMenu:
|
||||
new MenuForm(this.player, { jumpToInstance: false });
|
||||
break;
|
||||
default:
|
||||
this.player.sendMessage(`§cUnknown option: ${option}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
renameInstanceForm() {
|
||||
InstanceFormBuilder.buildRenameInstance(this.instanceName).show(this.player).then((response) => {
|
||||
if (response.canceled)
|
||||
return;
|
||||
const newName = response.formValues[0];
|
||||
if (newName === '') {
|
||||
this.player.sendMessage('§cInstance name cannot be empty.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
structureCollection.rename(this.instanceName, newName);
|
||||
this.instanceName = newName;
|
||||
} catch (e) {
|
||||
this.player.sendMessage(`§cError renaming instance: ${e.message}`);
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setLayerForm() {
|
||||
InstanceFormBuilder.buildSetLayer(this.instance.getBounds().max.y, this.instance.getLayer()).show(this.player).then((response) => {
|
||||
if (response.canceled)
|
||||
return;
|
||||
this.instance.setLayer(parseInt(response.formValues[0]));
|
||||
});
|
||||
}
|
||||
|
||||
async statisticsForm() {
|
||||
const statsForm = await InstanceFormBuilder.buildStatistics(this.instance)
|
||||
statsForm.form.show(this.player).then((response) => {
|
||||
if (response.canceled && response.cancelationReason === FormCancelationReason.UserBusy)
|
||||
this.player.sendMessage(statsForm.stats);
|
||||
});
|
||||
}
|
||||
|
||||
settingsForm() {
|
||||
InstanceFormBuilder.buildSettings(this.instance).show(this.player).then((response) => {
|
||||
if (response.canceled)
|
||||
return;
|
||||
this.instance.setVerifierEnabled(response.formValues[0]);
|
||||
this.instance.setLayer(parseInt(response.formValues[1]));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { ActionFormData, ModalFormData } from '@minecraft/server-ui';
|
||||
import { MenuFormBuilder } from '../MenuFormBuilder';
|
||||
import { StructureVerifier } from './StructureVerifier';
|
||||
import { StructureStatistics } from './StructureStatistics';
|
||||
import { TicksPerSecond } from '@minecraft/server';
|
||||
|
||||
export class InstanceFormBuilder {
|
||||
static buildInstance(instance, options) {
|
||||
const location = instance.getLocation();
|
||||
const form = new ActionFormData()
|
||||
.title(MenuFormBuilder.menuTitle)
|
||||
let body = `Instance: §a${instance.getName()}\n§fStructure: §2${instance.getStructureId()}\n`;
|
||||
if (instance.hasLocation())
|
||||
body += `§7(${location.location.x} ${location.location.y} ${location.location.z} in ${location.dimensionId})\n`;
|
||||
form.body(body);
|
||||
options.forEach(option => {
|
||||
form.button(`${option}`);
|
||||
});
|
||||
return form;
|
||||
}
|
||||
|
||||
static buildRenameInstance(currentName) {
|
||||
return new ModalFormData()
|
||||
.title(MenuFormBuilder.menuTitle)
|
||||
.textField('Enter a new name for the instance:', currentName)
|
||||
.submitButton('Rename');
|
||||
}
|
||||
|
||||
static async buildStatistics(instance) {
|
||||
const buildStatisticsForm = new ActionFormData()
|
||||
.title(MenuFormBuilder.menuTitle)
|
||||
const structureVerifier = new StructureVerifier(instance, { isEnabled: true, trackPlayerDistance: 0, intervalOrLifetime: 30 * TicksPerSecond });
|
||||
const verification = await structureVerifier.verifyStructure();
|
||||
const statistics = new StructureStatistics(instance, verification);
|
||||
const statsMessage = statistics.getMessage();
|
||||
buildStatisticsForm.body(statsMessage);
|
||||
return { form: buildStatisticsForm, stats: statsMessage };
|
||||
}
|
||||
|
||||
static buildSettings(instance) {
|
||||
return new ModalFormData()
|
||||
.title(MenuFormBuilder.menuTitle)
|
||||
.label('Use the slider to select the layer. Use 0 for all layers.')
|
||||
.toggle('Block Validation', instance.options.verifier.isEnabled)
|
||||
.slider("Layer", 0, maxLayer, 1, currentLayer)
|
||||
.submitButton('§aApply');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { Vector } from "../../lib/Vector";
|
||||
import { world } from "@minecraft/server";
|
||||
import { Option } from "../Option";
|
||||
|
||||
export class InstanceOptions extends Option {
|
||||
#DP_NAMESPACE = "instanceOptions";
|
||||
instanceName = void 0;
|
||||
structureId = void 0;
|
||||
isEnabled = false;
|
||||
dimensionId = void 0;
|
||||
worldLocation = new Vector();
|
||||
currentLayer = 0;
|
||||
verifier = {
|
||||
isEnabled: true,
|
||||
trackPlayerDistance: 5,
|
||||
intervalOrLifetime: 10
|
||||
};
|
||||
|
||||
static getInstanceStrucetureId(instanceName) {
|
||||
const options = new InstanceOptions(instanceName, void 0);
|
||||
return options.structureId;
|
||||
}
|
||||
|
||||
constructor(instanceName, structureId) {
|
||||
this.instanceName = instanceName;
|
||||
this.structureId = structureId;
|
||||
this.load();
|
||||
}
|
||||
|
||||
save() {
|
||||
this.saveToDP(this.#DP_NAMESPACE, this.instanceName, this);
|
||||
}
|
||||
|
||||
load() {
|
||||
this.loadFromDP(this.#DP_NAMESPACE, this.instanceName);
|
||||
this.worldLocation = Vector.from(this.worldLocation);
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.clearDP(this.#DP_NAMESPACE, this.instanceName);
|
||||
}
|
||||
|
||||
getDimension() {
|
||||
return world.getDimension(this.dimensionId);
|
||||
}
|
||||
|
||||
enable() {
|
||||
this.isEnabled = true;
|
||||
this.save();
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.isEnabled = false;
|
||||
this.save();
|
||||
}
|
||||
|
||||
rename(newName) {
|
||||
this.clear();
|
||||
this.instanceName = newName;
|
||||
this.save();
|
||||
}
|
||||
|
||||
move(dimensionId, worldLocation) {
|
||||
this.dimensionId = dimensionId;
|
||||
this.worldLocation = Vector.from(worldLocation).floor();
|
||||
this.save();
|
||||
}
|
||||
|
||||
setLayer(layer) {
|
||||
this.currentLayer = layer;
|
||||
this.save();
|
||||
}
|
||||
|
||||
setVerifierEnabled(enable) {
|
||||
this.verifier.isEnabled = enable;
|
||||
this.save();
|
||||
}
|
||||
|
||||
setVerifierDistance(distance) {
|
||||
this.verifier.trackPlayerDistance = distance;
|
||||
this.save();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user