Advertisement
NanoBob

Event system

Jul 18th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.70 KB | None | 0 0
  1. local events={
  2. }
  3.  
  4. function addEvent(name)
  5.    if events[name]==nil then
  6.       events[name]={
  7.       }
  8.    else
  9.       return false,"This event already exists."
  10.    end
  11. end
  12.  
  13. function addEventHandler(event,func)
  14.    if events[event]==nil then
  15.       events[event]={
  16.       }
  17.       --   return true,"Event did not yet exist, event was added"
  18.    end
  19.    events[event][#events[event]+1]={
  20.       ["func"]=func,
  21.    }
  22. end
  23.  
  24. function removeEventHandler(event,func)
  25.    if events[event]==nil then
  26.       return false,"There is no such event"
  27.    end
  28.    for id,data in pairs(events[event]) do
  29.       if data.func==func then
  30.          events[event][id]=nil
  31.          return true,"Event handler succesfully removed"
  32.       end
  33.    end
  34.    return false,("No handlers for that function")
  35. end
  36.  
  37. function removeEvent(event)
  38.     if events[event]==nil then
  39.         return false,"No such event exists."
  40.     end
  41.     events[evenf]=nil
  42.     return true,"Event (and it's handlers) succesfully removed"
  43. end
  44.  
  45. function triggerEvent(event, ... )
  46.     if events[event]==nil then
  47.         return false,"No such event exists"
  48.     end
  49.     for id, data in pairs(events[event]) do
  50.         data.func(...)
  51.     end
  52. end
  53.  
  54. local computerCraft={
  55.     "char","key","key_up","paste","timer","alarm","task_complete","redstone","terminate","disk","disk_eject","peripheral","peripheral_detach","rednet_message","modem_message","http_success","http_failure","mouse_click","mouse_up","mouse_scroll","mouse_drag","monitor_touch","monitor_resize","term_resize","turtle_inventory"
  56. }
  57.  
  58. function setupComputercraftEvents()
  59.     for _,event in ipairs(computerCraft) do
  60.         addEvent(event)
  61.     end
  62. end
  63.  
  64. function handleCCEvents()
  65.     local event,a1,a2,a3,a4,a5=os.pullEvent()
  66.     triggerEvent(event,a1,a2,a3,a4,a5)
  67. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement