-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.ts
122 lines (113 loc) · 4.67 KB
/
instance.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { startInstance, instanceExists, stopInstance, killInstance, restartInstance, getInstanceState, getInstancePlayerCount, getInstanceMaxPlayers, getInstancePlayerList, sendRconCommand, getInstanceGame, isRconAvailable, InstanceState, Player, waitTillRconAvailable, getInstanceName, Game, getInstanceGameVersion, getInstanceDescription, getInstancePort, getInstancePath, setInstanceName, setInstanceDescription, setInstancePort, setInstanceAutoStart, trySendRconCommand, sendCommand } from 'https://raw.githubusercontent.com/Lodestone-Team/lodestone/dev/core/src/deno_ops/instance_control/instance_control.ts';
import { getCurrentInstanceUUID } from "https://raw.githubusercontent.com/Lodestone-Team/lodestone/dev/core/src/deno_ops/prelude/prelude.ts";
export class Instance {
uuid!: string;
public constructor(uuid: string) {
if (instanceExists(uuid)) {
this.uuid = uuid;
} else {
throw new Error("Instance does not exist");
}
}
public getUUID(): string {
return this.uuid;
}
public static async current(): Promise<Instance> {
return new Instance(getCurrentInstanceUUID()!)
}
public async start(block: boolean): Promise<void> {
await startInstance(block, this.uuid);
}
public async stop(block: boolean): Promise<void> {
await stopInstance(block, this.uuid);
}
public async restart(block: boolean): Promise<void> {
await restartInstance(block, this.uuid);
}
public async kill(): Promise<void> {
await killInstance(this.uuid);
}
public async state(): Promise<InstanceState> {
return await getInstanceState(this.uuid);
}
public async isRunning(): Promise<boolean> {
return (await this.state()) === "Running";
}
public async isStopped(): Promise<boolean> {
return (await this.state()) === "Stopped";
}
public async isStarting(): Promise<boolean> {
return (await this.state()) === "Starting";
}
public async isStopping(): Promise<boolean> {
return (await this.state()) === "Stopping";
}
public async playerCount(): Promise<number> {
return await getInstancePlayerCount(this.uuid);
}
public async maxPlayerCount(): Promise<number> {
return await getInstanceMaxPlayers(this.uuid);
}
public async playerList(): Promise<Player[]> {
return await getInstancePlayerList(this.uuid);
}
public async name(): Promise<string> {
return await getInstanceName(this.uuid);
}
public async game(): Promise<Game> {
return await getInstanceGame(this.uuid);
}
public async gameVersion(): Promise<string> {
return await getInstanceGameVersion(this.uuid);
}
public async description(): Promise<string> {
return await getInstanceDescription(this.uuid);
}
public async port(): Promise<number> {
return await getInstancePort(this.uuid);
}
public async path(): Promise<string> {
return await getInstancePath(this.uuid);
}
public async sendCommand(command: string): Promise<void> {
await sendCommand(command, this.uuid);
}
public async setName(name: string): Promise<void> {
await setInstanceName(name, this.uuid);
}
public async setDescription(description: string): Promise<void> {
await setInstanceDescription(description, this.uuid);
}
public async setInstancePort(port: number): Promise<void> {
await setInstancePort(port, this.uuid);
}
public async setAutoStart(autoStart: boolean): Promise<void> {
await setInstanceAutoStart(autoStart, this.uuid);
}
}
export class MinecraftJavaInstance extends Instance {
private constructor(uuid: string) {
super(uuid);
}
public static override async current(): Promise<MinecraftJavaInstance> {
return await this.new(getCurrentInstanceUUID()!);
}
public static async new(uuid: string): Promise<MinecraftJavaInstance> {
if (await (await getInstanceGame(uuid)).type !== "MinecraftJava") {
throw new Error("Current instance is not a MinecraftJava instance");
}
return new MinecraftJavaInstance(getCurrentInstanceUUID()!);
}
public async waitTillRconAvailable(): Promise<void> {
await waitTillRconAvailable(this.uuid);
}
public async isRconAvailable(): Promise<boolean> {
return await isRconAvailable(this.uuid);
}
public async trySendRconCommand(command: string): Promise<string | null> {
return await trySendRconCommand(command, this.uuid)
}
public async sendRconCommand(command: string): Promise<string> {
return await sendRconCommand(command, this.uuid);
}
}