CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugin_loader.js
Views: 789
1
const assert = require('assert')
2
3
module.exports = inject
4
5
function inject (bot, options) {
6
let loaded = false
7
const pluginList = []
8
bot.once('inject_allowed', onInjectAllowed)
9
10
function onInjectAllowed () {
11
loaded = true
12
injectPlugins()
13
}
14
15
function loadPlugin (plugin) {
16
assert.ok(typeof plugin === 'function', 'plugin needs to be a function')
17
18
if (hasPlugin(plugin)) {
19
return
20
}
21
22
pluginList.push(plugin)
23
24
if (loaded) {
25
plugin(bot, options)
26
}
27
}
28
29
function loadPlugins (plugins) {
30
// While type checking if already done in the other function, it's useful to do
31
// it here to prevent situations where only half the plugin list is loaded.
32
assert.ok(plugins.filter(plugin => typeof plugin === 'function').length === plugins.length, 'plugins need to be an array of functions')
33
34
plugins.forEach((plugin) => {
35
loadPlugin(plugin)
36
})
37
}
38
39
function injectPlugins () {
40
pluginList.forEach((plugin) => {
41
plugin(bot, options)
42
})
43
}
44
45
function hasPlugin (plugin) {
46
return pluginList.indexOf(plugin) >= 0
47
}
48
49
bot.loadPlugin = loadPlugin
50
bot.loadPlugins = loadPlugins
51
bot.hasPlugin = hasPlugin
52
}
53
54