diff --git a/scripts/classes/InstanceEditForm.js b/scripts/classes/InstanceEditForm.js index ec68058..c4acdb7 100644 --- a/scripts/classes/InstanceEditForm.js +++ b/scripts/classes/InstanceEditForm.js @@ -110,8 +110,13 @@ export class InstanceEditForm { this.player.sendMessage('§cInstance name cannot be empty.'); return; } - structureCollection.rename(this.instanceName, newName); - this.instanceName = newName; + try { + structureCollection.rename(this.instanceName, newName); + this.instanceName = newName; + } catch (e) { + this.player.sendMessage(`§cError renaming instance: ${e.message}`); + return; + } }); } diff --git a/scripts/classes/InstanceEditFormBuilder.js b/scripts/classes/InstanceEditFormBuilder.js index 5e66dec..118a062 100644 --- a/scripts/classes/InstanceEditFormBuilder.js +++ b/scripts/classes/InstanceEditFormBuilder.js @@ -6,7 +6,7 @@ export class InstanceEditFormBuilder { const location = instance.getLocation(); const form = new ActionFormData() .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()) body += `§7(${location.location.x} ${location.location.y} ${location.location.z} in ${location.dimensionId})\n`; form.body(body); diff --git a/scripts/classes/MenuForm.js b/scripts/classes/MenuForm.js index 8827156..d717e75 100644 --- a/scripts/classes/MenuForm.js +++ b/scripts/classes/MenuForm.js @@ -2,7 +2,6 @@ import { forceShow } from '../utils'; import { structureCollection } from './StructureCollection'; import { MenuFormBuilder } from './MenuFormBuilder'; import { InstanceEditForm } from './InstanceEditForm'; -import { world } from '@minecraft/server'; export class MenuForm { constructor(player, { jumpToInstance = true } = {}) { @@ -13,7 +12,7 @@ export class MenuForm { async show(jumpToInstance = true) { let instanceName; if (jumpToInstance) { - instanceName = this.getInstanceNameAtLocation(); + instanceName = structureCollection.getStructure(this.player.dimension.id, this.player.location, { useLayers: false })?.name; if (instanceName) { new InstanceEditForm(this.player, instanceName); return; @@ -25,14 +24,6 @@ export class MenuForm { 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() { try { return forceShow(this.player, MenuFormBuilder.buildAllInstanceName()).then((response) => { diff --git a/scripts/classes/MenuFormBuilder.js b/scripts/classes/MenuFormBuilder.js index 8716a91..bd7038f 100644 --- a/scripts/classes/MenuFormBuilder.js +++ b/scripts/classes/MenuFormBuilder.js @@ -46,7 +46,7 @@ export class MenuFormBuilder { let body = "How to Add Structures:\n" body += "§7- Save a structure using a structure block or the /structure command.\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() .title(this.menuTitle) .body(body); diff --git a/scripts/classes/Raycaster.js b/scripts/classes/Raycaster.js index fdeb365..030b58c 100644 --- a/scripts/classes/Raycaster.js +++ b/scripts/classes/Raycaster.js @@ -10,9 +10,8 @@ export class Raycaster { let location = startLocation; let distance = 0; while (distance < maxDistance) { - const locatedStructures = structureCollection.getStructures(dimension.id, location, { useLayers }); - if (locatedStructures.length !== 0) { - const structure = locatedStructures[0]; + const structure = structureCollection.getStructure(dimension.id, location, { useLayers }); + if (structure) { const block = structure.getBlock(structure.toStructureCoords(location)); if (block?.type.id !== 'minecraft:air') { blocks.push({ @@ -22,8 +21,14 @@ export class Raycaster { if (getFirst) break; } - if (collideWithWorldBlocks && !dimension.getBlock(location)?.isAir) - break; + try { + if (collideWithWorldBlocks && !dimension.getBlock(location)?.isAir) + break; + } catch (e) { + if (e.name === 'LocationOutOfWorldBoundariesError') + break; + throw e; + } } location = { x: location.x + (direction.x*this.STEP_SIZE), diff --git a/scripts/classes/StructureCollection.js b/scripts/classes/StructureCollection.js index 605f6e9..4082036 100644 --- a/scripts/classes/StructureCollection.js +++ b/scripts/classes/StructureCollection.js @@ -53,11 +53,14 @@ class StructureCollection { 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) { - const locatedStructures = this.getStructures(dimensionId, location); - if (locatedStructures.length === 0) + const structure = this.getStructure(dimensionId, location); + if (!structure) return void 0; - const structure = locatedStructures[0]; return structure.getBlock(structure.toStructureCoords(location)); } @@ -70,7 +73,7 @@ class StructureCollection { rename(instanceName, newName) { const structure = this.get(instanceName); if (this.structures[newName]) - throw new Error(`Instance ${newName} already exists.`); + throw new Error(`Instance '${newName}' already exists.`); structure.rename(newName); this.structures[newName] = structure; delete this.structures[instanceName]; diff --git a/scripts/classes/StructureInstance.js b/scripts/classes/StructureInstance.js index a8e19cb..1b35582 100644 --- a/scripts/classes/StructureInstance.js +++ b/scripts/classes/StructureInstance.js @@ -59,6 +59,10 @@ export class StructureInstance { return this.#structure; } + getStructureId() { + return this.#options.structureId; + } + getLocation() { return { dimensionId: this.#options.dimensionId, location: this.#options.worldLocation }; }