• Print

Author Topic: Untested Advanced Cleanup Script  (Read 4490 times)

0 Members and 1 Guest are viewing this topic.

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Untested Advanced Cleanup Script
« on: February 11, 2014, 03:03:49 pm »
Just thought I would dump this untested cleanup script here, I will test it and fix it up later. But feel free to tinker, or throw suggestions.

Code: Lua
  1. --[[-------------------------------------------------------------------------------------------------------------------------
  2. --      ULX Cleanup for ULX SVN/ULib SVN by LuaTenshi
  3. ------------------------------------------------------------------------------------------------------------------------]]--
  4.  
  5. local function FindOwner( ent )
  6.         local owner = owner or nil
  7.         local cppi,_ = ent:CPPIGetOwner()
  8.         owner = cppi or ent.FPPOwner or ent.Owner
  9.         return owner
  10. end
  11.  
  12. local ignoreList =
  13. {
  14.         "player",            -- Not our territory to mess with.
  15.         "worldspawn",        -- The world. Leave it be.
  16.         "gmod_anchor",       -- Used for sliders connected to the world. Could be used for other stuff too, should be safe to ignore.
  17.         "npc_grenade_frag",  -- Grenades, no reason we should muck around with these.
  18.         "prop_combine_ball", -- Energy balls, ditto as above.
  19.         "npc_satchel",       -- Ditto.
  20.         "prop_door_rotating"
  21. }
  22.  
  23. local fuzzyIgnoreList =
  24. {
  25.         "env_",
  26.         "trigger_",
  27.         "info_",
  28.         "func_",
  29.         "predicted_",
  30.         "shadow_",
  31.         "point_",
  32.         "spawn",
  33.         "player"
  34. }
  35.  
  36. local function IsItFuzzy( ent )
  37.         local entClass = ent:GetClass()
  38.         for _,v in pairs(fuzzyIgnoreList) do
  39.                 if( string.find( string.lower(entClass), string.lower(v) ) ) then
  40.                         return true
  41.                 end
  42.         end
  43.         return false
  44. end
  45.  
  46. local clnupflags = {"r","c","cf","g","f","m"}
  47. function ulx.clnup( calling_ply, string_arg, command )
  48.         if string_arg == "r" then
  49.                 if command == "1" or command == "true" or command == "t" then
  50.                         for _,v in pairs(ents.GetAll()) do
  51.                                 if (v and IsValid(v)) and not table.HasValue( ignoreList, v:GetClass() ) then
  52.                                         if FindOwner(v) and IsValid(FindOwner(v)) then
  53.                                                 SafeRemoveEntity( v )
  54.                                         end
  55.                                 end
  56.                         end
  57.                 else
  58.                         game.CleanUpMap()
  59.                 end
  60.         elseif string_arg == "c" then
  61.                 for _,v in pairs(ents.FindByClass( command ))
  62.                         if v and IsValid(v) then
  63.                                 if not table.HasValue( ignoreList, v:GetClass() ) then
  64.                                         if not IsItFuzzy(v) then SafeRemoveEntity( v ) end
  65.                                 end
  66.                         end
  67.                 end
  68.         elseif string_arg == "cf" then
  69.                 for _,v in pairs(ents.GetAll()) do
  70.                         if (v and IsValid(v)) and (string.find( string.lower(v:GetClass()), command)) then
  71.                                 if not table.HasValue( ignoreList, v:GetClass() ) then
  72.                                         if not IsItFuzzy(v) then SafeRemoveEntity( v ) end
  73.                                 end
  74.                         end
  75.                 end
  76.         elseif string_arg == "g" then
  77.                 local scanner = {}
  78.                 for _,v in pairs(ents.GetAll()) do
  79.                         if (v and IsValid(v)) and not table.HasValue( ignoreList, v:GetClass() ) then
  80.                                 if FindOwner(v) and IsValid(FindOwner(v)) then
  81.                                         scanner[FindOwner(v)] = (0 or scanner[FindOwner(v)]) + 1
  82.                                 end
  83.                         end
  84.                 end    
  85.                 local target = table.GetWinningKey( scanner )
  86.                 target:ConCommand("gmod_cleanup")
  87.         elseif string_arg == "f" then
  88.                 for _,v in pairs(ents.GetAll()) do
  89.                         if v:GetPhysicsObject():IsMotionEnabled() and not table.HasValue( ignoreList, v:GetClass() ) then
  90.                                 if command == "1" or command == "true" or command == "t" then
  91.                                         if not IsItFuzzy(v) then SafeRemoveEntity( v ) end
  92.                                 else
  93.                                         if FindOwner(v) and IsValid(FindOwner(v)) then
  94.                                                 SafeRemoveEntity( v )
  95.                                         end
  96.                                 end
  97.                         end
  98.                 end
  99.         elseif string_arg == "m" then
  100.                 for _,v in pairs(ents.FindByModel( ulx.standardizeModel(command) ))
  101.                         if v and IsValid(v) then
  102.                                 if not table.HasValue( ignoreList, v:GetClass() ) then
  103.                                         if not IsItFuzzy(v) then SafeRemoveEntity( v ) end
  104.                                 end
  105.                         end
  106.                 end
  107.         else
  108.                 game.CleanUpMap()
  109.         end
  110. end
  111. local clnup = ulx.command( "Extra Utility", "ulx clnup", ulx.clnup, {"!cleanup","!clnup"} )
  112. clnup:addParam{ type=ULib.cmds.StringArg, completes=clnupflags, default="r", hint="flag", error="Invalid flag \"%s\" specified", ULib.cmds.optional }
  113. clnup:addParam{ type=ULib.cmds.StringArg, hint="command", ULib.cmds.takeRestOfLine }
  114. clnup:defaultAccess( ULib.ACCESS_SUPERADMIN )
  115. clnup:help([[ Cleanup the server with style.
  116.  
  117.         r - Reset the map, or remove all player owned entities.
  118.         c - Cleanup all entities with the specified class.
  119.         cf - Cleanup all entities with the class name that contains the specified string.
  120.         g - Cleanup the player with the most props spawned.
  121.         f - Cleanup all un-frozen props.
  122.         m - Cleanup all entities that have the specified model.
  123. ]])
  124.  
« Last Edit: July 05, 2014, 06:07:44 pm by LuaTenshi »
I cry every time I see that I am not a respected member of this community.

  • Print