@name DoorCore Ultimate
@inputs Master
@persist Doors:table Players:table
@trigger all

#[
    Scriptis' DoorCore v2
    -add [player / aim door]
    -remove [player / aim door]
    
    Based on the fancy-ass code made by Crypto
]#

runOnChat(1)
interval(100)

if (first()) {
    function number isDoor(E:entity) {
        for (I=1, Doors:count()) {
            if (Doors[I,entity]==E) {return I}
        }
        return 0
    }
}

if (chatClk(owner())) {
    if (owner():lastSaid():sub(1,1)=="-") {
        local Chat=owner():lastSaid():sub(2):explode(" ")
        switch(Chat[1,string]) {
            case "add",
                if (Chat[2,string]) {
                    local Player=findPlayerByName(Chat[2,string])
                    printColor(vec(0,255,255),"[DoorCoreU] ",vec(0,255,0),Player:name(),vec(255)," has been added to the door list.")
                    Players[Player:steamID(),string]=Player:steamID()
                } elseif (owner():aimEntity():wirelink():hasInput("Fade")) {
                    local Door=owner():aimEntity()
                    if (!isDoor(Door)) {
                        printColor(vec(0,255,255),"[DoorCoreU] ",vec(0,255,0),"Door",vec(255)," has been added.")
                        Doors[Doors:count()+1,entity]=Door
                    } else {
                        printColor(vec(0,255,255),"[DoorCoreU] ",vec(255,0,0),"This is already a door.")
                    }
                } else {
                    printColor(vec(0,255,255),"[DoorCoreU] ",vec(255,0,0),"This is not a valid door.")
                }
            break
            
            case "remove",
                if (Chat[2,string]) {
                    local Player=findPlayerByName(Chat[2,string])
                    printColor(vec(0,255,255),"[DoorCoreU] ",vec(255,0,0),Players:exists(Player:steamID()) ? Player:name() : "Nobody",vec(255)," has been removed from the door list.")
                    Players:remove(Player:steamID())
                } elseif (isDoor(owner():aimEntity())) {
                    printColor(vec(0,255,255),"[DoorCoreU] ",vec(0,255,0),"Door",vec(255)," has been removed from the door list.")
                    Doors:removeEntity(isDoor(owner():aimEntity()))
                } else {
                    printColor(vec(0,255,255),"[DoorCoreU] ",vec(255,0,0),"This is not a door.")
                }
            break
            
            case "list",
                printColor(vec(0,255,255),"[DoorCoreU] ",vec(255),"The following people are on the door list:")
                foreach(I,V:entity=players()) {
                    if (Players:exists(V:steamID())) {
                        printColor(vec(0,255,0),V:name())
                    }
                }
            break
            
        }
    }
}

for (I=1, Doors:count()) {
    findByClass("player")
    local Wirelink=Doors[I,entity]:wirelink()
    local Player=findClosest(Doors[I,entity]:pos())
    if (Master | (Players:exists(Player:steamID()) & Player:keyUse() & Player:aimEntity()==Doors[I,entity])) {
        Wirelink["Fade",number]=1
        #print(_HUD_PRINTCENTER,Player:name())
    } else {
        Wirelink["Fade",number]=0
    }
}

