diff --git a/addons/events/CfgFunctions.hpp b/addons/events/CfgFunctions.hpp index c750c20c1..106b47ccd 100644 --- a/addons/events/CfgFunctions.hpp +++ b/addons/events/CfgFunctions.hpp @@ -84,6 +84,7 @@ class CfgFunctions { }; PATHTO_FNC(addMarkerEventHandler); PATHTO_FNC(removeMarkerEventHandler); + PATHTO_FNC(registerChatCommand); }; }; }; diff --git a/addons/events/XEH_postInit.sqf b/addons/events/XEH_postInit.sqf index 9410cca5d..c482da94e 100644 --- a/addons/events/XEH_postInit.sqf +++ b/addons/events/XEH_postInit.sqf @@ -28,3 +28,41 @@ if (isServer) then { }; }]; }; + +// custom chat command system +[QGVAR(chatMessageSent), { + params ["_message"]; + + if ((_message select [0,1]) isEqualTo "#") then { + private _index = _message find " "; + + // no argument + if (_index isEqualTo -1) then { + _index = count _message; + }; + + private _command = _message select [1, _index - 1]; + private _argument = _message select [_index + 1]; + + // check if command is available + private _access = ["all"]; + + if (IS_ADMIN || isServer) then { + _access pushBack "admin"; + }; + + if (IS_ADMIN_LOGGED || isServer) then { + _access pushBack "adminlogged"; + }; + + (GVAR(customChatCommands) getVariable _command) params ["_code", "_availableFor"]; + + if (_availableFor in _access) then { + [[_argument], _code] call { + // prevent bad code from overwriting protected variables + private ["_message", "_index", "_command", "_argument", "_access", "_code", "_availableFor"]; + (_this select 0) call (_this select 1); + }; + }; + }; +}] call CBA_fnc_addEventHandler; diff --git a/addons/events/config.cpp b/addons/events/config.cpp index 36efc01cb..8ac015726 100644 --- a/addons/events/config.cpp +++ b/addons/events/config.cpp @@ -15,3 +15,12 @@ class CfgPatches { #include "CfgEventHandlers.hpp" #include "CfgFunctions.hpp" + +class RscDisplayChat { + onKeyDown = QUOTE(\ + if ((_this select 1) in [ARR_2(DIK_RETURN,DIK_NUMPADENTER)]) then {\ + [ARR_2('GVAR(chatMessageSent)',[ARR_2(ctrlText ((_this select 0) displayctrl 101),_this select 0)])] call CBA_fnc_localEvent;\ + };\ + false\ + ); +}; diff --git a/addons/events/fnc_registerChatCommand.sqf b/addons/events/fnc_registerChatCommand.sqf new file mode 100644 index 000000000..f2caa0867 --- /dev/null +++ b/addons/events/fnc_registerChatCommand.sqf @@ -0,0 +1,55 @@ +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_registerChatCommand + +Description: + Register a custom chat command on the local machine. + +Parameters: + _command - Chat command + _code - Code to execute after command was entered. + _availableFor - "all", "admin" or "adminLogged" (optional, default: "admin") + +Returns: + _return - true: Success, false: Error + +Examples: + (begin example) + // '#skipTime 12' will make it night + ["skipTime", { parseNumber (_this select 0) remoteExec ["skipTime"]; }, "admin"] call CBA_fnc_registerChatCommand; + (end) + +Author: + commy2 +---------------------------------------------------------------------------- */ +#include "script_component.hpp" +SCRIPT(registerChatCommand); + +if (isNil QGVAR(customChatCommands)) then { + GVAR(customChatCommands) = [] call CBA_fnc_createNamespace; +}; + +params [ + ["_command", "", [""]], + ["_code", {}, [{}]], + ["_availableFor", "admin", [""]] +]; + +if (_command isEqualTo "") exitWith { + WARNING("chat command cannot be empty string"); + false +}; + +if (_code isEqualTo {}) exitWith { + WARNING_1("code for chat command %1 cannot be empty function",str _command); + false +}; + +_availableFor = toLower _availableFor; + +if !(_availableFor in ["all", "admin", "adminlogged"]) exitWith { + WARNING_1("wrong argument %2 for chat command %1.",str _command,str _availableFor); + false +}; + +GVAR(customChatCommands) setVariable [_command, [_code, _availableFor]]; +true