prevent structureVerifier from having multiple instances

This commit is contained in:
ForestOfLight
2025-06-04 19:05:44 -07:00
Unverified
parent 6ad128cc9c
commit 7b0afc529d
4 changed files with 19 additions and 6 deletions
@@ -136,7 +136,15 @@ export class InstanceForm {
} }
async statisticsForm() { async statisticsForm() {
const statsForm = await InstanceFormBuilder.buildStatistics(this.instance) let statsForm;
try {
statsForm = await InstanceFormBuilder.buildStatistics(this.instance);
} catch (e) {
if (e.message === 'StructureVerifier is already running.') {
this.player.sendMessage('§cA verification is already in progress. Please wait until it finishes.');
return;
}
}
statsForm.form.show(this.player).then((response) => { statsForm.form.show(this.player).then((response) => {
if (response.canceled && response.cancelationReason === FormCancelationReason.UserBusy) if (response.canceled && response.cancelationReason === FormCancelationReason.UserBusy)
this.player.sendMessage(statsForm.stats); this.player.sendMessage(statsForm.stats);
@@ -5,6 +5,8 @@ import { StructureStatistics } from '../Structure/StructureStatistics';
import { TicksPerSecond } from '@minecraft/server'; import { TicksPerSecond } from '@minecraft/server';
export class InstanceFormBuilder { export class InstanceFormBuilder {
static structureVerifier;
static buildInstance(instance, options) { static buildInstance(instance, options) {
const location = instance.getLocation(); const location = instance.getLocation();
const form = new ActionFormData() const form = new ActionFormData()
@@ -29,9 +31,12 @@ export class InstanceFormBuilder {
static async buildStatistics(instance) { static async buildStatistics(instance) {
const buildStatisticsForm = new ActionFormData() const buildStatisticsForm = new ActionFormData()
.title(MenuFormBuilder.menuTitle) .title(MenuFormBuilder.menuTitle)
const structureVerifier = new StructureVerifier(instance, { isEnabled: true, trackPlayerDistance: 0, intervalOrLifetime: 30 * TicksPerSecond, isStandalone: true }); if (this.structureVerifier)
const verification = await structureVerifier.verifyStructure(); throw new Error('StructureVerifier is already running.');
this.structureVerifier = new StructureVerifier(instance, { isEnabled: true, trackPlayerDistance: 0, intervalOrLifetime: 30 * TicksPerSecond, isStandalone: true });
const verification = await this.structureVerifier.verifyStructure();
const statistics = new StructureStatistics(instance, verification); const statistics = new StructureStatistics(instance, verification);
this.structureVerifier = void 0;
const statsMessage = statistics.getMessage(); const statsMessage = statistics.getMessage();
buildStatisticsForm.body(statsMessage); buildStatisticsForm.body(statsMessage);
return { form: buildStatisticsForm, stats: statsMessage }; return { form: buildStatisticsForm, stats: statsMessage };
@@ -41,7 +46,7 @@ export class InstanceFormBuilder {
return new ModalFormData() return new ModalFormData()
.title(MenuFormBuilder.menuTitle) .title(MenuFormBuilder.menuTitle)
.toggle('Block Validation', { defaultValue: instance.options.verifier.isEnabled, tooltip: 'Shows missing and incorrect block overlay.' }) .toggle('Block Validation', { defaultValue: instance.options.verifier.isEnabled, tooltip: 'Shows missing and incorrect block overlay.' })
.toggle('Distance-Based Block Validation', { defaultValue: instance.verifier.getTrackPlayerDistance() !== 0, tooltip: 'If enabled, the verifier will only check blocks within a certain distance from the player.' }) .toggle('Distance-Based Block Validation', { defaultValue: instance.verifier.getTrackPlayerDistance() !== 0, tooltip: `If enabled, the verifier will only check within ${instance.verifier.getTrackPlayerDistance()} blocks of the player. This should stay enabled unless your structure is very small.` })
.label('Use the slider to select the layer. Use 0 for all layers.') .label('Use the slider to select the layer. Use 0 for all layers.')
.slider("Layer", 0, instance.getMaxLayer(), { defaultValue: instance.getLayer(), valueStep: 1 }) .slider("Layer", 0, instance.getMaxLayer(), { defaultValue: instance.getLayer(), valueStep: 1 })
.submitButton('§2Apply'); .submitButton('§2Apply');
@@ -149,7 +149,7 @@ export class StructureInstance {
} }
hasLocation() { hasLocation() {
return this.options.dimensionId && this.options.worldLocation.x !== 0 && this.options.worldLocation.y !== 0 && this.options.worldLocation.z !== 0; return this.options.dimensionId && (this.options.worldLocation.x !== 0 || this.options.worldLocation.y !== 0 || this.options.worldLocation.z !== 0);
} }
hasLayers() { hasLayers() {
+1 -1
View File
@@ -91,7 +91,7 @@ export class MenuForm {
const structureId = response.formValues[0]; const structureId = response.formValues[0];
if (structureId === '') if (structureId === '')
return void 0; return void 0;
if (!structureCollection.getWorldStructureIds().includes(structureId)) { if (!structureCollection.getWorldStructureIds().some(id => id.replace('mystructure:', '') === structureId)) {
this.player.sendMessage(`§cStructure ID '${structureId}' not found. If you're looking for a structure that you put in the structures folder, please restart your world and try again.`); this.player.sendMessage(`§cStructure ID '${structureId}' not found. If you're looking for a structure that you put in the structures folder, please restart your world and try again.`);
return void 0; return void 0;
} }