Fix requireindex and mineflayer.js

This commit is contained in:
mhsjlw 2018-02-06 22:24:20 -05:00
parent 72e42982b0
commit 241cc0e014
7 changed files with 170 additions and 106 deletions

View file

@ -44,7 +44,6 @@
"range": "^0.0.3", "range": "^0.0.3",
"request": "^2.83.0", "request": "^2.83.0",
"request-promise": "^4.1.0", "request-promise": "^4.1.0",
"requireindex": "~1.1.0",
"spiralloop": "^1.0.2", "spiralloop": "^1.0.2",
"vec3": "^0.1.3" "vec3": "^0.1.3"
}, },

View file

@ -1,7 +1,7 @@
const mc = require('minecraft-protocol'); const mc = require('minecraft-protocol');
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
const path = require('path'); const path = require('path');
const requireIndex = require('requireindex'); const requireIndex = require('./lib/requireindex');
require('emit-then').register(); require('emit-then').register();
if (process.env.NODE_ENV === 'dev'){ if (process.env.NODE_ENV === 'dev'){
require('longjohn'); require('longjohn');
@ -33,7 +33,7 @@ class MCServer extends EventEmitter {
connect(options) { connect(options) {
const plugins = requireIndex(path.join(__dirname, 'lib', 'plugins')); const plugins = requireIndex(path.join(__dirname, 'lib', 'plugins'));
this._server = mc.createServer(options); this._server = mc.createServer(options);
Object.keys(plugins) Object.keys(plugins)
.filter(pluginName => plugins[pluginName].server!=undefined) .filter(pluginName => plugins[pluginName].server!=undefined)
.forEach(pluginName => plugins[pluginName].server(this, options)); .forEach(pluginName => plugins[pluginName].server(this, options));
@ -42,4 +42,4 @@ class MCServer extends EventEmitter {
this._server.on('listening', () => this.emit('listening',this._server.socketServer.address().port)); this._server.on('listening', () => this.emit('listening',this._server.socketServer.address().port));
this.emit('asap'); this.emit('asap');
} }
} }

View file

@ -1,7 +1,7 @@
const Vec3 = require("vec3").Vec3; const Vec3 = require("vec3").Vec3;
const path = require('path'); const path = require('path');
const requireIndex = require('requireindex'); const requireIndex = require('../requireindex');
const plugins = requireIndex(path.join(__dirname,'..', 'plugins')); const plugins = requireIndex(path.join(__dirname,'..', 'plugins'));
const Command = require('flying-squid').Command; const Command = require('flying-squid').Command;

View file

@ -4,7 +4,7 @@ const mobsById=require("minecraft-data")(version).mobs;
const objectsById=require("minecraft-data")(version).objects; const objectsById=require("minecraft-data")(version).objects;
const Entity = require("prismarine-entity"); const Entity = require("prismarine-entity");
const path = require('path'); const path = require('path');
const requireIndex = require('requireindex'); const requireIndex = require('../requireindex');
const plugins = requireIndex(path.join(__dirname,'..', 'plugins')); const plugins = requireIndex(path.join(__dirname,'..', 'plugins'));
const Item = require("prismarine-item")(version); const Item = require("prismarine-item")(version);
const UserError = require('flying-squid').UserError; const UserError = require('flying-squid').UserError;
@ -323,4 +323,4 @@ module.exports.entity=function(entity,serv) {
entity._writeOthersNearby('attach_entity',p); entity._writeOthersNearby('attach_entity',p);
} }
}; };

59
src/lib/requireindex.js Normal file
View file

@ -0,0 +1,59 @@
// Adapted from https://github.com/stephenhandley/requireindex (under the MIT license)
const fs = require('fs')
const path = require('path')
module.exports = function(dir, basenames) {
let requires = {}
if (arguments.length === 2) {
// If basenames argument is passed, explicitly include those files
basenames.forEach(function (basename) {
let filepath = path.resolve(path.join(dir, basename))
requires[basename] = require(filepath)
})
} else if (arguments.length === 1) {
// If basenames arguments isn't passed, require all JavaScript
// Files (except for those prefixed with _) and all directories
let files = fs.readdirSync(dir)
// Sort files in lowercase alpha for Linux
files.sort((a, b) => {
a = a.toLowerCase()
b = b.toLowerCase()
if (a < b) {
return -1
} else if (b < a) {
return 1
} else {
return 0
}
})
files.forEach(filename => {
// Ignore `index.js` and files prefixed with underscore and
if ((filename === 'index.js') || (filename[0] === '_') || (filename[0] === '.')) {
return
}
let filepath = path.resolve(path.join(dir, filename))
let ext = path.extname(filename)
let stats = fs.statSync(filepath)
// Don't require non-javascript files (.txt, .md, etc.)
if (stats.isFile() && !(['.js', '.node', '.json'].includes(ext))) {
return
}
let basename = path.basename(filename, ext)
requires[basename] = require(filepath)
})
} else {
throw new Error('Must pass directory as first argument')
}
return requires
}

View file

@ -60,7 +60,6 @@ describe('server with mineflayer connection', () => {
} }
beforeEach(async () => { beforeEach(async () => {
jest.setTimeout(60 * 1000)
const options = settings const options = settings
options['online-mode'] = false options['online-mode'] = false
options['port'] = 25566 options['port'] = 25566
@ -110,7 +109,6 @@ describe('server with mineflayer connection', () => {
} }
test('can dig', async () => { test('can dig', async () => {
jest.setTimeout(60 * 1000)
await Promise.all([waitSpawnZone(bot, 2), waitSpawnZone(bot2, 2), onGround(bot), onGround(bot2)]) await Promise.all([waitSpawnZone(bot, 2), waitSpawnZone(bot2, 2), onGround(bot), onGround(bot2)])
const pos = bot.entity.position.offset(0, -1, 0).floored() const pos = bot.entity.position.offset(0, -1, 0).floored()
@ -122,7 +120,6 @@ describe('server with mineflayer connection', () => {
}) })
test('can place a block', async () => { test('can place a block', async () => {
jest.setTimeout(60 * 1000)
await Promise.all([waitSpawnZone(bot, 2), waitSpawnZone(bot2, 2), onGround(bot), onGround(bot2)]) await Promise.all([waitSpawnZone(bot, 2), waitSpawnZone(bot2, 2), onGround(bot), onGround(bot2)])
const pos = bot.entity.position.offset(0, -2, 0).floored() const pos = bot.entity.position.offset(0, -2, 0).floored()
@ -147,94 +144,95 @@ describe('server with mineflayer connection', () => {
}) })
}) })
// describe('commands', () => { describe('commands', () => {
// test('has an help command', async () => { jest.setTimeout(10 * 1000)
// await waitLoginMessage(bot) test('has an help command', async () => {
// bot.chat('/help') await waitLoginMessage(bot)
// await once(bot, 'message') bot.chat('/help')
// }) await once(bot, 'message')
// test('can use /particle', async () => { })
// bot.chat('/particle 5 10 100 100 100') test('can use /particle', async () => {
// await once(bot._client, 'world_particles') bot.chat('/particle 5 10 100 100 100')
// }) await once(bot._client, 'world_particles')
// test('can use /playsound', async () => { })
// bot.chat('/playsound ambient.weather.rain') test('can use /playsound', async () => {
// await once(bot, 'soundEffectHeard') bot.chat('/playsound ambient.weather.rain')
// }) await once(bot, 'soundEffectHeard')
// })
// function waitDragon () {
// return new Promise((done) => { function waitDragon () {
// const listener = (entity) => { return new Promise((done) => {
// if (entity.name == 'EnderDragon') { const listener = (entity) => {
// bot.removeListener('entitySpawn', listener) if (entity.name == 'EnderDragon') {
// done() bot.removeListener('entitySpawn', listener)
// } done()
// } }
// bot.on('entitySpawn', listener) }
// }) bot.on('entitySpawn', listener)
// } })
// }
// test('can use /summon', async () => {
// bot.chat('/summon EnderDragon') test('can use /summon', async () => {
// await waitDragon() bot.chat('/summon EnderDragon')
// }) await waitDragon()
// test('can use /kill', async () => { })
// bot.chat('/summon EnderDragon') test('can use /kill', async () => {
// await waitDragon() bot.chat('/summon EnderDragon')
// bot.chat('/kill @e[type=EnderDragon]') await waitDragon()
// const entity = await once(bot, 'entityDead') bot.chat('/kill @e[type=EnderDragon]')
// expect(entity.name).toEqual('EnderDragon') const entity = await once(bot, 'entityDead')
// }) expect(entity.name).toEqual('EnderDragon')
// describe('can use /tp', () => { })
// test('can tp myself', async () => { describe('can use /tp', () => {
// bot.chat('/tp 2 3 4') test('can tp myself', async () => {
// await once(bot, 'forcedMove') bot.chat('/tp 2 3 4')
// assertPosEqual(bot.entity.position, new Vec3(2, 3, 4)) await once(bot, 'forcedMove')
// }) assertPosEqual(bot.entity.position, new Vec3(2, 3, 4))
// test('can tp somebody else', async () => { })
// bot.chat('/tp bot2 2 3 4') test('can tp somebody else', async () => {
// await once(bot2, 'forcedMove') bot.chat('/tp bot2 2 3 4')
// assertPosEqual(bot2.entity.position, new Vec3(2, 3, 4)) await once(bot2, 'forcedMove')
// }) assertPosEqual(bot2.entity.position, new Vec3(2, 3, 4))
// test('can tp to somebody else', async () => { })
// await onGround(bot) test('can tp to somebody else', async () => {
// bot.chat('/tp bot2 bot') await onGround(bot)
// await once(bot2, 'forcedMove') bot.chat('/tp bot2 bot')
// assertPosEqual(bot2.entity.position, bot.entity.position) await once(bot2, 'forcedMove')
// }) assertPosEqual(bot2.entity.position, bot.entity.position)
// test('can tp with relative positions', async () => { })
// await onGround(bot) test('can tp with relative positions', async () => {
// const initialPosition = bot.entity.position.clone() await onGround(bot)
// bot.chat('/tp ~1 ~-2 ~3') const initialPosition = bot.entity.position.clone()
// await once(bot, 'forcedMove') bot.chat('/tp ~1 ~-2 ~3')
// assertPosEqual(bot.entity.position, initialPosition.offset(1, -2, 3)) await once(bot, 'forcedMove')
// }) assertPosEqual(bot.entity.position, initialPosition.offset(1, -2, 3))
// test('can tp somebody else with relative positions', async () => { })
// await Promise.all([onGround(bot), onGround(bot2)]) test('can tp somebody else with relative positions', async () => {
// const initialPosition = bot2.entity.position.clone() await Promise.all([onGround(bot), onGround(bot2)])
// bot.chat('/tp bot2 ~1 ~-2 ~3') const initialPosition = bot2.entity.position.clone()
// await once(bot2, 'forcedMove') bot.chat('/tp bot2 ~1 ~-2 ~3')
// assertPosEqual(bot2.entity.position, initialPosition.offset(1, -2, 3)) await once(bot2, 'forcedMove')
// }) assertPosEqual(bot2.entity.position, initialPosition.offset(1, -2, 3))
// }) })
// test('can use /deop', async () => { })
// await waitLoginMessage(bot) test('can use /deop', async () => {
// bot.chat('/deop bot') await waitLoginMessage(bot)
// await waitMessage(bot, 'bot is deopped') bot.chat('/deop bot')
// bot.chat('/op bot') await waitMessage(bot, 'bot is deopped')
// await waitMessage(bot, 'You do not have permission to use this command') bot.chat('/op bot')
// serv.getPlayer('bot').op = true await waitMessage(bot, 'You do not have permission to use this command')
// }) serv.getPlayer('bot').op = true
// test('can use /setblock', async() => { })
// await once(bot, 'chunkColumnLoad') test('can use /setblock', async () => {
// bot.chat('/setblock 1 2 3 95 0') await once(bot, 'chunkColumnLoad')
// let [, newBlock] = await once(bot, 'blockUpdate:' + new Vec3(1, 2, 3), {array: true}) bot.chat('/setblock 1 2 3 95 0')
// expect(newBlock.type).toEqual(95) let [, newBlock] = await once(bot, 'blockUpdate:' + new Vec3(1, 2, 3), {array: true})
// }) expect(newBlock.type).toEqual(95)
// test('can use /xp', async() => { })
// bot.chat('/xp 100') test('can use /xp', async () => {
// await once(bot, 'experience') bot.chat('/xp 100')
// expect(bot.experience.points).toEqual(100) await once(bot, 'experience')
// }) expect(bot.experience.points).toEqual(100)
// }) })
})
}) })

View file

@ -1,27 +1,35 @@
const net = require('net') const net = require('net')
const squid = require('flying-squid') const squid = require('flying-squid')
const settings = require('../config/default-settings')
describe('server', () => { describe('server', () => {
let server let serv
beforeAll(done => { beforeAll(done => {
server = squid.createMCServer({ logging: false }) const options = settings
options['online-mode'] = false
options['port'] = 25566
options['view-distance'] = 2
options['worldFolder'] = undefined
options['logging'] = false
serv = squid.createMCServer(options)
server.on('listening', () => { serv.on('listening', () => {
done() done()
}) })
}) })
afterAll(done => { afterAll(done => {
server._server.close() serv._server.close()
server._server.on('close', () => { serv._server.on('close', () => {
done() done()
}) })
}) })
test('is running', done => { test('is running', done => {
const client = net.Socket() const client = net.Socket()
client.connect(server._server.socketServer.address().port, '127.0.0.1', done) client.connect(serv._server.socketServer.address().port, '127.0.0.1', done)
client.on('error', done) client.on('error', done)
}) })
}) })