oaaron99

Untitled

Dec 2nd, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.53 KB | None | 0 0
  1. /**************************************************************************
  2. * *
  3. * Colored Chat Functions *
  4. * Author: exvel, Editor: Popoklopsi, Powerlord, Bara *
  5. * Version: 1.1.3 *
  6. * *
  7. **************************************************************************/
  8.  
  9.  
  10. #if defined _colors_included
  11. #endinput
  12. #endif
  13. #define _colors_included
  14.  
  15. #define MAX_MESSAGE_LENGTH 250
  16. #define MAX_COLORS 16
  17.  
  18. #define SERVER_INDEX 0
  19. #define NO_INDEX -1
  20. #define NO_PLAYER -2
  21.  
  22. enum Colors
  23. {
  24. Color_Default = 0,
  25. Color_Darkred,
  26. Color_Pink,
  27. Color_Green,
  28. Color_Lightgreen,
  29. Color_Lime,
  30. Color_Red,
  31. Color_Grey,
  32. Color_Olive,
  33. Color_A,
  34. Color_Lightblue,
  35. Color_Blue,
  36. Color_D,
  37. Color_Purple,
  38. Color_Darkrange,
  39. Color_Orange
  40. }
  41.  
  42. /* Colors' properties */
  43. new String:CTag[][] = {"{default}", "{darkred}", "{pink}", "{green}", "{lightgreen}", "{lime}", "{red}", "{grey}", "{olive}", "{a}", "{lightblue}", "{blue}", "{d}", "{purple}", "{darkorange}", "{orange}"};
  44. new String:CTagCode[][] = {"\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\x09", "\x0A", "\x0B", "\x0C", "\x0D", "\x0E", "\x0F", "\x10"};
  45. new bool:CTagReqSayText2[] = {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
  46. new bool:CEventIsHooked = false;
  47. new bool:CSkipList[MAXPLAYERS+1] = {false,...};
  48.  
  49. /* Game default profile */
  50. new bool:CProfile_Colors[] = {true, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false};
  51. new CProfile_TeamIndex[] = {NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX, NO_INDEX};
  52. new bool:CProfile_SayText2 = false;
  53.  
  54. /**
  55. * Prints a message to a specific client in the chat area.
  56. * Supports color tags.
  57. *
  58. * @param client Client index.
  59. * @param szMessage Message (formatting rules).
  60. * @return No return
  61. *
  62. * On error/Errors: If the client is not connected an error will be thrown.
  63. */
  64. stock CPrintToChat(client, const String:szMessage[], any:...)
  65. {
  66. if (client <= 0 || client > MaxClients)
  67. ThrowError("Invalid client index %d", client);
  68.  
  69. if (!IsClientInGame(client))
  70. ThrowError("Client %d is not in game", client);
  71.  
  72. decl String:szBuffer[MAX_MESSAGE_LENGTH];
  73. decl String:szCMessage[MAX_MESSAGE_LENGTH];
  74.  
  75. SetGlobalTransTarget(client);
  76.  
  77. Format(szBuffer, sizeof(szBuffer), "\x01%s", szMessage);
  78. VFormat(szCMessage, sizeof(szCMessage), szBuffer, 3);
  79.  
  80. new index = CFormat(szCMessage, sizeof(szCMessage));
  81.  
  82. if (index == NO_INDEX)
  83. PrintToChat(client, "%s", szCMessage);
  84. else
  85. CSayText2(client, index, szCMessage);
  86. }
  87.  
  88. stock CReplyToCommand(client, const String:szMessage[], any:...)
  89. {
  90.  
  91. decl String:szCMessage[MAX_MESSAGE_LENGTH];
  92. VFormat(szCMessage, sizeof(szCMessage), szMessage, 3);
  93.  
  94. if (client == 0)
  95. {
  96. CRemoveTags(szCMessage, sizeof(szCMessage));
  97. PrintToServer("%s", szCMessage);
  98. }
  99. else if (GetCmdReplySource() == SM_REPLY_TO_CONSOLE)
  100. {
  101. CRemoveTags(szCMessage, sizeof(szCMessage));
  102. PrintToConsole(client, "%s", szCMessage);
  103. }
  104. else
  105. {
  106. CPrintToChat(client, "%s", szCMessage);
  107. }
  108. }
  109.  
  110.  
  111. /**
  112. * Prints a message to all clients in the chat area.
  113. * Supports color tags.
  114. *
  115. * @param client Client index.
  116. * @param szMessage Message (formatting rules)
  117. * @return No return
  118. */
  119. stock CPrintToChatAll(const String:szMessage[], any:...)
  120. {
  121. decl String:szBuffer[MAX_MESSAGE_LENGTH];
  122.  
  123. for (new i = 1; i <= MaxClients; i++)
  124. {
  125. if (IsClientInGame(i) && !IsFakeClient(i) && !CSkipList[i])
  126. {
  127. SetGlobalTransTarget(i);
  128. VFormat(szBuffer, sizeof(szBuffer), szMessage, 2);
  129.  
  130. CPrintToChat(i, "%s", szBuffer);
  131. }
  132.  
  133. CSkipList[i] = false;
  134. }
  135. }
  136.  
  137. /**
  138. * Prints a message to a specific client in the chat area.
  139. * Supports color tags and teamcolor tag.
  140. *
  141. * @param client Client index.
  142. * @param author Author index whose color will be used for teamcolor tag.
  143. * @param szMessage Message (formatting rules).
  144. * @return No return
  145. *
  146. * On error/Errors: If the client or author are not connected an error will be thrown.
  147. */
  148. stock CPrintToChatEx(client, author, const String:szMessage[], any:...)
  149. {
  150. if (client <= 0 || client > MaxClients)
  151. ThrowError("Invalid client index %d", client);
  152.  
  153. if (!IsClientInGame(client))
  154. ThrowError("Client %d is not in game", client);
  155.  
  156. if (author < 0 || author > MaxClients)
  157. ThrowError("Invalid client index %d", author);
  158.  
  159. decl String:szBuffer[MAX_MESSAGE_LENGTH];
  160. decl String:szCMessage[MAX_MESSAGE_LENGTH];
  161.  
  162. SetGlobalTransTarget(client);
  163.  
  164. Format(szBuffer, sizeof(szBuffer), "\x01%s", szMessage);
  165. VFormat(szCMessage, sizeof(szCMessage), szBuffer, 4);
  166.  
  167. new index = CFormat(szCMessage, sizeof(szCMessage), author);
  168.  
  169. if (index == NO_INDEX)
  170. PrintToChat(client, "%s", szCMessage);
  171. else
  172. CSayText2(client, author, szCMessage);
  173. }
  174.  
  175. /**
  176. * Prints a message to all clients in the chat area.
  177. * Supports color tags and teamcolor tag.
  178. *
  179. * @param author Author index whos color will be used for teamcolor tag.
  180. * @param szMessage Message (formatting rules).
  181. * @return No return
  182. *
  183. * On error/Errors: If the author is not connected an error will be thrown.
  184. */
  185. stock CPrintToChatAllEx(author, const String:szMessage[], any:...)
  186. {
  187. if (author < 0 || author > MaxClients)
  188. ThrowError("Invalid client index %d", author);
  189.  
  190. if (!IsClientInGame(author))
  191. ThrowError("Client %d is not in game", author);
  192.  
  193. decl String:szBuffer[MAX_MESSAGE_LENGTH];
  194.  
  195. for (new i = 1; i <= MaxClients; i++)
  196. {
  197. if (IsClientInGame(i) && !IsFakeClient(i) && !CSkipList[i])
  198. {
  199. SetGlobalTransTarget(i);
  200. VFormat(szBuffer, sizeof(szBuffer), szMessage, 3);
  201.  
  202. CPrintToChatEx(i, author, "%s", szBuffer);
  203. }
  204.  
  205. CSkipList[i] = false;
  206. }
  207. }
  208.  
  209. /**
  210. * Removes color tags from the string.
  211. *
  212. * @param szMessage String.
  213. * @return No return
  214. */
  215. stock CRemoveTags(String:szMessage[], maxlength)
  216. {
  217. for (new i = 0; i < MAX_COLORS; i++)
  218. ReplaceString(szMessage, maxlength, CTag[i], "", false);
  219.  
  220. ReplaceString(szMessage, maxlength, "{teamcolor}", "", false);
  221. }
  222.  
  223. /**
  224. * Checks whether a color is allowed or not
  225. *
  226. * @param tag Color Tag.
  227. * @return True when color is supported, otherwise false
  228. */
  229. stock CColorAllowed(Colors:color)
  230. {
  231. if (!CEventIsHooked)
  232. {
  233. CSetupProfile();
  234.  
  235. CEventIsHooked = true;
  236. }
  237.  
  238. return CProfile_Colors[color];
  239. }
  240.  
  241. /**
  242. * Replace the color with another color
  243. * Handle with care!
  244. *
  245. * @param color color to replace.
  246. * @param newColor color to replace with.
  247. * @noreturn
  248. */
  249. stock CReplaceColor(Colors:color, Colors:newColor)
  250. {
  251. if (!CEventIsHooked)
  252. {
  253. CSetupProfile();
  254.  
  255. CEventIsHooked = true;
  256. }
  257.  
  258. CProfile_Colors[color] = CProfile_Colors[newColor];
  259. CProfile_TeamIndex[color] = CProfile_TeamIndex[newColor];
  260.  
  261. CTagReqSayText2[color] = CTagReqSayText2[newColor];
  262. Format(CTagCode[color], sizeof(CTagCode[]), CTagCode[newColor])
  263. }
  264.  
  265. /**
  266. * This function should only be used right in front of
  267. * CPrintToChatAll or CPrintToChatAllEx and it tells
  268. * to those funcions to skip specified client when printing
  269. * message to all clients. After message is printed client will
  270. * no more be skipped.
  271. *
  272. * @param client Client index
  273. * @return No return
  274. */
  275. stock CSkipNextClient(client)
  276. {
  277. if (client <= 0 || client > MaxClients)
  278. ThrowError("Invalid client index %d", client);
  279.  
  280. CSkipList[client] = true;
  281. }
  282.  
  283. /**
  284. * Replaces color tags in a string with color codes
  285. *
  286. * @param szMessage String.
  287. * @param maxlength Maximum length of the string buffer.
  288. * @return Client index that can be used for SayText2 author index
  289. *
  290. * On error/Errors: If there is more then one team color is used an error will be thrown.
  291. */
  292. stock CFormat(String:szMessage[], maxlength, author=NO_INDEX)
  293. {
  294. decl String:szGameName[30];
  295.  
  296. GetGameFolderName(szGameName, sizeof(szGameName));
  297.  
  298. /* Hook event for auto profile setup on map start */
  299. if (!CEventIsHooked)
  300. {
  301. CSetupProfile();
  302. HookEvent("server_spawn", CEvent_MapStart, EventHookMode_PostNoCopy);
  303.  
  304. CEventIsHooked = true;
  305. }
  306.  
  307. new iRandomPlayer = NO_INDEX;
  308.  
  309. // On CS:GO set invisible precolor
  310. if (StrEqual(szGameName, "csgo", false))
  311. Format(szMessage, maxlength, " \x01\x0B\x01%s", szMessage);
  312.  
  313. /* If author was specified replace {teamcolor} tag */
  314. if (author != NO_INDEX)
  315. {
  316. if (CProfile_SayText2)
  317. {
  318. ReplaceString(szMessage, maxlength, "{teamcolor}", "\x03", false);
  319.  
  320. iRandomPlayer = author;
  321. }
  322. /* If saytext2 is not supported by game replace {teamcolor} with green tag */
  323. else
  324. ReplaceString(szMessage, maxlength, "{teamcolor}", CTagCode[Color_Green], false);
  325. }
  326. else
  327. ReplaceString(szMessage, maxlength, "{teamcolor}", "", false);
  328.  
  329. /* For other color tags we need a loop */
  330. for (new i = 0; i < MAX_COLORS; i++)
  331. {
  332. /* If tag not found - skip */
  333. if (StrContains(szMessage, CTag[i], false) == -1)
  334. continue;
  335.  
  336. /* If tag is not supported by game replace it with green tag */
  337. else if (!CProfile_Colors[i])
  338. ReplaceString(szMessage, maxlength, CTag[i], CTagCode[Color_Green], false);
  339.  
  340. /* If tag doesn't need saytext2 simply replace */
  341. else if (!CTagReqSayText2[i])
  342. ReplaceString(szMessage, maxlength, CTag[i], CTagCode[i], false);
  343.  
  344. /* Tag needs saytext2 */
  345. else
  346. {
  347. /* If saytext2 is not supported by game replace tag with green tag */
  348. if (!CProfile_SayText2)
  349. ReplaceString(szMessage, maxlength, CTag[i], CTagCode[Color_Green], false);
  350.  
  351. /* Game supports saytext2 */
  352. else
  353. {
  354. /* If random player for tag wasn't specified replace tag and find player */
  355. if (iRandomPlayer == NO_INDEX)
  356. {
  357. /* Searching for valid client for tag */
  358. iRandomPlayer = CFindRandomPlayerByTeam(CProfile_TeamIndex[i]);
  359.  
  360. /* If player not found replace tag with green color tag */
  361. if (iRandomPlayer == NO_PLAYER)
  362. ReplaceString(szMessage, maxlength, CTag[i], CTagCode[Color_Green], false);
  363.  
  364. /* If player was found simply replace */
  365. else
  366. ReplaceString(szMessage, maxlength, CTag[i], CTagCode[i], false);
  367.  
  368. }
  369. /* If found another team color tag throw error */
  370. else
  371. {
  372. //ReplaceString(szMessage, maxlength, CTag[i], "");
  373. ThrowError("Using two team colors in one message is not allowed");
  374. }
  375. }
  376.  
  377. }
  378. }
  379.  
  380. return iRandomPlayer;
  381. }
  382.  
  383. /**
  384. * Founds a random player with specified team
  385. *
  386. * @param color_team Client team.
  387. * @return Client index or NO_PLAYER if no player found
  388. */
  389. stock CFindRandomPlayerByTeam(color_team)
  390. {
  391. if (color_team == SERVER_INDEX)
  392. return 0;
  393. else
  394. {
  395. for (new i = 1; i <= MaxClients; i++)
  396. {
  397. if (IsClientInGame(i) && GetClientTeam(i) == color_team)
  398. return i;
  399. }
  400. }
  401.  
  402. return NO_PLAYER;
  403. }
  404.  
  405. /**
  406. * Sends a SayText2 usermessage to a client
  407. *
  408. * @param szMessage Client index
  409. * @param maxlength Author index
  410. * @param szMessage Message
  411. * @return No return.
  412. */
  413. stock CSayText2(client, author, const String:szMessage[])
  414. {
  415. new Handle:hBuffer = StartMessageOne("SayText2", client, USERMSG_RELIABLE|USERMSG_BLOCKHOOKS);
  416.  
  417. if(GetFeatureStatus(FeatureType_Native, "GetUserMessageType") == FeatureStatus_Available && GetUserMessageType() == UM_Protobuf)
  418. {
  419. PbSetInt(hBuffer, "ent_idx", author);
  420. PbSetBool(hBuffer, "chat", true);
  421. PbSetString(hBuffer, "msg_name", szMessage);
  422. PbAddString(hBuffer, "params", "");
  423. PbAddString(hBuffer, "params", "");
  424. PbAddString(hBuffer, "params", "");
  425. PbAddString(hBuffer, "params", "");
  426. }
  427. else
  428. {
  429. BfWriteByte(hBuffer, author);
  430. BfWriteByte(hBuffer, true);
  431. BfWriteString(hBuffer, szMessage);
  432. }
  433.  
  434. EndMessage();
  435. }
  436.  
  437. /**
  438. * Creates game color profile
  439. * This function must be edited if you want to add more games support
  440. *
  441. * @return No return.
  442. */
  443. stock CSetupProfile()
  444. {
  445. decl String:szGameName[30];
  446. GetGameFolderName(szGameName, sizeof(szGameName));
  447.  
  448. if (StrEqual(szGameName, "cstrike", false))
  449. {
  450. CProfile_Colors[Color_Lightgreen] = true;
  451. CProfile_Colors[Color_Red] = true;
  452. CProfile_Colors[Color_Blue] = true;
  453. CProfile_Colors[Color_Olive] = true;
  454. CProfile_TeamIndex[Color_Lightgreen] = SERVER_INDEX;
  455. CProfile_TeamIndex[Color_Red] = 2;
  456. CProfile_TeamIndex[Color_Blue] = 3;
  457. CProfile_SayText2 = true;
  458. }
  459. else if (StrEqual(szGameName, "csgo", false))
  460. {
  461. CProfile_Colors[Color_Default] = true;
  462. CProfile_Colors[Color_Darkred] = true;
  463. CProfile_Colors[Color_Pink] = true;
  464. CProfile_Colors[Color_Green] = true;
  465. CProfile_Colors[Color_Lightgreen] = true;
  466. CProfile_Colors[Color_Lime] = true;
  467. CProfile_Colors[Color_Red] = true;
  468. CProfile_Colors[Color_Grey] = true;
  469. CProfile_Colors[Color_Olive] = true;
  470. CProfile_Colors[Color_A] = true;
  471. CProfile_Colors[Color_Lightblue] = true;
  472. CProfile_Colors[Color_Blue] = true;
  473. CProfile_Colors[Color_D] = true;
  474. CProfile_Colors[Color_Purple] = true;
  475. CProfile_Colors[Color_Darkrange] = true;
  476. CProfile_Colors[Color_Orange] = true;
  477. CProfile_Colors[Color_Red] = true;
  478. CProfile_Colors[Color_Blue] = true;
  479. CProfile_Colors[Color_Olive] = true;
  480. CProfile_Colors[Color_Darkred] = true;
  481. CProfile_Colors[Color_Lime] = true;
  482. CProfile_Colors[Color_Purple] = true;
  483. CProfile_Colors[Color_Grey] = true;
  484. CProfile_Colors[Color_Orange] = true;
  485. CProfile_TeamIndex[Color_Red] = 2;
  486. CProfile_TeamIndex[Color_Blue] = 3;
  487. CProfile_SayText2 = true;
  488. }
  489. else if (StrEqual(szGameName, "tf", false))
  490. {
  491. CProfile_Colors[Color_Lightgreen] = true;
  492. CProfile_Colors[Color_Red] = true;
  493. CProfile_Colors[Color_Blue] = true;
  494. CProfile_Colors[Color_Olive] = true;
  495. CProfile_TeamIndex[Color_Lightgreen] = SERVER_INDEX;
  496. CProfile_TeamIndex[Color_Red] = 2;
  497. CProfile_TeamIndex[Color_Blue] = 3;
  498. CProfile_SayText2 = true;
  499. }
  500. else if (StrEqual(szGameName, "left4dead", false) || StrEqual(szGameName, "left4dead2", false))
  501. {
  502. CProfile_Colors[Color_Lightgreen] = true;
  503. CProfile_Colors[Color_Red] = true;
  504. CProfile_Colors[Color_Blue] = true;
  505. CProfile_Colors[Color_Olive] = true;
  506. CProfile_TeamIndex[Color_Lightgreen] = SERVER_INDEX;
  507. CProfile_TeamIndex[Color_Red] = 3;
  508. CProfile_TeamIndex[Color_Blue] = 2;
  509. CProfile_SayText2 = true;
  510. }
  511. else if (StrEqual(szGameName, "hl2mp", false))
  512. {
  513. /* hl2mp profile is based on mp_teamplay convar */
  514. if (GetConVarBool(FindConVar("mp_teamplay")))
  515. {
  516. CProfile_Colors[Color_Red] = true;
  517. CProfile_Colors[Color_Blue] = true;
  518. CProfile_Colors[Color_Olive] = true;
  519. CProfile_TeamIndex[Color_Red] = 3;
  520. CProfile_TeamIndex[Color_Blue] = 2;
  521. CProfile_SayText2 = true;
  522. }
  523. else
  524. {
  525. CProfile_SayText2 = false;
  526. CProfile_Colors[Color_Olive] = true;
  527. }
  528. }
  529. else if (StrEqual(szGameName, "dod", false))
  530. {
  531. CProfile_Colors[Color_Olive] = true;
  532. CProfile_SayText2 = false;
  533. }
  534. /* Profile for other games */
  535. else
  536. {
  537. if (GetUserMessageId("SayText2") == INVALID_MESSAGE_ID)
  538. {
  539. CProfile_SayText2 = false;
  540. }
  541. else
  542. {
  543. CProfile_Colors[Color_Red] = true;
  544. CProfile_Colors[Color_Blue] = true;
  545. CProfile_TeamIndex[Color_Red] = 2;
  546. CProfile_TeamIndex[Color_Blue] = 3;
  547. CProfile_SayText2 = true;
  548. }
  549. }
  550. }
  551.  
  552. public Action:CEvent_MapStart(Handle:event, const String:name[], bool:dontBroadcast)
  553. {
  554. CSetupProfile();
  555.  
  556. for (new i = 1; i <= MaxClients; i++)
  557. CSkipList[i] = false;
  558. }
Add Comment
Please, Sign In to add comment