Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function tablelength(table)
- if table == null then return 0 end
- -- Count the amount of keys in the table and return that number.
- local count = 0
- for key in pairs(table) do count = count + 1 end
- return count
- end
- function checkSignal()
- remote = redstone.getInput("back")
- if remote == true then
- signal = true
- end
- end
- function log(level, msg)
- if level == null then return false end
- if msg == null then return false end
- -- Allow the user to customize which log level to display and which one to hide.
- if string.upper(level) == "INFO" then
- if return_setting("info") then
- print(string.format("[%s] %s", string.upper(level), tostring(msg)))
- else
- -- The user manually edited the "info" key and we want them to be notified of it.
- if criticalDisplayed == null then
- print(string.format("[%s] %s", string.upper("Note"), "\"[INFO]\" messages are now hidden."))
- criticalDisplayed = true
- end
- end
- elseif string.upper(level) == "WARNING" then
- if return_setting("warning") then print(string.format("[%s] %s", string.upper(level), tostring(msg))) end
- elseif string.upper(level) == "ERROR" then
- if return_setting("error") then print(string.format("[%s] %s", string.upper(level), tostring(msg))) end
- elseif string.upper(level) == "DEBUG" then
- if return_setting("debug") then print(string.format("[%s] %s", string.upper(level), tostring(msg))) end
- end
- return false
- end
- function load_images()
- if fs.exists("dialImage") ~= true then
- log("Info", "Downloading images...")
- shell.run("pastebin get XR8h7Wmm dialImage")
- log("Info", "Images downloaded.")
- else
- log("Info", "Images already downloaded. Skipping...")
- end
- end
- function edit_setting(key, value)
- if key == null then return false end
- if value == null then return false end
- if settings == null then settings = {} end
- settings[key] = value
- end
- function return_setting(key)
- if key == null then return false end
- if settings == null then return false end
- if not validate_setting(key) then return false end
- return settings[key]
- end
- function remove_setting(key)
- if key == null then return false end
- if settings == null then return false end
- settings[key] = null
- end
- function save_settings(filename)
- if filename == null then return false end
- if settings == null then return false end
- if fs.exists(filename) then fs.delete(filename) end
- if filecontent ~= null then filecontent = null end
- local filecontent = textutils.serialize(settings)
- local file = fs.open(filename, "w")
- file.write(filecontent)
- file.close()
- return true
- end
- function load_settings(filename)
- if filename == null then return false end
- if not fs.exists(filename) then return false end
- settings = null
- local file = fs.open(filename, "r")
- settings = textutils.unserialize(file.readAll())
- file.close()
- -- Fallback in case the user wrongly edited the file and it detects no settings.
- if settings == null then
- edit_setting("info", true)
- edit_setting("warning", true)
- edit_setting("error", true)
- edit_setting("debug", false)
- log("Error", string.format("%q is corrupt.", filename))
- log("Warning", string.format("Deleting %q...", filename))
- fs.delete(filename)
- log("Info", "Loading default settings...")
- end
- return true
- end
- function validate_setting(key)
- if key == null then return false end
- if settings == null then return false end
- -- Only 4 different Sundial modes are supported.
- if key == "mode" then
- if settings[key] == 1 or settings[key] == 2 or settings[key] == 3 or settings[key] == 4 then
- return true
- else
- return false
- end
- -- Check if these keys are booleans.
- elseif key == "info" or key == "warning" or key == "error" or key == "debug" then
- if settings[key] == true or settings[key] == false then
- return true
- else
- return false
- end
- else
- return false
- end
- end
- function get_mode_name(value)
- if value == 1 then
- return "Sunrise"
- elseif value == 2 then
- return "Always Day"
- elseif value == 3 then
- return "Sunset"
- elseif value == 4 then
- return "Always Night"
- else
- return "Unknown"
- end
- end
- -- Locally (and temporarily) set log level defaults.
- -- If the settings exist in the file, the file's settings will be used instead.
- edit_setting("info", true)
- edit_setting("warning", true)
- edit_setting("error", true)
- edit_setting("debug", false)
- log("Info", "Hello, Minecraft! Program started.")
- if fs.exists("savedSettings") then
- log("Info", "Loading settings...")
- if load_settings("savedSettings") then
- if tablelength(settings) == 1 then
- log("Info", string.format("%i setting loaded.", tablelength(settings)))
- else
- log("Info", string.format("%i settings loaded.", tablelength(settings)))
- end
- log("Debug", "User enabled debug mode.")
- for key in pairs(settings) do
- log("Debug", string.format("Setting %q has value %q.", tostring(key), tostring(return_setting(key))))
- end
- else
- log("Warning", "Couldn't load settings.")
- end
- else
- log("Info", "No settings file present. Skipping...")
- end
- load_images()
- mon=peripheral.find("monitor")
- red = "top"
- x,y = mon.getSize()
- term.redirect(mon)
- image = paintutils.loadImage("dialImage")
- paintutils.drawImage(image,1,1)
- term.redirect(term.native())
- while true do
- os.startTimer(3)
- event, param1, param2, param3 = os.pullEvent()
- if event == "timer" then
- sleep(0.05)
- elseif event == "monitor_touch" then
- if param2 < x/2 and param3 < y/2 then
- edit_setting("mode", 1)
- elseif param2 > x/2 and param2 < x/2*2 and param3 < y/2 then
- edit_setting("mode", 2)
- elseif param2 < x/2 and param3 > y/2 then
- edit_setting("mode", 3)
- elseif param2 > x/2 and param2 < x/2*2 and param3 > y/2 then
- edit_setting("mode", 4)
- end
- log("Info", string.format("Mode %q selected.", get_mode_name(return_setting("mode"))))
- log("Info", "Saving settings...")
- if save_settings("savedSettings") then
- if tablelength(settings) == 1 then
- log("Info", string.format("%i setting saved.", tablelength(settings)))
- else
- log("Info", string.format("%i settings saved.", tablelength(settings)))
- end
- for key in pairs(settings) do
- log("Debug", string.format("Setting %q has value %q.", tostring(key), tostring(return_setting(key))))
- end
- else
- log("Error", "Couldn't save settings.")
- end
- end
- checkSignal()
- --rs.setOutput(red,false)
- if return_setting("mode") == 1 then
- if signal == true then
- if os.time() > 6 or os.time() < 5 then
- rs.setOutput(red, true)
- else
- rs.setOutput(red,false)
- signal = false
- edit_setting("mode", 2)
- end
- end
- elseif return_setting("mode") == 2 then
- if signal == true then
- if os.time() > 17 or os.time() < 6 then
- rs.setOutput(red, true)
- else
- rs.setOutput(red,false)
- signal = false
- edit_setting("mode", 3)
- end
- end
- elseif return_setting("mode") == 3 then
- if signal == true then
- if os.time() > 19 or os.time() < 18 then
- rs.setOutput(red, true)
- else
- rs.setOutput(red,false)
- signal = false
- edit_setting("mode", 4)
- end
- end
- elseif return_setting("mode") == 4 then
- if signal == true then
- if os.time() < 19 and os.time() > 5 then
- rs.setOutput(red, true)
- else
- rs.setOutput(red,false)
- signal = false
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement