-
Notifications
You must be signed in to change notification settings - Fork 0
/
minecraft.js
165 lines (136 loc) · 4 KB
/
minecraft.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const express = require('express');
const { join } = require('path');
const morgan = require('morgan');
const fs = require('fs');
const spawn = require('child_process').spawn;
let minecraftServerProcess;
const SERVER_PROPERTIES_LOCATION = './server.properties';
const MINECRAFT_STATUS = {
RUNNING: 'RUNNING',
NOT_RUNNING: 'NOT_RUNNING'
};
let serverStatus = MINECRAFT_STATUS.NOT_RUNNING;
const app = express();
// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(
join(__dirname, 'express-minecraft.log'),
{ flags: 'a' }
);
// setup the logger
app.use(morgan('combined', { stream: accessLogStream }));
app.use(express.json());
const port = 3000;
app.get('/', (req, res) => res.send('MinecraftExpressApp is up and running!'));
app.get('/status', (req, res) => {
// talk to Minecraft to get status
const status = getMinecraftStatus();
res.send({ status: status });
});
app.post('/start', (req, res) => {
if (serverStatus == MINECRAFT_STATUS.NOT_RUNNING) {
startMinecraft();
}
res.send({ status: MINECRAFT_STATUS.RUNNING });
});
app.post('/restart', (req, res) => {
restartMinecraft();
res.send({ status: MINECRAFT_STATUS.RUNNING });
});
app.delete('/shutdown', (req, res) => {
if (serverStatus == MINECRAFT_STATUS.RUNNING) {
shutdownMinecraft();
}
res.send({ status: MINECRAFT_STATUS.NOT_RUNNING });
});
app.post('/command', (req, res) => {
const command = req.body;
minecraftServerProcess.stdin.write(command.command + '\n');
let buffer = [];
let collector = data => {
data = data.toString();
buffer.push(data.split(']: ')[1]);
};
minecraftServerProcess.stdout.on('data', collector);
setTimeout(function () {
minecraftServerProcess.stdout.removeListener('data', collector);
res.send(buffer.join(''));
}, 250);
});
app.get('/properties', async (req, res) => {
try {
const result = await getProperties(SERVER_PROPERTIES_LOCATION);
res.send(result);
} catch (e) {
res.status(500).send();
}
});
app.put('/properties', async (req, res) => {
try {
const properties = req.body;
await writeProperties(SERVER_PROPERTIES_LOCATION, properties);
res.status(204).send();
} catch (e) {
res.status(500).send();
}
});
app.listen(port, () => console.log('Server started'));
function startMinecraft() {
const path = join(__dirname, 'minecraft', 'minecraft_server-run.jar');
minecraftServerProcess = spawn(
'java',
['-Xmx1024M', '-Xms1024M', '-jar', path, 'nogui'],
{
cwd: join(__dirname, 'minecraft')
}
);
// Listen for events coming from the minecraft server process - in this case,
// just log out messages coming from the server
function log(data) {
process.stdout.write(data.toString());
}
minecraftServerProcess.stdout.on('data', log);
minecraftServerProcess.stderr.on('data', log);
serverStatus = MINECRAFT_STATUS.RUNNING;
}
function shutdownMinecraft() {
minecraftServerProcess.kill();
serverStatus = MINECRAFT_STATUS.NOT_RUNNING;
}
function restartMinecraft() {
if (serverStatus == MINECRAFT_STATUS.RUNNING) {
shutdownMinecraft();
}
startMinecraft();
}
function getMinecraftStatus() {
return serverStatus;
}
function parseProperties(input) {
var output = {};
input.split('\n').forEach(line => {
if (line[0] === '#') {
return;
}
let parts = line.split('=');
// this use case handles empty lines and the end of the file
if (parts[0] === '') {
return;
}
let key = parts[0].trim();
let value = parts[1].trim();
output[key] = value;
});
return output;
}
async function getProperties(path) {
const text = (await fs.promises.readFile(path)).toString('utf-8');
return parseProperties(text);
}
async function writeProperties(path, properties) {
let string = '#Minecraft server properties\n#(last boot timestamp)\n';
for (const key in properties) {
const sub_string = `${key}=${properties[key]}\n`;
string = string.concat(sub_string);
}
return await fs.promises.writeFile(SERVER_PROPERTIES_LOCATION, string);
}