@name AutoDoor
@persist Allowed:array Cooldown Holder:entity
@persist Door:entity
@model @model models/props_c17/clock01.mdl
@outputs A
runOnChat(1)
interval(100)
if (duped()) {reset()}
if (first()) {
function number is(E:entity) {
    for (I=1, Allowed:count()) {if (E==Allowed[I,entity]) {return 1}} 
    return 0
}
}


findByClass("player")
local User=findClosest(Door:pos())
if (User:keyUse() & User:aimEntity()==Door & (User==owner() | is(User))) {
    A=1
    Holder=User
}
if (chatClk(owner()) & owner():lastSaid():sub(1,1)=="-") {
    hideChat(1)
    Args=owner():lastSaid():lower():explode(" ")
    if (Args[1,string]=="-reset") {print("Resetting") reset()}
    if (Args[1,string]=="-add") {
        Players=players()
        for (I=1, Players:count()) {
            if (Players[I,entity]:name():lower():matchFirst(Args[2,string])) {
                Allowed[Allowed:count()+1,entity]=Players[I,entity]
                print("Added "+Players[I,entity]:name()+" to door list")
                break
            }
        }
    }
    if (Args[1,string]=="-remove") {
        for (I=1, Allowed:count()) {
            if (Allowed[I,entity]:name():lower():matchFirst(Args[2,string])) {
                print("Popping "+Allowed[I,entity]:name()+" from table")
                Allowed:removeEntity(I)
            }
        }
    }
}
if (first()) {Door=entity():isWeldedTo()}
if (Holder:keyUse()) {
    A=1
} else {Holder=noentity() A=0}
