forked from clusterio/gridworld
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.ts
More file actions
70 lines (62 loc) · 1.79 KB
/
instance.ts
File metadata and controls
70 lines (62 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import * as lib from "@clusterio/lib";
import { BaseInstancePlugin } from "@clusterio/host";
type BoundaryConfig = {
tileX: number;
tileY: number;
tileSize: number;
surfaceName: string;
};
export class InstancePlugin extends BaseInstancePlugin {
private warnedMissingConfig = false;
private getBoundaryConfig(logMissing: boolean): BoundaryConfig | null {
const tileX = this.instance.config.get("gridworld.tile_x");
const tileY = this.instance.config.get("gridworld.tile_y");
const tileSize = this.instance.config.get("gridworld.tile_size");
const surfaceName = this.instance.config.get("gridworld.surface_name");
if (
typeof tileX !== "number"
|| typeof tileY !== "number"
|| typeof tileSize !== "number"
|| typeof surfaceName !== "string"
) {
if (logMissing && !this.warnedMissingConfig) {
this.logger.warn("Missing gridworld boundary config; skipping boundary setup.");
this.warnedMissingConfig = true;
}
return null;
}
return {
tileX,
tileY,
tileSize,
surfaceName,
};
}
private async sendBoundaryConfig(logMissing: boolean) {
const config = this.getBoundaryConfig(logMissing);
if (!config) {
return;
}
const escapedSurfaceName = lib.escapeString(config.surfaceName);
await this.sendRcon(
`/sc gridworld.set_config({tile_x = ${config.tileX}, tile_y = ${config.tileY}, tile_size = ${config.tileSize}, surface_name = "${escapedSurfaceName}"})`,
);
}
async onStart() {
await this.sendBoundaryConfig(true);
}
async onInstanceConfigFieldChanged(field: string) {
if (!field.startsWith("gridworld.")) {
return;
}
if (
field !== "gridworld.tile_x"
&& field !== "gridworld.tile_y"
&& field !== "gridworld.tile_size"
&& field !== "gridworld.surface_name"
) {
return;
}
await this.sendBoundaryConfig(false);
}
}