forked from PrismarineJS/flying-squid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (68 loc) · 2.9 KB
/
index.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
if (typeof process !== 'undefined' && !process.browser && process.platform !== 'browser' && parseInt(process.versions.node.split('.')[0]) < 18) {
console.error('[\x1b[31mCRITICAL\x1b[0m] Node.JS 18 or newer is required')
console.error('[\x1b[31mCRITICAL\x1b[0m] You can download the new version from https://nodejs.org/')
console.error(`[\x1b[31mCRITICAL\x1b[0m] Your current Node.JS version is: ${process.versions.node}`)
process.exit(1)
}
const { createServer } = require('minecraft-protocol')
const { EventEmitter } = require('events')
const { testedVersions, latestSupportedVersion, oldestSupportedVersion } = require('./lib/version')
const Command = require('./lib/command')
const plugins = require('./lib/plugins')
const { onceWithTimeout } = require('./lib/utils')
module.exports = {
createMCServer,
Behavior: require('./lib/behavior'),
Command: require('./lib/command'),
generations: require('./lib/generations'),
experience: require('./lib/experience'),
UserError: require('./lib/user_error'),
testedVersions
}
function createMCServer (options) {
options = options || {}
const mcServer = new MCServer()
mcServer.connect(options)
return mcServer
}
class MCServer extends EventEmitter {
constructor () {
plugins.initPlugins()
super()
this._server = null
this.pluginsReady = false
}
connect (options) {
this.commands = new Command({})
this._server = createServer(options)
const registry = require('prismarine-registry')(options.version)
if (!registry?.version) throw new Error(`Server version '${options.version}' is not supported, no data for version`)
const versionData = registry.version
if (versionData['>'](latestSupportedVersion)) {
throw new Error(`Server version '${options.version}' is not supported. Latest supported version is '${latestSupportedVersion}'.`)
} else if (versionData['<'](oldestSupportedVersion)) {
throw new Error(`Server version '${options.version}' is not supported. Oldest supported version is '${oldestSupportedVersion}'.`)
}
// internal features until merged into minecraft-data
const customFeatures = {}
this.registry = registry
this.supportFeature = feature => customFeatures[feature] ?? registry.supportFeature(feature)
const promises = []
for (const plugin of plugins.builtinPlugins) {
promises.push(plugin.server?.(this, options))
}
Promise.all(promises).then(() => {
this.emit('pluginsReady')
this.pluginsReady = true
this.debug?.('Loaded plugins')
})
this.waitForReady = (timeout) => {
if (this.pluginsReady) return Promise.resolve()
return onceWithTimeout(this, 'pluginsReady', timeout)
}
if (options.logging === true) this.createLog()
this._server.on('error', error => this.emit('error', error))
this._server.on('listening', () => this.emit('listening', this._server.socketServer.address().port))
this.emit('asap')
}
}