Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.63 KB | None | 0 0
  1. local Player = FindMetaTable('Player')
  2.  
  3. function Player:PS_PlayerSpawn()
  4. if not self:PS_CanPerformAction() then return end
  5.  
  6. -- TTT ( and others ) Fix
  7. if TEAM_SPECTATOR != nil and self:Team() == TEAM_SPECTATOR then return end
  8. if TEAM_SPEC != nil and self:Team() == TEAM_SPEC then return end
  9. if TEAM_PROPS != nil and self:Team() == TEAM_PROPS then return end
  10. if TEAM_HIDING != nil and self:Team() == TEAM_HIDING then return end
  11.  
  12. -- Murder Spectator Fix (they don't specify the above enums when making teams)
  13. -- https://github.com/mechanicalmind/murder/blob/master/gamemode/sv_spectate.lua#L15
  14. if self.Spectating then return end
  15.  
  16. timer.Simple(1, function()
  17. if !IsValid(self) then return end
  18. for item_id, item in pairs(self.PS_Items) do
  19. local ITEM = PS.Items[item_id]
  20. if item.Equipped then
  21. ITEM:OnEquip(self, item.Modifiers)
  22. end
  23. end
  24. end)
  25. end
  26.  
  27. function Player:PS_PlayerDeath()
  28. for item_id, item in pairs(self.PS_Items) do
  29. if item.Equipped then
  30. local ITEM = PS.Items[item_id]
  31. ITEM:OnHolster(self, item.Modifiers)
  32. end
  33. end
  34. end
  35.  
  36. function Player:PS_PlayerInitialSpawn()
  37. self.PS_Points = 0
  38. self.PS_Items = {}
  39.  
  40. -- Send stuff
  41. timer.Simple(1, function()
  42. if !IsValid(self) then return end
  43.  
  44. self:PS_LoadData()
  45. self:PS_SendClientsideModels()
  46. end)
  47.  
  48. if PS.Config.NotifyOnJoin then
  49. if PS.Config.ShopKey ~= '' then
  50. timer.Simple(5, function() -- Give them time to load up
  51. if !IsValid(self) then return end
  52. self:PS_Notify('Press ' .. PS.Config.ShopKey .. ' to open the Credit Store!')
  53. end)
  54. end
  55.  
  56. if PS.Config.ShopCommand ~= '' then
  57. timer.Simple(5, function() -- Give them time to load up
  58. if !IsValid(self) then return end
  59. self:PS_Notify('Type ' .. PS.Config.ShopCommand .. ' in console to open the Credit Store!')
  60. end)
  61. end
  62.  
  63. if PS.Config.ShopChatCommand ~= '' then
  64. timer.Simple(5, function() -- Give them time to load up
  65. if !IsValid(self) then return end
  66. self:PS_Notify('Type ' .. PS.Config.ShopChatCommand .. ' in chat to open the Credit Store!')
  67. end)
  68. end
  69.  
  70. timer.Simple(10, function() -- Give them time to load up
  71. if !IsValid(self) then return end
  72. self:PS_Notify('You have ' .. self:PS_GetPoints() .. ' ' .. PS.Config.PointsName .. ' to spend!')
  73. end)
  74. end
  75.  
  76. if PS.Config.CheckVersion and PS.BuildOutdated and self:IsAdmin() then
  77. timer.Simple(5, function()
  78. if !IsValid(self) then return end
  79. self:PS_Notify("PointShop is out of date, please tell the server owner!")
  80. end)
  81. end
  82.  
  83. if PS.Config.PointsOverTime then
  84. timer.Create('PS_PointsOverTime_' .. self:UniqueID(), PS.Config.PointsOverTimeDelay * 60, 0, function()
  85. if !IsValid(self) then return end
  86. if ply:IsUserGroup( "vip" ) then
  87. self:PS_GivePoints(PS.Config.PointsOverTimeAmountVip)
  88. else
  89. self:PS_GivePoints(PS.Config.PointsOverTimeAmount)
  90. end
  91. self:PS_Notify("You've been given ", PS.Config.PointsOverTimeAmount, " ", PS.Config.PointsName, " for playing on the server!")
  92. end)
  93. end
  94.  
  95. end
  96.  
  97. function Player:PS_PlayerDisconnected()
  98. PS.ClientsideModels[self] = nil
  99.  
  100. if timer.Exists('PS_PointsOverTime_' .. self:UniqueID()) then
  101. timer.Destroy('PS_PointsOverTime_' .. self:UniqueID())
  102. end
  103. end
  104.  
  105. function Player:PS_Save()
  106. PS:SetPlayerData(self, self.PS_Points, self.PS_Items)
  107. end
  108.  
  109. function Player:PS_LoadData()
  110. self.PS_Points = 0
  111. self.PS_Items = {}
  112.  
  113. PS:GetPlayerData(self, function(points, items)
  114. self.PS_Points = points
  115. self.PS_Items = items
  116.  
  117. self:PS_SendPoints()
  118. self:PS_SendItems()
  119. end)
  120. end
  121.  
  122. function Player:PS_CanPerformAction(itemname)
  123. local allowed = true
  124. local itemexcept = false
  125. if itemname then itemexcept = PS.Items[itemname].Except end
  126.  
  127. if (self.IsSpec and self:IsSpec()) and not itemexcept then allowed = false end
  128. if not self:Alive() and not itemexcept then allowed = false end
  129.  
  130.  
  131. if not allowed then
  132. self:PS_Notify('You\'re not allowed to do that at the moment!')
  133. end
  134.  
  135. return allowed
  136. end
  137.  
  138. -- points
  139.  
  140. function Player:PS_GivePoints(points)
  141. self.PS_Points = self.PS_Points + points
  142. PS:GivePlayerPoints(self, points)
  143. self:PS_SendPoints()
  144. end
  145.  
  146. function Player:PS_TakePoints(points)
  147. self.PS_Points = self.PS_Points - points >= 0 and self.PS_Points - points or 0
  148. PS:TakePlayerPoints(self, points)
  149. self:PS_SendPoints()
  150. end
  151.  
  152. function Player:PS_SetPoints(points)
  153. self.PS_Points = points
  154. PS:SetPlayerPoints(self, points)
  155. self:PS_SendPoints()
  156. end
  157.  
  158. function Player:PS_GetPoints()
  159. return self.PS_Points and self.PS_Points or 0
  160. end
  161.  
  162. function Player:PS_HasPoints(points)
  163. return self.PS_Points >= points
  164. end
  165.  
  166. -- give/take items
  167.  
  168. function Player:PS_GiveItem(item_id)
  169. if not PS.Items[item_id] then return false end
  170.  
  171. self.PS_Items[item_id] = { Modifiers = {}, Equipped = false }
  172.  
  173. PS:GivePlayerItem(self, item_id, self.PS_Items[item_id])
  174.  
  175. self:PS_SendItems()
  176.  
  177. return true
  178. end
  179.  
  180. function Player:PS_TakeItem(item_id)
  181. if not PS.Items[item_id] then return false end
  182. if not self:PS_HasItem(item_id) then return false end
  183.  
  184. self.PS_Items[item_id] = nil
  185.  
  186. PS:TakePlayerItem(self, item_id)
  187.  
  188. self:PS_SendItems()
  189.  
  190. return true
  191. end
  192.  
  193. -- buy/sell items
  194.  
  195. function Player:PS_BuyItem(item_id)
  196. local ITEM = PS.Items[item_id]
  197. if not ITEM then return false end
  198.  
  199. local points = PS.Config.CalculateBuyPrice(self, ITEM)
  200.  
  201. if not self:PS_HasPoints(points) then return false end
  202. if not self:PS_CanPerformAction(item_id) then return end
  203.  
  204. if ITEM.AdminOnly and not self:IsAdmin() then
  205. self:PS_Notify('This item is Admin only!')
  206. return false
  207. end
  208.  
  209. if ITEM.AllowedUserGroups and #ITEM.AllowedUserGroups > 0 then
  210. if not table.HasValue(ITEM.AllowedUserGroups, self:PS_GetUsergroup()) then
  211. self:PS_Notify('You\'re not in the right group to buy this item!')
  212. return false
  213. end
  214. end
  215.  
  216. local cat_name = ITEM.Category
  217. local CATEGORY = PS:FindCategoryByName(cat_name)
  218.  
  219. if CATEGORY.AllowedUserGroups and #CATEGORY.AllowedUserGroups > 0 then
  220. if not table.HasValue(CATEGORY.AllowedUserGroups, self:PS_GetUsergroup()) then
  221. self:PS_Notify('You\'re not in the right group to buy this item!')
  222. return false
  223. end
  224. end
  225.  
  226. if CATEGORY.CanPlayerSee then
  227. if not CATEGORY:CanPlayerSee(self) then
  228. self:PS_Notify('You\'re not allowed to buy this item!')
  229. return false
  230. end
  231. end
  232.  
  233. if ITEM.CanPlayerBuy then -- should exist but we'll check anyway
  234. local allowed, message
  235. if ( type(ITEM.CanPlayerBuy) == "function" ) then
  236. allowed, message = ITEM:CanPlayerBuy(self)
  237. elseif ( type(ITEM.CanPlayerBuy) == "boolean" ) then
  238. allowed = ITEM.CanPlayerBuy
  239. end
  240.  
  241. if not allowed then
  242. self:PS_Notify(message or 'You\'re not allowed to buy this item!')
  243. return false
  244. end
  245. end
  246.  
  247. self:PS_TakePoints(points)
  248.  
  249. self:PS_Notify('Bought ', ITEM.Name, ' for ', points, ' ', PS.Config.PointsName)
  250.  
  251. ITEM:OnBuy(self)
  252.  
  253. if ITEM.SingleUse then
  254. self:PS_Notify('Single use item. You\'ll have to buy this item again next time!')
  255. return
  256. end
  257.  
  258. self:PS_GiveItem(item_id)
  259. self:PS_EquipItem(item_id)
  260. end
  261.  
  262. function Player:PS_SellItem(item_id)
  263. if not PS.Items[item_id] then return false end
  264. if not self:PS_HasItem(item_id) then return false end
  265.  
  266. local ITEM = PS.Items[item_id]
  267.  
  268. if ITEM.CanPlayerSell then -- should exist but we'll check anyway
  269. local allowed, message
  270. if ( type(ITEM.CanPlayerSell) == "function" ) then
  271. allowed, message = ITEM:CanPlayerSell(self)
  272. elseif ( type(ITEM.CanPlayerSell) == "boolean" ) then
  273. allowed = ITEM.CanPlayerSell
  274. end
  275.  
  276. if not allowed then
  277. self:PS_Notify(message or 'You\'re not allowed to sell this item!')
  278. return false
  279. end
  280. end
  281.  
  282. local points = PS.Config.CalculateSellPrice(self, ITEM)
  283. self:PS_GivePoints(points)
  284.  
  285. ITEM:OnHolster(self)
  286. ITEM:OnSell(self)
  287.  
  288. self:PS_Notify('Sold ', ITEM.Name, ' for ', points, ' ', PS.Config.PointsName)
  289.  
  290. return self:PS_TakeItem(item_id)
  291. end
  292.  
  293. function Player:PS_HasItem(item_id)
  294. return self.PS_Items[item_id] or false
  295. end
  296.  
  297. function Player:PS_HasItemEquipped(item_id)
  298. if not self:PS_HasItem(item_id) then return false end
  299.  
  300. return self.PS_Items[item_id].Equipped or false
  301. end
  302.  
  303. function Player:PS_NumItemsEquippedFromCategory(cat_name)
  304. local count = 0
  305.  
  306. for item_id, item in pairs(self.PS_Items) do
  307. local ITEM = PS.Items[item_id]
  308. if ITEM.Category == cat_name and item.Equipped then
  309. count = count + 1
  310. end
  311. end
  312.  
  313. return count
  314. end
  315.  
  316. -- equip/hoster items
  317.  
  318. function Player:PS_EquipItem(item_id)
  319. if not PS.Items[item_id] then return false end
  320. if not self:PS_HasItem(item_id) then return false end
  321. if not self:PS_CanPerformAction(item_id) then return false end
  322.  
  323. local ITEM = PS.Items[item_id]
  324.  
  325. if type(ITEM.CanPlayerEquip) == 'function' then
  326. allowed, message = ITEM:CanPlayerEquip(self)
  327. elseif type(ITEM.CanPlayerEquip) == 'boolean' then
  328. allowed = ITEM.CanPlayerEquip
  329. end
  330.  
  331. if not allowed then
  332. self:PS_Notify(message or 'You\'re not allowed to equip this item!')
  333. return false
  334. end
  335.  
  336. local cat_name = ITEM.Category
  337. local CATEGORY = PS:FindCategoryByName(cat_name)
  338.  
  339. if CATEGORY and CATEGORY.AllowedEquipped > -1 then
  340. if self:PS_NumItemsEquippedFromCategory(cat_name) + 1 > CATEGORY.AllowedEquipped then
  341. self:PS_Notify('Only ' .. CATEGORY.AllowedEquipped .. ' item' .. (CATEGORY.AllowedEquipped == 1 and '' or 's') .. ' can be equipped from this category!')
  342. return false
  343. end
  344. end
  345.  
  346. if CATEGORY.SharedCategories then
  347. local ConCatCats = CATEGORY.Name
  348. for p, c in pairs( CATEGORY.SharedCategories ) do
  349. if p ~= #CATEGORY.SharedCategories then
  350. ConCatCats = ConCatCats .. ', ' .. c
  351. else
  352. if #CATEGORY.SharedCategories ~= 1 then
  353. ConCatCats = ConCatCats .. ', and ' .. c
  354. else
  355. ConCatCats = ConCatCats .. ' and ' .. c
  356. end
  357. end
  358. end
  359. local NumEquipped = self.PS_NumItemsEquippedFromCategory
  360. for id, item in pairs(self.PS_Items) do
  361. if not self:PS_HasItemEquipped(id) then continue end
  362. local CatName = PS.Items[id].Category
  363. local Cat = PS:FindCategoryByName( CatName )
  364. if not Cat.SharedCategories then continue end
  365. for _, SharedCategory in pairs( Cat.SharedCategories ) do
  366. if SharedCategory == CATEGORY.Name then
  367. if Cat.AllowedEquipped > -1 and CATEGORY.AllowedEquipped > -1 then
  368. if NumEquipped(self,CatName) + NumEquipped(self,CATEGORY.Name) + 1 > Cat.AllowedEquipped then
  369. self:PS_Notify('Only ' .. Cat.AllowedEquipped .. ' item'.. (Cat.AllowedEquipped == 1 and '' or 's') ..' can be equipped over ' .. ConCatCats .. '!')
  370. return false
  371. end
  372. end
  373. end
  374. end
  375. end
  376. end
  377.  
  378. self.PS_Items[item_id].Equipped = true
  379.  
  380. ITEM:OnEquip(self, self.PS_Items[item_id].Modifiers)
  381.  
  382. self:PS_Notify('Equipped ', ITEM.Name, '.')
  383.  
  384. PS:SavePlayerItem(self, item_id, self.PS_Items[item_id])
  385.  
  386. self:PS_SendItems()
  387. end
  388.  
  389. function Player:PS_HolsterItem(item_id)
  390. if not PS.Items[item_id] then return false end
  391. if not self:PS_HasItem(item_id) then return false end
  392. if not self:PS_CanPerformAction(item_id) then return false end
  393.  
  394. self.PS_Items[item_id].Equipped = false
  395.  
  396. local ITEM = PS.Items[item_id]
  397.  
  398. if type(ITEM.CanPlayerHolster) == 'function' then
  399. allowed, message = ITEM:CanPlayerHolster(self)
  400. elseif type(ITEM.CanPlayerHolster) == 'boolean' then
  401. allowed = ITEM.CanPlayerHolster
  402. end
  403.  
  404. if not allowed then
  405. self:PS_Notify(message or 'You\'re not allowed to holster this item!')
  406. return false
  407. end
  408.  
  409. ITEM:OnHolster(self)
  410.  
  411. self:PS_Notify('Holstered ', ITEM.Name, '.')
  412.  
  413. PS:SavePlayerItem(self, item_id, self.PS_Items[item_id])
  414.  
  415. self:PS_SendItems()
  416. end
  417.  
  418. -- modify items
  419.  
  420. function Player:PS_ModifyItem(item_id, modifications)
  421. if not PS.Items[item_id] then return false end
  422. if not self:PS_HasItem(item_id) then return false end
  423. if not type(modifications) == "table" then return false end
  424. if not self:PS_CanPerformAction(item_id) then return false end
  425.  
  426. local ITEM = PS.Items[item_id]
  427.  
  428. for key, value in pairs(modifications) do
  429. self.PS_Items[item_id].Modifiers[key] = value
  430. end
  431.  
  432. ITEM:OnModify(self, self.PS_Items[item_id].Modifiers)
  433.  
  434. PS:SavePlayerItem(self, item_id, self.PS_Items[item_id])
  435.  
  436. self:PS_SendItems()
  437. end
  438.  
  439. -- clientside Models
  440.  
  441. function Player:PS_AddClientsideModel(item_id)
  442. if not PS.Items[item_id] then return false end
  443. if not self:PS_HasItem(item_id) then return false end
  444.  
  445. net.Start('PS_AddClientsideModel')
  446. net.WriteEntity(self)
  447. net.WriteString(item_id)
  448. net.Broadcast()
  449.  
  450. if not PS.ClientsideModels[self] then PS.ClientsideModels[self] = {} end
  451.  
  452. PS.ClientsideModels[self][item_id] = item_id
  453. end
  454.  
  455. function Player:PS_RemoveClientsideModel(item_id)
  456. if not PS.Items[item_id] then return false end
  457. if not self:PS_HasItem(item_id) then return false end
  458. if not PS.ClientsideModels[self] or not PS.ClientsideModels[self][item_id] then return false end
  459.  
  460. net.Start('PS_RemoveClientsideModel')
  461. net.WriteEntity(self)
  462. net.WriteString(item_id)
  463. net.Broadcast()
  464.  
  465. PS.ClientsideModels[self][item_id] = nil
  466. end
  467.  
  468. -- menu stuff
  469.  
  470. function Player:PS_ToggleMenu(show)
  471. net.Start('PS_ToggleMenu')
  472. net.Send(self)
  473. end
  474.  
  475. -- send stuff
  476.  
  477. function Player:PS_SendPoints()
  478. net.Start('PS_Points')
  479. net.WriteEntity(self)
  480. net.WriteInt(self.PS_Points, 32)
  481. net.Broadcast()
  482. end
  483.  
  484. function Player:PS_SendItems()
  485. net.Start('PS_Items')
  486. net.WriteEntity(self)
  487. net.WriteTable(self.PS_Items)
  488. net.Broadcast()
  489. end
  490.  
  491. function Player:PS_SendClientsideModels()
  492. net.Start('PS_SendClientsideModels')
  493. net.WriteTable(PS.ClientsideModels)
  494. net.Send(self)
  495. end
  496.  
  497. -- notifications
  498.  
  499. function Player:PS_Notify(...)
  500. local str = table.concat({...}, '')
  501.  
  502. net.Start('PS_SendNotification')
  503. net.WriteString(str)
  504. net.Send(self)
  505. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement