npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

voxel-plugins

v0.5.0

Published

dynamically load plugins for voxel-engine

Downloads

16

Readme

voxel-plugins

An API for loading and enabling plugins in voxel.js with support for soft dependencies.

For a list of available plugins, see the wiki.

Build Status

Loading plugins

First construct the plugin system:

var createPlugins = require('voxel-plugins');
var plugins = createPlugins(game, {require:require});

You can now add as many plugins as you want, as follows:

plugins.add('foo', {});

giving the plugin name and an options object. The name should be require()-able module, and the options are plugin-specific. Adding plugins will perform the require, examine the optional pluginInfo for dependency ordering, build up the loading graph, and save the options, but not yet instantiate the plugin objects.

Once plugins are added, they can all be loaded (in the proper order, uses tsort with the optional dependency info) by calling loadAll():

plugins.loadAll();

At any time, plugins can be disabled and re-enabled (they start out enabled at load), as long as they implement a simple API (see next section):

plugins.enable('foo');
plugins.disable('foo');
plugins.toggle('foo')
var flag = plugins.isEnabled('foo');

and the plugins can be queried like so:

console.log(plugins.list());
var pluginInstance = plugins.get('foo');

Plugin development

Construction

To write a plugin supported by this API all you need to do is make an NPM module, and have it accept (game, opts) then return a new object:

module.exports = function(game, opts) {
  return new Foo(game, opts)
}

function Foo(game, opts) {
    // initialization and setup here
    // game is the voxel-engine instance, opts is an optional {} for configuration
}

That's it. Many voxel.js addon modules already support this convention. As long as they do, voxel-plugins will be able to load them.

Enabling and disabling

voxel-plugins supports dynamically enabling/disabling plugins, if the individual plugin supports it. To support it, just implement enable() and disable() methods, and call enable() in your constructor:

function Foo(game, opts) {
    // initialization and setup here

    // must call enable() in your constructor
    this.enable();
}

Foo.prototype.enable = function() {
    // enable your plugin, making changes to the gameplay as needed
}

Foo.prototype.disable = function() {
    // disable your plugin, returning the game to a state as if it was never enabled
}

In enable() you register event handlers, in disable() remove them, and so on. Implementing enable/disable support allows your plugin to be enabled and disabled using voxel-plugins-ui, or otherwise.

Soft dependencies & load order

Often it is useful to call into another plugin instance during initialization, but in order for this to work the other plugin must be loaded before yours. To do this, set the pluginInfo property on the exported object to contain the loadAfter property with a list of plugins to load first, for example (from voxel-workbench):

module.exports.pluginInfo = {
    loadAfter: ['voxel-registry', 'craftingrecipes']
}

Since the above plugins will be loaded first, you can now safely execute game.plugins.get('voxel-registry') (etc.) in your constructor or enable() method.

voxel-plugins performs a topological sort to determine the correct load order. For a more complex dependency tree, check out voxel-reach, voxel-mine, voxel-use, and voxel-harvest.

Adding game content

voxel-plugins only handles loading and enabling/disabling plugins.

To implement the actual functionality of your plugin (modifying the game, etc.), you can use the voxel-engine APIs (instance passed in the game parameter), or other plugin APIs. voxel-plugins itself is available in game.plugins and can be used to lookup other plugins for this purpose. See also voxel-registry.

License

MIT