Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local api = require ("graph_api")
- local config = require ("config_api")
- -- Configuration Setup
- CONFIG = Config:new("farm_client_config")
- CONFIG:fetchFromFile()
- if CONFIG:size() ~= 4 then
- CONFIG.properties = {}
- CONFIG.properties["DEFAULT_TOOLTIP_TEXT"] = "Select where your Gearshift is located to pass redstone signal to it; When you are ready, hit TOGGLE button"
- CONFIG.properties["CYCLE_TIMER"] = 60
- CONFIG.properties["AUTO_TOGGLE"] = false
- CONFIG.properties["REDSTONE_OUTPUT"] = "NONE"
- CONFIG:updateFile()
- end
- INFO_UPDATE_SECONDS_SLEEP = 0.5
- local UPDATE_TIMER = nil
- local root_width, root_height = term.getSize()
- local root_terminal = term.current()
- local root_window = window.create(root_terminal, 1, 1, root_width, root_height)
- root_window.setBackgroundColor(colors.cyan)
- root_window.setTextColor(colors.green)
- root_window.clear()
- term.redirect(root_window)
- --------------
- -----CODE-----
- --------------
- local cycle_metadata = {status="IDLE", redstone_output="NONE", alarms={}, alarm_tokens={}, timers={}, last_harvest=0}
- -- Event handlers
- function redstoneOutputEventHandler(raw_event)
- local event, previous_tag, tag, button = raw_event[1], raw_event[2], raw_event[3], raw_event[4]
- cycle_metadata["redstone_output"] = tag
- CONFIG.properties["REDSTONE_OUTPUT"] = tag
- CONFIG:updateFile()
- end
- function alarmEventHandler(raw_event)
- local event, id = raw_event[1], raw_event[2]
- for index, alarm in ipairs(cycle_metadata["alarm_tokens"]) do
- if id == alarm then
- local current_time = math.floor(os.time())
- table.remove(cycle_metadata["alarms"], index)
- table.remove(cycle_metadata["alarm_tokens"], index)
- table.insert(cycle_metadata["alarms"], current_time)
- table.insert(cycle_metadata["alarm_tokens"], os.setAlarm(current_time))
- table.insert(cycle_metadata["timers"], os.startTimer(CONFIG.properties["CYCLE_TIMER"]))
- cycle_metadata["status"] = "RUNNING"
- cycle_metadata["last_harvest"] = os.epoch("utc") / 1000.0
- redstone.setOutput(cycle_metadata["redstone_output"], true)
- break
- end
- end
- end
- function bussinesTimerEventHandler(raw_event)
- local event, id = raw_event[1], raw_event[2]
- for _, timer in pairs(cycle_metadata["timers"]) do
- if id == timer then
- cycle_metadata["status"] = "WAITING"
- redstone.setOutput(cycle_metadata["redstone_output"], false)
- break
- end
- end
- if id == UPDATE_TIMER then
- updateStatus()
- return
- end
- end
- function bussinesEventHandler(event, eventData)
- if event == "redstone_output" then
- redstoneOutputEventHandler(eventData)
- else if event == "alarm" then
- alarmEventHandler(eventData)
- else if event == "timer" then
- bussinesTimerEventHandler(eventData)
- end
- end
- end
- end
- -- Redstone Output Selection Buttons
- local redstone_buttons = api.createDirectionButtons(root_width - 11, 2, 3, 2, colors.white, colors.red, "redstone_output", root_window)
- info_box = MutableTextArea:new(
- root_window,
- 3,
- 3,
- 34,
- 5,
- colors.lightGray,
- colors.white,
- true,
- colors.gray,
- CONFIG.properties["DEFAULT_TOOLTIP_TEXT"]
- )
- info_box:refresh()
- -- Info Box Update callback
- local alarm_cache = nil
- function ternary(o, t, f)
- if o then
- return t
- else
- return f
- end
- end
- function time24Wrapper(time, add)
- local t = time + add
- return ternary(t >= 24, t - 24, t)
- end
- function calculateDeltaTime(start, finish)
- result = finish - start
- return ternary(result < 0, result + 24, result)
- end
- function getNextAlarm(current_time)
- if (alarm_cache ~= nil) then
- return alarm_cache
- end
- delta, m = 0, 25
- for i, v in ipairs(cycle_metadata["alarms"]) do
- local d = calculateDeltaTime(current_time, v);
- if d < m then
- m = v
- delta = d
- end
- end
- alarm_cache = m
- return m
- end
- function updateStatus()
- current_time = os.time()
- info_box:setText("Status: " .. cycle_metadata["status"] .. "\n" .. "Current time: " .. textutils.formatTime(current_time, true) .. "\n")
- if (cycle_metadata["status"] == "WAITING") then
- alarm_cache = nil -- reset alarm cache
- info_box:setText(
- info_box.text ..
- "Time to the next harvest: " ..
- textutils.formatTime(
- calculateDeltaTime(current_time, getNextAlarm(current_time)),
- true
- )
- )
- else if (cycle_metadata["status"] == "RUNNING") then
- local t = math.ceil(cycle_metadata["last_harvest"] + CONFIG.properties["CYCLE_TIMER"] - (os.epoch("utc") / 1000.0))
- info_box:setText(
- "Harvesting... ~" .. ternary(t > 0, t, 0) .. " seconds left"
- )
- end
- end
- info_box:refresh()
- UPDATE_TIMER = os.startTimer(INFO_UPDATE_SECONDS_SLEEP)
- end
- -- Toggle Farm Button Action
- function toggleFarm(window, data)
- if data["redstone_output"] == "NONE" then
- info_box:setText("You have to select redstone output!")
- info_box:setTextColor(colors.red)
- info_box:refresh()
- window.setBackgroundColor(colors.red)
- return
- end
- info_box:setTextColor(colors.white)
- data["status"] = data["status"] == "IDLE" and "WAITING" or "IDLE"
- redstone.setOutput(data["redstone_output"], false)
- for _, button in pairs(api.buttons) do
- if button["window"] ~= window then
- button["available"] = not button["available"]
- end
- end
- CONFIG.properties["AUTO_TOGGLE"] = data["status"] ~= "IDLE"
- CONFIG:updateFile()
- if data["status"] == "WAITING" then
- data["alarms"] = {
- 21.0,
- 18.0,
- 15.0,
- 12.0,
- 9.0,
- 6.0,
- 3.0,
- 0.0
- }
- for _, v in ipairs(data["alarms"]) do
- table.insert(data["alarm_tokens"], os.setAlarm(v))
- end
- if (UPDATE_TIMER == nil) then
- UPDATE_TIMER = os.startTimer(INFO_UPDATE_SECONDS_SLEEP)
- end
- else
- for _, alarm in pairs(data["alarm_tokens"]) do
- os.cancelAlarm(alarm)
- end
- for _, timer in pairs(data["timers"]) do
- os.cancelTimer(timer)
- end
- data["alarm_tokens"] = {}
- data["timers"] = {}
- UPDATE_TIMER = nil
- end
- end
- -- Toggle farm button
- local toggle_farm_button = api.createButton(" TOGGLE", 13, 9, 12, 3, true, colors.white, colors.red, colors.green, toggleFarm, root_window, cycle_metadata)
- api.registerButton(toggle_farm_button)
- -- Config Callbacks
- if CONFIG.properties["AUTO_TOGGLE"] and CONFIG.properties["REDSTONE_OUTPUT"] ~= "NONE" then
- local redstone_button = redstone_buttons[CONFIG.properties["REDSTONE_OUTPUT"]]
- redstone_button["on_click"](redstone_button["window"], redstone_button["custom"])
- redstone_button["window"].setBackgroundColor(redstone_button["color_active"])
- redstone_button["window"].clear()
- api.simulateButtonClick(toggle_farm_button)
- end
- -- Start Event Loop
- term.redirect(root_window)
- root_terminal.setCursorPos(0, 0)
- api.eventHandlerPipeline(bussinesEventHandler)
Add Comment
Please, Sign In to add comment