Advertisement
Guest User

Server.lua

a guest
Sep 1st, 2014
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1.  
  2. if not SERVER then return end
  3.  
  4. util.AddNetworkString( "npcshop_senddata" )
  5.  
  6. function SpawnnpcShop()
  7. local shop = ents.Create("npc_shop")
  8. shop:SetPos( Vector(5094.956055, -3482.516602, 292.031250) ) --Don't forget spawn vectors here!
  9. shop:SetAngles( Angle(8.140065, -90.498817, 0.000000) )
  10. shop:Spawn()
  11. shop:DropToFloor()
  12. end
  13.  
  14. hook.Add("InitPostEntity", "SpawnnpcShop", SpawnnpcShop)
  15.  
  16. hook.Add("EntityTakeDamage", "PReventNPCfromdying", function( target, dmginfo )
  17. if target:IsNPC() and target:GetClass() == "npc_shop" then
  18. dmginfo:ScaleDamage(0)
  19. end
  20. end)
  21.  
  22. local function IsDonator(ply)
  23. local isdonator = false
  24. for k,v in pairs(NPCSHOP.UserGroups) do
  25. if ply:GetUserGroup() == v then
  26. isdonator = true
  27. break
  28. end
  29. end
  30. return isdonator
  31. end
  32.  
  33. //Utilizes darkrp's notify
  34. local function Notify(ply, text)
  35. if not IsValid(ply) then return end
  36. umsg.Start("_Notify", ply)
  37. umsg.String(text)
  38. umsg.Short(0)
  39. umsg.Long(4)
  40. umsg.End()
  41. end
  42.  
  43. local function SaveVehicles()
  44. local str = util.TableToJSON(NPCSHOP.PlayerVehicles)
  45. file.Write( "npcshopsaves.txt", str )
  46. end
  47. local function LoadVehicles()
  48. local str = file.Read( "npcshopsaves.txt", "DATA" ) or "[]"
  49. NPCSHOP.PlayerVehicles = util.JSONToTable(str)
  50. end
  51. LoadVehicles()
  52. function SendVehicles(ply)
  53. local sid = ply:SteamID()
  54. local cars = NPCSHOP.PlayerVehicles[sid] or {}
  55.  
  56. net.Start("npcshop_senddata")
  57. net.WriteTable(cars)
  58. net.Send(ply)
  59. end
  60.  
  61. hook.Add("PlayerInitialSpawn", "plyinitspawnnpcshop", function(ply)
  62. timer.Simple(2, function()
  63. SendVehicles(ply)
  64. end)
  65. end)
  66.  
  67. local meta = FindMetaTable("Player")
  68. function meta:AddVehicle( class )
  69. local sid = self:SteamID()
  70. if not NPCSHOP.PlayerVehicles[sid] then NPCSHOP.PlayerVehicles[sid] = {} end
  71.  
  72. NPCSHOP.PlayerVehicles[sid][class] = true
  73. SaveVehicles()
  74. SendVehicles(self)
  75. end
  76.  
  77. local function SpawnVehicle(ply, class)
  78. if not NPCSHOP.VehicleLookup[class] then return end
  79. if not NPCSHOP.Vehicles[NPCSHOP.VehicleLookup[class]] then return end
  80.  
  81. if IsValid(ply.currentcar) then
  82. local d = ply.currentcar:GetDriver()
  83. if IsValid(d) and d != ply then
  84. Notify(d, "This car has been removed by its owner!")
  85. end
  86. ply.currentcar:Remove()
  87. end
  88.  
  89. local vehicle = list.Get("Vehicles")[class]
  90. if not vehicle then return end
  91.  
  92. local car = ents.Create(vehicle.Class)
  93. if not car then return end
  94.  
  95. car:SetModel(vehicle.Model)
  96.  
  97. if vehicle.KeyValues then
  98. for k, v in pairs(vehicle.KeyValues) do
  99. car:SetKeyValue(k, v)
  100. end
  101. end
  102.  
  103. car.VehicleName = class
  104. car.VehicleTable = vehicle
  105. car.Owner = ply
  106.  
  107. local carspawns = NPCSHOP.CarSpawn[game.GetMap()]
  108. local pos = carspawns.pos
  109. local ang = carspawns.ang
  110.  
  111. car:SetPos()
  112. car:SetAngles()
  113.  
  114. car:Spawn()
  115. car:Activate()
  116. car:SetCollisionGroup(COLLISION_GROUP_WEAPON)
  117. car.SID = ply.SID
  118. car.ClassOverride = vehicle.Class
  119. if vehicle.Members then
  120. table.Merge(car, vehicle.Members)
  121. end
  122. car:keysOwn(ply)
  123. gamemode.Call("PlayerSpawnedVehicle", ply, car)
  124.  
  125. ply.currentcar = car
  126. end
  127.  
  128. concommand.Add("_npcshopbtnclick", function(ply, _, args)
  129. if #args != 1 then return end
  130. if not IsValid(ply) then return end
  131.  
  132. if ply:GetPos():Distance(ents.FindByClass("npc_shop")[1]:GetPos()) > 80 then return end
  133.  
  134. local class = args[1]
  135. if not NPCSHOP.VehicleLookup[class] then return end
  136. if not NPCSHOP.Vehicles[NPCSHOP.VehicleLookup[class]] then return end
  137.  
  138. local cltbl = NPCSHOP.Vehicles[NPCSHOP.VehicleLookup[class]]
  139.  
  140. if #cltbl.job > 0 then
  141. if not table.HasValue(cltbl.job, ply:Team()) then
  142. Notify(ply, "You're not in the correct job to spawn/purchase this!")
  143. return
  144. end
  145. end
  146.  
  147. if ply:OwnsVehicle(class) then
  148. SpawnVehicle(ply, class)
  149. return
  150. end
  151.  
  152. if cltbl.donatoronly and not IsDonator(ply) then
  153. Notify(ply, "You need to be Donator to buy this vehicle!")
  154. return
  155. end
  156.  
  157. if not ply:canAfford(cltbl.price) then
  158. Notify(ply, "You do not have sufficient funds to purchase this!")
  159. return
  160. end
  161.  
  162. ply:addMoney(-cltbl.price)
  163. Notify(ply, "You've bought the '" .. cltbl.name .. "' for "..(CUR or "$")..(cltbl.price).."!")
  164. ply:AddVehicle(class)
  165.  
  166. umsg.Start("_updatenpcshopgui", ply)
  167. umsg.String(class)
  168. umsg.End()
  169. end)
  170.  
  171. // DarkRP doesn't give me any way to check for job changes, then this shit is needed!
  172.  
  173. local bkp = meta.SetTeam
  174. meta.SetTeam = function(self, job)
  175. bkp(self, job)
  176.  
  177. if IsValid(self.currentcar) then
  178. local class = self.currentcar.VehicleName
  179. local cltbl = NPCSHOP.Vehicles[NPCSHOP.VehicleLookup[class]]
  180.  
  181. if #cltbl.job > 0 then
  182. if not table.HasValue(cltbl.job, self:Team()) then
  183. self.currentcar:Remove()
  184. Notify(self, "Your current car isn't allowed for your new job!")
  185. return
  186. end
  187. end
  188. end
  189. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement