Plugin manager
Every connection owns a PluginManager. It starts the built-in core and recorder plugins, then accepts custom plugins for that connection.
const { ProxyPlugin } = require("JNbot");
class ChatLogger extends ProxyPlugin {
constructor(connection) {
super(connection, {
name: "chat-logger",
description: "Logs outgoing chat messages",
});
}
start() {
this.pipeline.on("chat", this.onChat);
}
stop() {
this.pipeline.off("chat", this.onChat);
}
onChat = (packet) => {
if (packet.direction === "c2s" && !packet.unparsed) {
console.log(packet.data.message);
}
};
}
proxy.on("connection", (connection) => {
connection.pluginManager.enablePlugin(ChatLogger);
});
Plugin is an alias for ProxyPlugin. Plugins receive the connection, metadata, the packet pipeline, and tell(message) for chat feedback.
Manage plugins
const manager = connection.pluginManager;
manager.hasPlugin("recorder");
manager.getPlugin("recorder");
manager.enablePlugin(ChatLogger);
manager.disablePlugin("chat-logger");
enablePlugin() starts the plugin immediately. It throws when a plugin with the same name is already active. disablePlugin() calls stop(), removes its commands, and returns whether it found a plugin.
A connection closes all of its plugins and clears its packet pipeline. Keep each plugin's handlers removable in stop().