Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. if not SERVER then return end
  2.  
  3. CONFIG = {}
  4. CONFIG.saveweapons = true -- true if you want to save the player's weapons ans false if you don't
  5. CONFIG.saveteam = true -- true if you want to save the player's team and false if you don't
  6. CONFIG.savepos = true -- true if you want to save the player's pos and false if you don't
  7. CONFIG.savemodel = true -- true if you want to save the player's model and false if you don't
  8. CONFIG.savehealth = true -- true if you want to save the player's health and false if you don't
  9. CONFIG.savearmor = true -- true if you want to save the player's armor and false if you don't
  10. CONFIG.teams = {TEAM_CITIZEN} --team that doesn't use the save spawn/weapon/team
  11. CONFIG.auto_time = 600 --Time between each auto saves (IN SECONDS)
  12. CONFIG.print = true -- Print a message in the console when the server as saved the player's data
  13. CONFIG.dontsavedamage = true -- If the player will not save anything after being damaged (cooldown in Seconds)
  14. CONFIG.dontsavecooldown = 60 -- see above, it's the cooldown when he will be able to disconnect and has is data saved
  15. CONFIG.cooldownfinish = "You can now disconnect, your data will be saved." -- sentence when the cooldown is finish
  16. CONFIG.cooldownstart = "Wait "..CONFIG.dontsavecooldown.." before leaving, else it will not save your data" -- sentence when the cooldown starts
  17.  
  18.  
  19. local plyMeta = FindMetaTable("Player")
  20.  
  21.  
  22. function plyMeta:LoadThings()
  23.  
  24. local read = file.Read( "save_module/"..string.lower(game.GetMap()).."/" .. self:SteamID64() .. ".txt", "DATA" )
  25. if not file.Exists( "save_module", "DATA" ) then file.CreateDir( "save_module", "DATA" ) return end
  26. if not file.Exists("save_module/"..string.lower(game.GetMap()),"DATA") then file.CreateDir( "save_module/"..string.lower(game.GetMap()), "DATA" ) end
  27. if not file.Exists("save_module/"..string.lower(game.GetMap()).."/"..self:SteamID64()..".txt", "DATA") then
  28. local data = {pos = Vector(0,0,0), team = TEAM_MINEUR, weapon = {""}}
  29. file.Write("save_module/"..string.lower(game.GetMap()).."/"..self:SteamID64()..".txt", util.TableToJSON(data) ) return
  30. end
  31.  
  32. local things = util.JSONToTable(read)
  33.  
  34. return things
  35. end
  36.  
  37. function plyMeta:SaveThings()
  38. if not self:Alive() then return end
  39. if not file.Exists( "save_module", "DATA" ) then file.CreateDir( "save_module", "DATA" ) end
  40. if not file.Exists("save_module/"..string.lower(game.GetMap()),"DATA") then file.CreateDir( "save_module/"..string.lower(game.GetMap()), "DATA" ) end
  41. local weapondata ={}
  42. local ammodata = {}
  43. local datapos, datateam, datamodel, dataarmor, datahealth
  44. if CONFIG.saveweapons then
  45. for k,v in ipairs(self:GetWeapons()) do
  46. table.insert(weapondata,k, v:GetClass())
  47.  
  48. if v:GetPrimaryAmmoType() != -1 then
  49. if ammodata[v:GetPrimaryAmmoType()] then
  50. if ammodata[v:GetPrimaryAmmoType()].ammotype then
  51. if ammodata[v:GetPrimaryAmmoType()].ammotype == v:GetPrimaryAmmoType() then continue end
  52. end
  53. end
  54. table.insert(ammodata,v:GetPrimaryAmmoType(), {ammotype = v:GetPrimaryAmmoType(), ammonum = self:GetAmmoCount(v:GetPrimaryAmmoType())})
  55. end
  56. end
  57. end
  58. if CONFIG.savepos then datapos = self:GetPos() end
  59. if CONFIG.savemodel then datamodel = self:GetModel() end
  60. if CONFIG.saveteam then datateam = self:Team() end
  61. if CONFIG.savehealth then datahealth = {hp = self:Health(),maxhp = self:GetMaxHealth()} end
  62. if CONFIG.savearmor then dataarmor = self:Armor() end
  63. local data = {pos = datapos, team = datateam, weapon = weapondata, ammo = ammodata, model = datamodel, health = datahealth, armor = dataarmor}
  64.  
  65. if self:GetNWBool("savecooldown") then data = {"no"} end
  66.  
  67. file.Write("save_module/"..string.lower(game.GetMap()).."/"..self:SteamID64()..".txt", util.TableToJSON(data) )
  68. end
  69.  
  70. hook.Add("PlayerDisconnected", "savethings", function(ply)
  71. ply:SaveThings()
  72.  
  73. end)
  74.  
  75. hook.Add("ShutDown", "ShutDownSaveThings", function()
  76.  
  77. for k,v in pairs(player.GetAll()) do
  78. v:SaveThings()
  79. end
  80. if CONFIG.print then
  81. print("[SAVE MODULE]DATA SAVED FOR ALL PLAYERS")
  82. end
  83.  
  84. end)
  85.  
  86. hook.Add("PlayerInitialSpawn", "loadthings", function(ply)
  87.  
  88. local data = ply:LoadThings()
  89. if not data then return end
  90. if data == "no" then return end
  91. if table.HasValue(CONFIG.teams, data.team) then return end
  92.  
  93. timer.Simple(1, function()
  94. if CONFIG.saveteam then
  95. ply:SetTeam(data.team)
  96. ply:ConCommand("say /job "..team.GetName(data.team))
  97. ply:Spawn()
  98. end
  99.  
  100. if CONFIG.savepos then
  101. ply:SetPos(data.pos)
  102. end
  103.  
  104. if CONFIG.saveweapons then
  105. if data.weapon then
  106. for k,v in pairs(data.weapon) do
  107. ply:Give(v)
  108. end
  109. for k,v in pairs(data.ammo) do
  110. ply:SetAmmo(v.ammonum,v.ammotype)
  111. end
  112. end
  113. end
  114.  
  115. if CONFIG.savemodel then
  116. ply:SetModel(data.model)
  117. end
  118.  
  119. if CONFIG.savehealth then
  120. ply:SetMaxHealth(data.health.maxhp)
  121. ply:SetHealth(data.health.hp)
  122. end
  123.  
  124. if CONFIG.savearmor then
  125. ply:SetArmor(data.armor)
  126. end
  127. end)
  128. end)
  129.  
  130. hook.Add("EntityTakeDamage", "DontSaveWhenDamage", function(target, damage)
  131. if !CONFIG.dontsavedamage then return end
  132. if !target:IsPlayer() then return end
  133. if damage:GetAttacker() == target then return end
  134. if not (damage:GetAttacker():IsNPC() or damage:GetAttacker():IsPlayer()) then return end
  135. if target:GetNWBool("savecooldown", false) then
  136. timer.Adjust("savecooldown"..target:UniqueID(), CONFIG.dontsavecooldown, 1, function() if IsValid(target) then target:PrintMessage(HUD_PRINTTALK,CONFIG.cooldownfinish) target:SetNWBool("savecooldown", false) end end)
  137. else
  138. target:SetNWBool("savecooldown", true)
  139. target:PrintMessage(HUD_PRINTTALK,CONFIG.cooldownstart)
  140. timer.Create("savecooldown"..target:UniqueID(), CONFIG.dontsavecooldown, 1, function() if IsValid(target) then target:PrintMessage(HUD_PRINTTALK,CONFIG.cooldownfinish) target:SetNWBool("savecooldown", false) end end)
  141. end
  142. end)
  143.  
  144. hook.Add("PlayerDeath", "removecooldowndeath", function(ply)
  145. if timer.Exists("savecooldown"..ply:UniqueID()) then timer.Destroy("savecooldown"..ply:UniqueID()) target:SetNWBool("savecooldown", false) end
  146.  
  147. end)
  148.  
  149. timer.Create("SaveThingsAuto", CONFIG.auto_time, 0, function()
  150. local time = 0
  151. for k,v in pairs(player.GetAll()) do
  152. timer.Simple(time, function()
  153. v:SaveThings()
  154. end)
  155. time = time+0.5
  156. end
  157. if CONFIG.print then
  158. print("[SAVE MODULE]DATA SAVED FOR ALL PLAYERS")
  159. end
  160.  
  161. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement