Skip to main content

Usage examples

Block a chat command​

const { Proxy } = require("JNbot");

const proxy = new Proxy();

proxy.on("connection", (connection) => {
connection.pipeline.on("chat", (packet) => {
if (packet.direction !== "c2s" || packet.unparsed) return;
if (packet.data.message !== "/home") return;

packet.drop();
connection.tell("The proxy blocked /home.");
});
});

Rewrite server chat​

proxy.on("connection", (connection) => {
connection.pipeline.on("system_chat", (packet) => {
if (packet.direction !== "s2c" || packet.unparsed) return;

packet.data.content = "[server] " + packet.data.content;
packet.markModified();
});
});

Send a packet without waiting for traffic​

proxy.on("connection", (connection) => {
const sent = connection.write("s2c", "system_chat", {
content: "Connected through JNbot Proxy",
overlay: false,
});

if (!sent) console.log("Client is not ready for a PLAY packet yet.");
});

Record one player​

proxy.on("connection", (connection) => {
if (connection.username !== "Player") return;

const recorder = connection.pluginManager.getPlugin("recorder");
recorder?.startRecording({ fileName: "player-route" });
});

Add a custom plugin​

const { ProxyPlugin } = require("JNbot");

class JoinNotice extends ProxyPlugin {
constructor(connection) {
super(connection, {
name: "join-notice",
description: "Shows a message after the first server packet",
});
}

start() {
this.pipeline.once("*", () => {
this.tell("The upstream connection is ready.");
});
}
}

proxy.on("connection", (connection) => {
connection.pluginManager.enablePlugin(JoinNotice);
});