Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. --[[
  2. pointshop/sh_init.lua
  3. first file included on both states.
  4. ]]--
  5.  
  6. PS = {}
  7. PS.__index = PS
  8.  
  9. PS.Items = {}
  10. PS.Categories = {}
  11. PS.ClientsideModels = {}
  12.  
  13. include("sh_config.lua")
  14. include("sh_player_extension.lua")
  15.  
  16. -- validation
  17.  
  18. function PS:ValidateItems(items)
  19. if type(items) ~= 'table' then return {} end
  20.  
  21. -- Remove any items that no longer exist
  22. for item_id, item in pairs(items) do
  23. if not self.Items[item_id] then
  24. items[item_id] = nil
  25. end
  26. end
  27.  
  28. return items
  29. end
  30.  
  31. function PS:ValidatePoints(points)
  32. if type(points) != 'number' then return 0 end
  33.  
  34. return points >= 0 and points or 0
  35. end
  36.  
  37. -- Utils
  38.  
  39. function PS:FindCategoryByName(cat_name)
  40. for id, cat in pairs(self.Categories) do
  41. if cat.Name == cat_name then
  42. return cat
  43. end
  44. end
  45.  
  46. return false
  47. end
  48.  
  49. -- Initialization
  50.  
  51. function PS:Initialize()
  52. if SERVER then self:LoadDataProvider() end
  53. if SERVER and self.Config.CheckVersion then self:CheckVersion() end
  54.  
  55. self:LoadItems()
  56. end
  57.  
  58. -- Loading
  59.  
  60. function PS:LoadItems()
  61. local _, dirs = file.Find('pointshop/items/*', 'LUA')
  62.  
  63. for _, category in pairs(dirs) do
  64. local f, _ = file.Find('pointshop/items/' .. category .. '/__category.lua', 'LUA')
  65.  
  66. if #f > 0 then
  67. CATEGORY = {}
  68.  
  69. CATEGORY.Name = ''
  70. CATEGORY.Icon = ''
  71. CATEGORY.Order = 0
  72. CATEGORY.AllowedEquipped = -1
  73. CATEGORY.AllowedUserGroups = {}
  74. CATEGORY.CanPlayerSee = function() return true end
  75. CATEGORY.ModifyTab = function(tab) return end
  76.  
  77. if SERVER then AddCSLuaFile('pointshop/items/' .. category .. '/__category.lua') end
  78. include('pointshop/items/' .. category .. '/__category.lua')
  79.  
  80. if not PS.Categories[category] then
  81. PS.Categories[category] = CATEGORY
  82. end
  83.  
  84. local files, _ = file.Find('pointshop/items/' .. category .. '/*.lua', 'LUA')
  85.  
  86. for _, name in pairs(files) do
  87. if name ~= '__category.lua' then
  88. if SERVER then AddCSLuaFile('pointshop/items/' .. category .. '/' .. name) end
  89.  
  90. ITEM = {}
  91.  
  92. ITEM.__index = ITEM
  93. ITEM.ID = string.gsub(string.lower(name), '.lua', '')
  94. ITEM.Category = CATEGORY.Name
  95. ITEM.Price = 0
  96.  
  97. -- model and material are missing but there's no way around it, there's a check below anyway
  98.  
  99. ITEM.AdminOnly = false
  100. ITEM.AllowedUserGroups = {} -- this will fail the #ITEM.AllowedUserGroups test and continue
  101. ITEM.SingleUse = false
  102. ITEM.NoPreview = false
  103.  
  104. ITEM.CanPlayerBuy = true
  105. ITEM.CanPlayerSell = true
  106.  
  107. ITEM.CanPlayerEquip = true
  108. ITEM.CanPlayerHolster = true
  109.  
  110. ITEM.OnBuy = function() end
  111. ITEM.OnSell = function() end
  112. ITEM.OnEquip = function() end
  113. ITEM.OnHolster = function() end
  114. ITEM.OnModify = function() end
  115. ITEM.ModifyClientsideModel = function(ITEM, ply, model, pos, ang)
  116. return model, pos, ang
  117. end
  118.  
  119. include('pointshop/items/' .. category .. '/' .. name)
  120.  
  121. if not ITEM.Name then
  122. ErrorNoHalt("[POINTSHOP] Item missing name: " .. category .. '/' .. name .. "\n")
  123. continue
  124. elseif not ITEM.Price then
  125. ErrorNoHalt("[POINTSHOP] Item missing price: " .. category .. '/' .. name .. "\n")
  126. continue
  127. elseif not ITEM.Model and not ITEM.Material then
  128. ErrorNoHalt("[POINTSHOP] Item missing model or material: " .. category .. '/' .. name .. "\n")
  129. continue
  130. end
  131.  
  132. -- precache
  133.  
  134. if ITEM.Model then
  135. util.PrecacheModel(ITEM.Model)
  136. end
  137.  
  138. -- item hooks
  139. local item = ITEM
  140.  
  141. for prop, val in pairs(item) do
  142. if type(val) == "function" then -- although this hooks every function, it doesn't matter because the non-hook functions will never get called
  143. hook.Add(prop, 'PS_Item_' .. item.Name .. '_' .. prop, function(...)
  144. for _, ply in pairs(player.GetAll()) do
  145. if ply:PS_HasItemEquipped(item.ID) then -- hooks are only called if the player has the item equipped
  146. item[prop](item, ply, ply.PS_Items[item.ID].Modifiers, unpack({...}))
  147. end
  148. end
  149. end)
  150. end
  151. end
  152.  
  153. self.Items[ITEM.ID] = ITEM
  154.  
  155. ITEM = nil
  156. end
  157. end
  158.  
  159. CATEGORY = nil
  160. end
  161. end
  162. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement