Form Error handling & small code cleanup

This commit is contained in:
ForestOfLight
2025-04-13 14:27:41 -07:00
Unverified
parent b74362166f
commit 1a56192682
7 changed files with 31 additions and 23 deletions
+7 -2
View File
@@ -110,8 +110,13 @@ export class InstanceEditForm {
this.player.sendMessage('§cInstance name cannot be empty.'); this.player.sendMessage('§cInstance name cannot be empty.');
return; return;
} }
structureCollection.rename(this.instanceName, newName); try {
this.instanceName = newName; structureCollection.rename(this.instanceName, newName);
this.instanceName = newName;
} catch (e) {
this.player.sendMessage(`§cError renaming instance: ${e.message}`);
return;
}
}); });
} }
+1 -1
View File
@@ -6,7 +6,7 @@ export class InstanceEditFormBuilder {
const location = instance.getLocation(); const location = instance.getLocation();
const form = new ActionFormData() const form = new ActionFormData()
.title(MenuFormBuilder.menuTitle) .title(MenuFormBuilder.menuTitle)
let body = `Instance: §2${instance.name}\n`; let body = `Instance: §a${instance.name}\n§fStructure: §2${instance.getStructureId()}\n`;
if (instance.hasLocation()) if (instance.hasLocation())
body += `§7(${location.location.x} ${location.location.y} ${location.location.z} in ${location.dimensionId})\n`; body += `§7(${location.location.x} ${location.location.y} ${location.location.z} in ${location.dimensionId})\n`;
form.body(body); form.body(body);
+1 -10
View File
@@ -2,7 +2,6 @@ import { forceShow } from '../utils';
import { structureCollection } from './StructureCollection'; import { structureCollection } from './StructureCollection';
import { MenuFormBuilder } from './MenuFormBuilder'; import { MenuFormBuilder } from './MenuFormBuilder';
import { InstanceEditForm } from './InstanceEditForm'; import { InstanceEditForm } from './InstanceEditForm';
import { world } from '@minecraft/server';
export class MenuForm { export class MenuForm {
constructor(player, { jumpToInstance = true } = {}) { constructor(player, { jumpToInstance = true } = {}) {
@@ -13,7 +12,7 @@ export class MenuForm {
async show(jumpToInstance = true) { async show(jumpToInstance = true) {
let instanceName; let instanceName;
if (jumpToInstance) { if (jumpToInstance) {
instanceName = this.getInstanceNameAtLocation(); instanceName = structureCollection.getStructure(this.player.dimension.id, this.player.location, { useLayers: false })?.name;
if (instanceName) { if (instanceName) {
new InstanceEditForm(this.player, instanceName); new InstanceEditForm(this.player, instanceName);
return; return;
@@ -25,14 +24,6 @@ export class MenuForm {
new InstanceEditForm(this.player, instanceName); new InstanceEditForm(this.player, instanceName);
} }
getInstanceNameAtLocation() {
const locatedStructures = structureCollection.getStructures(this.player.dimension.id, this.player.location, { useLayers: false });
if (locatedStructures.length === 0)
return void 0;
const structure = locatedStructures[0];
return structure.name;
}
async getInstanceNameFromForm() { async getInstanceNameFromForm() {
try { try {
return forceShow(this.player, MenuFormBuilder.buildAllInstanceName()).then((response) => { return forceShow(this.player, MenuFormBuilder.buildAllInstanceName()).then((response) => {
+1 -1
View File
@@ -46,7 +46,7 @@ export class MenuFormBuilder {
let body = "How to Add Structures:\n" let body = "How to Add Structures:\n"
body += "§7- Save a structure using a structure block or the /structure command.\n" body += "§7- Save a structure using a structure block or the /structure command.\n"
body += "§7OR\n" body += "§7OR\n"
body += "§7- Add a mcstructure file to this pack's structures folder. It will not appear in the list of structures, so enter the filename (without '.mcstructure') as the Structure ID."; body += "§7- Add a .mcstructure file to this pack's structures folder. When selecting your structure, select the 'Other' option and then use the filename (without '.mcstructure') as the Structure ID. After its first use, it will be added to the list of structures.";
return new ActionFormData() return new ActionFormData()
.title(this.menuTitle) .title(this.menuTitle)
.body(body); .body(body);
+10 -5
View File
@@ -10,9 +10,8 @@ export class Raycaster {
let location = startLocation; let location = startLocation;
let distance = 0; let distance = 0;
while (distance < maxDistance) { while (distance < maxDistance) {
const locatedStructures = structureCollection.getStructures(dimension.id, location, { useLayers }); const structure = structureCollection.getStructure(dimension.id, location, { useLayers });
if (locatedStructures.length !== 0) { if (structure) {
const structure = locatedStructures[0];
const block = structure.getBlock(structure.toStructureCoords(location)); const block = structure.getBlock(structure.toStructureCoords(location));
if (block?.type.id !== 'minecraft:air') { if (block?.type.id !== 'minecraft:air') {
blocks.push({ blocks.push({
@@ -22,8 +21,14 @@ export class Raycaster {
if (getFirst) if (getFirst)
break; break;
} }
if (collideWithWorldBlocks && !dimension.getBlock(location)?.isAir) try {
break; if (collideWithWorldBlocks && !dimension.getBlock(location)?.isAir)
break;
} catch (e) {
if (e.name === 'LocationOutOfWorldBoundariesError')
break;
throw e;
}
} }
location = { location = {
x: location.x + (direction.x*this.STEP_SIZE), x: location.x + (direction.x*this.STEP_SIZE),
+7 -4
View File
@@ -53,11 +53,14 @@ class StructureCollection {
return Object.values(this.structures).filter(structure => structure.isLocationActive(dimensionId, structure.toStructureCoords(location), options)); return Object.values(this.structures).filter(structure => structure.isLocationActive(dimensionId, structure.toStructureCoords(location), options));
} }
getStructure(dimensionId, location, options = {}) {
return this.getStructures(dimensionId, location, options)[0];
}
fetchStructureBlock(dimensionId, location) { fetchStructureBlock(dimensionId, location) {
const locatedStructures = this.getStructures(dimensionId, location); const structure = this.getStructure(dimensionId, location);
if (locatedStructures.length === 0) if (!structure)
return void 0; return void 0;
const structure = locatedStructures[0];
return structure.getBlock(structure.toStructureCoords(location)); return structure.getBlock(structure.toStructureCoords(location));
} }
@@ -70,7 +73,7 @@ class StructureCollection {
rename(instanceName, newName) { rename(instanceName, newName) {
const structure = this.get(instanceName); const structure = this.get(instanceName);
if (this.structures[newName]) if (this.structures[newName])
throw new Error(`Instance ${newName} already exists.`); throw new Error(`Instance '${newName}' already exists.`);
structure.rename(newName); structure.rename(newName);
this.structures[newName] = structure; this.structures[newName] = structure;
delete this.structures[instanceName]; delete this.structures[instanceName];
+4
View File
@@ -59,6 +59,10 @@ export class StructureInstance {
return this.#structure; return this.#structure;
} }
getStructureId() {
return this.#options.structureId;
}
getLocation() { getLocation() {
return { dimensionId: this.#options.dimensionId, location: this.#options.worldLocation }; return { dimensionId: this.#options.dimensionId, location: this.#options.worldLocation };
} }