wip: Construct API

This commit is contained in:
ForestOfLight
2026-05-29 23:27:11 +02:00
Unverified
parent f750710732
commit 2b90020e37
25 changed files with 454 additions and 29 deletions
+18
View File
@@ -0,0 +1,18 @@
import { AddonAPI } from "../lib/AddonAPIKit/AddonAPIKit";
import { PACK_IDENTIFIER } from "../consts";
import { InstancesController } from "./controllers/InstancesController";
import { structureCollection } from "../classes/Structure/StructureCollection";
import { BuildersController } from "./controllers/BuildersController";
import { Builders } from "../classes/Builder/Builders";
class ConstructAPI extends AddonAPI {
constructor(version) {
super(PACK_IDENTIFIER, version);
const instancesController = new InstancesController(structureCollection);
this.setupController(instancesController);
const buildersController = new BuildersController(Builders);
this.setupController(buildersController);
}
}
export const constructAPI = new ConstructAPI("1.0.0");
@@ -0,0 +1,37 @@
import { BuilderNotFoundError } from "../../classes/Errors/BuilderNotFoundError";
import { APICallerError } from "../../lib/AddonAPIKit/AddonAPIKit";
import { BuilderIdParameterModel } from "../models/BuildersModel";
export class BuildersController extends APIController {
#context;
constructor(context) {
super({
"builders:get": { callback: this.getBuilder, parameterModel: BuilderIdParameterModel, returnModel: InstanceModel },
"builders:edit": { callback: this.editBuilder, parameterModel: InstanceModel, returnModel: InstanceModel }
});
this.#context = context;
}
getBuilder(playerId) {
try {
const builder = this.#context.get(playerId);
return builder.asPacket();
} catch(error) {
if (error instanceof BuilderNotFoundErrors)
throw new APICallerError(error);
throw error;
}
}
editBuilder(builderOptions) {
try {
const builder = this.#context.get(builderOptions.playerId);
builder.setOptions(builderOptions);
} catch(error) {
if (error instanceof BuilderNotFoundError)
throw new APICallerError(error);
throw error;
}
}
}
@@ -0,0 +1,80 @@
import { InstanceExistsError } from "../../classes/Errors/InstanceExistsError";
import { InstanceNotFoundError } from "../../classes/Errors/InstanceNotFoundError";
import { StructureNotFoundError } from "../../classes/Errors/StructureNotFoundError";
import { APICallerError, VoidModel } from "../../lib/AddonAPIKit/AddonAPIKit";
import { AddInstanceParameterModel, InstanceModel, InstanceNameParameterModel, InstancesModel, StructureMaterialsModel } from "../models/InstancesModel";
export class InstancesController extends APIController {
#context;
constructor(context) {
super({
"instances": { callback: this.getInstances, parameterModel: VoidModel, returnModel: InstancesModel },
"instance:get": { callback: this.getInstance, parameterModel: InstanceNameParameterModel, returnModel: InstanceModel },
"instance:add": { callback: this.addInstance, parameterModel: AddInstanceParameterModel, returnModel: InstanceModel },
"instance:edit": { callback: this.editInstance, parameterModel: InstanceModel, returnModel: InstanceModel },
"instance:delete": { callback: this.deleteInstance, parameterModel: InstanceNameParameterModel, returnModel: VoidModel },
"instance:materials": { callback: this.getMaterials, parameterModel: InstanceNameParameterModel, returnModel: StructureMaterialsModel }
});
this.#context = context;
}
getInstances() {
return this.#context.getInstanceNames();
}
getInstance(instanceName) {
try {
const instance = this.#context.get(instanceName);
return instance.asPacket();
} catch(error) {
if (error instanceof InstanceNotFoundError)
throw new APICallerError(error);
throw error;
}
}
addInstance(instanceName, structureId) {
try {
const instance = this.#context.add(instanceName, structureId);
return instance.asPacket();
} catch(error) {
if (error instanceof InstanceExistsError || error instanceof StructureNotFoundError)
throw new APICallerError(error);
throw error;
}
}
editInstance(instanceName, instanceOptions) {
try {
const instance = this.#context.get(instanceName);
instance.setOptions(instanceOptions);
} catch(error) {
if (error instanceof InstanceNotFoundError || error instanceof InstanceExistsError || error instanceof StructureNotFoundError)
throw new APICallerError(error);
throw error;
}
}
deleteInstance(instanceName) {
try {
this.#context.delete(instanceName);
} catch (error) {
if (error instanceof InstanceNotFoundError)
throw new APICallerError(error);
throw error;
}
}
getMaterials(instanceName) {
try {
const instance = this.#context.get(instanceName);
const structureMaterials = instance.getActiveMaterials();
return structureMaterials.allMaterials;
} catch(error) {
if (error instanceof InstanceNotFoundError)
throw new APICallerError(error);
throw error;
}
}
}
@@ -0,0 +1,13 @@
import { PROTO } from "../../lib/AddonAPIKit/AddonAPIKit";
export const BuilderModel = PROTO.Object({
playerId: PROTO.String,
easyPlace: PROTO.Boolean,
fastEasyPlace: PROTO.Boolean,
materialGrabber: PROTO.Boolean,
materialInstanceName: PROTO.String
});
export const BuilderIdParameterModel = PROTO.Object({
playerId: PROTO.String
});
@@ -0,0 +1,44 @@
import { PROTO } from '../../lib/AddonAPIKit/AddonAPIKit';
const LocationModel = PROTO.Object({
x: PROTO.Float64,
y: PROTO.Float64,
z: PROTO.Float64
});
export const InstanceModel = PROTO.Object({
name: PROTO.String,
structureId: PROTO.String,
isEnabled: PROTO.Boolean,
dimensionId: PROTO.Optional(PROTO.String),
location: PROTO.Optional(LocationModel),
bounds: PROTO.Optional(PROTO.Object({
min: LocationModel,
max: LocationModel
})),
currentLayer: PROTO.Int16,
maxLayer: PROTO.Int16,
verifier: PROTO.Object({
isEnabled: PROTO.Boolean,
trackPlayerDistance: PROTO.Int8,
particleLifetime: PROTO.Int32
})
});
export const InstancesModel = PROTO.Array(InstanceModel);
export const StructureMaterialsModel = PROTO.Map(PROTO.String, PROTO.Int32);
export const InstanceNameParameterModel = PROTO.Object({
instanceName: PROTO.String
});
export const AddInstanceParameterModel = PROTO.Object({
instanceName: PROTO.String,
structureId: PROTO.String
});
export const EditInstanceParameterModel = PROTO.Object({
instanceName: PROTO.String,
instance: InstanceModel
});