Version 2.0.0
1229 downloads
This resource extends clientside mp.events object to make clientside command handling easier, which means no more splitting strings to use with if/else if or switch/case in playerCommand event
Installing
Put mp-commands into your server's client_packages directory, then add require('mp-commands'); to client_packages/index.js.
Clientside API
/**
* Adds a clientside command.
* @param {string} name Name of the command.
* @param {function} handlerFn Function that will run when the command is used.
* @throws {TypeError} name argument must be a string.
* @throws {TypeError} handlerFn argument must be a function.
* @throws {Error} Command with the given name already exists.
*/
mp.events.addCommand(name, handlerFn);
/**
* Returns clientside command names.
* @return {string[]}
*/
mp.events.getCommandNames();
/**
* Removes a clientside command.
* @param {string} name Name of the command to remove.
* @return {boolean}
*/
mp.events.removeCommand(name);
/**
* Removes all clientside commands.
*/
mp.events.removeAllCommands();
/**
* (v2.0.0) Adds a console command.
* @param {string} name Name of the console command.
* @param {function} handlerFn Function that will run when the console command is used.
* @throws {TypeError} name argument must be a string.
* @throws {TypeError} handlerFn argument must be a function.
* @throws {Error} Console command with the given name already exists.
*/
mp.console.addCommand(name, handlerFn);
/**
* (v2.0.0) Returns console command names.
* @return {string[]}
*/
mp.console.getCommandNames();
/**
* (v2.0.0) Removes a console command.
* @param {string} name Name of the console command to remove.
* @return {boolean}
*/
mp.console.removeCommand(name);
/**
* (v2.0.0) Removes all console commands.
*/
mp.console.removeAllCommands();
Example
/*
Looks like it's serverside code but it's not, updates GTA V's cash display.
Usage: /updmoney [amount]
*/
mp.events.addCommand("updmoney", function (amount) {
amount = Number(amount);
if (!Number.isInteger(amount)) {
mp.gui.chat.push("Invalid amount.");
return;
}
mp.game.stats.statSetInt(mp.game.joaat("SP0_TOTAL_CASH"), amount, false);
});
mp.events.addCommand("cmds", function () {
mp.gui.chat.push(`Commands: ${mp.events.getCommandNames().join(", ")}`);
});
Example (Console)
NOTE: Console is a RAGE Multiplayer 1.1 feature, which means code below won't work on stable/prerelease/0.3.7 branch.
// Write "hat 123 0" to the console and press enter to give yourself a cool helmet.
mp.console.addCommand("hat", function (drawable, texture = 0) {
drawable = Number(drawable);
texture = Number(texture);
if (!Number.isInteger(drawable) || !Number.isInteger(texture)) {
mp.console.logError("Invalid drawable/texture.");
return;
}
if (drawable < 0) {
mp.players.local.clearProp(0);
} else {
mp.players.local.setPropIndex(0, drawable, texture, true);
}
});
// Write "cmds" to the console to see available console commands.
mp.console.addCommand("cmds", function() {
mp.console.logInfo(`Console commands: ${mp.console.getCommandNames().join(", ")}`);
});
Notes
I recommend loading mp-commands as the first script in client_packages/index.js so the functions become available in other scripts.
Command names are case sensitive.