form start
This commit is contained in:
@@ -1,37 +1,41 @@
|
||||
import { Structure } from './Structure';
|
||||
import { StructureInstance } from './StructureInstance';
|
||||
import { world } from '@minecraft/server';
|
||||
|
||||
class StructureCollection {
|
||||
#structures;
|
||||
structures;
|
||||
|
||||
constructor() {
|
||||
this.#structures = {};
|
||||
this.structures = {};
|
||||
}
|
||||
|
||||
add(name) {
|
||||
if (this.#structures[name]) {
|
||||
throw new Error(`Structure ${name} already exists.`);
|
||||
}
|
||||
const structure = new Structure(name);
|
||||
this.#structures[name] = structure;
|
||||
add(instanceName, structureId) {
|
||||
if (this.structures[instanceName])
|
||||
throw new Error(`Instance ${instanceName} already exists.`);
|
||||
const structure = new StructureInstance(instanceName, structureId);
|
||||
this.structures[instanceName] = structure;
|
||||
return structure;
|
||||
}
|
||||
|
||||
get(name) {
|
||||
const structure = this.#structures[name];
|
||||
get(instanceName) {
|
||||
const structure = this.structures[instanceName];
|
||||
if (!structure) {
|
||||
throw new Error(`Structure ${name} not found.`);
|
||||
throw new Error(`Instance ${instanceName} not found.`);
|
||||
}
|
||||
return structure;
|
||||
}
|
||||
|
||||
remove(name) {
|
||||
const struct = this.get(name);
|
||||
struct.remove();
|
||||
delete this.#structures[name];
|
||||
remove(instanceName) {
|
||||
const struct = this.get(instanceName);
|
||||
struct.removePlacement();
|
||||
delete this.structures[instanceName];
|
||||
}
|
||||
|
||||
getInstanceNames() {
|
||||
return Object.keys(this.structures);
|
||||
}
|
||||
|
||||
getStructuresAtLocation(location) {
|
||||
return Object.values(this.#structures).filter(structure => structure.isLocationActive(structure.toStructureCoords(location)));
|
||||
return Object.values(this.structures).filter(structure => structure.isLocationActive(structure.toStructureCoords(location)));
|
||||
}
|
||||
|
||||
fetchStructureBlock(location) {
|
||||
@@ -41,6 +45,21 @@ class StructureCollection {
|
||||
const structure = locatedStructures[0];
|
||||
return structure.getBlock(structure.toStructureCoords(location));
|
||||
}
|
||||
|
||||
getWorldStructureIds() {
|
||||
return world.structureManager.getWorldStructureIds()
|
||||
.filter(id => id.startsWith('mystructure:'))
|
||||
.map(id => id.replace('mystructure:', ''));
|
||||
}
|
||||
|
||||
rename(instanceName, newName) {
|
||||
const structure = this.get(instanceName);
|
||||
if (this.structures[newName])
|
||||
throw new Error(`Instance ${newName} already exists.`);
|
||||
this.structures[newName] = structure;
|
||||
delete this.structures[instanceName];
|
||||
structure.name = newName;
|
||||
}
|
||||
}
|
||||
|
||||
export const structureCollection = new StructureCollection();
|
||||
Reference in New Issue
Block a user