Advertisement
killer64

server interface

Dec 11th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.34 KB | None | 0 0
  1. local secret="-snip-"
  2. local wi=infutil.wrap("webinterface")
  3. local ami=infutil.wrap("adventure map interface")
  4. local sc
  5. local function connect()
  6.     if sc then
  7.         wi.socket_close(sc)
  8.         sc=nil
  9.     end
  10.     local e
  11.     while not sc do
  12.         e,sc=pcall(wi.socket_create,"localhost",1338)
  13.         if not e then
  14.             sc=nil
  15.         end
  16.         if not sc then
  17.             sleep(5)
  18.         end
  19.     end
  20.     wi.socket_setAutoFlush(sc,true)
  21.     wi.socket_listen(sc)
  22.     wi.socket_write(sc,secret.."\n")
  23. end
  24. connect()
  25. local function tell(txt,plr)
  26.     if plr then
  27.         ami.getPlayerByName(plr).sendChat(txt)
  28.     else
  29.         for k,v in pairs(ami.getPlayerUsernames()) do
  30.             ami.getPlayerByName(v).sendChat(txt)
  31.         end
  32.     end
  33. end
  34. while true do
  35.     local p={os.pullEvent()}
  36.     if p[3]==sc then
  37.         if p[1]=="socket_connectionClosed" then
  38.             connect()
  39.         elseif p[1]=="socket_line" then
  40.             local dat=pt.d(p[4])
  41.             if dat[1]=="say" then
  42.                 tell(unpack(dat,2))
  43.             elseif dat[1]=="lua" then
  44.                 local e,r=loadstring(dat[2])
  45.                 if not e then
  46.                     wi.socket_write(sc,pt.c("lua_resp",false,r))
  47.                 else
  48.                     wi.socket_write(sc,pt.c("lua_resp",true,pcall(r)))
  49.                 end
  50.             end
  51.         end
  52.     end
  53.     if p[1]=="chat_message" or p[1]=="player_login" or p[1]=="player_logout" or p[1]=="player_respawn" or p[1]=="player_change_world" or p[1]=="chat_death" or p[1]=="modem_message" then
  54.         wi.socket_write(sc,pt.c("event",unpack(p)).."\n")
  55.     end
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement