Skip to content

Commit

Permalink
Refactor: allow usage in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Sep 3, 2023
1 parent 2113acf commit 07bc819
Show file tree
Hide file tree
Showing 11 changed files with 473 additions and 424 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock=false
public-hoist-pattern=*
33 changes: 18 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
"email": "[email protected]"
}
],
"bin": {
"flying-squid": "app.js"
},
"bin": "app.js",
"scripts": {
"prepublishOnly": "cp docs/README.md README.md",
"lint": "standard test/*.test.js src/**/*.js src/**/**/*.js src/*.js examples/*.js *.js",
Expand All @@ -21,7 +19,11 @@
"test": "npm run mocha_test",
"pretest": "npm run lint"
},
"keywords": [],
"keywords": [
"browser",
"minecraft server",
"java minecraft server"
],
"license": "MIT",
"engines": {
"node": ">=8"
Expand All @@ -31,21 +33,22 @@
"diamond-square": "^1.2.0",
"emit-then": "^2.0.0",
"event-promise": "^0.0.1",
"exit-hook": "^2.2.1",
"flatmap": "^0.0.3",
"long": "^5.1.0",
"minecraft-data": "^3.0.0",
"minecraft-protocol": "^1.15.0",
"mkdirp": "^2.1.3",
"minecraft-data": "^3.42.1",
"minecraft-protocol": "^1.44.0",
"moment": "^2.10.6",
"needle": "^2.5.0",
"node-gzip": "^1.1.2",
"prismarine-chunk": "^1.20.1",
"prismarine-entity": "^2.0.0",
"prismarine-item": "^1.5.0",
"prismarine-nbt": "^2.0.0",
"prismarine-provider-anvil": "^2.3.0",
"prismarine-windows": "^2.0.0",
"prismarine-world": "^3.0.0",
"node-rsa": "^1.1.1",
"prismarine-chunk": "^1.34.0",
"prismarine-entity": "^2.2.0",
"prismarine-item": "^1.14.0",
"prismarine-nbt": "^2.2.1",
"prismarine-provider-anvil": "^2.7.0",
"prismarine-windows": "^2.8.0",
"prismarine-world": "^3.6.2",
"random-seed": "^0.3.0",
"range": "^0.0.3",
"readline": "^1.3.0",
Expand All @@ -63,11 +66,11 @@
},
"devDependencies": {
"expect": "^29.1.2",
"flying-squid": "file:.",
"longjohn": "^0.2.12",
"minecraft-wrap": "^1.2.3",
"mineflayer": "^4.0.0",
"mocha": "^10.0.0",
"flying-squid": "file:.",
"standard": "^17.0.0"
}
}
25 changes: 15 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
if (typeof process !== 'undefined' && parseInt(process.versions.node.split('.')[0]) < 18) {
//@ts-check
//@ts-ignore
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 mc = require('minecraft-protocol')
const { createServer } = require('minecraft-protocol')

const EventEmitter = require('events').EventEmitter
const path = require('path')
const requireIndex = require('./lib/requireindex')
const supportedVersions = require('./lib/version').supportedVersions
const Command = require('./lib/command')
const plugins = require('./lib/plugins')
require('emit-then').register()
if (process.env.NODE_ENV === 'dev') {
require('longjohn')
Expand Down Expand Up @@ -38,25 +40,28 @@ function createMCServer (options) {

class MCServer extends EventEmitter {
constructor () {
plugins.initPlugins()
super()
this._server = null
}

connect (options) {
this.options = options
const version = require('minecraft-data')(options.version).version
//@ts-ignore
if (!supportedVersions.some(v => v.includes(version.majorVersion))) {
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.commands = new Command({})
this._server = mc.createServer(options)
Object.keys(plugins)
.filter(pluginName => plugins[pluginName].server !== undefined)
.forEach(pluginName => plugins[pluginName].server(this, options))
this._server = createServer(options)

for (const plugin of plugins.builtinPlugins) plugin.server?.(this, options)

// @ts-ignore
if (options.logging === true) this.createLog()
this._server.on('error', error => this.emit('error', error))
//@ts-ignore
this._server.on('listening', () => this.emit('listening', this._server.socketServer.address().port))
this.emit('asap')
}
Expand Down
Loading

0 comments on commit 07bc819

Please sign in to comment.