Advertisement
Guest User

startup

a guest
Aug 29th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.58 KB | None | 0 0
  1. function tablelength(table)
  2.   if table == null then return 0 end
  3.  
  4.   -- Count the amount of keys in the table and return that number.
  5.   local count = 0
  6.   for key in pairs(table) do count = count + 1 end
  7.   return count
  8. end
  9.  
  10. function checkSignal()
  11.  remote = redstone.getInput("back")
  12.  if remote == true then
  13.  signal = true
  14.  end
  15.  
  16. end
  17.  
  18.  
  19. function log(level, msg)
  20.   if level == null then return false end
  21.   if msg == null then return false end
  22.  
  23.   -- Allow the user to customize which log level to display and which one to hide.
  24.   if string.upper(level) == "INFO" then
  25.     if return_setting("info") then
  26.       print(string.format("[%s] %s", string.upper(level), tostring(msg)))
  27.     else
  28.       -- The user manually edited the "info" key and we want them to be notified of it.
  29.       if criticalDisplayed == null then
  30.         print(string.format("[%s] %s", string.upper("Note"), "\"[INFO]\" messages are now hidden."))
  31.         criticalDisplayed = true
  32.       end
  33.     end
  34.   elseif string.upper(level) == "WARNING" then
  35.     if return_setting("warning") then print(string.format("[%s] %s", string.upper(level), tostring(msg))) end
  36.   elseif string.upper(level) == "ERROR" then
  37.     if return_setting("error") then print(string.format("[%s] %s", string.upper(level), tostring(msg))) end
  38.   elseif string.upper(level) == "DEBUG" then
  39.     if return_setting("debug") then print(string.format("[%s] %s", string.upper(level), tostring(msg))) end
  40.   end
  41.  
  42.   return false
  43. end
  44.  
  45. function load_images()
  46.   if fs.exists("dialImage") ~= true then
  47.     log("Info", "Downloading images...")
  48.     shell.run("pastebin get XR8h7Wmm dialImage")
  49.     log("Info", "Images downloaded.")
  50.   else
  51.     log("Info", "Images already downloaded. Skipping...")
  52.   end
  53. end
  54.  
  55. function edit_setting(key, value)
  56.   if key == null then return false end
  57.   if value == null then return false end
  58.   if settings == null then settings = {} end
  59.  
  60.   settings[key] = value
  61. end
  62.  
  63. function return_setting(key)
  64.   if key == null then return false end
  65.   if settings == null then return false end
  66.   if not validate_setting(key) then return false end
  67.  
  68.   return settings[key]
  69. end
  70.  
  71. function remove_setting(key)
  72.   if key == null then return false end
  73.   if settings == null then return false end
  74.  
  75.   settings[key] = null
  76. end
  77.  
  78. function save_settings(filename)
  79.   if filename == null then return false end
  80.   if settings == null then return false end
  81.   if fs.exists(filename) then fs.delete(filename) end
  82.   if filecontent ~= null then filecontent = null end
  83.  
  84.   local filecontent = textutils.serialize(settings)
  85.   local file = fs.open(filename, "w")
  86.   file.write(filecontent)
  87.   file.close()
  88.  
  89.   return true
  90. end
  91.  
  92. function load_settings(filename)
  93.   if filename == null then return false end
  94.   if not fs.exists(filename) then return false end
  95.   settings = null
  96.  
  97.   local file = fs.open(filename, "r")
  98.   settings = textutils.unserialize(file.readAll())
  99.   file.close()
  100.  
  101.   -- Fallback in case the user wrongly edited the file and it detects no settings.
  102.   if settings == null then
  103.     edit_setting("info", true)
  104.     edit_setting("warning", true)
  105.     edit_setting("error", true)
  106.     edit_setting("debug", false)
  107.     log("Error", string.format("%q is corrupt.", filename))
  108.     log("Warning", string.format("Deleting %q...", filename))
  109.     fs.delete(filename)
  110.     log("Info", "Loading default settings...")
  111.   end
  112.  
  113.   return true
  114. end
  115.  
  116. function validate_setting(key)
  117.   if key == null then return false end
  118.   if settings == null then return false end
  119.  
  120.   -- Only 4 different Sundial modes are supported.
  121.   if key == "mode" then
  122.     if settings[key] == 1 or settings[key] == 2 or settings[key] == 3 or settings[key] == 4 then
  123.       return true
  124.     else
  125.       return false
  126.     end
  127.   -- Check if these keys are booleans.
  128.   elseif key == "info" or key == "warning" or key == "error" or key == "debug" then
  129.     if settings[key] == true or settings[key] == false then
  130.       return true
  131.     else
  132.       return false
  133.     end
  134.   else
  135.     return false
  136.   end
  137. end
  138.  
  139. function get_mode_name(value)
  140.   if value == 1 then
  141.     return "Sunrise"
  142.   elseif value == 2 then
  143.     return "Always Day"
  144.   elseif value == 3 then
  145.     return "Sunset"
  146.   elseif value == 4 then
  147.     return "Always Night"
  148.   else
  149.     return "Unknown"
  150.   end
  151. end
  152.  
  153. -- Locally (and temporarily) set log level defaults.
  154. -- If the settings exist in the file, the file's settings will be used instead.
  155. edit_setting("info", true)
  156. edit_setting("warning", true)
  157. edit_setting("error", true)
  158. edit_setting("debug", false)
  159.  
  160. log("Info", "Hello, Minecraft! Program started.")
  161.  
  162. if fs.exists("savedSettings") then
  163.   log("Info", "Loading settings...")
  164.   if load_settings("savedSettings") then
  165.     if tablelength(settings) == 1 then
  166.       log("Info", string.format("%i setting loaded.", tablelength(settings)))
  167.     else
  168.       log("Info", string.format("%i settings loaded.", tablelength(settings)))
  169.     end
  170.     log("Debug", "User enabled debug mode.")
  171.     for key in pairs(settings) do
  172.       log("Debug", string.format("Setting %q has value %q.", tostring(key), tostring(return_setting(key))))
  173.     end
  174.   else
  175.     log("Warning", "Couldn't load settings.")
  176.   end
  177. else
  178.   log("Info", "No settings file present. Skipping...")
  179. end
  180.  
  181. load_images()
  182. mon=peripheral.find("monitor")
  183. red = "top"
  184. x,y = mon.getSize()
  185. term.redirect(mon)
  186. image = paintutils.loadImage("dialImage")
  187. paintutils.drawImage(image,1,1)
  188. term.redirect(term.native())
  189.  
  190. while true do
  191.   os.startTimer(3)
  192.   event, param1, param2, param3 = os.pullEvent()
  193.   if event == "timer" then
  194.     sleep(0.05)
  195.   elseif event == "monitor_touch" then
  196.     if param2 < x/2 and param3 < y/2 then
  197.       edit_setting("mode", 1)
  198.     elseif param2 > x/2 and param2 < x/2*2 and param3 < y/2 then
  199.       edit_setting("mode", 2)
  200.     elseif param2 < x/2 and param3 > y/2 then
  201.       edit_setting("mode", 3)
  202.     elseif param2 > x/2 and param2 < x/2*2 and param3 > y/2 then
  203.       edit_setting("mode", 4)
  204.     end
  205.  
  206.     log("Info", string.format("Mode %q selected.", get_mode_name(return_setting("mode"))))
  207.  
  208.     log("Info", "Saving settings...")
  209.     if save_settings("savedSettings") then
  210.       if tablelength(settings) == 1 then
  211.         log("Info", string.format("%i setting saved.", tablelength(settings)))
  212.       else
  213.         log("Info", string.format("%i settings saved.", tablelength(settings)))
  214.       end
  215.       for key in pairs(settings) do
  216.         log("Debug", string.format("Setting %q has value %q.", tostring(key), tostring(return_setting(key))))
  217.       end
  218.     else
  219.       log("Error", "Couldn't save settings.")
  220.     end
  221.  
  222.   end
  223.  
  224. checkSignal()
  225. --rs.setOutput(red,false)
  226.  
  227.     if return_setting("mode") == 1 then
  228.     if signal == true then
  229.     if os.time() > 6 or os.time() < 5 then
  230.      rs.setOutput(red, true)
  231.      else
  232.       rs.setOutput(red,false)
  233.       signal = false
  234.       edit_setting("mode", 2)
  235.     end
  236.     end
  237.   elseif return_setting("mode") == 2 then
  238.     if signal == true then
  239.     if os.time() > 17 or os.time() < 6 then
  240.      rs.setOutput(red, true)
  241.    else
  242.       rs.setOutput(red,false)
  243.       signal = false
  244.       edit_setting("mode", 3)
  245.       end
  246.       end
  247.   elseif return_setting("mode") == 3 then
  248.     if signal == true then
  249.     if os.time() > 19 or os.time() < 18 then
  250.       rs.setOutput(red, true)
  251.     else
  252.       rs.setOutput(red,false)
  253.       signal = false
  254.       edit_setting("mode", 4)
  255.     end
  256.     end
  257.   elseif return_setting("mode") == 4 then
  258.     if signal == true then
  259.     if os.time() < 19 and os.time() > 5 then
  260.       rs.setOutput(red, true)
  261.      else
  262.       rs.setOutput(red,false)
  263.       signal = false
  264.     end
  265.     end
  266.   end
  267. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement