CCCPaste

startup.lua

local detector = peripheral.find("environment_detector")
local gearshift = peripheral.find("Create_SequencedGearshift")

if not detector then
    error("No environment detector found")
end

if not gearshift then
    error("No Create Sequenced Gearshift found")
end

local isDoorOpen = false -- Initial state of the door
local minYLevel = -1 -- Minimym Y level to consider user valid (relative to detector)

local doorStateFile = fs.open(".door_state", "r")
if doorStateFile then
    isDoorOpen = doorStateFile.readLine() == "1"
    doorStateFile.close()
end

local allowedUsers = {
    "f48e6371-3ed0-4738-a5e0-0f29bc2da9d4", -- LunaFolf
	"442b1cce-8356-450d-9a5d-d1a040ee15ac", -- lifeline4603
	"9c4b7608-6411-45d7-b0ab-e13587c81074", -- PrinceOfCookies
	"fbd30864-b8d8-431e-83d6-a86d68281f7d", -- AdventurousMR
	"d94e1d90-c8db-4b98-8958-af7a57a99266", -- beLLecolond
	"dd91707a-b7a0-4ab7-9bbd-498a4f35e8b2", -- NoxxyTheStars

	
}

--[[
    Table Structure for detector.scanEntities():
    {
        {
            canFreeze = true,
            health = 20,
            id = 1,
            isGlowing = false,
            isInWall = false,
            maxHealth = 20,
            name = "LunaFolf",
            tags = {},
            uuid = "f48e6371-3ed0-4738-a5e0-0f29bc2da9d4",
            x = 3.4389338271924,
            y = 0,
            z = -0.18627323016072

        },
        ...
    }
--]]

function toggleDoor(open)
    if isDoorOpen == open then
        print("Door is already ", open and "open" or "closed")
        print("Skipping toggle.")
        return
    end

    gearshift.rotate(90, open and -1 or 1)
    isDoorOpen = open

    local doorStateFile = fs.open(".door_state", "w")
    if doorStateFile then
        doorStateFile.writeLine(open and "1" or "0")
        doorStateFile.close()
    end
end

-- Main loop
while true do
    local detectedEntities = nil
    if not gearshift.isRunning() then
        detectedEntities = detector.scanEntities(4) -- Scan within 4 blocks
    end
    local userDetected = false

    if detectedEntities ~= nil then
        for _, entity in ipairs(detectedEntities) do
            for _, allowedUUID in ipairs(allowedUsers) do
                if entity.uuid == allowedUUID and entity.y >= minYLevel then
                    userDetected = true
                    break
                end
            end
            if userDetected then
                break
            end
        end

        if userDetected and not isDoorOpen then
            toggleDoor(true)
        elseif not userDetected and isDoorOpen then
            toggleDoor(false)
        end
    end
end