BANNED
|
02-24-2018
, 17:30
Snippet: Setting RGB/RGBA values via a command
|
#1
|
Just a little something I've been tinkering with and finally got working the way I want it to. It'll be used in an upcoming update to my old Lo-Fi Longwave Beacon Deluxe plugin but I realize this could be very useful for people wanting an easy way to allow players to choose their own RGB/RGBA values for things like FuncommandsX's colorizing (or in my case, my fully separate player/weapon/cosmetic/building colorizing commands).
I've left some comment notes in there regarding some stuff. Got suggestions for improvement of this code? Send 'em my way!
Regex pattern was obtained from https://www.regular-expressions.info/numericranges.html
Update: Added new version 2 of the commands. Main difference is version 1 uses GetCmdArgString and regex checks the entire string using a huge regex pattern. Version 2 uses individual GetCmdArg's and a significantly smaller regex pattern to check each arg individually.
Version 1 - Tested, Working 100%
PHP Code:
public void OnPluginStart() { RegConsoleCmd("sm_rgb", Command_EnterRGB, "Usage: sm_rgb <0-255> <0-255> <0-255>"); RegConsoleCmd("sm_rgba", Command_EnterRGBA, "Usage: sm_rgba <0-255> <0-255> <0-255> <0-255>"); }
public Action Command_EnterRGB(int iClient, int iArgs) { if (iArgs < 3) { ReplyToCommand(iClient, "[SM] Usage: sm_rgb <0-255> <0-255> <0-255>"); return Plugin_Handled; } if (IsClientInGame(iClient)) { char strCmdArg[13]; GetCmdArgString(strCmdArg, sizeof(strCmdArg)); TrimString(strCmdArg);
char strError[128]; char strPattern[] = "^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"; RegexError iReturn; Regex hRGB = new Regex(strPattern, _, strError, sizeof(strError), iReturn); hRGB.Match(strCmdArg, iReturn); /* * I'm not sure what I'm doing here with this if statement. * The regex never seemed to produce a proper error code if it detected something invalid. * I'd always end up getting iReturn = 0 and an empty strError string. * The documentation on the regex error codes for the return is sparse. */ if (iReturn == REGEX_ERROR_NONE) { char strRed[4], strGreen[4], strBlue[4]; hRGB.GetSubString(1, strRed, sizeof(strRed)); hRGB.GetSubString(2, strGreen, sizeof(strGreen)); hRGB.GetSubString(3, strBlue, sizeof(strBlue));
/* * !strlen + regex = easy detection of invalid values. * The above regex pattern returns empty substrings if something doesn't match it. * For example, things like negative integers or A-F for hex numbers aren't passed through. * So doing silly stuff like "-255 0 0" or "25A 0 0" won't work. */ if (!strlen(strRed) || !strlen(strGreen) || !strlen(strBlue)) { ReplyToCommand(iClient, "[SM] Invalid value detected"); return Plugin_Handled; }
/* * Do what you want here. * For example, here's a snippet from my Lo-Fi Longwave Beacon plugin * * Create integers to store the substring values. * int iRed, iGreen, iBlue; * * Convert them to integers. * StringToIntEx(strRed, iRed, 10); * StringToIntEx(strGreen, iGreen, 10); * StringToIntEx(strBlue, iBlue, 10); * * I modified the global int array that held the RGBA values of the beacon * and changed it to g_iBeaconRGB2[MAXPLAYERS + 1][4] for per-player colors. * g_iBeaconRGB2[iClient][0] = iRed; * g_iBeaconRGB2[iClient][1] = iGreen; * g_iBeaconRGB2[iClient][2] = iBlue;
* ReplyToCommand(iClient, "[SM] Your beacon color is now %i %i %i", iRed, iGreen, iBlue); * return Plugin_Handled; */ } } return Plugin_Handled; }
public Action Command_EnterRGBA(int iClient, int iArgs) { if (iArgs < 4) { ReplyToCommand(iClient, "[SM] Usage: sm_rgba <0-255> <0-255> <0-255> <0-255>"); return Plugin_Handled; } if (IsClientInGame(iClient)) { char strCmdArg[16]; GetCmdArgString(strCmdArg, sizeof(strCmdArg)); TrimString(strCmdArg);
char strError[128]; char strPattern[] = "^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"; RegexError iReturn; Regex hRGB = new Regex(strPattern, _, strError, sizeof(strError), iReturn); hRGB.Match(strCmdArg, iReturn);
if (iReturn == REGEX_ERROR_NONE) { char strRed[4], strGreen[4], strBlue[4], strAlpha[4]; hRGB.GetSubString(1, strRed, sizeof(strRed)); hRGB.GetSubString(2, strGreen, sizeof(strGreen)); hRGB.GetSubString(3, strBlue, sizeof(strBlue)); hRGB.GetSubString(4, strAlpha, sizeof(strAlpha));
if (!strlen(strRed) || !strlen(strGreen) || !strlen(strBlue) || !strlen(strAlpha)) { ReplyToCommand(iClient, "[SM] Invalid value detected"); return Plugin_Handled; } } } return Plugin_Handled; }
Version 2 - Untested
PHP Code:
public void OnPluginStart() { RegConsoleCmd("sm_rgb", Command_EnterRGB, "Usage: sm_rgb <0-255> <0-255> <0-255>"); RegConsoleCmd("sm_rgba", Command_EnterRGBA, "Usage: sm_rgba <0-255> <0-255> <0-255> <0-255>"); }
public Action Command_EnterRGB(int iClient, int iArgs) { if (iArgs < 3) { ReplyToCommand(iClient, "[SM] Usage: sm_rgb <0-255> <0-255> <0-255>"); return Plugin_Handled; } if (IsClientInGame(iClient)) { char strCmdArg1[4], strCmdArg2[4], strCmdArg3[4]; GetCmdArg(1, strCmdArg1, sizeof(strCmdArg1)); GetCmdArg(2, strCmdArg2, sizeof(strCmdArg2)); GetCmdArg(3, strCmdArg3, sizeof(strCmdArg3));
char strError[128]; char strPattern[] = "^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"; RegexError iReturn; Regex hRGB = new Regex(strPattern, _, strError, sizeof(strError), iReturn);
char strRed[4], strGreen[4] strBlue[4]; hRGB.Match(strCmdArg1, iReturn); if (iReturn == REGEX_ERROR_NONE) { hRGB.GetSubString(1, strRed, sizeof(strRed)); if (!strlen(strRed)) { ReplyToCommand("[SM] Value %s is outside the valid range of 0-255.", strRed); return Plugin_Handled; } }
hRGB.Match(strCmdArg2, iReturn); if (iReturn == REGEX_ERROR_NONE) { hRGB.GetSubString(1, strGreen, sizeof(strGreen)); if (!strlen(strGreen)) { ReplyToCommand("[SM] Value %s is outside the valid range of 0-255.", strGreen); return Plugin_Handled; } }
hRGB.Match(strCmdArg3, iReturn); if (iReturn == REGEX_ERROR_NONE) { hRGB.GetSubString(1, strBlue, sizeof(strBlue)); if (!strlen(strBlue)) { ReplyToCommand("[SM] Value %s is outside the valid range of 0-255.", strBlue); return Plugin_Handled; } } } return Plugin_Handled; }
public Action Command_EnterRGBA(int iClient, int iArgs) { if (iArgs < 4) { ReplyToCommand(iClient, "[SM] Usage: sm_rgba <0-255> <0-255> <0-255> <0-255>"); return Plugin_Handled; } if (IsClientInGame(iClient)) { char strCmdArg1[4], strCmdArg2[4], strCmdArg3[4], strCmdArg4[4]; GetCmdArg(1, strCmdArg1, sizeof(strCmdArg1)); GetCmdArg(2, strCmdArg2, sizeof(strCmdArg2)); GetCmdArg(3, strCmdArg3, sizeof(strCmdArg3)); GetCmdArg(4, strCmdArg4, sizeof(strCmdArg4));
char strError[128]; char strPattern[] = "^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"; RegexError iReturn; Regex hRGB = new Regex(strPattern, _, strError, sizeof(strError), iReturn);
char strRed[4], strGreen[4] strBlue[4], strAlpha[4]; hRGB.Match(strCmdArg1, iReturn); if (iReturn == REGEX_ERROR_NONE) { hRGB.GetSubString(1, strRed, sizeof(strRed)); if (!strlen(strRed)) { ReplyToCommand("[SM] Red value %s is outside the valid range of 0-255.", strRed); return Plugin_Handled; } }
hRGB.Match(strCmdArg2, iReturn); if (iReturn == REGEX_ERROR_NONE) { hRGB.GetSubString(1, strGreen, sizeof(strGreen)); if (!strlen(strGreen)) { ReplyToCommand("[SM] Blue value %s is outside the valid range of 0-255.", strGreen); return Plugin_Handled; } }
hRGB.Match(strCmdArg3, iReturn); if (iReturn == REGEX_ERROR_NONE) { hRGB.GetSubString(1, strBlue, sizeof(strBlue)); if (!strlen(strBlue)) { ReplyToCommand("[SM] Green value %s is outside the valid range of 0-255.", strBlue); return Plugin_Handled; } }
hRGB.Match(strCmdArg4, iReturn); if (iReturn == REGEX_ERROR_NONE) { hRGB.GetSubString(1, strAlpha, sizeof(strAlpha)); if (!strlen(strAlpha)) { ReplyToCommand("[SM] Alpha value %s is outside the valid range of 0-255.", strAlpha); return Plugin_Handled; } } } return Plugin_Handled; }
Last edited by 404UserNotFound; 02-25-2018 at 02:07.
|
|