Advertisement
Guest User

BB Human Classes API

a guest
Dec 3rd, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.82 KB | None | 0 0
  1. /*
  2.     BB Human classes (Extension for Base Builder Zombie mod)
  3.     1.0
  4.     Prince (rewrite: Mega)
  5.  
  6.     Api:
  7.         natives:
  8.  
  9.         bb_is_user_zombie(client)
  10.             @return - if zombie -> true, else -> false
  11.  
  12.         bb_get_user_class(client)
  13.             @return - player human class
  14.  
  15.         bb_set_user_class(client, class)
  16.             - forcefully sets player's new class
  17.             @return - -1 on disconnected player, 0 on success
  18.  
  19.         bb_register_hclass(const cName[], const cDesc[], const cMenuName[], iClassFlag)
  20.             cName - Class name which will be printed in chat after successful class selection
  21.             cDesc - Class description which will be shown in class menu
  22.             cMenuName - Class name which will be shown in class menu
  23.             iClassFlag - Class access flag (you can use any of existing Amxmodx flags, eg. ADMIN_ALL - everyone has access, ADMIN_LEVEL_H - access is restricted to "t" flag)
  24.             @return - On success, returns new class ID, on failure, returns -1
  25.        
  26.  
  27.         forwards:
  28.         bb_human_spawned(client, class)
  29.             gets called when human is spawned.
  30.  
  31.         bb_player_hclass_changed(client, class)
  32.             gets called when human changes class(if no class is selected, it gets called immediately after class selection, otherwise, it gets called on next player spawn)
  33.  
  34.         bb_player_selected_class(client, class)
  35.             gets called when player selects the class from the menu (it can be blocked if you return 0 or less in that event!)
  36.  
  37. */
  38.  
  39.  
  40. #include <amxmodx>
  41. #include <hamsandwich>
  42. #include <colorchat>
  43.  
  44. enum _:FORWARD_LIST {
  45.     PLAYER_SPAWNED = 0,
  46.     PLAYER_CHANGED_CLASS,
  47.     PLAYER_SELECTED_CLASS
  48. }
  49.  
  50. new bbforwards[FORWARD_LIST], gDummyRes
  51.  
  52. new const bbforwards_names[FORWARD_LIST][] = {
  53.     "bb_human_spawned",
  54.     "bb_player_hclass_changed",
  55.     "bb_player_selected_hclass"
  56. }
  57.  
  58. new const bbnatives[][][] = {
  59.     { "bb_is_user_zombie", "native_is_user_zombie" },
  60.     { "bb_get_user_hclass", "native_get_user_hclass" },
  61.     { "bb_set_user_hclass", "native_set_user_hclass" },
  62.     { "bb_register_hclass", "native_register_hclass" }
  63. }
  64.  
  65. new iRegisteredClasses = 0, bool:bArraysAreSet = false;
  66.  
  67. new Array:className, Array:classDesc, Array:classMenuName, Array:classFlags
  68.  
  69. new playerclass[33], newclass[33]
  70.  
  71. public plugin_init() {
  72.     register_plugin("BB Human Classes", "1.0", "Prince (rewrite: Mega)")
  73.    
  74.     RegisterHam(Ham_Spawn, "player", "fwPlayerSpawnPost", 1)
  75.  
  76.     SetUpArrays()
  77.  
  78.     for(new i = 0; i < sizeof(bbforwards_names); i++) bbforwards[i] = CreateMultiForward(bbforwards_names[i], ET_CONTINUE, FP_CELL, FP_CELL)
  79.  
  80.     native_register_hclass("No class", "", "No class", ADMIN_ALL)
  81.  
  82.     register_clcmd("say /hclass", "CmdHClass")
  83. }
  84.  
  85. public fwPlayerSpawnPost(id) {
  86.     if(!is_user_alive(id) || native_is_user_zombie(id)) return HAM_IGNORED
  87.  
  88.     if(newclass[id] > -1) {
  89.         playerclass[id] = newclass[id]
  90.         newclass[id] = -1
  91.         ExecuteForward(bbforwards[PLAYER_CHANGED_CLASS], gDummyRes, id, playerclass[id])
  92.     }
  93.  
  94.     ExecuteForward(bbforwards[PLAYER_SPAWNED], gDummyRes, id, playerclass[id])
  95.     return HAM_IGNORED
  96. }
  97.  
  98. public CmdHClass(id) {
  99.     if(native_is_user_zombie(id)) return
  100.  
  101.     new sClassName[32], sClassDesc[48], sMenuItem[96], menu = menu_create("Human classes: ", "CmdHClass_Handler")
  102.     for(new i = 0; i < iRegisteredClasses; i++) {
  103.         ArrayGetString(classMenuName, i, sClassName, charsmax(sClassName))
  104.         ArrayGetString(classDesc, i, sClassDesc, charsmax(sClassName))
  105.  
  106.         formatex(sMenuItem, charsmax(sMenuItem), "%s %s", sClassName, sClassDesc)
  107.  
  108.         menu_additem(menu, sMenuItem)
  109.     }
  110.  
  111.     menu_display(id, menu)
  112. }
  113.  
  114. public CmdHClass_Handler(id, menu, item) {
  115.     if(item == MENU_EXIT || native_is_user_zombie(id)) {
  116.         menu_destroy(menu)
  117.         return PLUGIN_CONTINUE
  118.     }
  119.     new iClass = item+1, cName[32]
  120.     new iFlags = ArrayGetCell(classFlags, iClass)
  121.  
  122.     ExecuteForward(bbforwards[PLAYER_SELECTED_CLASS], gDummyRes, id, iClass)
  123.     if(gDummyRes < 1 || iClass == playerclass[id]) return PLUGIN_HANDLED
  124.    
  125.     if(!(get_user_flags(id) & iFlags)) {
  126.         ColorChat(id, TEAM_COLOR, "[Human Classes]^x01 You don't have access to that class!")
  127.         return PLUGIN_HANDLED
  128.     }
  129.  
  130.     if(playerclass[id] > 0) {
  131.         newclass[id] = iClass
  132.         ColorChat(id, TEAM_COLOR, "[Human Classes]^x01 Class will be changed after your next spawn.")
  133.     } else {
  134.         playerclass[id] = iClass
  135.         ArrayGetString(className, iClass, cName, charsmax(cName))
  136.         ColorChat(id, TEAM_COLOR, "[Human Classes]^x01 You've successfully selected^x04 %s^x01 class.", cName)
  137.         ExecuteForward(bbforwards[PLAYER_CHANGED_CLASS], gDummyRes, id, iClass)
  138.     }
  139.  
  140.     return PLUGIN_CONTINUE
  141. }
  142.  
  143. public SetUpArrays() {
  144.     className = ArrayCreate(32, 1)
  145.     classDesc = ArrayCreate(48, 1)
  146.     classMenuName = ArrayCreate(32, 1)
  147.     classFlags = ArrayCreate(1, 1)
  148.  
  149.     bArraysAreSet = true
  150. }
  151.  
  152. public native_is_user_zombie(id) {
  153.     if(!is_user_connected(id)) return -1
  154.  
  155.     return (get_user_team(id) == 1?true:false)
  156. }
  157.  
  158. public native_get_user_class(id) {
  159.     if(!is_user_connected(id)) return -1
  160.  
  161.     return playerclass[id]
  162. }
  163.  
  164. public native_set_user_class(id, iClass) {
  165.     if(!is_user_connected(id)) return -1
  166.     if(playerclass[id] > 0) {
  167.         newclass[id] = iClass
  168.         ColorChat(id, TEAM_COLOR, "[Human Classes]^x01 Class will be changed after you spawn as a human again.")
  169.     } else {
  170.         playerclass[id] = iClass
  171.         ExecuteForward(bbforwards[PLAYER_CHANGED_CLASS], gDummyRes, id, iClass)
  172.     }
  173.    
  174.     return 0;
  175. }
  176.  
  177. public native_register_hclass(const cName[], const cDesc[], const cMenuName[], iClassFlag) {
  178.     param_convert(1)
  179.     param_convert(2)
  180.     param_convert(3)
  181.     if(strlen(cName) < 3 || strlen(cMenuName) < 3 || !bArraysAreSet) return -1
  182.  
  183.     ArrayPushString(className, cName)
  184.     ArrayPushString(classDesc, cDesc)
  185.     ArrayPushString(classMenuName, cMenuName)
  186.     ArrayPushCell(classFlags, iClassFlag)
  187.  
  188.     iRegisteredClasses++
  189.     return iRegisteredClasses-1
  190. }
  191.  
  192. public client_putinserver(id) {
  193.     playerclass[id] = 0
  194.     newclass[id] = -1
  195. }
  196.  
  197. public plugin_natives() {
  198.     for(new i = 0; i < sizeof(bbnatives); i++) register_native(bbnatives[i][0], bbnatives[i][1])
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement