Advertisement
Zekrommaster110

[LUA | CC1.4.7] CC Remote Tool v2 - CLIENT

Sep 18th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. -- CC REMOTE TOOL v2 - CLIENT
  2.  
  3. -- Copyright 2018 zekro Development (Ringo Hoffmann)
  4.  
  5. -- Permission is hereby granted, free of charge, to any person obtaining
  6. -- a copy of this software and associated documentation files (the "Software"),
  7. -- to deal in the Software without restriction, including without limitation
  8. -- the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. -- and/or sell copies of the Software, and to permit persons to whom the Software
  10. -- is furnished to do so, subject to the following conditions:
  11.  
  12. -- The above copyright notice and this permission notice shall be included in all
  13. -- copies or substantial portions of the Software.
  14.  
  15. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. -- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. -- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21.  
  22. ---------------------------------- CONSTS ----------------------------------
  23. SIDES      = {"top", "bottom", "front", "back", "left", "right"}
  24. STATUSFILE = "status.dat"
  25. VERSION    = "C2.1.0"
  26. ----------------------------------------------------------------------------
  27.  
  28. function fatal(error)
  29.     print("[ERROR] ", error)
  30.     exit()
  31. end
  32.  
  33. function mountModem()
  34.     for k, v in pairs(SIDES) do
  35.         if pcall(rednet.open, v) then
  36.             return true
  37.         end
  38.     end
  39.     return false
  40. end
  41.  
  42. function writeFile(filename, table)
  43.     local file = fs.open(filename, "w")
  44.     file.write(textutils.serialize(table))
  45.     file.close()
  46. end
  47.  
  48. function readFile(filename)
  49.     local file = fs.open(filename, "r")
  50.     if file ~= nil then
  51.         local table = textutils.unserialize(file.readAll())
  52.         file.close()
  53.         return table
  54.     end
  55.     return nil
  56. end
  57.  
  58. function tobool(n)
  59.     return n == 1
  60. end
  61.  
  62. ----------------------------------- MAIN -----------------------------------
  63.  
  64. local rsout, serverid = ...
  65.  
  66. -- print(rand())
  67. -- exit()
  68.  
  69. if rsout == nil then
  70.     fatal("No redstone output side defined as start argument.")
  71. end
  72.  
  73. if serverid == nil then
  74.     fatal("No server id defined as second start argument.")
  75. end
  76. serverid = tonumber(serverid)
  77.  
  78. print(
  79.     "--------------------\n",
  80.     "CC REMOTE TOOL\n",
  81.     "(c) zekro 2018\n",
  82.     "version ", VERSION, "\n",
  83.     "--------------------\n"
  84. )
  85.  
  86. if mountModem() then
  87.     print("Modem mounted.")
  88. else
  89.     fatal("Failed to mount modem.")
  90. end
  91.  
  92. status = readFile(STATUSFILE)
  93.  
  94. if status == nil then
  95.     status = 0
  96. end
  97.  
  98. print("Set saved status: " .. tostring(status))
  99. redstone.setOutput(rsout, tobool(status))
  100.  
  101. print("Waiting for 3 seconds...")
  102. os.sleep(3)
  103.  
  104. print("Sending status message to server...")
  105. rednet.send(serverid, status)
  106.  
  107. print("Listening for events...")
  108. while true do
  109.     id, message = rednet.receive()
  110.     if id == serverid then
  111.         print("[ SERVER ] " .. tostring(message))
  112.         redstone.setOutput(rsout, tobool(message))
  113.         writeFile(STATUSFILE, message)
  114.     end
  115. end
  116.  
  117. ----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement