Skip to content

Commit f9d84fc

Browse files
committed
Make config easier to work with
1 parent 713cd36 commit f9d84fc

8 files changed

+56
-17
lines changed

command.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Config } from "./config.ts";
22
import { JavaServer, JavaServerEvents } from "./java_server.ts";
33
import get from "https://deno.land/x/[email protected]/src/object/get.ts";
44
import { useEvents } from "./events.ts";
5+
import { defaultsDeep } from "./defaults_deep.ts";
56

67
interface CommandEvent {
78
player: string;
@@ -49,8 +50,7 @@ export function useCommand(javaServer: JavaServer) {
4950

5051
useEvents(javaServer);
5152

52-
// @ts-ignore
53-
javaServer.config = { command: DEFAULT_CONFIG, ...javaServer.config };
53+
defaultsDeep(javaServer.config, { command: DEFAULT_CONFIG });
5454
javaServer.config.command.initialized = true;
5555

5656
const regex = new RegExp(

defaults_deep.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export function defaultsDeep(
2+
target: Record<string, any>,
3+
...sources: Record<string, any>[]
4+
) {
5+
sources.forEach((source) => {
6+
for (const key in source) {
7+
if (target.hasOwnProperty(key)) {
8+
if (!Array.isArray(source[key]) && typeof source[key] === "object") {
9+
defaultsDeep(target[key], source[key]);
10+
}
11+
} else {
12+
target[key] = source[key];
13+
}
14+
}
15+
});
16+
17+
return target;
18+
}
19+
20+
export type DeepPartial<T> = {
21+
[P in keyof T]?: DeepPartial<T[P]>;
22+
};

events.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Config } from "./config.ts";
22
import { JavaServer, JavaServerEvents } from "./java_server.ts";
33
import get from "https://deno.land/x/[email protected]/src/object/get.ts";
4+
import { defaultsDeep } from "./defaults_deep.ts";
45

56
export interface EventsConfig {
67
initialized: boolean;
@@ -139,8 +140,7 @@ export function useEvents(javaServer: JavaServer) {
139140
return;
140141
}
141142

142-
// @ts-ignore
143-
javaServer.config = { events: DEFAULT_CONFIG, ...javaServer.config };
143+
defaultsDeep(javaServer.config, { events: DEFAULT_CONFIG });
144144
javaServer.config.events.initialized = true;
145145

146146
javaServer.on("console", (consoleLine) => {

java_server.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EventEmitter } from "https://deno.land/[email protected]/node/events.ts";
22
import { iter } from "https://deno.land/[email protected]/io/util.ts";
33
import { Config } from "./config.ts";
44
import { Buffer } from "https://deno.land/[email protected]/node/buffer.ts";
5+
import { defaultsDeep, DeepPartial } from "./defaults_deep.ts";
56

67
export interface JavaServerConfig {
78
jar: string;
@@ -46,9 +47,11 @@ export class JavaServer extends EventEmitter {
4647
public config: Config;
4748
private process?: Deno.Process;
4849

49-
constructor(config: Partial<Config> = {}) {
50+
constructor(config: DeepPartial<Config> = {}) {
5051
super();
51-
this.config = { javaServer: DEFAULT_CONFIG, ...config } as Config;
52+
this.config = defaultsDeep(config, {
53+
javaServer: DEFAULT_CONFIG,
54+
}) as Config;
5255

5356
this.stdinIter();
5457
}

mod.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ import { useUtils } from "./utils.ts";
4343
// Example of using ScriptServer, which is both JavaServer & RconConnection
4444
() => {
4545
try {
46-
const scriptServer = new ScriptServer();
46+
const scriptServer = new ScriptServer({
47+
command: {
48+
prefix: "!",
49+
},
50+
});
4751

4852
useEvents(scriptServer.javaServer);
4953
useCommand(scriptServer.javaServer);
@@ -55,7 +59,9 @@ import { useUtils } from "./utils.ts";
5559
});
5660
});
5761

58-
scriptServer.start();
62+
console.log(scriptServer.config);
63+
64+
// scriptServer.start();
5965
} catch (error) {
6066
console.error(error);
6167
}

rcon_connection.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EventEmitter } from "https://deno.land/[email protected]/node/events.ts";
22
import { Buffer } from "https://deno.land/[email protected]/node/buffer.ts";
33
import { iter } from "https://deno.land/[email protected]/io/util.ts";
44
import { Config } from "./config.ts";
5+
import { defaultsDeep, DeepPartial } from "./defaults_deep.ts";
56

67
export interface RconConnectionConfig {
78
host: string;
@@ -32,9 +33,11 @@ export class RconConnection extends EventEmitter {
3233
private connection?: Deno.Conn;
3334
private execId: number = RequestPacketId.Exec;
3435

35-
constructor(config: Partial<Config> = {}) {
36+
constructor(config: DeepPartial<Config> = {}) {
3637
super();
37-
this.config = { rconConnection: DEFAULT_CONFIG, ...config } as Config;
38+
this.config = defaultsDeep(config, {
39+
rconConnection: DEFAULT_CONFIG,
40+
}) as Config;
3841

3942
this.tick();
4043
}

script_server.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EventEmitter } from "https://deno.land/[email protected]/node/events.ts";
22
import { JavaServer } from "./java_server.ts";
33
import { RconConnection } from "./rcon_connection.ts";
44
import get from "https://deno.land/x/[email protected]/src/object/get.ts";
5+
import { defaultsDeep, DeepPartial } from "./defaults_deep.ts";
56
import { Config } from "./config.ts";
67

78
export interface ScriptServerConfig {
@@ -31,9 +32,12 @@ export class ScriptServer extends EventEmitter {
3132
public rconConnection: RconConnection;
3233
public config: Config;
3334

34-
constructor(config: Partial<Config> = {}) {
35+
constructor(config: DeepPartial<Config> = {}) {
3536
super();
36-
this.config = { scriptServer: DEFAULT_CONFIG, ...config } as Config;
37+
console.log(config);
38+
this.config = defaultsDeep(config, {
39+
scriptServer: DEFAULT_CONFIG,
40+
}) as Config;
3741
this.javaServer = new JavaServer(config);
3842
this.rconConnection = new RconConnection(config);
3943
}

utils.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Config } from "./config.ts";
22
import { RconConnection } from "./rcon_connection.ts";
33
import get from "https://deno.land/x/[email protected]/src/object/get.ts";
4+
import { defaultsDeep } from "./defaults_deep.ts";
45

56
export type Dimension =
67
| "minecraft:overworld"
@@ -52,8 +53,7 @@ export function useUtils(rconConnection: RconConnection) {
5253
return;
5354
}
5455

55-
rconConnection.config = {
56-
// @ts-ignore
56+
defaultsDeep(rconConnection.config, {
5757
utils: {
5858
initialized: false,
5959
flavorSpecific: {
@@ -99,6 +99,7 @@ export function useUtils(rconConnection: RconConnection) {
9999
x: parseFloat(location![1]),
100100
y: parseFloat(location![2]),
101101
z: parseFloat(location![3]),
102+
dimension: await this.getDimension(player),
102103
};
103104
},
104105
async teleport(target, location) {
@@ -114,9 +115,9 @@ export function useUtils(rconConnection: RconConnection) {
114115
},
115116
},
116117
},
117-
},
118-
...rconConnection.config,
119-
};
118+
} as UtilsConfig,
119+
});
120+
120121
rconConnection.config.utils.initialized = true;
121122

122123
rconConnection.utils = get(

0 commit comments

Comments
 (0)