Advertisement
Arc13

Pocket Peripherals

Oct 26th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.93 KB | None | 0 0
  1. if not fs.exists("/crypt_wper") then
  2.   local httpPastebin = http.get("http://pastebin.com/raw/afktfgQ0")
  3.   local httpPastebinData = httpPastebin.readAll()
  4.   httpPastebin.close()
  5.  
  6.   local fileData = fs.open("/crypt_wper", "w")
  7.   fileData.write(httpPastebinData)
  8.   fileData.close()
  9. end
  10.  
  11. if not pocket then
  12.   error("This API is made for Wireless Pocket Computer")
  13. end
  14.  
  15. if not peripheral.find("modem") then
  16.   error("This API require a wireless modem")
  17. end
  18.  
  19. os.loadAPI("crypt_wper")
  20.  
  21. local sSendKey = "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlHZk1BMEdDU3FHU0liM0RRRUJBUVVBQTRHTkFEQ0JpUUtCZ1FEckJlOHU1K1B5MVVKdDRka3JRdnBicEM0aAp1dG01OURJeDNETGxHM3ZPSGpTY1RaYXdZVXhsQ1lsTjRpd2k1eGpmS1NXU1Y4YlFZSHJYMVJVWWN3VXdXdlZBCklzak9remthZmtkYml5NjU0bWdad1R2SFFIUXhqODZCZVhUU0JGNmt6L3M2RSsyTGlXM0JRSnRKVHpPdjUwQWcKdVEzclA4Mnhkb2Z1QytYRjJRSURBUUFCCi0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQo"
  22. local sReceiveKey = "LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JSUNkZ0lCQURBTkJna3Foa2lHOXcwQkFRRUZBQVNDQW1Bd2dnSmNBZ0VBQW9HQkFLeGxMbXh5MitkRW1wVXEKVVo1eFN2cUFzeEtmdk9MLzZvWUJqQ2tMdjdUNFRacnZZRExxYUFCcDZaWWVKenFOZTZNRGxUd0xnbnNRbVJxegpYQXBqVGZ4TURLaFlLWkhoTStwRzZ2NWJHdE5iR3E0cllodDFMR05nR3VNZVZjS3pkd0JJaFpOdE5LWlBGK2RVCm1zVGJyQWtRRWx5V05ZVENzQ3phUjVDTEpFME5BZ01CQUFFQ2dZQmNKWUVZNnBQc0c5bGNuZEFlbG9QelBkTjUKd3ZRZGlaajdvOXdkSEhDTjlLTFdpOWlwbm0yVXV3ZmwrNXdRU2d1cDZJSmVJL0RNZ3VTelJ1ZU5qZUxpZGRiMwpLQWE4c0tYS0wvaGIvUUlNMFVWeHMyc0MwUHVMQ2VMenQ5RlR3ZUtxbVdGQkE4NThhMW16RHk0eVhENjIvbWtLCllwZ2I5TlA1bzc5Z201V1dnUUpCQU5LVGRZbDg5WmZqV2txMWxFT3BRaWRyd0psMnlLaS9ZdEZuZTNnUDExS1oKS2d4TWNDeS9xSFpWTysxUS9mbHdGdzZJTlBsMlNKZ201bUNwQWNMai9wRUNRUURSbFVpQlFQekJ1NWJ4dGRpTQp0VFltSGlKbWs5U0FXTDZDL2xDall0a0RBNHJ1M0k2S1dFRTZWaFYrc2FSUVJUOG42QkpWZ1RyUWk3WjNHUlRaCklaeTlBa0JCKytNQXloOUxFUHJpSGpQdlg4SlRCL2k2SGZhU3FpWVU2QkJRZHVaS0E2U2haZkpnazAzK2hvYlkKdm1CWElVQ1lWU1lFWllRcDZVYVhVMTR4NjYrQkFrQWk2eE5jYlo1dkRMa1dabXpBN01Rcno2TjlvYTJyQUo0bApKd2Y2WU5LMVBOYmQreU5JaTNHNnJYTUJXOGw3a2ozMmdTVGsyQlliZnR1VXRqb1IveGFsQWtFQXU3eUpKR1NzClI2djRKWWtlVlFEYTEzUVQxaDcwNytYK1RFYUNwTm1mSllUL0g4ZlZOZjZFUDRYbXFNQ0wvbEh5MW5VT3cydDMKZ3VUR1V2TC8zR2F6VVE9PQotLS0tLUVORCBQUklWQVRFIEtFWS0tLS0tCg"
  23.  
  24. worldInterface = {}
  25. entityDetector = {}
  26. chatInterface = {}
  27.  
  28. local function sendMessage(sPeripheral, sMethod, tArgs)
  29.   local m = peripheral.find("modem")
  30.   tArgs = tArgs or {}
  31.  
  32.   m.transmit(60300, os.getComputerID(), crypt_wper.encrypt(textutils.serialise({peripheral = sPeripheral, method = sMethod, arguments = tArgs}), sSendKey))
  33.  
  34.   return true
  35. end
  36.  
  37. local function receiveMessage()
  38.   local m = peripheral.find("modem")
  39.  
  40.   m.open(os.getComputerID())
  41.  
  42.   local nTimeout = os.startTimer(5)
  43.  
  44.   while true do
  45.     local event, p1, p2, p3, p4, p5 = os.pullEvent()
  46.  
  47.     if event == "modem_message" and p3 == 60300 then
  48.       m.close(os.getComputerID())
  49.       message = crypt_wper.decrypt(p4, sReceiveKey)
  50.       os.cancelTimer(nTimeout)
  51.  
  52.       if type(message) == "string" and message:sub(1, 12) == "serverError:" then
  53.         return false, message:sub(13, string.len(message))
  54.       else
  55.         return message
  56.       end
  57.     elseif event == "timer" and p1 == nTimeout then
  58.       return false, "Timeout error"
  59.     end
  60.   end
  61. end
  62.  
  63. function worldInterface.getBiome(...)
  64.   sendMessage("WorldInterface", "getBiome", {...})
  65.   return receiveMessage()
  66. end
  67.  
  68. function worldInterface.getWeather(...)
  69.   sendMessage("WorldInterface", "getWeather", {...})
  70.   return receiveMessage()
  71. end
  72.  
  73. function worldInterface.getBlockInfos(...)
  74.   if #{...} < 3 then
  75.     return false, "getBlockInfos(X,Y,Z)"
  76.   end
  77.  
  78.   sendMessage("WorldInterface", "getBlockInfos", {...})
  79.   return receiveMessage()
  80. end
  81.  
  82. function worldInterface.getBlockDatatags(...)
  83.   if #{...} < 3 then
  84.     return false, "getBlockDatatags(X,Y,Z)"
  85.   end
  86.  
  87.   sendMessage("WorldInterface", "getBlockDatatags", {...})
  88.   return receiveMessage()
  89. end
  90.  
  91. function worldInterface.getRealDate(...)
  92.   sendMessage("WorldInterface", "getRealDate", {...})
  93.   return receiveMessage()
  94. end
  95.  
  96. function worldInterface.getPlayerList(...)
  97.   sendMessage("WorldInterface", "getPlayerList", {...})
  98.   return receiveMessage()
  99. end
  100.  
  101. function worldInterface.getMethods(...)
  102.   return {"getBiome", "getWeather", "getBlockInfos", "getBlockDatatags", "getRealDate", "getPlayerList", "getMethods", "scanRegion", "getColor", "rawLaunch"}
  103. end
  104.  
  105. function worldInterface.scanRegion(...)
  106.   if #{...} < 6 then
  107.     return false, "scanRegion(X,Y,Z,dX,dY,dZ)"
  108.   end
  109.  
  110.   sendMessage("WorldInterface", "scanRegion", {...})
  111.   return receiveMessage()
  112. end
  113.  
  114. function worldInterface.getColor(...)
  115.   if #{...} < 3 then
  116.     return false, "getColor(X,Y,Z)"
  117.   end
  118.  
  119.   sendMessage("WorldInterface", "getColor", {...})
  120.   return receiveMessage()
  121. end
  122.  
  123. function worldInterface.rawLaunch(sMethod, ...)
  124.   if not sMethod then
  125.     return false, "rawLaunch(Method, ...)"
  126.   end
  127.  
  128.   sendMessage("WorldInterface", sMethod, {...})
  129.   return receiveMessage()
  130. end
  131.  
  132. function entityDetector.getEntityListAdvanced(...)
  133.   if #{...} < 4 then
  134.     return false, "getEntityListAdvanced(Range,[X],[Y],[Z])"
  135.   end
  136.  
  137.   sendMessage("EntityDetector", "getEntityListAdvanced", {...})
  138.   return receiveMessage()
  139. end
  140.  
  141. function entityDetector.getEntityList(...)
  142.   if #{...} < 4 then
  143.     return false, "getEntityList(Range,[X],[Y],[Z])"
  144.   end
  145.  
  146.   sendMessage("EntityDetector", "getEntityList", {...})
  147.   return receiveMessage()
  148. end
  149.  
  150. function entityDetector.getPlayerDetail(...)
  151.   if #{...} < 1 then
  152.     return false, "getPlayerDetail(PlayerName)"
  153.   end
  154.  
  155.   sendMessage("EntityDetector", "getPlayerDetail", {...})
  156.   return receiveMessage()
  157. end
  158.  
  159. function entityDetector.getMethods(...)
  160.   return {"getEntityListAdvanced", "getEntityList", "getPlayerDetail", "getMethods", "rawLaunch"}
  161. end
  162.  
  163. function entityDetector.rawLaunch(sMethod, ...)
  164.   if not sMethod then
  165.     return false, "rawLaunch(Method, ...)"
  166.   end
  167.  
  168.   sendMessage("EntityDetector", sMethod, {...})
  169.   return receiveMessage()
  170. end
  171.  
  172. function chatInterface.sendGlobalMessage(...)
  173.   if #{...} < 1 then
  174.     return false, "sendGlobalMessage(Message)"
  175.   end
  176.  
  177.   sendMessage("ChatInterface", "sendGlobalMessage", {...})
  178.   return receiveMessage()
  179. end
  180.  
  181. function chatInterface.sendPlayerMessage(...)
  182.   if #{...} < 2 then
  183.     return false, "sendPlayerMessage(Player, Message)"
  184.   end
  185.  
  186.   sendMessage("ChatInterface", "sendPlayerMessage", {...})
  187.   return receiveMessage()
  188. end
  189.  
  190. function chatInterface.setName(...)
  191.   if #{...} < 1 then
  192.     return false, "setName(Name)"
  193.   end
  194.  
  195.   sendMessage("ChatInterface", "setName", {...})
  196.   return receiveMessage()
  197. end
  198.  
  199. function chatInterface.getName(...)
  200.   sendMessage("ChatInterface", "getName", {...})
  201.   return receiveMessage()
  202. end
  203.  
  204. function chatInterface.getMethods(...)
  205.   return {"sendGlobalMessage", "sendPlayerMessage", "setName", "getName", "getMethods", "rawLaunch"}
  206. end
  207.  
  208. function chatInterface.rawLaunch(sMethod, ...)
  209.   if not sMethod then
  210.     return false, "rawLaunch(Method, ...)"
  211.   end
  212.  
  213.   sendMessage("ChatInterface", sMethod, {...})
  214.   return receiveMessage()
  215. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement