forked from PrismarineJS/node-minecraft-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateServer.js
More file actions
75 lines (66 loc) · 2.31 KB
/
Copy pathcreateServer.js
File metadata and controls
75 lines (66 loc) · 2.31 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
71
72
73
74
75
'use strict'
const DefaultServerImpl = require('./server')
const NodeRSA = require('node-rsa')
const plugins = [
require('./server/handshake'),
require('./server/keepalive'),
require('./server/login'),
require('./server/ping')
]
module.exports = createServer
function createServer (options = {}) {
const {
host = undefined, // undefined means listen to all available ipv4 and ipv6 adresses
// (see https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback for details)
'server-port': serverPort,
port = serverPort || 25565,
motd = 'A Minecraft server',
'max-players': maxPlayersOld = 20,
maxPlayers: maxPlayersNew = 20,
Server = DefaultServerImpl,
version,
favicon,
customPackets,
motdMsg, // This is when you want to send formated motd's from ChatMessage instances
socketType = 'tcp'
} = options
const maxPlayers = options['max-players'] !== undefined ? maxPlayersOld : maxPlayersNew
const optVersion = version === undefined || version === false ? require('./version').defaultVersion : version
const mcData = require('minecraft-data')(optVersion)
if (!mcData) throw new Error(`unsupported protocol version: ${optVersion}`)
const mcversion = mcData.version
const hideErrors = options.hideErrors || false
const server = new Server(mcversion.minecraftVersion, customPackets, hideErrors)
server.mcversion = mcversion
server.motd = motd
server.motdMsg = motdMsg
server.maxPlayers = maxPlayers
server.playerCount = 0
server.onlineModeExceptions = Object.create(null)
server.favicon = favicon
server.options = options
options.registryCodec = options.registryCodec || mcData.registryCodec || mcData.loginPacket?.dimensionCodec
// The RSA keypair can take some time to generate
// and is only needed for online-mode
// So we generate it lazily when needed
Object.defineProperty(server, 'serverKey', {
configurable: true,
get () {
this.serverKey = new NodeRSA({ b: 1024 })
return this.serverKey
},
set (value) {
delete this.serverKey
this.serverKey = value
}
})
server.on('connection', function (client) {
plugins.forEach(plugin => plugin(client, server, options))
})
if (socketType === 'ipc') {
server.listen(host)
} else {
server.listen(port, host)
}
return server
}