@name Stopper
@persist Ent:entity I Props:array Friends:array On P:entity

if(I < 20) {interval(300)}
else {
    runOnTick(1)
    runOnLast(1)
    runOnChat(1)
}

if(first()) {
    I = 0
    
    Friends = array(owner())
    On = 1
    
    function void correct() {
        owner():soundPlay("Crc" + curtime(), 1, "buttons/button14.wav")
    }
    
    function void wrong() {
        owner():soundPlay("Wrg" + curtime(), 1, "buttons/button11.wav")
    }
    
    function number isAllowed(P:entity) {
        local Can = 0
        foreach(ID, Pl:entity=Friends) {if(Pl:steamID() == P:steamID()) {Can = 1}}
        return Can
    }
    
    function entity closest(Ps:array) {
        local MaxDis = Ps[1, entity]:pos():distance(entity():pos())
        local Id = 1
        
        for(I = 2, Ps:count()) {
            if(Ps[I, entity]:pos():distance(entity():pos()) < MaxDis) {MaxDis = Ps[I, entity]:pos():distance(entity():pos()) Id = I   }
        }
        
        return Ps[Id, entity]
    }
    
    findByClass("player")
}

if(I < 20) {
    I += 1
    local E = propSpawn("models/props_building_details/Storefront_Template001a_Bars.mdl", entity():pos() + vec(sin(I * 18) * 175, cos(I * 18) * 175, 50), 1)
    local Ang = (E:pos() - entity():pos()):toAngle()
    
    E:setAng(ang(0, Ang:yaw(), 0))
    
    Props[Props:count() + 1, entity] = E
    
    holoCreate(I)
    #holoAlpha(I, 100)
    holoPos(I, E:pos())
    holoAng(I, ang(0, Ang:yaw() - 90, 0))
    #holoScale(I, vec(5, 0.1, 10))
    holoModel(I, "models/props_c17/streetsign004e.mdl")
}

if(changed(I) & I >= 20) {
    foreach(ID, Prop:entity=Props) {
        Prop:setAlpha(0)
        Prop:propNotSolid(1)
    }
}

if(last()) {
    foreach(ID, Prop:entity=Props) {Prop:propDelete()}
}

if(I >= 20 & On & findClosest(entity():pos()):pos():distance(entity():pos()) < 250) {
    if(!P:isValid() | changed(isAllowed(P)) | changed(Props[1, entity]:getAlpha())) {
        local Ps = findToArray()
        local E = 1
        P = Ps[1, entity]
        while(isAllowed(P)) {E += 1 P = Ps[E, entity]}
        P = closest(Ps)
        
        if(!isAllowed(P)) {
                foreach(ID, Prop:entity=Props) {
                    Prop:setAlpha(255)
                    Prop:propNotSolid(0)
                }
                
                if(P:pos():distance(entity():pos()) < 175) {
                    P:kill()
                }
        } else {
            foreach(ID, Prop:entity=Props) {
                Prop:setAlpha(0)
                Prop:propNotSolid(1)
            }
        }
    }
} else {
    if(Props[1, entity]:getAlpha() != 0) {
        foreach(ID, Prop:entity=Props) {
            Prop:setAlpha(0)
            Prop:propNotSolid(1)
        }
    }
}

if(chatClk(owner())) {
    if(owner():lastSaid():explode(" ")[1, string] == "!add") {
        local Ple = owner():aimEntity()
        
        if(Ple:type() != "player" & Ple:pos():distance(owner():pos()) < 100) {
            printColor(vec(255, 0, 0), "You need to be looking at the player you want to add.")
            wrong()
        } else {
            Friends[Friends:count() + 1, entity] = Ple
            printColor(vec(255), "Added ", vec(0, 150, 255), Ple:name(), vec(255), " to the friends list.")
            correct()
        }
    }
    
    if(owner():lastSaid() == "!list") {
        foreach(ID, Fr:entity=Friends) {
            printColor(vec(0, 150, 255), ID, ": ", vec(255), Fr:name())
        }
    }
    
    if(owner():lastSaid():explode(" ")[1, string] == "!remove") {
        local Ple = Friends[owner():lastSaid():explode(" ")[2, string]:toNumber(), entity]
        Friends:remove(owner():lastSaid():explode(" ")[2, string]:toNumber())
        printColor(vec(255), "Removed ", vec(0, 150, 255), Ple:name(), vec(255), "(" + owner():lastSaid():explode(" ")[2, string]:toNumber() + ") from the friends list.")
        correct()
    }
    
    if(owner():lastSaid() == "!on") {if(On != 1) {correct() for(I2 = 1, 20) {holoAlpha(I2, 0)}} On = 1}
    if(owner():lastSaid() == "!off") {if(On != 0) {correct() for(I2 = 1, 20) {holoAlpha(I2, 255)}} On = 0}
}
