Other API features

Dependency interfaces, system messages and hot reload.

Dependencies and RequireInterface

mod.require['module-name'] retrieves another module's interface through the Toolbox manager. It differs from Node.js require() of a file. Declare distributed dependencies in module.json; a missing required module can throw.

For named exports, the default interface is { globalMod, clientMod, networkMod }. A custom RequireInterface(globalMod, clientMod, networkMod, requiredBy) can expose a convenient public API. For example, library returns its network instance directly, allowing mod.require.library.player.

Instance availability depends on the calling context: a global component cannot assume a game connection exists. mod.globalMod and mod.clientMod access the module's own components when those exports exist. Do not access a component that was not defined.

Module and connection information

mod.info includes module metadata and its path. The network interface exposes serverId, serverList, serverIp, connection and client/patch information. Connection fields are meaningful while the connection is active. Use mod.game.me for character state rather than TCP connection information.

System messages

mod.parseSystemMessage(message) converts a system-message string into { id, tokens }; mod.buildSystemMessage(message, data) performs the reverse operation. buildSystemMessage accepts either { id, tokens } or a message name and separate token object. Names resolve through the client's system-message map.

// Within a handler whose event has a system-message string:
const parsed = mod.parseSystemMessage(event.message);
mod.log(parsed.id, parsed.tokens);
const rebuilt = mod.buildSystemMessage(parsed);

The wire string starts with @ and separates tokens using vertical tabs (\v). Avoid hardcoded numeric IDs when symbolic names are available. Unknown names or invalid formats can throw.

Hot reload

Optional instance methods saveState() and loadState(state) transfer temporary state across module reloads. The manager captures state, unloads the module, recreates instances and restores the data. This is not persistent storage: use settings to survive closing Toolbox.

module.exports.NetworkMod = class Counter {
    constructor(mod) {
        this.count = 0;
        mod.command.add('count-example', () => mod.command.message(String(++this.count)));
    }
    saveState() { return { count: this.count }; }
    loadState(state) { this.count = state?.count ?? 0; }
};

Sources: bin/mod.js, node_modules/tera-network-proxy/lib/connection/dispatch.js.