Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.46 KB | None | 0 0
  1. #include <amxmodx>
  2. #include <amxmisc>
  3. #include <fakemeta>
  4. #include <sqlx>
  5.  
  6. #define PLUG_NAME "HATS"
  7. #define PLUG_AUTH "SgtBane"
  8. #define PLUG_VERS "1.8"
  9. #define PLUG_TAG "HATS"
  10. #define PLUG_ADMIN ADMIN_RCON //Access flags required to give/remove hats
  11. #define PLUG_ADMINB ADMIN_CHAT //Access flags required to set personal hat if admin only is enabled
  12.  
  13. #define OFFSET_GLOWSET 100
  14.  
  15. #define HAT_ALL 0
  16. #define HAT_ADMIN 1
  17. #define HAT_TERROR 2
  18. #define HAT_COUNTER 3
  19.  
  20. #define menusize 220
  21. #define maxTry 15 //Number of tries to get someone a non-admin random hat before giving up.
  22. #define modelpath "models/hat"
  23.  
  24. stock fm_set_entity_visibility(index, visible = 1) set_pev(index, pev_effects, visible == 1 ? pev(index, pev_effects) & ~EF_NODRAW : pev(index, pev_effects) | EF_NODRAW)
  25.  
  26. new g_HatEnt[33]
  27. new CurrentHat[33]
  28. new CurrentMenu[33]
  29.  
  30. new HatFile[64]
  31. new MenuPages, TotalHats
  32.  
  33. #define MAX_HATS 64
  34. new HATMDL[MAX_HATS][26]
  35. new HATNAME[MAX_HATS][26]
  36. new HATREST[MAX_HATS]
  37.  
  38. new P_AdminOnly
  39. new P_AdminHats
  40. new P_RandomJoin
  41. new P_BotRandom
  42. new P_ForceHat
  43. new P_Glow
  44.  
  45. new Handle:g_sql_tuple;
  46. new Handle:g_sql_conn;
  47.  
  48. public plugin_init() {
  49. register_plugin(PLUG_NAME, PLUG_VERS, PLUG_AUTH)
  50. register_logevent("event_roundstart", 2, "1=Round_Start")
  51. register_event("TeamInfo", "event_team_info", "a" )
  52.  
  53. register_concmd("amx_givehat", "Give_Hat", PLUG_ADMIN, "<nick> <mdl #>")
  54. register_concmd("amx_removehats", "Remove_Hat", PLUG_ADMIN, " - Removes hats from everyone.")
  55.  
  56. register_menucmd(register_menuid("\yHat Menu: [Page"), (1<<0|1<<1|1<<2|1<<3|1<<4|1<<5|1<<6|1<<7|1<<8|1<<9),"MenuCommand")
  57. register_clcmd("say /hats", "ShowMenu", -1, "Shows Knife menu")
  58.  
  59. P_AdminOnly = register_cvar("hat_adminonly", "0") //Only admins can use the menu
  60. P_AdminHats = register_cvar("hat_adminhats", "1") //Allow hats for admins only (if 0, hats specifically for admins can be used by anyone)
  61. P_RandomJoin = register_cvar("hat_random", "1") //Random hats for players as they join
  62. P_BotRandom = register_cvar("hat_bots", "1") //Random hats for bots as they join
  63. P_ForceHat = register_cvar("hat_force", "0") //Force a specific hat (if not 0)
  64. P_Glow = register_cvar("hat_glow", "1") //0=None,1=GlowWithPlayer,2=TeamColor
  65.  
  66. // connect to the database and create the hats table if it doesn't exist
  67. g_sql_tuple = SQL_MakeDbTuple("", "", "", "hats")
  68. new error[128], errno
  69. g_sql_conn = SQL_Connect(g_sql_tuple, errno, error, 127)
  70. if (g_sql_conn == Empty_Handle) {
  71. server_print("[%s] can't connect to sql: %s", PLUG_TAG, error)
  72. return PLUGIN_HANDLED
  73. }
  74. if (!sqlite_TableExists(g_sql_conn, "hats"))
  75. {
  76. SQL_QueryAndIgnore(g_sql_conn, "CREATE TABLE hats ( auth_id TEXT PRIMARY KEY, hat_id INTEGER NOT NULL)")
  77. }
  78. return PLUGIN_CONTINUE
  79. }
  80.  
  81. public plugin_end() {
  82. SQL_FreeHandle(g_sql_conn)
  83. SQL_FreeHandle(g_sql_tuple)
  84. }
  85.  
  86. public ShowMenu(id) {
  87. if ((get_pcvar_num(P_AdminOnly) == 1 && get_user_flags(id) & PLUG_ADMIN) || (get_pcvar_num(P_AdminOnly) == 0 && get_pcvar_num(P_ForceHat) == 0)) {
  88. CurrentMenu[id] = 1
  89. ShowHats(id)
  90. } else {
  91. client_print(id,print_chat,"[%s] Only admins may currently use this menu.",PLUG_TAG)
  92. }
  93. return PLUGIN_HANDLED
  94. }
  95.  
  96. public ShowHats(id) {
  97. new keys = (1<<0|1<<1|1<<2|1<<3|1<<4|1<<5|1<<6|1<<7|1<<8|1<<9)
  98.  
  99. new szMenuBody[menusize + 1], WpnID
  100. new nLen = format(szMenuBody, menusize, "\yHat Menu: [Page %i/%i]^n",CurrentMenu[id],MenuPages)
  101.  
  102. new MnuClr[3]
  103. // Get Hat Names And Add Them To The List
  104. for (new hatid=0; hatid < 8; hatid++) {
  105. WpnID = ((CurrentMenu[id] * 8) + hatid - 8)
  106. if (WpnID < TotalHats) {
  107. menucolor(id, WpnID, MnuClr)
  108. nLen += format(szMenuBody[nLen], menusize-nLen, "^n\w%i.%s %s", hatid + 1, MnuClr, HATNAME[WpnID])
  109. }
  110. }
  111.  
  112. // Next Page And Previous/Close
  113. if (CurrentMenu[id] == MenuPages) {
  114. nLen += format(szMenuBody[nLen], menusize-nLen, "^n^n\d9. Next Page")
  115. } else {
  116. nLen += format(szMenuBody[nLen], menusize-nLen, "^n^n\w9. Next Page")
  117. }
  118.  
  119. if (CurrentMenu[id] > 1) {
  120. nLen += format(szMenuBody[nLen], menusize-nLen, "^n\w0. Previous Page")
  121. } else {
  122. nLen += format(szMenuBody[nLen], menusize-nLen, "^n\w0. Close")
  123. }
  124. show_menu(id, keys, szMenuBody, -1)
  125. return PLUGIN_HANDLED
  126. }
  127.  
  128. public MenuCommand(id, key) {
  129. switch(key)
  130. {
  131. case 8: //9 - [Next Page]
  132. {
  133. if (CurrentMenu[id] < MenuPages) CurrentMenu[id]++
  134. ShowHats(id)
  135. return PLUGIN_HANDLED
  136. }
  137. case 9: //0 - [Close]
  138. {
  139. CurrentMenu[id]--
  140. if (CurrentMenu[id] > 0) ShowHats(id)
  141. return PLUGIN_HANDLED
  142. }
  143. default:
  144. {
  145. new HatID = ((CurrentMenu[id] * 8) + key - 8)
  146. if (HatID < TotalHats) {
  147. if ((get_pcvar_num(P_AdminHats) == 0 && HATREST[HatID] == HAT_ADMIN) || (get_pcvar_num(P_AdminHats) == 1 && HATREST[HatID] == HAT_ADMIN && get_user_flags(id) & PLUG_ADMINB) || HATREST[HatID] == HAT_ALL || (HATREST[HatID] == get_user_team(id) + 1)) {
  148. Set_Hat(id,HatID,id)
  149. } else {
  150. if (HATREST[HatID] == HAT_TERROR && get_user_team(id) == 2) {
  151. client_print(id,print_chat,"[%s] This hat is currently set as a Terrorist Hat.",PLUG_TAG)
  152. } else if (HATREST[HatID] == HAT_COUNTER && get_user_team(id) == 1) {
  153. client_print(id,print_chat,"[%s] This hat is currently set as Counter Terrorist.",PLUG_TAG)
  154. } else {
  155. client_print(id,print_chat,"[%s] This hat is currently set as Admin Only.",PLUG_TAG)
  156. }
  157. }
  158. }
  159. }
  160. }
  161. return PLUGIN_HANDLED
  162. }
  163.  
  164. public plugin_precache() {
  165. new cfgDir[32]
  166. get_configsdir(cfgDir,31)
  167. formatex(HatFile,63,"%s/HatList.ini",cfgDir)
  168. command_load()
  169. new tmpfile [101]
  170. for (new i = 1; i < TotalHats; ++i) {
  171. format(tmpfile, 100, "%s/%s", modelpath, HATMDL[i])
  172. if (file_exists (tmpfile)) {
  173. precache_model(tmpfile)
  174. server_print("[%s] Precached %s", PLUG_TAG, HATMDL[i])
  175. } else {
  176. server_print("[%s] Failed to precache %s", PLUG_TAG, tmpfile)
  177. }
  178. }
  179. }
  180.  
  181. public client_authorized(id) {
  182. new auth_id[32], error[128]
  183. get_user_authid(id, auth_id, 31)
  184.  
  185. new Handle:query = SQL_PrepareQuery(g_sql_conn, "SELECT hat_id FROM hats where auth_id = '%s'", auth_id)
  186. if (!SQL_Execute(query)) {
  187. SQL_QueryError(query, error, 127)
  188. server_print("[%s] error executing query to get hats: %s", PLUG_TAG, error)
  189. } else if (SQL_NumResults(query)) {
  190. // found some results, let's get the first result
  191. new qcolHatId = SQL_FieldNameToNum(query, "hat_id")
  192. new hat_id = SQL_ReadResult(query, qcolHatId)
  193. Set_Hat(id, hat_id, 0)
  194. }
  195. }
  196.  
  197. public client_putinserver(id) {
  198. if (get_pcvar_num(P_ForceHat) == 1) {
  199. new forceID = get_pcvar_num(P_ForceHat)
  200. if (forceID <= TotalHats - 1) {
  201. forcehat(id, forceID)
  202. } else {
  203. set_pcvar_num(P_ForceHat, 0)
  204. }
  205. } else if (get_pcvar_num(P_RandomJoin) == 1 || (get_pcvar_num(P_BotRandom) == 1 && is_user_bot(id))) {
  206. if (get_pcvar_num(P_ForceHat) == 0) Random_Hat(id)
  207. }
  208. return PLUGIN_CONTINUE
  209. }
  210.  
  211. public event_team_info() {
  212. if (get_pcvar_num(P_ForceHat) != 0) return
  213. new id = read_data(1)
  214. if (HATREST[CurrentHat[id]] == HAT_ALL) return
  215. if (HATREST[CurrentHat[id]] == HAT_ADMIN && get_user_flags(id) & PLUG_ADMINB) return
  216.  
  217. new team[3]
  218. read_data(2, team, 2)
  219. switch(team[0]) {
  220. case 'C': {
  221. if (HATREST[CurrentHat[id]] != HAT_COUNTER) Random_Hat(id)
  222. }
  223. case 'T': {
  224. if (HATREST[CurrentHat[id]] != HAT_TERROR) Random_Hat(id)
  225. }
  226. case 'S': {
  227. Set_Hat(id, 0, 0)
  228. }
  229. }
  230. return
  231. }
  232. public event_roundstart() {
  233. new forceID = get_pcvar_num(P_ForceHat)
  234. for (new i = 0; i < get_maxplayers(); ++i) {
  235. if (is_user_connected(i) && g_HatEnt[i] > 0) {
  236. if (forceID != 0) {
  237. forcehat(i, forceID)
  238. }
  239. glowhat(i)
  240. }
  241. }
  242. return PLUGIN_CONTINUE
  243. }
  244.  
  245. public Give_Hat(id, req_flag)
  246. {
  247. if( !(get_user_flags(id) & req_flag) )
  248. return PLUGIN_HANDLED
  249.  
  250. new smodelnum[5], name[32]
  251. read_argv(1,name,31)
  252. read_argv(2,smodelnum,4)
  253.  
  254. new player = cmd_target(id,name,2)
  255. if (!player) {
  256. client_print(id,print_chat,"[%s] Player With That Name Does Not Exist.",PLUG_TAG)
  257. return PLUGIN_HANDLED
  258. }
  259.  
  260. new imodelnum = (str_to_num(smodelnum))
  261. if (imodelnum > MAX_HATS) return PLUGIN_HANDLED
  262.  
  263. Set_Hat(player,imodelnum,id)
  264.  
  265. return PLUGIN_CONTINUE
  266. }
  267.  
  268. public Remove_Hat(id, req_flag)
  269. {
  270. if( !(get_user_flags(id) & req_flag) )
  271. return PLUGIN_HANDLED
  272.  
  273. for (new i = 0; i < get_maxplayers(); ++i) {
  274. if (is_user_connected(i) && g_HatEnt[i] > 0) {
  275. Set_Hat(id, 0, 0)
  276. }
  277. }
  278. client_print(id,print_chat,"[%s] Removed hats from everyone.",PLUG_TAG)
  279. return PLUGIN_CONTINUE
  280. }
  281.  
  282. public Random_Hat(id) {
  283. new bool:foundrnd = false, cntTry = 0, randID = random_num (1, TotalHats - 1)
  284. while (cntTry < maxTry && foundrnd == false) {
  285. randID = random_num (1, TotalHats - 1)
  286. cntTry += 1
  287. if (HATREST[randID] == HAT_ALL) foundrnd = true
  288. if (HATREST[randID] == HAT_ADMIN && get_user_flags(id) & PLUG_ADMINB) foundrnd = true
  289. if ((get_user_team(id) != 0) && HATREST[CurrentHat[id]] == get_user_team(id) + 1) foundrnd = true
  290. }
  291. if (foundrnd == true) { //If a valid random hat is found, apply it.
  292. Set_Hat(id, randID , 0)
  293. } else { //Otherwise, don't use any hat.
  294. Set_Hat(id, 0, 0)
  295. }
  296. return PLUGIN_CONTINUE
  297. }
  298.  
  299. public Set_Hat(player, imodelnum, targeter) {
  300. new name[32]
  301. new tmpfile[101]
  302. format(tmpfile, 100, "%s/%s", modelpath, HATMDL[imodelnum])
  303. get_user_name(player, name, 31)
  304. if (imodelnum == 0) {
  305. if(g_HatEnt[player] > 0) {
  306. fm_set_entity_visibility(g_HatEnt[player], 0)
  307. }
  308. if (targeter != 0) {
  309. client_print(targeter, print_chat, "[%s] Removed hat from %s",PLUG_TAG,name)
  310. }
  311.  
  312. // clear the hat from the player in sqlite
  313. sqlite_save_hat(player, imodelnum)
  314. } else if (file_exists(tmpfile)) {
  315. if(g_HatEnt[player] < 1) {
  316. g_HatEnt[player] = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"))
  317. if(g_HatEnt[player] > 0) {
  318. set_pev(g_HatEnt[player], pev_movetype, MOVETYPE_FOLLOW)
  319. set_pev(g_HatEnt[player], pev_aiment, player)
  320. set_pev(g_HatEnt[player], pev_rendermode, kRenderNormal)
  321. engfunc(EngFunc_SetModel, g_HatEnt[player], tmpfile)
  322. }
  323. } else {
  324. engfunc(EngFunc_SetModel, g_HatEnt[player], tmpfile)
  325. }
  326. glowhat(player)
  327. CurrentHat[player] = imodelnum
  328. if (targeter != 0) {
  329. client_print(targeter, print_chat, "[%s] Set %s on %s",PLUG_TAG,HATNAME[imodelnum],name)
  330. }
  331.  
  332. // save the hat to sqlite
  333. sqlite_save_hat(player, imodelnum)
  334. }
  335. }
  336.  
  337. sqlite_save_hat(player_id, hat_id) {
  338. new auth_id[32]
  339. get_user_authid(player_id, auth_id, 31)
  340. SQL_QueryAndIgnore(g_sql_conn, "REPLACE INTO hats (auth_id, hat_id) VALUES ('%s', %d)", auth_id, hat_id)
  341. }
  342.  
  343. public command_load() {
  344. if(file_exists(HatFile)) {
  345. HATMDL[0] = ""
  346. HATNAME[0] = "None"
  347. TotalHats = 1
  348. new TempCrapA[2]
  349. new sfLineData[128]
  350. new file = fopen(HatFile,"rt")
  351. while(file && !feof(file)) {
  352. fgets(file,sfLineData,127)
  353.  
  354. // Skip Comment ; // and Empty Lines
  355. if (sfLineData[0] == ';' || strlen(sfLineData) < 1 || (sfLineData[0] == '/' && sfLineData[1] == '/')) continue
  356.  
  357. // BREAK IT UP!
  358. parse(sfLineData, HATMDL[TotalHats], 25, HATNAME[TotalHats], 25, TempCrapA, 1)
  359.  
  360. if (TempCrapA[0] == 'A' || TempCrapA[0] == '1') {
  361. HATREST[TotalHats] = HAT_ADMIN
  362. } else if (TempCrapA[0] == 'T' || TempCrapA[0] == '2') {
  363. HATREST[TotalHats] = HAT_TERROR
  364. } else if (TempCrapA[0] == 'C' || TempCrapA[0] == '3') {
  365. HATREST[TotalHats] = HAT_COUNTER
  366. } else {
  367. HATREST[TotalHats] = HAT_ALL
  368. }
  369. TotalHats += 1
  370. if(TotalHats >= MAX_HATS) {
  371. server_print("[%s] Reached hat limit",PLUG_TAG)
  372. break
  373. }
  374. }
  375. if(file) fclose(file)
  376. }
  377. MenuPages = floatround((TotalHats / 8.0), floatround_ceil)
  378. server_print("[%s] Loaded %i hats, and Generated %i pages",PLUG_TAG,TotalHats,MenuPages)
  379. }
  380.  
  381.  
  382. menucolor(id, ItemID, MnuClr[3]) {
  383. //If its the hat they currently have on
  384. if (ItemID == CurrentHat[id]) {
  385. MnuClr = "\d"
  386. return
  387. }
  388. if (HATREST[ItemID] != HAT_ALL) {
  389. //If its an AdminHat&They are NOT an admin
  390. if (HATREST[ItemID] == HAT_ADMIN && get_pcvar_num(P_AdminHats) == 1) {
  391. if (get_user_flags(id) & PLUG_ADMINB) {
  392. MnuClr = "\y"
  393. } else {
  394. MnuClr = "\r"
  395. }
  396. //If this is a hat set for there team or not
  397. } else if (HATREST[ItemID] != get_user_team(id) + 1) {
  398. MnuClr = "\r"
  399. } else {
  400. MnuClr = "\y"
  401. }
  402. } else {
  403. MnuClr = "\w"
  404. }
  405. return
  406. }
  407.  
  408. glowhat(id) {
  409. if (!pev_valid(g_HatEnt[id])) return
  410. if (get_pcvar_num(P_Glow) != 0) { //If Glowing Hats Are Enabled
  411. set_pev(g_HatEnt[id], pev_renderfx, kRenderFxGlowShell)
  412. if (get_pcvar_num(P_Glow) == 2) { //If Not Team Specific, Use Player Glow On Hat
  413. new Float:curcolors[3], Float:curamt
  414. pev(id, pev_rendercolor, curcolors)
  415. pev(id, pev_renderamt, curamt)
  416. set_pev(g_HatEnt[id], pev_rendercolor, curcolors)
  417. set_pev(g_HatEnt[id], pev_renderamt, curamt)
  418. } else { //If Team Specific, Red=T, Blue=CT
  419. if (get_user_team(id) == 1) {
  420. set_pev(g_HatEnt[id], pev_rendercolor, {200.0, 0.0, 0.0})
  421. } else if (get_user_team(id) == 2) {
  422. set_pev(g_HatEnt[id], pev_rendercolor, {0.0, 0.0, 200.0})
  423. }
  424. set_pev(g_HatEnt[id], pev_renderamt, 50.0)
  425. }
  426. } else {
  427. set_pev(g_HatEnt[id], pev_renderfx, kRenderFxNone)
  428. set_pev(g_HatEnt[id], pev_renderamt, 0.0)
  429. }
  430. fm_set_entity_visibility(g_HatEnt[id], 1)
  431. return
  432. }
  433.  
  434. forcehat(id, forceID) {
  435. if (forceID == 0) forceID = get_pcvar_num(P_ForceHat)
  436. if (forceID != 0) {
  437. if (forceID <= TotalHats - 1) {
  438. if (forceID != CurrentHat[id]) Set_Hat(id, forceID, 0)
  439. } else {
  440. set_pcvar_num(P_ForceHat, 0)
  441. }
  442. }
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement