Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CBA_fnc_registerChatCommand #507

Merged
merged 1 commit into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions addons/events/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class CfgFunctions {
};
PATHTO_FNC(addMarkerEventHandler);
PATHTO_FNC(removeMarkerEventHandler);
PATHTO_FNC(registerChatCommand);
};
};
};
38 changes: 38 additions & 0 deletions addons/events/XEH_postInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 9 additions & 0 deletions addons/events/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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\
);
};
55 changes: 55 additions & 0 deletions addons/events/fnc_registerChatCommand.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_registerChatCommand

Description:
Register a custom chat command on the local machine.

Parameters:
_command - Chat command <STRING>
_code - Code to execute after command was entered. <CODE>
_availableFor - "all", "admin" or "adminLogged" (optional, default: "admin") <STRING>

Returns:
_return - true: Success, false: Error <BOOLEAN>

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