@name Scriptis' Raid Entrance Autdoor
@persist Timer Players:array
@outputs Fade
if (first()) {
    timer("PrimaryLoop",1000)
    function number allowed(E:entity) {
        for(I=1, Players:count()) {
            if (Players[I,entity]==E) {
                return 1
            }
        }
        return 0
    }
    entity():isWeldedTo():createWire(entity(),"Fade","Fade")
}

interval(1000)
runOnChat(1)

findByClass("player")
Ply=findClosest(entity():pos())
if (Ply:aimEntity()==entity():isWeldedTo() & Ply:keyUse() & allowed(Ply)) {
    Timer=8
}

if (chatClk(owner())) {
    local Player=owner()
    local Msg=Player:lastSaid()
    if (Msg:sub(1,4)=="-add") {
        local Find=Msg:sub(6)
        foreach(I,V:entity=players()) {
            if (V:name():lower():matchFirst(Find:lower())) {
                printColor(vec(0,255,255),"[RaidAutoDoor] ",vec(255),"Added ",vec(0,255,0),V:name(),vec(255)," to door list.")
                Players[Players:count()+1,entity]=V
            }
        }
    } elseif (Msg:sub(1,7)=="-remove") {
        Find=Msg:sub(9)
        for (I=1, Players:count()) {
            local Player=Players[I,entity]
            if (Player:name():lower():matchFirst(Find:lower())) {
                printColor(vec(0,255,255),"[RaidAutoDoor] ",vec(255),"Removed ",vec(255,0,0),Player:name(),vec(255)," from door list.")
                Players:removeEntity(I)
            }
        }
    }
}

if (clk("PrimaryLoop")) {
    Timer=clamp(Timer-1,0,8)
    if (Timer) {
        soundPlay(Timer,0,"ui/buttonclick.wav")
        soundPitch(Timer,10*Timer+20)
    }
    Fade=Timer>0
    timer("PrimaryLoop",1000)
}
