flying-squid/src/index.js
Romain Beaumont 2e749ff49f
move to normal position, fix #40 + multi version for position for 1.8 and 1.12
normal position is now possible because I understood it's necessary
to stored the last sent position in order to compensate for the loss
of precision next time a relative position packet is sent

I introduced a features.json which makes it possible to describe
high level differences between version and use that to handle multi version code
2018-05-21 00:04:19 +02:00

53 lines
1.8 KiB
JavaScript

const mc = require('minecraft-protocol')
const EventEmitter = require('events').EventEmitter
const path = require('path')
const requireIndex = require('./lib/requireindex')
const supportedVersions = require('./lib/version').supportedVersions
require('emit-then').register()
if (process.env.NODE_ENV === 'dev') {
require('longjohn')
}
const supportFeature = require('./lib/supportFeature')
module.exports = {
createMCServer: createMCServer,
Behavior: require('./lib/behavior'),
Command: require('./lib/command'),
generations: require('./lib/generations'),
experience: require('./lib/experience'),
UserError: require('./lib/user_error'),
portal_detector: require('./lib/portal_detector')
}
function createMCServer (options) {
options = options || {}
const mcServer = new MCServer()
mcServer.connect(options)
return mcServer
}
class MCServer extends EventEmitter {
constructor () {
super()
this._server = null
}
connect (options) {
const version = require('minecraft-data')(options.version).version
if (supportedVersions.indexOf(version.majorVersion) === -1) {
throw new Error(`Version ${version.minecraftVersion} is not supported.`)
}
this.supportFeature = feature => supportFeature(feature, version.majorVersion)
const plugins = requireIndex(path.join(__dirname, 'lib', 'plugins'))
this._server = mc.createServer(options)
Object.keys(plugins)
.filter(pluginName => plugins[pluginName].server !== undefined)
.forEach(pluginName => plugins[pluginName].server(this, options))
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')
}
}