Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.47 KB | None | 0 0
  1. #pragma semicolon 1
  2.  
  3. #include <sourcemod>
  4. #include <sdktools>
  5. #include <cstrike>
  6. #include <clientprefs>
  7.  
  8. #include <smlib>
  9. #include <csgomorecolors>
  10.  
  11. #pragma newdecls required
  12.  
  13. #define PLUGIN_VERSION "1.0.0+build.10"
  14.  
  15. #define MCC_MAX_COLOR_LENGTH 32
  16. #define MCC_MAX_CMD_LENGTH 32
  17. #define MCC_MAX_FLAG_LENGTH 8
  18.  
  19. #define MCC_DEFAULT_COLOR "White"
  20.  
  21. #define MCC_SETCLIENTMODELCOLOR_SUCCESS 0
  22. #define MCC_SETCLIENTMODELCOLOR_COLORNOTFOUND 1
  23. #define MCC_SETCLIENTMODELCOLOR_NOTCOLORABLE 2
  24. #define MCC_SETCLIENTMODELCOLOR_COLORNOACCESS 3
  25. #define MCC_SETCLIENTMODELCOLOR_INVALIDCLIENT 4
  26.  
  27. Handle g_hColorCookie;
  28. ArrayList g_hModelsArray;
  29. ArrayList g_hColorsArray;
  30. KeyValues g_hModelColorsKV;
  31.  
  32. bool g_bCookiesAreLoaded[MAXPLAYERS + 1];
  33.  
  34. public Plugin myinfo =
  35. {
  36. name = "Model Color Changer",
  37. author = "Locomotiver, Ariistuujj",
  38. description = "Change player model colors",
  39. version = PLUGIN_VERSION,
  40. url = "GFLClan.com"
  41. };
  42.  
  43. public void OnPluginStart()
  44. {
  45. LoadTranslations("common.phrases");
  46. LoadTranslations("mcolorchanger.phrases");
  47.  
  48. CreateConVar("mcolorchanger_version", PLUGIN_VERSION, "Current version of Model Color Changer", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY|FCVAR_DONTRECORD);
  49.  
  50. g_hColorCookie = RegClientCookie("mcolorchanger_color", "Client's preferred color", CookieAccess_Protected);
  51.  
  52. RegConsoleCmd("sm_mccmenu", Command_MCCMenu, "Open the menu for changing your model color");
  53. RegConsoleCmd("sm_mccset", Command_MCCSet, "Change your model color");
  54. RegAdminCmd("sm_mccsetother", Command_MCCSetOther, ADMFLAG_ROOT, "Change the model color of the target");
  55.  
  56. HookEvent("player_spawn", Event_PlayerSpawn);
  57. }
  58.  
  59. public void OnMapStart()
  60. {
  61. RefreshModels();
  62. RefreshColors();
  63. }
  64.  
  65. void RefreshModels()
  66. {
  67. if (g_hModelsArray != null)
  68. delete g_hModelsArray;
  69.  
  70. g_hModelsArray = new ArrayList(PLATFORM_MAX_PATH);
  71.  
  72. char sModelsPath[PLATFORM_MAX_PATH];
  73. char sFileLine[PLATFORM_MAX_PATH];
  74.  
  75. BuildPath(Path_SM, sModelsPath, sizeof(sModelsPath), "configs/mcolorchanger/models.txt");
  76.  
  77. File hModelsFile = OpenFile(sModelsPath, "r");
  78.  
  79. if (hModelsFile != null)
  80. {
  81. while (!hModelsFile.EndOfFile())
  82. {
  83. if (!hModelsFile.ReadLine(sFileLine, sizeof(sFileLine)))
  84. break;
  85.  
  86. TrimString(sFileLine);
  87.  
  88. if (strlen(sFileLine) > 0 && FileExists(sFileLine))
  89. g_hModelsArray.PushString(sFileLine);
  90. }
  91.  
  92. delete hModelsFile;
  93. }
  94. else
  95. {
  96. LogError("[SM] no file found for models (configs/mcolorchanger/models.txt)");
  97. }
  98. }
  99.  
  100. void RefreshColors()
  101. {
  102. g_hColorsArray = new ArrayList(MCC_MAX_COLOR_LENGTH);
  103.  
  104. char sColorsPath[PLATFORM_MAX_PATH];
  105.  
  106. BuildPath(Path_SM, sColorsPath, sizeof(sColorsPath), "configs/mcolorchanger/colors.txt");
  107.  
  108. if (g_hModelColorsKV != null)
  109. delete g_hModelColorsKV;
  110.  
  111. g_hModelColorsKV = new KeyValues("ModelColors");
  112. g_hModelColorsKV.ImportFromFile(sColorsPath);
  113.  
  114. SMCParser hSMC = new SMCParser();
  115.  
  116. hSMC.OnEnterSection = SMC_RefreshColors_NewSection;
  117.  
  118. SMCError eSMCReturn = hSMC.ParseFile(sColorsPath);
  119.  
  120. if (eSMCReturn != SMCError_Okay)
  121. {
  122. char sError[255];
  123.  
  124. hSMC.GetErrorString(eSMCReturn, sError, sizeof(sError));
  125.  
  126. if (sError[0] != '\0')
  127. LogError("%s", sError);
  128. }
  129.  
  130. delete hSMC;
  131.  
  132. SortADTArray(g_hColorsArray, Sort_Ascending, Sort_String);
  133. }
  134.  
  135. public SMCResult SMC_RefreshColors_NewSection(SMCParser hSMC, const char[] sName, bool bQuotes)
  136. {
  137. char sRootSection[MCC_MAX_COLOR_LENGTH];
  138.  
  139. g_hModelColorsKV.GetSectionName(sRootSection, sizeof(sRootSection));
  140.  
  141. if (!StrEqual(sName, sRootSection, false))
  142. g_hColorsArray.PushString(sName);
  143. }
  144.  
  145. public void OnClientConnected(int iClient)
  146. {
  147. g_bCookiesAreLoaded[iClient] = false;
  148. }
  149.  
  150. public void OnClientCookiesCached(int iClient)
  151. {
  152. if (IsValidClient(iClient))
  153. {
  154. char sColorCookieValue[MCC_MAX_COLOR_LENGTH];
  155.  
  156. GetClientCookie(iClient, g_hColorCookie, sColorCookieValue, sizeof(sColorCookieValue));
  157.  
  158. if (sColorCookieValue[0] == '\0')
  159. SetClientCookie(iClient, g_hColorCookie, MCC_DEFAULT_COLOR);
  160.  
  161. g_bCookiesAreLoaded[iClient] = true;
  162. }
  163. }
  164.  
  165. public Action Event_PlayerSpawn(Event hEvent, const char[] sName, bool bDontBroadcast)
  166. {
  167. int iClient = GetClientOfUserId(hEvent.GetInt("userid"));
  168.  
  169. if (!g_bCookiesAreLoaded[iClient] && AreClientCookiesCached(iClient))
  170. OnClientCookiesCached(iClient);
  171.  
  172. if (g_bCookiesAreLoaded[iClient])
  173. CreateTimer(3.0, Timer_PlayerSpawnDelay, iClient, TIMER_FLAG_NO_MAPCHANGE);
  174. else if (IsValidClient(iClient))
  175. CPrintToChat(iClient, "%t %t", "MCC_MSG_TAG", "MCC_MSG_COOKIESNOTLOADED");
  176.  
  177. return Plugin_Continue;
  178. }
  179.  
  180. public Action Timer_PlayerSpawnDelay(Handle hTimer, int iClient)
  181. {
  182. if (IsValidClient(iClient))
  183. {
  184. char sColorCookieValue[MCC_MAX_COLOR_LENGTH];
  185.  
  186. GetClientCookie(iClient, g_hColorCookie, sColorCookieValue, sizeof(sColorCookieValue));
  187.  
  188. int iSCMCReturn = SetClientModelColor(iClient, sColorCookieValue);
  189.  
  190. switch (iSCMCReturn)
  191. {
  192. case MCC_SETCLIENTMODELCOLOR_SUCCESS:
  193. CPrintToChat(iClient, "%t %t", "MCC_MSG_TAG", "MCC_MSG_SETCOLOR", sColorCookieValue);
  194.  
  195. case MCC_SETCLIENTMODELCOLOR_COLORNOTFOUND:
  196. {
  197. CPrintToChat(iClient, "%t %t", "MCC_MSG_TAG", "MCC_MSG_COLORNOTFOUND");
  198. SetClientCookie(iClient, g_hColorCookie, MCC_DEFAULT_COLOR);
  199. }
  200.  
  201. case MCC_SETCLIENTMODELCOLOR_NOTCOLORABLE:
  202. {
  203. CPrintToChat(iClient, "%t %t", "MCC_MSG_TAG", "MCC_MSG_NOTCOLORABLE");
  204. SetClientCookie(iClient, g_hColorCookie, MCC_DEFAULT_COLOR);
  205. }
  206.  
  207. case MCC_SETCLIENTMODELCOLOR_COLORNOACCESS:
  208. {
  209. CPrintToChat(iClient, "%t %t", "MCC_MSG_TAG", "MCC_MSG_COLORNOACCESS");
  210. SetClientCookie(iClient, g_hColorCookie, MCC_DEFAULT_COLOR);
  211. }
  212. }
  213. }
  214. }
  215.  
  216.  
  217. public Action Command_MCCMenu(int iClient, int iArgs)
  218. {
  219. char sColorCookieValue[MCC_MAX_COLOR_LENGTH];
  220. Menu hColorSelectMenu = new Menu(MenuHandler_ColorSelect);
  221.  
  222. hColorSelectMenu.SetTitle("%T", "MCC_MENU_COLORSELECT", iClient);
  223. GetClientCookie(iClient, g_hColorCookie, sColorCookieValue, sizeof(sColorCookieValue));
  224.  
  225. if (!StrEqual(MCC_DEFAULT_COLOR, sColorCookieValue, false))
  226. hColorSelectMenu.AddItem(MCC_DEFAULT_COLOR, MCC_DEFAULT_COLOR);
  227. else
  228. hColorSelectMenu.AddItem(MCC_DEFAULT_COLOR, MCC_DEFAULT_COLOR, ITEMDRAW_DISABLED);
  229.  
  230. char sColor[MCC_MAX_COLOR_LENGTH];
  231. int iColor;
  232. char sFlag[MCC_MAX_FLAG_LENGTH];
  233.  
  234. for (int i; i < g_hColorsArray.Length; i++)
  235. {
  236. g_hColorsArray.GetString(i, sColor, sizeof(sColor));
  237. iColor = GetSectionSymbolOfColor(sColor);
  238.  
  239. if (iColor != -1)
  240. {
  241. g_hModelColorsKV.JumpToKeySymbol(iColor);
  242.  
  243. g_hModelColorsKV.GetString("flag", sFlag, sizeof(sFlag));
  244.  
  245. g_hModelColorsKV.Rewind();
  246.  
  247. if (HasPermission(iClient, "b") || HasPermission(iClient, sFlag))
  248. {
  249. if (!StrEqual(sColor, sColorCookieValue, false))
  250. hColorSelectMenu.AddItem(sColor, sColor);
  251. else
  252. hColorSelectMenu.AddItem(sColor, sColor, ITEMDRAW_DISABLED);
  253. }
  254. }
  255. }
  256.  
  257. hColorSelectMenu.Display(iClient, MENU_TIME_FOREVER);
  258.  
  259. return Plugin_Handled;
  260. }
  261.  
  262. public int MenuHandler_ColorSelect(Menu hColorSelectMenu, MenuAction eAction, int iClient, int iItem)
  263. {
  264. switch (eAction)
  265. {
  266. case MenuAction_Select:
  267. {
  268. char sItem[MCC_MAX_COLOR_LENGTH];
  269.  
  270. hColorSelectMenu.GetItem(iItem, sItem, sizeof(sItem));
  271.  
  272. int iSCMCReturn = SetClientModelColor(iClient, sItem);
  273.  
  274. switch (iSCMCReturn)
  275. {
  276. case MCC_SETCLIENTMODELCOLOR_SUCCESS:
  277. {
  278. CPrintToChat(iClient, "%t %t", "MCC_MSG_TAG", "MCC_MSG_SETCOLOR", sItem);
  279. SetClientCookie(iClient, g_hColorCookie, sItem);
  280. }
  281.  
  282. case MCC_SETCLIENTMODELCOLOR_NOTCOLORABLE:
  283. CPrintToChat(iClient, "%t %t", "MCC_MSG_TAG", "MCC_MSG_NOTCOLORABLE");
  284. }
  285. }
  286. case MenuAction_End:
  287. {
  288. delete hColorSelectMenu;
  289. }
  290. }
  291. }
  292.  
  293. public Action Command_MCCSet(int iClient, int iArgs)
  294. {
  295. if (iArgs != 1)
  296. {
  297. char sCommandName[MCC_MAX_CMD_LENGTH];
  298.  
  299. GetCmdArg(0, sCommandName, sizeof(sCommandName));
  300. CReplyToCommand(iClient, "%t %t", "MCC_MSG_TAG", "MCC_CMD_MCCSET_USAGE", sCommandName);
  301.  
  302. return Plugin_Handled;
  303. }
  304.  
  305. char sColor[MCC_MAX_COLOR_LENGTH];
  306.  
  307. GetCmdArg(1, sColor, sizeof(sColor));
  308.  
  309. int iSCMCReturn = SetClientModelColor(iClient, sColor);
  310.  
  311. switch (iSCMCReturn)
  312. {
  313. case MCC_SETCLIENTMODELCOLOR_SUCCESS:
  314. {
  315. CPrintToChat(iClient, "%t %t", "MCC_MSG_TAG", "MCC_MSG_SETCOLOR", sColor);
  316. SetClientCookie(iClient, g_hColorCookie, sColor);
  317. }
  318.  
  319. case MCC_SETCLIENTMODELCOLOR_COLORNOTFOUND:
  320. CPrintToChat(iClient, "%t %t", "MCC_MSG_TAG", "MCC_MSG_COLORNOTFOUND");
  321.  
  322. case MCC_SETCLIENTMODELCOLOR_NOTCOLORABLE:
  323. CPrintToChat(iClient, "%t %t", "MCC_MSG_TAG", "MCC_MSG_NOTCOLORABLE");
  324.  
  325. case MCC_SETCLIENTMODELCOLOR_COLORNOACCESS:
  326. CPrintToChat(iClient, "%t %t", "MCC_MSG_TAG", "MCC_MSG_COLORNOACCESS");
  327. }
  328.  
  329. return Plugin_Handled;
  330. }
  331.  
  332. public Action Command_MCCSetOther(int iClient, int iArgs)
  333. {
  334. if (iArgs != 2)
  335. {
  336. char sCommandName[MCC_MAX_CMD_LENGTH];
  337.  
  338. GetCmdArg(0, sCommandName, sizeof(sCommandName));
  339. CReplyToCommand(iClient, "%t %t", "MCC_MSG_TAG", "MCC_CMD_MCCSETOTHER_USAGE", sCommandName);
  340.  
  341. return Plugin_Handled;
  342. }
  343.  
  344. int iTargetCount;
  345. char sTarget[MAX_NAME_LENGTH];
  346. int iTargetList[MAXPLAYERS + 1];
  347. char sTargetName[MAX_NAME_LENGTH];
  348. bool bTranslateTargetName;
  349.  
  350. GetCmdArg(1, sTarget, sizeof(sTarget));
  351. iTargetCount = ProcessTargetString(sTarget, iClient, iTargetList, sizeof(sTarget), COMMAND_FILTER_ALIVE, sTargetName, sizeof(sTargetName), bTranslateTargetName);
  352.  
  353. if (iTargetCount <= 0)
  354. {
  355. ReplyToTargetError(iClient, iTargetCount);
  356.  
  357. return Plugin_Handled;
  358. }
  359.  
  360. char sColor[MCC_MAX_COLOR_LENGTH];
  361.  
  362. GetCmdArg(2, sColor, sizeof(sColor));
  363.  
  364. int iSCMCReturn;
  365.  
  366. for (int iTarget; iTarget < iTargetCount; iTarget++)
  367. {
  368. iSCMCReturn = SetClientModelColor(iTargetList[iTarget], sColor, false);
  369.  
  370. switch (iSCMCReturn)
  371. {
  372. case MCC_SETCLIENTMODELCOLOR_SUCCESS:
  373. {
  374. CPrintToChat(iTargetList[iTarget], "%t %t", "MCC_MSG_TAG", "MCC_MSG_SETCOLOR", sColor);
  375. LogAction(iClient, iTargetList[iTarget], "\"%L\" set the model color of \"%L\" to %s", iClient, iTargetList[iTarget], sColor);
  376. }
  377.  
  378. case MCC_SETCLIENTMODELCOLOR_COLORNOTFOUND:
  379. {
  380. CPrintToChat(iClient, "%t %t", "MCC_MSG_TAG", "MCC_MSG_COLORNOTFOUND");
  381.  
  382. return Plugin_Handled;
  383. }
  384. }
  385. }
  386.  
  387. char sActivityTag[16];
  388.  
  389. Format(sActivityTag, sizeof(sActivityTag), "%t ", "MCC_CMD_TAG");
  390.  
  391. if (!bTranslateTargetName)
  392. ShowActivity2(iClient, sActivityTag, "%t", "MCC_CMD_MCCSETOTHER_ACTIVITY", sTargetName, sColor);
  393. else
  394. ShowActivity2(iClient, sActivityTag, "%t", "MCC_CMD_MCCSETOTHER_ACTIVITY_ML", sTargetName, sColor);
  395.  
  396. return Plugin_Handled;
  397. }
  398.  
  399. int SetClientModelColor(int iClient, const char[] sColor, bool bCheckAccess = true)
  400. {
  401. if (!IsValidClient(iClient, true, false))
  402. return MCC_SETCLIENTMODELCOLOR_INVALIDCLIENT;
  403.  
  404. int iColor = GetSectionSymbolOfColor(sColor);
  405.  
  406. if (iColor == -1)
  407. return MCC_SETCLIENTMODELCOLOR_COLORNOTFOUND;
  408.  
  409. int iRed = 255;
  410. int iGreen = 255;
  411. int iBlue = 255;
  412. int iAlpha = 255;
  413. char sFlag[MCC_MAX_FLAG_LENGTH];
  414.  
  415. if (!StrEqual(MCC_DEFAULT_COLOR, sColor, false))
  416. {
  417. g_hModelColorsKV.JumpToKeySymbol(iColor);
  418.  
  419. iRed = g_hModelColorsKV.GetNum("r", iRed);
  420. iGreen = g_hModelColorsKV.GetNum("g", iGreen);
  421. iBlue = g_hModelColorsKV.GetNum("b", iBlue);
  422. iAlpha = g_hModelColorsKV.GetNum("alpha", iAlpha);
  423. g_hModelColorsKV.GetString("flag", sFlag, sizeof(sFlag));
  424.  
  425. g_hModelColorsKV.Rewind();
  426. }
  427.  
  428. LogMessage("Red : %i, Green : %i, Blue : %i", iRed, iGreen, iBlue);
  429.  
  430. if (!IsClientModelColorable(iClient))
  431. return MCC_SETCLIENTMODELCOLOR_NOTCOLORABLE;
  432.  
  433. if (bCheckAccess && (!HasPermission(iClient, "b") && !HasPermission(iClient, sFlag)))
  434. return MCC_SETCLIENTMODELCOLOR_COLORNOACCESS;
  435.  
  436. SetEntityRenderMode(iClient, RENDER_NORMAL);
  437. SetEntityRenderColor(iClient, iRed, iGreen, iBlue, iAlpha);
  438.  
  439. return MCC_SETCLIENTMODELCOLOR_SUCCESS;
  440. }
  441.  
  442. int GetSectionSymbolOfColor(const char[] sColor)
  443. {
  444. char sSectionName[MCC_MAX_COLOR_LENGTH];
  445. int iSectionSymbol = -1;
  446.  
  447. if (g_hModelColorsKV.GotoFirstSubKey())
  448. {
  449. do
  450. {
  451. g_hModelColorsKV.GetSectionName(sSectionName, sizeof(sSectionName));
  452.  
  453. if (StrEqual(sColor, sSectionName, false))
  454. {
  455. g_hModelColorsKV.GetSectionSymbol(iSectionSymbol);
  456.  
  457. break;
  458. }
  459. }
  460. while (g_hModelColorsKV.GotoNextKey());
  461. }
  462.  
  463. g_hModelColorsKV.Rewind();
  464.  
  465. return iSectionSymbol;
  466. }
  467.  
  468. bool IsClientModelColorable(int iClient)
  469. {
  470. char sClientModel[PLATFORM_MAX_PATH];
  471. char sCurrentModel[PLATFORM_MAX_PATH];
  472.  
  473. GetClientModel(iClient, sClientModel, sizeof(sClientModel));
  474.  
  475. for (int i; i < g_hModelsArray.Length; i++)
  476. {
  477. g_hModelsArray.GetString(i, sCurrentModel, sizeof(sCurrentModel));
  478.  
  479. if (StrEqual(sCurrentModel, sClientModel, false))
  480. return true;
  481. }
  482.  
  483. return false;
  484. }
  485.  
  486. stock bool IsValidClient(int iClient, bool bAllowBots = false, bool bAllowDead = true)
  487. {
  488. if (!(1 <= iClient <= MaxClients) || !IsClientInGame(iClient) || (IsFakeClient(iClient) && !bAllowBots) || IsClientSourceTV(iClient) || IsClientReplay(iClient) || (!bAllowDead && !IsPlayerAlive(iClient)))
  489. return false;
  490.  
  491. return true;
  492. }
  493.  
  494. stock bool HasPermission(int iClient, char[] sFlagString)
  495. {
  496. if (StrEqual(sFlagString, ""))
  497. return true;
  498.  
  499. AdminId eAdmin = GetUserAdmin(iClient);
  500.  
  501. if (eAdmin == INVALID_ADMIN_ID)
  502. return false;
  503.  
  504. int iFlags = ReadFlagString(sFlagString);
  505.  
  506. if (CheckAccess(eAdmin, "", iFlags, true))
  507. return true;
  508.  
  509. return false;
  510. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement