Advertisement
Rochet2

Command parser

Jan 27th, 2012
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.76 KB | None | 0 0
  1. -- Command functions:
  2. --[=[
  3. local function command_name(event, pPlayer, pMessage, pType, pLanguage, pMisc, MSG) -- Change command_name to your custom command's name
  4.     -- do something
  5.     -- MSG[1] is the command name and first argument is MSG[2], second is MSG[3].
  6.     -- example of the MSG table: MSG = {"command_name", "1st argument", "2nd argument"}
  7. end
  8. ]=]
  9.  
  10. local function cast(event, pPlayer, pMessage, pType, pLanguage, pMisc, MSG)
  11.     pPlayer:CastSpell(MSG[2])
  12. end
  13.  
  14. local function fart(event, pPlayer, pMessage, pType, pLanguage, pMisc, MSG)
  15.     pPlayer:PlaySoundToPlayer(17339)
  16. end
  17.  
  18. local function broadcast(event, pPlayer, pMessage, pType, pLanguage, pMisc, MSG)
  19.     SendWorldMessage(MSG[2], MSG[3])
  20. end
  21.  
  22. -- Command table:
  23. local T =
  24. {
  25. --[=[
  26.     ["command_name"] = { command_name, -- Change command_name to your custom command's name
  27.         {}, -- "number" and "string" accepted
  28.         '', -- Must be able to use command group specified here (arcemu command groups) Example: az
  29.         'Usage: .command_name', -- the help text for the command. If empty, then show default arguments.
  30.     },
  31. ]=]
  32.     ["cast"] = {
  33.         cast,
  34.         {"number"},
  35.         '',
  36.         '',
  37.     },
  38.     ["fart"] = {
  39.         fart,
  40.         {},
  41.         '',
  42.         '',
  43.     },
  44.     ["broadcast"] = {
  45.         broadcast,
  46.         {"string", "number"},
  47.         'az',
  48.         'Usage: .broadcast Text Type\nText must be closed with quotes if it contains spaces: "Hello world"\nType can be 1 for areatrigger message and 2 for broadcast message',
  49.     },
  50. }
  51.  
  52. --
  53.  
  54. local function explode(str)
  55.     local pos, arr = 2, {}
  56.     for KEY,VALUE in function() return string.find(str,' ',pos,false) end do
  57.         local X = string.sub(str, pos, KEY-1)
  58.         if(tonumber(X)) then
  59.             X = tonumber(X)
  60.         else
  61.             X = X:gsub('("[^"]*")', function(a) return a:gsub('%%20', " ") end):gsub('"', "")
  62.         end
  63.         table.insert(arr, X)
  64.         pos = VALUE + 1
  65.     end
  66.     local X = string.sub(str,pos)
  67.     if(tonumber(X)) then
  68.         X = tonumber(X)
  69.     else
  70.         X = X:gsub('("[^"]*")', function(a) return a:gsub('%%20', " ") end):gsub('"', "")
  71.     end
  72.     table.insert(arr, X)
  73.     return arr
  74. end
  75.  
  76. local function ON_CHAT(event, pPlayer, pMessage, pType, pLanguage, pMisc)
  77.     if(pPlayer:IsGm() and pMessage:find("[.!]%a+") == 1) then
  78.         local MSG = explode(pMessage:gsub('("[^"]*")', function(a) return a:gsub(' ', "%%20") end))
  79.         if(MSG[1]:len() < 4) then -- Just for caution. We dont want to override every single existing short command. Remove this IF statement if you like.
  80.             return
  81.         end
  82.         MSG[1] = MSG[1]:lower()
  83.         local CMDp
  84.         if(T[MSG[1]]) then
  85.             CMDp = MSG[1]
  86.         else
  87.             for command,_ in pairs(T) do
  88.                 if(command:find(MSG[1]) == 1) then
  89.                     CMDp = command
  90.                     break
  91.                 end
  92.             end
  93.         end
  94.         if(CMDp) then
  95.             if(T[CMDp][3] and T[CMDp][3] ~= '' and not pPlayer:CanUseCommand(T[CMDp][3])) then
  96.                 return
  97.             end
  98.             local PASS = true
  99.             for k,v in pairs(T[CMDp][2]) do
  100.                 if(not MSG[k+1] or type(MSG[k+1]) ~= v) then
  101.                     if(T[CMDp][4] and T[CMDp][4] ~= "") then
  102.                         pPlayer:SendBroadcastMessage(T[CMDp][4])
  103.                     else
  104.                         pPlayer:SendBroadcastMessage("Usage: ."..CMDp.." "..(table.concat(T[CMDp][2], " ")))
  105.                     end
  106.                     return false
  107.                 end
  108.             end
  109.             T[CMDp][1](event, pPlayer, pMessage, pType, pLanguage, pMisc, MSG)
  110.             return false
  111.         end
  112.     end
  113. end
  114.  
  115. RegisterServerHook(16, ON_CHAT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement