Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- http://pastebin.com/1yDdHUvM
- os.loadAPI("ini")
- local modLIP_ServerPatchedVersion = ini.serverPatchedVersion
- local reloadUsersTimeout = ini.userlistTimeout
- local rsSide = ini.rsOutput
- local driveUser = peripheral.wrap(ini.driveUser)
- local modem = peripheral.wrap(ini.wirelessModem)
- local wi = peripheral.find("WorldInterface")
- local ed = peripheral.find("EntityDetector")
- local usersAPI = "/"..driveUser.getMountPath().."/users"
- if not os.loadAPI(usersAPI) then
- error("Users API not found",0)
- return
- end
- local _setCursorPos = term.setCursorPos
- local _clear,_clearLine = term.clear,term.clearLine
- local _write = term.write
- local _json = textutils.serializeJSON
- local _startTimer = os.startTimer
- local _pullEventRaw = os.pullEventRaw
- local _loadAPI,_unloadAPI = os.loadAPI,os.unloadAPI
- local _clock = os.clock
- local _floor = math.floor
- local _format = string.format
- if not (modem and wi and ed) then
- _setCursorPos(1,1)
- _clear()
- print(_format([[modem: %s
- WorldInterface: %s
- EntityDetector: %s]],
- modem and "ok" or "missing",
- wi and "ok" or "missing",
- ed and "ok" or "missing")
- )
- error("Missing Peripheral",0)
- return
- end
- local w,h = term.getSize()
- local function output(line,str)
- _setCursorPos(1,line)
- _clearLine()
- _write(str)
- end
- local function strTime(sec)
- local str,d,h,m,s="",_floor(sec/86400),_floor(sec/3600)%24,_floor(sec/60)%60,_floor(sec%60)
- if d>0 then str = d>1 and d.." days " or "1 day " end
- if h>0 then str = str..h.."h" end
- if m>0 then str = str..(m>9 and m or "0"..m) end
- if h==0 then str = m>0 and str..":"..(s>9 and s or "0"..s) or s.."s" end
- return str
- end
- local function _(query)
- local b,r = false,"405 Method Not Allowed"
- if #query>=2 and query[1]=="WorldInterface" then
- if query[2]=="getBiome" then
- if #query>=5 and tonumber(query[3]) and tonumber(query[4]) and tonumber(query[5]) then
- b = true
- r = wi.getBiome(tonumber(query[3]),tonumber(query[4]),tonumber(query[5]))
- else
- b = false
- r = [[WorldInterface.getBiome(X,Y,Z)]]
- end
- elseif query[2]=="getWeather" then
- b = true
- r = wi.getWeather()
- elseif query[2]=="getBlockInfos" then
- if #query>=5 and tonumber(query[3]) and tonumber(query[4]) and tonumber(query[5]) then
- b = true
- r = wi.getBlockInfos(tonumber(query[3]),tonumber(query[4]),tonumber(query[5]))
- else
- b = false
- r = [[WorldInterface.getBlockInfos(X,Y,Z)]]
- end
- elseif modLIP_ServerPatchedVersion and query[2]=="getBlockDatatags" then
- if #query>=5 and tonumber(query[3]) and tonumber(query[4]) and tonumber(query[5]) then
- b = true
- r = wi.getBlockDatatags(tonumber(query[3]),tonumber(query[4]),tonumber(query[5]))
- else
- b = false
- r = [[WorldInterface.getBlockDatatags(X,Y,Z)]]
- end
- elseif query[2]=="getRealDate" then
- b = true
- r = wi.getRealDate()
- elseif query[2]=="getPlayerList" then
- b = true
- r = wi.getPlayerList()
- end
- elseif #query>=2 and query[1]=="EntityDetector" then
- if query[2]=="getEntityList" then
- if #query>=6 and tonumber(query[3]) and tonumber(query[4]) and tonumber(query[5]) and tonumber(query[6]) then
- b = true
- r = ed.getEntityList(tonumber(query[3]),tonumber(query[4]),tonumber(query[5]),tonumber(query[6]))
- else
- b = false
- r = [[EntityDetector.getEntityList(Range,X,Y,Z)]]
- end
- elseif modLIP_ServerPatchedVersion and query[2]=="getEntityListAdvanced" then
- if #query>=6 and tonumber(query[3]) and tonumber(query[4]) and tonumber(query[5]) and tonumber(query[6]) then
- b = true
- r = ed.getEntityListAdvanced(tonumber(query[3]),tonumber(query[4]),tonumber(query[5]),tonumber(query[6]))
- else
- b = false
- r = [[EntityDetector.getEntityListAdvanced(Range,X,Y,Z)]]
- end
- elseif query[2]=="getPlayerDetail" then
- if #query>=3 then
- b = true
- r = ed.getPlayerDetail(tostring(query[3]))
- else
- b = false
- r = [[EntityDetector.getPlayerDetail(PlayerName)]]
- end
- end
- end
- return b and r, r
- end
- local function terminate()
- _setCursorPos(1,1)
- _clear()
- print("/server - "..string.char(169).." SukaiPoppuGo")
- error("Terminated",0)
- end
- local function process(from, query)
- local _key = query[1]
- local valid = type(users.key[_key])~="nil" and users.key[_key]
- query = query[2]
- if valid then
- local b,r
- if type(query)=="table" and #query>=1 then
- b,r = _(query)
- end
- modem.transmit(from,os.getComputerID(),{b,r})
- end
- output(4,(valid and "200" or "403").." "..tostring(_key))
- output(5,_format("to: %s, from: %s, dist: %s",tostring(to), tostring(from), tostring(dist)))
- output(6,_json(query))
- end
- modem.open(os.getComputerID())
- _setCursorPos(1,1)
- _clear()
- output(1,"#"..os.getComputerID().." - "..os.getComputerLabel())
- output(3,string.rep(string.char(140),w))
- local reloadUsers = _startTimer(reloadUsersTimeout)
- local t = _startTimer(5)
- while true do
- output(2,"Running: "..strTime(_clock()))
- local e, param, to, from, query, dist = _pullEventRaw()
- if e=="terminate" then
- terminate()
- return
- elseif e=="timer" and param==reloadUsers then
- _unloadAPI(usersAPI) _loadAPI(usersAPI)
- reloadUsers = _startTimer(reloadUsersTimeout)
- elseif e=="timer" and param==t then
- t = _startTimer(5)
- --sleep(.1)
- elseif e=="modem_message" and type(query)=="table" and #query==2 then
- process(from, query)
- end
- end
Add Comment
Please, Sign In to add comment