|
| 1 | +local allowed = { |
| 2 | + ["ucs_table"] = true, |
| 3 | + ["ucs_mineable"] = true |
| 4 | +} |
| 5 | + |
| 6 | +local function SaveEnts() |
| 7 | + local saved = {} |
| 8 | + for k,v in ipairs( ents.FindByClass( "ucs_*" ) ) do |
| 9 | + if v:GetNWBool( "UCSPersist" ) then |
| 10 | + local class = v:GetClass() |
| 11 | + local typ = class == "ucs_table" and v:GetTableType() or v:GetMineableType() |
| 12 | + table.insert( saved, { class, typ, v:GetPos(), v:GetAngles() } ) |
| 13 | + end |
| 14 | + end |
| 15 | + file.CreateDir( "ucs/persist" ) |
| 16 | + file.Write( "ucs/persist/"..game.GetMap()..".json", util.TableToJSON( saved ) ) |
| 17 | +end |
| 18 | + |
| 19 | +properties.Add( "ucschangetype", { |
| 20 | + MenuLabel = "Change Type", |
| 21 | + Order = 1600, |
| 22 | + PrependSpacer = true, |
| 23 | + MenuIcon = "icon16/brick_edit.png", |
| 24 | + Filter = function( self, ent, ply ) |
| 25 | + return IsValid( ent ) and allowed[ent:GetClass()] and ply:IsAdmin() |
| 26 | + end, |
| 27 | + MenuOpen = function( self, option, ent, tr ) |
| 28 | + local sub = option:AddSubMenu() |
| 29 | + local tbl = ent:GetClass() == "ucs_table" and CraftingTable or MineableEntity |
| 30 | + for k,v in pairs( tbl ) do |
| 31 | + sub:AddOption( v.Name or "Invalid Type", function() self:SetType( ent, k ) end ) |
| 32 | + end |
| 33 | + end, |
| 34 | + Action = function() end, |
| 35 | + SetType = function( self, ent, typ ) |
| 36 | + self:MsgStart() |
| 37 | + net.WriteEntity( ent ) |
| 38 | + net.WriteString( typ ) |
| 39 | + self:MsgEnd() |
| 40 | + end, |
| 41 | + Receive = function( self, len, ply ) |
| 42 | + if !ply:IsAdmin() then return end |
| 43 | + local ent = net.ReadEntity() |
| 44 | + local typ = net.ReadString() |
| 45 | + local pos = ent:GetPos() |
| 46 | + local ang = ent:GetAngles() |
| 47 | + local class = ent:GetClass() |
| 48 | + ent:Remove() |
| 49 | + |
| 50 | + local e = ents.Create( class ) |
| 51 | + e:SetPos( pos ) |
| 52 | + e:SetAngles( ang ) |
| 53 | + if class == "ucs_table" then |
| 54 | + e:SetTableType( typ ) |
| 55 | + else |
| 56 | + e:SetMineableType( typ ) |
| 57 | + end |
| 58 | + e:Spawn() |
| 59 | + end |
| 60 | +} ) |
| 61 | + |
| 62 | +properties.Add( "ucssavetomap", { |
| 63 | + MenuLabel = "Save to Map", |
| 64 | + Order = 1601, |
| 65 | + MenuIcon = "icon16/drive_disk.png", |
| 66 | + Filter = function( self, ent, ply ) |
| 67 | + return IsValid( ent ) and allowed[ent:GetClass()] and ply:IsAdmin() and !ent:GetNWBool( "UCSPersist" ) |
| 68 | + end, |
| 69 | + Action = function( self, ent ) |
| 70 | + self:MsgStart() |
| 71 | + net.WriteEntity( ent ) |
| 72 | + self:MsgEnd() |
| 73 | + end, |
| 74 | + Receive = function( self, len, ply ) |
| 75 | + local ent = net.ReadEntity() |
| 76 | + ent:SetNWBool( "UCSPersist", true ) |
| 77 | + ent:SetMoveType( MOVETYPE_NONE ) |
| 78 | + SaveEnts() |
| 79 | + end |
| 80 | +} ) |
| 81 | + |
| 82 | +properties.Add( "ucsremovefrommap", { |
| 83 | + MenuLabel = "Remove From Map", |
| 84 | + Order = 1602, |
| 85 | + MenuIcon = "icon16/drive_delete.png", |
| 86 | + Filter = function( self, ent, ply ) |
| 87 | + return IsValid( ent ) and allowed[ent:GetClass()] and ply:IsAdmin() and ent:GetNWBool( "UCSPersist" ) |
| 88 | + end, |
| 89 | + Action = function( self, ent ) |
| 90 | + self:MsgStart() |
| 91 | + net.WriteEntity( ent ) |
| 92 | + self:MsgEnd() |
| 93 | + end, |
| 94 | + Receive = function( self, len, ply ) |
| 95 | + local ent = net.ReadEntity() |
| 96 | + ent:SetNWBool( "UCSPersist", false ) |
| 97 | + ent:SetMoveType( MOVETYPE_VPHYSICS ) |
| 98 | + SaveEnts() |
| 99 | + end |
| 100 | +} ) |
| 101 | + |
| 102 | +if SERVER then |
| 103 | + hook.Add( "InitPostEntity", "UCSLoadSavedEnts", function() |
| 104 | + local name = "ucs/persist/"..game.GetMap()..".json" |
| 105 | + if file.Exists( name, "DATA" ) then |
| 106 | + local tbl = util.JSONToTable( file.Read( name ) ) |
| 107 | + for k,v in pairs( tbl ) do |
| 108 | + local e = ents.Create( v[1] ) |
| 109 | + if v[1] == "ucs_table" then |
| 110 | + e:SetTableType( v[2] ) |
| 111 | + else |
| 112 | + e:SetMineableType( v[2] ) |
| 113 | + end |
| 114 | + e:SetPos( v[3] ) |
| 115 | + e:SetAngles( v[4] ) |
| 116 | + e:Spawn() |
| 117 | + e:SetNWBool( "UCSPersist", true ) |
| 118 | + e:SetMoveType( MOVETYPE_NONE ) |
| 119 | + end |
| 120 | + print( "Spawned "..#tbl.." UCS entities." ) |
| 121 | + end |
| 122 | + end ) |
| 123 | +end |
0 commit comments