forked from PrismarineJS/mineflayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_loader.js
52 lines (40 loc) · 1.17 KB
/
plugin_loader.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
const assert = require('assert')
module.exports = inject
function inject (bot, options) {
let loaded = false
const pluginList = []
bot.once('inject_allowed', onInjectAllowed)
function onInjectAllowed () {
loaded = true
injectPlugins()
}
function loadPlugin (plugin) {
assert.ok(typeof plugin === 'function', 'plugin needs to be a function')
if (hasPlugin(plugin)) {
return
}
pluginList.push(plugin)
if (loaded) {
plugin(bot, options)
}
}
function loadPlugins (plugins) {
// While type checking if already done in the other function, it's useful to do
// it here to prevent situations where only half the plugin list is loaded.
assert.ok(plugins.filter(plugin => typeof plugin === 'function').length === plugins.length, 'plugins need to be an array of functions')
plugins.forEach((plugin) => {
loadPlugin(plugin)
})
}
function injectPlugins () {
pluginList.forEach((plugin) => {
plugin(bot, options)
})
}
function hasPlugin (plugin) {
return pluginList.indexOf(plugin) >= 0
}
bot.loadPlugin = loadPlugin
bot.loadPlugins = loadPlugins
bot.hasPlugin = hasPlugin
}