@name PropSaver
@inputs 
@outputs 
@persist [E2 Me]:entity Props:array Name:string Path:string 
@persist N Ready SavingSpeed Size:vector Ent Text:string [CornerPos1 CornerPos2]:vector 
@trigger 
runOnTick(1)
runOnChat(1)

findIncludeClass("prop_physics")
findExcludeModel("models/beer/wiremod/gate_e2_mini.mdl")
findInBox(CornerPos1,CornerPos2)
Find = findToArray()

if (first()) {
    E2 = entity()
    Me = owner()
    
    N = -1
    
    SavingSpeed = 600
    Size = vec(1,1,1)
    
    Scale = vec(10,10,10) * 20
    
    holoCreate(1)
    holoScaleUnits(1,Scale)
    holoPos(1,entity():toWorld(vec(0,0,40)))
    holoAng(1,entity():angles())
    holoColor(1,vec(255,255,255),100)
    holoParent(1,entity())
    
    CornerPos1 = Scale / 2
    CornerPos2 = -CornerPos1
    
    CornerPos1 = holoEntity(1):toWorld(CornerPos1)
    CornerPos2 = holoEntity(1):toWorld(CornerPos2)
    
    CornerScale = vec() + 1
    
    holoCreate(2)
    holoPos(2,CornerPos1)
    holoScaleUnits(2,CornerScale)
    holoParent(2,1)
    
    holoCreate(3)
    holoPos(3,CornerPos2)
    holoScaleUnits(3,CornerScale)
    holoParent(3,1)
}

if (chatClk(Me)) {
    if (Me:lastSaid():explode(" "):string(1) == "!save") {
        hideChat(1)
        
        if (entity():isValid()) {
            Props = Find
            
            Name = Me:lastSaid():sub(7)
            Path = ">e2shared/Holograms/"+Name+".txt"
            
Text = "@name "+Name+"
@inputs 
@outputs 
@persist Table:table 
runOnLast(1)

E = entity()

if (last()) {propDeleteAll()}

if (first()) {"
            fileWrite(Path,Text)
            
            N = 0
            Ready = 0
        }
    }
}

if (N >= 0 && Ready == 0) {timer("N",SavingSpeed)}

if (clk("N")) {
    N += 1
    Prop = Props[N,entity]
        
    if (Prop:getMaterial() == "") {}
    else {Material = " P:setMaterial(\""+Prop:getMaterial()+"\")"}
    
    Color = " P:setColor(vec4("+Prop:getColor4():x()+","+Prop:getColor4():y()+","+Prop:getColor4():z()+","+Prop:getColor4():w()+"))"
    Pos = "E:toWorld(vec("+round((Prop:pos():x()-entity():pos():x())*Size:x(),4)+","+round((Prop:pos():y()-entity():pos():y())*Size:y(),4)+","+round((Prop:pos():z()-entity():pos():z())*Size:z(),4)+"))"
    Ang = "E:toWorld(ang("+round(Prop:angles():pitch(),4)+","+round(Prop:angles():yaw(),4)+","+round(Prop:angles():roll(),4)+"))"
    
    if (Prop:getColor() == vec(255,255,255)) {}
    else {Col = " P:setColor("+Color+")"}
    
    fileAppend(Path,"
    local P = propSpawn(\""+Prop:model()+"\","+Pos+","+Ang+",1) "+Material+Color+""
    )
    
    print("Prop "+N+" Created!")
    
    if (N == Props:count()) {
        timer("done", SavingSpeed)
        stoptimer("N")
        
        N = -1
    }
}

if (clk("done")) {
fileAppend(">e2shared/Holograms/"+Name+".txt","\n}")
        
    print("\""+Name+"\" has saved.")
    
    Ready = 1
}
