SHARE
TWEET

CS2D Simple Command Processor

VaiN474 Oct 27th, 2015 (edited) 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. proc = {}
  2. commands = {}
  3.  
  4. -- simple example of a command definition
  5. -- these could be defined throughout your admin scripts
  6. commands["!broadcast"] = {
  7.     enabled = true,
  8.     permission = "any", -- handle this however you like
  9.     arguments = 1,
  10.     syntax = "<Message>",
  11.     func = function(id,arguments)
  12.         local message = arguments[1]
  13.         if not message then return false end
  14.         -- example of a good use for string.sub()
  15.         -- prevent @C at end of message, showing message centered
  16.         if message:sub(-2) == "@C" then message = message:sub(1, string.len(message) - 2) end
  17.         msg(player(id,"name") .. " (broadcast): " .. message)
  18.     end
  19. }
  20.  
  21. -- check if the player has used a chat command
  22. function proc.check(id,txt)
  23.     -- gets the first word if it starts with a single "!" or "@" with optional space
  24.     local cmd = txt:match("^([!|@][%w]+)[%s]?")
  25.     if not cmd then return 0 end
  26.  
  27.     -- check if the command exists
  28.     if commands[cmd] == nil then
  29.         msg2(id,"Command not found")
  30.         return 1
  31.     end
  32.  
  33.     -- get everything after the first space
  34.     local aftercmd = txt:match("[%s](.*)")
  35.  
  36.     -- send to command processor
  37.     proc.process(id,cmd,aftercmd)
  38.  
  39.     return 1 -- don't output command to chat
  40. end
  41.  
  42. function proc.process(id,cmd,txt)
  43.     -- check if command is enabled
  44.     if commands[cmd].enabled == false then
  45.         msg2(id,"Command Disabled")
  46.         return
  47.     end
  48.  
  49.     -- check permissions here
  50.  
  51.     -- get command arguments, we know how many there should be
  52.     -- from the command definition
  53.     local arg_count = commands[cmd].arguments
  54.     local arguments = {}
  55.     if arg_count > 0 then
  56.         if not txt then -- argument(s) are missing, show how it should be
  57.             msg2(id, "Wrong Syntax")
  58.             msg2(id, "Syntax: " .. cmd .. " " .. commands[cmd].syntax)
  59.             return
  60.         end
  61.  
  62.         local count = 0
  63.         for word in txt:gmatch("[^%s]+") do -- loop through each word
  64.             count = count + 1
  65.             if count <= arg_count then -- argument expected, let's cache it
  66.                 table.insert(arguments, word)
  67.             else
  68.                 -- no more arguments expected
  69.                 -- let's just add this word to the last argument
  70.                 arguments[#arguments] = arguments[#arguments] .. " " .. word
  71.             end
  72.         end
  73.  
  74.         if count < arg_count then -- not enough arguments for this command
  75.             msg2(id, "Wrong Syntax")
  76.             msg2(id, "Syntax: " .. cmd .. " " .. commands[cmd].syntax)
  77.             return
  78.         end
  79.  
  80.     -- we didn't expect any arguments but something was included
  81.     -- we'll pass it all as one argument
  82.     elseif arg_count <= 0 and txt ~= nil and txt ~= " " then
  83.         arguments = {txt}
  84.     end
  85.  
  86.     -- let's run the function for this command and pass the args
  87.     local ret
  88.     if #arguments > 0 then
  89.         ret = commands[cmd].func(id,arguments)
  90.     else
  91.         ret = commands[cmd].func(id)
  92.     end
  93.  
  94.     -- check the return value from the command function
  95.     if ret ~= nil then
  96.         -- something was returned, check for error
  97.         -- you can do this however you like, it's just a bool in this example
  98.         if ret == false then
  99.             msg2(id,"You fail.")
  100.             return
  101.         end
  102.     end
  103.  
  104.     -- command processed successfully, log it
  105.     print("*** Command Used by " .. player(id,"name") .. "\n*** CMD: " .. cmd .. (txt and " " .. txt or ""))
  106.  
  107. end
  108.  
  109. -- of course, to get it started, we need to add a hook
  110. addhook("say","proc.say")
  111. function proc.say(id,txt)
  112.     local ret = proc.check(id,txt)
  113.     if ret ~= nil then return ret end
  114. end
  115.  
  116. msg("Example Command Processor by VaiN")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top