Apps and Commands
Put <name>.js in <world>/scripts/ and load it with /script load <name>. Use the world folder on the dedicated server or inside the single-player instance. The app name is also the root of any commands it registers.
Register commands
Save the following as scripts/visit_counter.js.
let visits = 0;
function __config() {
return {
scope: "global",
command_permission: "ops",
arguments: {
increment: {type: "int", min: 1, max: 10}
},
commands: {
count: () => visits,
"add <amount_increment>": "add",
reset: () => (visits = 0)
}
};
}
function add(amount = 1) {
visits += amount;
return visits;
}
- Run
/script load visit_counter. - Run
/visit_counter add 3and then/visit_counter count. The count is3. - Run
/visit_counter resetto clear the counter. The custom argument rejects values outside1–10.
Each key in commands describes a subcommand. Fixed words are literals and <argument> reads a command argument. An empty key binds the root command. A command can bind a function by name or use a JavaScript callback directly. Carpet passes parsed arguments to the function in command order.
__config() field | Effect |
|---|---|
scope | Defaults to "player". Use "global" to share top-level variables across players. You can also load an app explicitly with /script load <name> global. |
commands | Maps subcommand patterns to function names or callbacks. |
command_permission | Controls the app's own commands. Accepted values include "all", "players", "server", "ops", levels 1–4, or a function that receives the player or null. The default allows all sources. |
arguments | Defines reusable argument types. In this example, <amount_increment> uses the configured increment type. Common built-in suffixes include _int, _pos, and _block. |
stay_loaded | Defaults to true. Set it to false to skip automatic loading when the world opens with scriptsAutoload enabled. |
Call functions from /script
Use /script in <app> invoke <function> <arguments>. Arguments are comma-separated.
| Command argument access | Example |
|---|---|
commandScript only | /script in visit_counter invoke add 3 passes a JSON number. Strings, booleans, arrays, and objects can also use JSON syntax. |
commandScript and commandScriptACE | /script in visit_counter invoke add 1 + 2 evaluates the JavaScript expression first. |
| JavaScript default parameter | /script in visit_counter invoke add uses 1. With commandScriptACE you can also pass undefined explicitly. |
The underlying add function in this example does not apply the custom command argument bounds. Put validation in the function as well if it must apply to invoke and other callers.
Reload and unload
Run /script load <name> again after editing an app. A successful /reload also reloads active apps and reinstalls their commands. Top-level variables are initialized again. Use /script unload <name> to remove active commands, event handlers, and scheduled callbacks while keeping the script file.
Share a library
module.exports = {
greet: name => "Hello, " + name
};
const greetings = require("greetings");
function greet(name) {
return greetings.greet(name);
}
Run /script load welcome and /script in welcome invoke greet "Alex". require("greetings") loads the library and returns its module.exports.
Download an app
Set scriptsAppStore to a GitHub repository directory in the form <owner>/<repo>/contents/<path>. Run /script download tools/example.js with commandScriptACE to fetch that path relative to the configured directory. Carpet saves the .js file in the world's scripts/ directory and loads it.