@name DoorCore
@persist Doors:table Players:table
@trigger none

runOnChat(1)
interval(300)

if (first()) {
    function number allowed(Ent:entity) {
        for (I=1, Players:count()) {
            if (Players[I,entity]==Ent) {
                return 1
            }
        }
        return 0
    }
}

for (I=1, Doors:count()) {
    local Door=Doors[I,wirelink]
    findByClass("player")
    local Player=findClosest(Door:entity():pos())
    if (Player:keyUse() & allowed(Player) & Player:pos():distance(Door:entity():pos())<80 ) {
        Door["Fade",number]=1
    } else {Door["Fade",number]=0}
}

if (chatClk(owner())) {
    local Player=owner()
    local Msg=Player:lastSaid()
    if (Msg:sub(1,5)=="-door") {
        Doors[Doors:count()+1,wirelink]=Player:aimEntity():wirelink()
        printColor(vec(0,255,255),"[DoorCore] ",vec(255),"Added door.")
    } elseif (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),"[DoorCore] ",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),"[DoorCore] ",vec(255),"Removed ",vec(255,0,0),Player:name(),vec(255)," from door list.")
                Players[I,entity]=noentity()
            }
        }
    } elseif (Msg=="-list") {
        printColor(vec(0,255,255),"[DoorCore] ",vec(255),"The following players are on ",vec(0,0,255),""+Doors:count(),vec(255)," doors:")
        for (I=1, Players:count()) {
            printColor(vec(255),Players[I,entity]:name())
        }
    }
}
