Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.46 KB | None | 0 0
  1. util.AddNetworkString("Playersaving_AdminMenu_Open")
  2. util.AddNetworkString("Playersaving_AdminMenu_Change")
  3. local Playersaving = {}
  4. Playersaving.MySQL = {}
  5. Playersaving.Lang = {}
  6. Playersaving.RestrictedJobs_Weapons = {}
  7.  
  8. /* -------------------------------------------------
  9. --- MYSQL CONFIG -----------------------------------
  10. --------------------------------------------------*/
  11. Playersaving.MySQL.Active = false -- Should Playersaving use MySQL?
  12. Playersaving.MySQL.Host = ""
  13. Playersaving.MySQL.User = ""
  14. Playersaving.MySQL.Pass = ""
  15. Playersaving.MySQL.DB = ""
  16. Playersaving.MySQL.Port = 3306
  17.  
  18. /* -------------------------------------------------
  19. --- INGAME CONFIG SETTINGS -------------------------
  20. --------------------------------------------------*/
  21. Playersaving.Config_CMD = "!psconfig"
  22. Playersaving.Config_Ranks = { -- Ranks that can access the Ingame-Config
  23. "superadmin",
  24. "admin"
  25. }
  26.  
  27. /* -------------------------------------------------
  28. --- STANDART CONFIG --------------------------------
  29. --- (ONLY TO HAVE VALUES AT STARTUP) ---------------
  30. --- DONT EDIT! IT WILL NOT AFFECT ANYTHING ---------
  31. --------------------------------------------------*/
  32. Playersaving.BlacklistedWeps = { -- Generally blacklisted Weapons
  33. "weapon_policebaton",
  34. "weaponchecker",
  35. "door_ram",
  36. "arrest_stick",
  37. "stunstick",
  38. "unarrest_stick",
  39. "weaponchecker",
  40. }
  41. Playersaving.BlacklistedJobs = { -- Generally blacklisted Jobs
  42. "Admin",
  43. }
  44. Playersaving.RestrictedJobs_Weapons["weapon_glock2"] = {"Civil Protection","Mayor"} -- blacklisted weapons if the player is in a specified job
  45. Playersaving.AutosaveActive = true
  46. Playersaving.AutosaveTime = 300 -- Time Automatic save
  47. Playersaving.SaveCommandActive = true
  48. Playersaving.SaveCommandTime = 60 -- Wait time - Manual save
  49. Playersaving.SaveCommand = "!save" -- Command to save manually
  50. Playersaving.SaveOnDisconnect = true
  51. Playersaving.SaveOnlyGroups = false -- Should only specified groups be saved
  52. Playersaving.SaveGroups = {"superadmin","admin"} -- Groups that will be saved (Only if Playersaving.SaveOnlyGroups is true)
  53. Playersaving.SaveHealth = true -- Players Health
  54. Playersaving.SaveArmor = true -- Players Armor
  55. Playersaving.SavePos = true -- Players position
  56. Playersaving.SaveWeapons = true -- Weapons that are not blacklisted
  57. Playersaving.SaveAmmo = true -- Players Weapon Ammo
  58. Playersaving.SaveHunger = false -- DarkRP FOOD MOD
  59. Playersaving.Lang = "EN" -- EN OR DE
  60.  
  61. /* -------------------------------------------------
  62. --- LANG CONFIG ------------------------------------
  63. --------------------------------------------------*/
  64. local l = {}
  65. function Playersaving.LoadLang ()
  66. if Playersaving.Lang == "EN" then
  67. l.Saved = "Your character has been saved!"
  68. l.Loaded = "Your character has been loaded!"
  69. l.Cant = "You cant save yet! You need to wait %seconds% seconds!"
  70. l.Cant2 = "You cant save! Invalid Group/Team"
  71. end
  72. if Playersaving.Lang == "DE" then
  73. l.Saved = "Dein Charakter wurde gespeichert!"
  74. l.Loaded = "Dein Charakter wurde geladen!"
  75. l.Cant = "Du kannst noch nicht speichern! Du musst noch %seconds% Sekunden warten!"
  76. l.Cant2 = "Du kannst nicht speichern! Falsche Gruppe/Team"
  77. end
  78. Playersaving.LangText = l
  79. local l = Playersaving.LangText
  80. end
  81.  
  82. /* -------------------------------------------------
  83. --- CODE -------------------------------------------
  84. --------------------------------------------------*/
  85.  
  86. -- SETUP / LOADING
  87. function Playersaving.Load()
  88. Playersaving.SetupMySQLite()
  89. end
  90.  
  91. function Playersaving.SetupMySQLite ()
  92. local query = [[CREATE TABLE IF NOT EXISTS playersaving_data (steamID64 TEXT, weapons TEXT, ammo TEXT, health TEXT, armor TEXT, food TEXT, posX TEXT, posY TEXT, posZ TEXT, map TEXT );
  93. CREATE TABLE IF NOT EXISTS playersaving_settings (BlacklistedWeps TEXT, BlacklistedJobs TEXT, BlacklistedWeps_Jobs TEXT, AutosaveActive TEXT, AutosaveTime TEXT, SaveCommandActive TEXT, SaveCommandTime TEXT, SaveCommand TEXT, SaveOnDisconnect TEXT, SaveOnlyGroups TEXT, SaveGroups TEXT, Lang TEXT, SaveHealth TEXT, SaveArmor TEXT, SavePos TEXT, SaveWeapons TEXT, SaveAmmo TEXT, SaveHunger TEXT );
  94. SELECT * FROM playersaving_settings;]]
  95.  
  96. if Playersaving.MySQL.Active then
  97. Playersaving.MySQL.Database:Query ( query )
  98. else
  99. sql.Query( query )
  100. end
  101. Playersaving.RetrieveSettings()
  102. end
  103.  
  104. function Playersaving.RetrieveSettings ()
  105. local query = "SELECT * FROM playersaving_settings;"
  106. if Playersaving.MySQL.Active then
  107. Playersaving.MySQL.Database:Query ( query, function ( res, err )
  108. if res and res[1] and res[1]["data"] and res[1]["data"][1] then
  109. Playersaving.LoadSettings(res[1]["data"][1])
  110. else
  111. RunConsoleCommand("playersaving_setup")
  112. timer.Simple(1,Playersaving.RetrieveSettings)
  113. end
  114. end)
  115. else
  116. local res = sql.Query( query )
  117. if res and res[1] then
  118. Playersaving.LoadSettings(res[1])
  119. else
  120. RunConsoleCommand("playersaving_setup")
  121. timer.Simple(1,Playersaving.RetrieveSettings)
  122. end
  123. end
  124. end
  125.  
  126. function Playersaving.LoadSettings( tbl )
  127. Playersaving.BlacklistedWeps = util.JSONToTable(tbl["BlacklistedWeps"])
  128. Playersaving.BlacklistedJobs = util.JSONToTable(tbl["BlacklistedJobs"])
  129. Playersaving.RestrictedJobs_Weapons = util.JSONToTable(tbl["BlacklistedWeps_Jobs"])
  130. Playersaving.AutosaveActive = tobool(tbl["AutosaveActive"])
  131. Playersaving.AutosaveTime = tonumber(tbl["AutosaveTime"])
  132. Playersaving.SaveCommandActive = tobool(tbl["SaveCommandActive"])
  133. Playersaving.SaveCommandTime = tonumber(tbl["SaveCommandTime"])
  134. Playersaving.SaveCommand = tbl["SaveCommand"]
  135. Playersaving.SaveOnDisconnect = tobool(tbl["SaveOnDisconnect"])
  136. Playersaving.SaveOnlyGroups = tobool(tbl["SaveOnlyGroups"])
  137. Playersaving.SaveGroups = util.JSONToTable(tbl["SaveGroups"])
  138. Playersaving.SaveHealth = tobool(tbl["SaveHealth"])
  139. Playersaving.SaveArmor = tobool(tbl["SaveArmor"])
  140. Playersaving.SavePos = tobool(tbl["SavePos"])
  141. Playersaving.SaveWeapons = tobool(tbl["SaveWeapons"])
  142. Playersaving.SaveAmmo = tobool(tbl["SaveAmmo"])
  143. Playersaving.SaveHunger = tobool(tbl["SaveHunger"])
  144. Playersaving.Lang = tbl["Lang"]
  145.  
  146. for k,v in pairs(player.GetAll()) do
  147. v.Playersaving_CanSave = true
  148. if timer.Exists(v:SteamID64().."_Playersaving_Timer") then timer.Remove(v:SteamID64().."_Playersaving_Timer") end
  149. if timer.Exists("Playersaving_CanSave_Timer_"..v:SteamID64()) then timer.Remove("Playersaving_CanSave_Timer_"..v:SteamID64()) end
  150. if Playersaving.AutosaveActive then
  151. timer.Create(v:SteamID64().."_Playersaving_Timer",Playersaving.AutosaveTime,0,function ()
  152. Playersaving.Save ( v )
  153. end)
  154. end
  155. end
  156. Playersaving.LoadLang ()
  157. end
  158.  
  159. if Playersaving.MySQL.Active then
  160. require("tmysql4")
  161. Playersaving.MySQL.Database, error = tmysql.Connect(Playersaving.MySQL.Host, Playersaving.MySQL.User, Playersaving.MySQL.Pass, Playersaving.MySQL.DB, Playersaving.MySQL.Port, nil, CLIENT_MULTI_STATEMENTS)
  162. if error then print(error) end
  163. end
  164.  
  165. -- ACTUAL CODE
  166. function Playersaving.Save ( ply )
  167. if (Playersaving.SaveOnlyGroups and !table.HasValue(Playersaving.SaveGroups,ply:GetUserGroup())) or table.HasValue(Playersaving.BlacklistedJobs,team.GetName(ply:Team())) then ply:ChatPrint(l.Cant2) return end
  168. ply.Playersaving = {}
  169. ply.Playersaving.Weps = {}
  170. ply.Playersaving.BlacklistedWeps = {}
  171. for _,v in pairs(ply:GetWeapons()) do
  172. for k,b in pairs(Playersaving.RestrictedJobs_Weapons) do
  173. if k == v:GetClass() and table.HasValue(b,ply:getJobTable()["name"]) then
  174. table.insert(ply.Playersaving.BlacklistedWeps,k)
  175. end
  176. end
  177. end
  178. for k,v in pairs(ply:GetWeapons()) do
  179. if !table.HasValue(ply.Playersaving.BlacklistedWeps,v:GetClass()) and !table.HasValue(Playersaving.BlacklistedWeps,v:GetClass()) then
  180. table.insert(ply.Playersaving.Weps,v:GetClass())
  181. end
  182. end
  183. ply.Playersaving.Weps = string.Implode(";",ply.Playersaving.Weps)
  184.  
  185. ply.Playersaving.Ammo = {}
  186. for k,v in pairs(ply:GetWeapons()) do
  187. if ply:GetAmmoCount( v:GetPrimaryAmmoType() ) > 0 and !table.HasValue( ply.Playersaving.Ammo,ply:GetAmmoCount( v:GetPrimaryAmmoType() ).."*"..v:GetPrimaryAmmoType() ) then
  188. table.insert( ply.Playersaving.Ammo,ply:GetAmmoCount( v:GetPrimaryAmmoType() ).."*"..v:GetPrimaryAmmoType() )
  189. end
  190. end
  191.  
  192. local pos = ply:GetPos()
  193. ply.Playersaving.posX = pos[1]
  194. ply.Playersaving.posY = pos[2]
  195. ply.Playersaving.posZ = pos[3]
  196.  
  197. local food
  198. if gmod.GetGamemode().Name == "darkrp" and ply:getDarkRPVar("Energy") then
  199. food = ply:getDarkRPVar("Energy")
  200. else
  201. food = 100
  202. end
  203.  
  204. local query = "DELETE FROM playersaving_data WHERE steamID64 = '"..ply:SteamID64().."'; INSERT INTO playersaving_data (steamID64, weapons, ammo, health, armor, food, posX, posY, posZ, map) VALUES ('"..ply:SteamID64().."','"..ply.Playersaving.Weps.."','"..string.Implode(";", ply.Playersaving.Ammo ).."','"..ply:Health().."','"..ply:Armor().."','"..food.."','"..ply.Playersaving.posX.."','"..ply.Playersaving.posY.."','"..ply.Playersaving.posZ.."','"..game.GetMap().."');"
  205.  
  206. if Playersaving.MySQL.Active then
  207. Playersaving.MySQL.Database:Query ( query )
  208. else
  209. sql.Query( query )
  210. end
  211.  
  212. ply:ChatPrint(l.Saved)
  213. end
  214.  
  215. function Playersaving.LoadPlayer ( ply )
  216. local function load ( result )
  217. print("Playersaving")
  218. PrintTable(result)
  219. if !result then return end
  220. ply.Playersaving = {}
  221. if Playersaving.SaveHealth then
  222. ply:SetHealth( tonumber( result["health"] ) )
  223. end
  224. if Playersaving.SaveArmor then
  225. ply:SetArmor( tonumber( result["armor"] ) )
  226. end
  227. if Playersaving.SaveWeapons then
  228. for k,v in pairs( string.Explode(";",result["weapons"]) ) do
  229. ply:Give( v )
  230. end
  231. end
  232. if Playersaving.SaveAmmo and string.find(result["ammo"],"*") then
  233. for k,v in pairs( string.Explode(";",result["ammo"]) ) do
  234. ply:SetAmmo( tonumber( string.Explode("*",v)[1] ), tonumber( string.Explode("*",v)[2] ) )
  235. end
  236. end
  237. if Playersaving.SavePos then
  238. if result["map"] == game.GetMap() then
  239. ply:SetPos(Vector(tonumber(result["posX"]),tonumber(result["posY"]),tonumber(result["posZ"])))
  240. end
  241. end
  242. if Playersaving.SaveFood then
  243. ply:setDarkRPVar("Energy", result["food"])
  244. end
  245. ply:ChatPrint(l.Loaded)
  246. end
  247.  
  248. local query = "SELECT * FROM playersaving_data WHERE steamID64 = '"..ply:SteamID64().."';"
  249. if Playersaving.MySQL.Active then
  250. Playersaving.MySQL.Database:Query ( query, function( res ) if res and res[1] and res[1]["data"] and res[1]["data"][1] then load(res[1]["data"][1]) end end)
  251. else
  252. local res = sql.Query( query )
  253. if res and res[1] then
  254. load( res[1] )
  255. end
  256. end
  257. end
  258.  
  259. function Playersaving.LoadPlayerSpawnFrom ( ply )
  260. local function load ( result )
  261. print("Playersaving")
  262. PrintTable(result)
  263. if !result then return end
  264. ply.Playersaving = {}
  265. if Playersaving.SaveWeapons then
  266. for k,v in pairs( string.Explode(";",result["weapons"]) ) do
  267. ply:Give( v )
  268. end
  269. end
  270. if Playersaving.SaveAmmo and string.find(result["ammo"],"*") then
  271. for k,v in pairs( string.Explode(";",result["ammo"]) ) do
  272. ply:SetAmmo( tonumber( string.Explode("*",v)[1] ), tonumber( string.Explode("*",v)[2] ) )
  273. end
  274. end
  275. end
  276.  
  277. local query = "SELECT * FROM playersaving_data WHERE steamID64 = '"..ply:SteamID64().."';"
  278. if Playersaving.MySQL.Active then
  279. Playersaving.MySQL.Database:Query ( query, function( res ) if res and res[1] and res[1]["data"] and res[1]["data"][1] then load(res[1]["data"][1]) end end)
  280. else
  281. local res = sql.Query( query )
  282. if res and res[1] then
  283. load( res[1] )
  284. end
  285. end
  286. end
  287.  
  288. function Playersaving.Delete ( ply )
  289. local query = "DELETE FROM playersaving_data WHERE steamID64 = '"..ply:SteamID64().."';"
  290. if Playersaving.MySQL.Active then
  291. Playersaving.MySQL.Database:Query ( query )
  292. sql.Query( query )
  293. else
  294. sql.Query( query )
  295. end
  296. end
  297.  
  298. concommand.Add("playersaving_saveall",function ( ply )
  299. if !ply:IsPlayer() or ply:IsSuperAdmin() then
  300. for k,v in pairs( player.GetAll() ) do
  301. Playersaving.Save ( v )
  302. end
  303. end
  304. end)
  305.  
  306. concommand.Add("playersaving_setup",function ( ply )
  307. if !ply:IsPlayer() or ply:IsSuperAdmin() then
  308.  
  309. local query = "INSERT INTO playersaving_settings (BlacklistedWeps, BlacklistedJobs, BlacklistedWeps_Jobs, AutosaveActive, AutosaveTime, SaveCommandActive, SaveCommandTime, SaveCommand, SaveOnDisconnect, SaveOnlyGroups, SaveGroups, Lang, SaveHealth, SaveArmor, SavePos, SaveWeapons, SaveAmmo, SaveHunger) VALUES ('"..util.TableToJSON( Playersaving.BlacklistedWeps ).."', '"..util.TableToJSON( Playersaving.BlacklistedJobs ).."', '"..util.TableToJSON( Playersaving.RestrictedJobs_Weapons ).."', '"..tostring(Playersaving.AutosaveActive).."', '"..tostring(Playersaving.AutosaveTime).."', '"..tostring(Playersaving.SaveCommandActive).."', '"..tostring(Playersaving.SaveCommandTime).."', '"..Playersaving.SaveCommand.."', '"..tostring(Playersaving.SaveOnDisconnect).."', '"..tostring(Playersaving.SaveOnlyGroups).."', '"..util.TableToJSON( Playersaving.SaveGroups ).."', '"..Playersaving.Lang.."', '"..tostring(Playersaving.SaveHealth).."', '"..tostring(Playersaving.SaveArmor).."', '"..tostring(Playersaving.SavePos).."', '"..tostring(Playersaving.SaveWeapons).."', '"..tostring(Playersaving.SaveAmmo).."', '"..tostring(Playersaving.SaveHunger).."');"
  310.  
  311. if Playersaving.MySQL.Active then
  312. Playersaving.MySQL.Database:Query ( query )
  313. else
  314. sql.Query( query )
  315. end
  316. end
  317. end)
  318.  
  319. hook.Add("PlayerInitialSpawn","Playersaving_InitialSpawn",function( ply )
  320. ply.Playersaving_CanSave = true
  321.  
  322. if Playersaving.AutosaveActive then
  323. timer.Create(ply:SteamID64().."_Playersaving_Timer",Playersaving.AutosaveTime,0,function ()
  324. Playersaving.Save ( ply )
  325. end)
  326. end
  327.  
  328. timer.Simple(1,function()
  329. Playersaving.LoadPlayer ( ply )
  330. end)
  331. end)
  332.  
  333. hook.Add("PlayerSpawn","Playersaving_Spawn",function( ply )
  334.  
  335. timer.Simple(6,function()
  336. Playersaving.LoadPlayer ( ply )
  337. end)
  338. end)
  339.  
  340. hook.Add("PlayerDisconnected","Playersaving_Disconnect",function ( ply )
  341. if timer.Exists(ply:SteamID64().."_Playersaving_Timer") then timer.Remove(ply:SteamID64().."_Playersaving_Timer") end
  342. if timer.Exists("Playersaving_CanSave_Timer_"..ply:SteamID64()) then timer.Remove("Playersaving_CanSave_Timer_"..ply:SteamID64()) end
  343. if Playersaving.SaveOnDisconnect then Playersaving.Save ( ply ) end
  344. end)
  345.  
  346. hook.Add("PlayerDeath","Playersaving_Death",Playersaving.Delete)
  347.  
  348. if Playersaving.SaveCommandActive then
  349. hook.Add("PlayerSay","Playersaving_SaveCMD",function( ply, text )
  350. if text == Playersaving.SaveCommand then
  351. if ply.Playersaving_CanSave then
  352. ply.Playersaving_CanSave = false
  353. timer.Create("Playersaving_CanSave_Timer_"..ply:SteamID64(),Playersaving.SaveCommandTime,0,function() ply.Playersaving_CanSave = true end)
  354. Playersaving.Save ( ply )
  355. else
  356. ply:ChatPrint(string.Replace(l.Cant,"%seconds%",tostring(math.Round(timer.TimeLeft("Playersaving_CanSave_Timer_"..ply:SteamID64()),0))))
  357. end
  358. return ""
  359. end
  360. end)
  361. end
  362.  
  363. hook.Add("PlayerSay","Playersaving_AdminMenuOPEN",function( ply, text )
  364. if text == Playersaving.Config_CMD then
  365. if !table.HasValue(Playersaving.Config_Ranks, ply:GetUserGroup()) then return end
  366. local query = "SELECT * FROM playersaving_settings;"
  367. if Playersaving.MySQL.Active then
  368. Playersaving.MySQL.Database:Query ( query, function( res ) net.Start("Playersaving_AdminMenu_Open") net.WriteTable(res[1]["data"][1]) net.Send(ply) end)
  369. else
  370. net.Start("Playersaving_AdminMenu_Open") net.WriteTable(sql.Query( query )[1]) net.Send(ply)
  371. end
  372. return ""
  373. end
  374. end)
  375.  
  376. net.Receive("Playersaving_AdminMenu_Change",function( _, ply )
  377. if !table.HasValue(Playersaving.Config_Ranks, ply:GetUserGroup()) then return end
  378. local t = net.ReadTable()
  379. local query = "DELETE FROM playersaving_settings; INSERT INTO playersaving_settings (BlacklistedWeps, BlacklistedJobs, BlacklistedWeps_Jobs, AutosaveActive, AutosaveTime, SaveCommandActive, SaveCommandTime, SaveCommand, SaveOnDisconnect, SaveOnlyGroups, SaveGroups, Lang, SaveHealth, SaveArmor, SavePos, SaveWeapons, SaveAmmo, SaveHunger) VALUES ('"..t['BlacklistedWeps'].."', '"..t['BlacklistedJobs'].."', '"..t['BlacklistedWeps_Jobs'].."', '"..t['AutosaveActive'].."', '"..t['AutosaveTime'].."', '"..t['SaveCommandActive'].."', '"..t['SaveCommandTime'].."', '"..t['SaveCommand'].."', '"..t['SaveOnDisconnect'].."', '"..t['SaveOnlyGroups'].."', '"..t['SaveGroups'].."', '"..t['Lang'].."', '"..t['SaveHealth'].."', '"..t['SaveArmor'].."', '"..t['SavePos'].."', '"..t['SaveWeapons'].."', '"..t['SaveAmmo'].."', '"..t['SaveHunger'].."');"
  380.  
  381. if Playersaving.MySQL.Active then
  382. Playersaving.MySQL.Database:Query ( query, Playersaving.RetrieveSettings)
  383. else
  384. sql.Query( query )
  385. Playersaving.RetrieveSettings()
  386. end
  387. end)
  388.  
  389. Playersaving.Load()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement