-- Peripherals
local chat = peripheral.wrap("right")
local detector = peripheral.wrap("left")
-- Settings
local SLOT_SUNRISE = 1
local SLOT_NO_RAIN = 2
local DAY_TRIGGER_TIME = 18.5 -- When to trigger sleep (18.5 is shortly after sunset)
local RESET_TIME_START = 6.0 -- Morning start to reset the day flag
local RESET_TIME_END = 7.0
local RAIN_COOLDOWN = 60 -- Seconds to wait before trying to stop rain again
-- State variables
local canTryDay = true
local lastRainAction = 0
-- Helper: Send Chat Message safely
local function sendChat(msg, prefix)
if chat and chat.sendMessage then
chat.sendMessage(msg, prefix or "Automation")
end
end
-- Helper: Perform Ritual
-- side: "front" or "top"
-- slot: inventory slot number
local function performRitual(side, slot, ritualName)
turtle.select(slot)
local count = turtle.getItemCount()
if count == 0 then
print("Error: No items in slot " .. slot)
sendChat("I am out of " .. ritualName .. " items! Please refill slot " .. slot, "Turtle Alert")
return false
end
local placed = false
-- Try to place the item.
-- Note: If a tablet is already in the brazier, place() returns false.
-- We pulse redstone regardless, just in case the brazier just needs activation.
if side == "front" then
placed = turtle.place()
redstone.setOutput("front", true)
sleep(0.5)
redstone.setOutput("front", false)
elseif side == "top" then
placed = turtle.placeUp()
redstone.setOutput("top", true)
sleep(0.5)
redstone.setOutput("top", false)
end
-- Calculate remaining items for the message
local left = turtle.getItemCount()
sendChat("Activating " .. ritualName .. "... (" .. left .. " left)", "Ritual Bot")
return true
end
local function main()
while true do
term.clear()
term.setCursorPos(1, 1)
print("=== Ritual Automation ===")
-- 1. Get Data
local time = os.time("ingame") or 0
local isRaining = false
if detector and detector.isRaining then
isRaining = detector.isRaining()
else
print("Warning: No Detector found!")
end
-- Display Status
print("Time: " .. string.format("%.2f", time))
print("Raining: " .. tostring(isRaining))
print("Sun Items: " .. turtle.getItemCount(SLOT_SUNRISE))
print("Rain Items: " .. turtle.getItemCount(SLOT_NO_RAIN))
print("Next Day Trigger: " .. tostring(canTryDay))
-- 2. Day/Night Logic
-- Reset flag in the morning
if time >= RESET_TIME_START and time < RESET_TIME_END then
canTryDay = true
end
-- Trigger Sunset Ritual
if time > DAY_TRIGGER_TIME and canTryDay then
canTryDay = false
performRitual("front", SLOT_SUNRISE, "Sunrise")
end
-- 3. Rain Logic
-- We check os.clock() to ensure we don't spam the ritual every 0.1 seconds
if isRaining and (os.clock() - lastRainAction > RAIN_COOLDOWN) then
lastRainAction = os.clock()
performRitual("top", SLOT_NO_RAIN, "Clear Skies")
end
os.sleep(1) -- Sleep 1 second to reduce server lag
end
end
-- Run the program
-- utilizing pcall to catch crashes and restart the loop if needed,
-- though the main loop handles most logic.
local ok, err = pcall(main)
if not ok then
print("Crashed: " .. tostring(err))
sendChat("I have crashed! " .. tostring(err), "Turtle Error")
end