Advertisement
Guest User

Untitled

a guest
Apr 6th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.10 KB | None | 0 0
  1. local component = require("component")
  2. local colors = require("colors")
  3. local sides = require("sides")
  4. local rs = component.redstone
  5.  
  6. local messages = {
  7.   ["lights"] = {
  8.     --Syntax: ["keyword"] = {function, all, other, arguments}
  9.     ["on"] = {rs.setBundledOutput, sides.back, colors.cyan, 15},
  10.     ["off"] = {rs.setBundledOutput, sides.back, colors.cyan, 0}
  11.   },
  12.   ["door"] = {
  13.     ["open"] = {rs.setBundledOutput, sides.back, colors.magenta, 15},
  14.     ["close"] = {rs.setBundledOutput, sides.back, colors.magenta, 0}
  15.   }
  16. }
  17.  
  18. local function getFunction(tbl, msg)
  19.   if tbl[1] and type(tbl[1]) ~= "table" then
  20.     return j
  21.   end
  22.   for i,j in pairs(tbl) do
  23.     if type(i) == "string" then
  24.       if msg:find(i) and type(j) == "table" then
  25.         return getFunction(j, msg)
  26.       end
  27.     end
  28.   end
  29. end
  30.  
  31. local function parseMessage(msg)
  32.   local fnc = getFunction(messages, msg)
  33.   if not fnc then return false, "no command found" end
  34.   return pcall(table.unpack(fnc))
  35. end
  36.  
  37. local testmsg = "Please turn the lights on"
  38. local result, response = parseMessage(testmsg)
  39. print(result)
  40. print(response)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement