📦 js Folder¶
plugins/KaMenu/js/ stores global JavaScript packages. Use JavaScript packages for script logic shared by multiple menus, such as text formatting, complex state calculation, reward messages, game logic, and list parsing.
Global JavaScript packages can be called from js: actions, {js:...} text placeholders, and condition expressions.
File Location¶
Runtime folder:
One .js file is one JavaScript package. The package ID is the relative path without the .js suffix, using / as the path separator:
plugins/KaMenu/js/reward/message.js -> reward/message
plugins/KaMenu/js/common/format_money.js -> common/format_money
When the server starts and plugins/KaMenu/js/ does not exist yet, KaMenu releases a built-in example:
If the folder already exists, KaMenu does not write example files automatically, avoiding changes to existing server files.
Naming And Size Limits¶
Package IDs may only use letters, numbers, _, -, ., and /. They cannot contain .., cannot start or end with /, and cannot contain consecutive //.
Each JavaScript package file is limited to 1 MiB. Files over the limit are skipped and a localized warning is printed to the console.
File Content¶
The .js file content is JavaScript code:
var amount = args[0] || "0";
var target = args[1] || name;
"Congratulations, " + target + " received " + amount + " coins";
The last expression is returned to {js:...}. js: actions do not require a return value and do not automatically display the return value to the player.
Calling Packages¶
Call from actions:
Call from text:
Call from conditions:
Arguments may be separated by commas or spaces:
Wrap an argument with single quotes, double quotes, or backticks when it contains a space or comma:
Inside JavaScript, read arguments with args[0], args[1], and so on.
Lookup Priority¶
When calling:
KaMenu checks:
- Current menu
JavaScript.reward/message - Global package
plugins/KaMenu/js/reward/message.js
If a menu JavaScript entry and a global package use the same name, the menu script has priority.
Execution Context¶
Each js:, {js:...}, and JavaScript package call uses an isolated execution context. Do not rely on variables or functions defined by a previous js: action.
Old style:
actions:
- 'js: var random = Math.floor(Math.random() * 100);'
- 'js: player.sendMessage("Random number: " + random);'
Recommended style:
actions:
- 'js: var random = Math.floor(Math.random() * 100); player.sendMessage("Random number: " + random);'
For complex logic, prefer a menu JavaScript block or a global JS package:
JavaScript:
random_message: |
var random = Math.floor(Math.random() * 100);
player.sendMessage("Random number: " + random);
Bottom:
type: notice
confirm:
text: '&a[ Random ]'
actions:
- 'js: [random_message]'
Built-in Variables¶
JavaScript packages can use:
player: current player objectuuid: player UUID stringname: player namelocation: player locationinventory: player inventoryworld: player's worldserver: Bukkit server instanceargs: package argument array
Helper Functions¶
tell(player, message): send a messagelog(message): print a[JS]logdelay(ticks, callback): run later on the main threadasyncDelay(ticks, callback): run later asynchronouslygetPlayer(name): get a player by namepapi(placeholder, targetPlayer?): resolve PlaceholderAPIkvar(variable, targetPlayer?): resolve KaMenu internal variablesdata(key)/gdata(key)/meta(key): read data variableslist(key)/glist(key): read list variables, returning JSON array strings
Reload¶
Global JavaScript packages are loaded on server startup, /km reload, or /km reload js.
Only .js files are loaded.
Troubleshooting¶
The script does not show text
js: actions do not display return values. Use tell, toast, title, or another feedback action, or use {js:[package]} in text.
JavaScript package not found
- Check that the file is under
plugins/KaMenu/js/ - Check that the suffix is
.js - Check that the package ID only uses letters, numbers,
_,-,., and/ - Check that the file size is no more than
1 MiB - Check that the script syntax can be compiled by the JavaScript engine; syntax errors count as failures during
/km reload jsand print console warnings - Check that the call uses
[package_name] - Run
/km reload js
A variable does not exist in the next js action
This is normal execution context isolation. Merge related code into one js: action, or move it into a menu JavaScript block / global JS package.