################
# > Duplicator: Duplicate anyones props. Does not duplicate entities or constraints.
# > How to use: Right click on any contraption and it will be spawned again nearby.
################

@name Duplicator
@persist Props:array Mod:array Pos:array Ang:array Mat:array Col:array Alp:array
@persist I Spawn Spawned:array AimEnt:entity Aim:entity
@persist NewPos:vector Parent:entity

interval(250)

if (changed(owner():keyAttack2()&owner():keyUse())&(owner():keyAttack2()&owner():keyUse())&!Props:count()) {
    
    NewPos=-vec(500,0,0)
    
    Aim=owner():aimEntity()
    Props=Aim:getConstraints()
    hint("Duplicating...",10)
    for (I=1,Props:count()) {
        if (Props[I,entity]:type()=="prop_physics") {
            Mod[I,string]=Props[I,entity]:model()
            Pos[I,vector]=Props[I,entity]:pos()
            Ang[I,angle]=Props[I,entity]:angles()
            Mat[I,string]=Props[I,entity]:getMaterial()
            Col[I,vector]=Props[I,entity]:getColor()
            Alp[I,number]=Props[I,entity]:getAlpha()
        }
    }
    T=1
    Parent=propSpawn(Aim:model(),Aim:pos()+NewPos,1)
    Parent:setAng(Aim:angles())
    Parent:setMaterial(Aim:getMaterial())
    Parent:setColor(Aim:getColor())
    Parent:setAlpha(Aim:getAlpha())
    hint("Spawning...",10)
}

if (T) {
    T++
    if (T==2) {
        Spawn=1
        T=0
    }
}

if (Spawn) {
    Spawned[Spawn,entity]=propSpawn(Mod[Spawn,string],1)
    Spawned[Spawn,entity]:setPos(Pos[Spawn,vector]+NewPos)
    Spawned[Spawn,entity]:setAng(Ang[Spawn,angle])
    Spawned[Spawn,entity]:setMaterial(Mat[Spawn,string])
    Spawned[Spawn,entity]:setColor(Col[Spawn,vector])
    Spawned[Spawn,entity]:setAlpha(Alp[Spawn,number])
    Spawned[Spawn,entity]:parentTo(Aim)
    Spawn+=1
    if (Spawn>Props:count()) {
        hint("Done.",10)
        selfDestruct()
    }
}
