Advertisement
taka_qiao

Untitled

Jul 23rd, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.38 KB | None | 0 0
  1. AddCSLuaFile()
  2.  
  3. if SERVER then
  4.     CreateConVar("cw_keep_attachments_post_death", 0, {FCVAR_ARCHIVE, FCVAR_NOTIFY}, "Should players keep their attachments after they die?")
  5.    
  6.     util.AddNetworkString("CW20_OVERWRITEATTACHMENTS")
  7.     util.AddNetworkString("CW20_NEWATTACHMENTS")
  8.     util.AddNetworkString("CW20_NEWATTACHMENTS_NOTIFY")
  9. end
  10.  
  11. CustomizableWeaponry.useAttachmentPossessionSystem = true -- change this to false if you want to disable attachment possession restrictions (aka all attachments available regardless of whether the players have them or not)
  12. CustomizableWeaponry.suppressOnSpawnAttachments = false
  13.  
  14. function CustomizableWeaponry:initCWVariables()
  15.     if not self.CWAttachments then
  16.         self.CWAttachments = {}
  17.     end
  18. end
  19.  
  20. local empty, space = "", " "
  21.  
  22. function CustomizableWeaponry:buildAttachmentString()
  23.     local final = ""
  24.    
  25.     -- loop through the attachment table, find attachments that are given on spawn and concatenate a string that we'll network with them
  26.     -- also add attachments to the serverside player attachment table to save a loop
  27.     for k, v in ipairs(CustomizableWeaponry.registeredAttachments) do
  28.         if GetConVarNumber(v.cvar) >= 1 then
  29.             if not self.CWAttachments[v.name] then
  30.                 self.CWAttachments[v.name] = true
  31.                
  32.                 if final == empty then
  33.                     final = v.name
  34.                 else
  35.                     final = final .. space .. v.name
  36.                 end
  37.             end
  38.         end
  39.     end
  40.    
  41.     return final
  42. end
  43.  
  44. function CustomizableWeaponry:decodeAttachmentString(str)
  45.     self.CWAttachments = self.CWAttachments or {}
  46.    
  47.     local result = string.Explode(space, str)
  48.    
  49.     for k, v in pairs(result) do
  50.         self.CWAttachments[v] = true
  51.     end
  52. end
  53.  
  54. function CustomizableWeaponry:postSpawn()
  55.     if not self.CWAttachments then
  56.         CustomizableWeaponry.initCWVariables(self)
  57.         return
  58.     end
  59.    
  60.     if CustomizableWeaponry.useAttachmentPossessionSystem and not CustomizableWeaponry.suppressOnSpawnAttachments then
  61.         if SERVER then
  62.             local keepAttachments = GetConVarNumber("cw_keep_attachments_post_death") >= 1
  63.            
  64.             if not keepAttachments then
  65.                 for k, v in pairs(self.CWAttachments) do
  66.                     self.CWAttachments[k] = nil
  67.                 end
  68.                
  69.                 local attachments = CustomizableWeaponry.buildAttachmentString(self)
  70.                
  71.                 if attachments ~= empty then
  72.                     net.Start("CW20_OVERWRITEATTACHMENTS")
  73.                         net.WriteString(attachments)
  74.                     net.Send(self)
  75.                 else
  76.                     SendUserMessage("CW20_LOSTATTACHMENTS", self)
  77.                 end
  78.             else
  79.                 local attachments = CustomizableWeaponry.buildAttachmentString(self)
  80.                
  81.                 if attachments ~= empty then
  82.                     net.Start("CW20_NEWATTACHMENTS")
  83.                         net.WriteString(attachments)
  84.                     net.Send(self)
  85.                 end
  86.             end
  87.         else
  88.             for k, v in pairs(self.CWAttachments) do
  89.                 self.CWAttachments[k] = nil
  90.             end
  91.         end
  92.     end
  93.    
  94.     CustomizableWeaponry.callbacks.processCategory(self, "postSpawn")
  95. end
  96.  
  97. function CustomizableWeaponry:hasAttachment(ply, att, lookIn)
  98.     if not self.useAttachmentPossessionSystem then
  99.         return true
  100.     end
  101.    
  102.     lookIn = lookIn or ply.CWAttachments
  103.    
  104.     local has = hook.Call("CW20HasAttachment", nil, ply, att, lookIn)
  105.    
  106.     if lookIn[att] or has then
  107.         return true
  108.     end
  109.    
  110.     return false
  111. end
  112.  
  113. -- this function checks whether the player has specified attachments in his inventory
  114. -- accepts an array
  115. function CustomizableWeaponry:hasSpecifiedAttachments(ply, tbl)
  116.     -- failsafes, failsafes everywhere
  117.     ply.CWAttachments = ply.CWAttachments or {}
  118.    
  119.     if not self.useAttachmentPossessionSystem then
  120.         return true
  121.     end
  122.    
  123.     for k, v in ipairs(tbl) do
  124.         if not ply.CWAttachments[v] then
  125.             return false
  126.         end
  127.     end
  128.    
  129.     return true
  130. end
  131.  
  132. function CustomizableWeaponry:countMissingAttachments(ply, tbl)
  133.     if not self.useAttachmentPossessionSystem then
  134.         return 0
  135.     end
  136.    
  137.     local count = 0
  138.    
  139.     for k, v in ipairs(tbl) do
  140.         if not ply.CWAttachments[v] then
  141.             count = count + 1
  142.         end
  143.     end
  144.    
  145.     return count
  146. end
  147.  
  148. function CustomizableWeaponry:updateAdminCVars()
  149.     for k, v in pairs(player.GetAll()) do
  150.         if v:IsAdmin() then
  151.             for k2, v2 in ipairs(CustomizableWeaponry.registeredAttachments) do
  152.                 v:ConCommand(v2.clcvar .. space .. GetConVarNumber(v2.cvar))
  153.             end
  154.         end
  155.     end
  156. end
  157.  
  158. function CustomizableWeaponry:giveAttachment(ply, att, noNetwork)
  159.     if not att or not ply or not IsValid(ply) then
  160.         return
  161.     end
  162.    
  163.     ply.CWAttachments[att] = true
  164.    
  165.     if not noNetwork then
  166.         umsg.Start("CW20_NEWATTACHMENT", ply)
  167.             umsg.String(att)
  168.         umsg.End()
  169.     end
  170. end
  171.  
  172. -- use this func when you want to send a certain amount of specific attachments to the client
  173. function CustomizableWeaponry:giveAttachments(tbl, dontNotify, overwrite)
  174.     CustomizableWeaponry.initCWVariables(self)
  175.    
  176.     -- add the attachments to the player's inventory
  177.     for k, v in ipairs(tbl) do
  178.         if CustomizableWeaponry.registeredAttachmentsSKey[v] then
  179.             self.CWAttachments[v] = true
  180.         end
  181.     end
  182.    
  183.     -- concatenate the table into a string
  184.     local result = table.concat(tbl, " ")
  185.    
  186.     if SERVER then
  187.         -- send it to the client
  188.        
  189.         if overwrite then
  190.             net.Start("CW20_OVERWRITEATTACHMENTS")
  191.                 net.WriteString(result)
  192.             net.Send(self)
  193.         else
  194.             if dontNotify then
  195.                 net.Start("CW20_NEWATTACHMENTS")
  196.                     net.WriteString(result)
  197.                 net.Send(self)
  198.             else
  199.                 net.Start("CW20_NEWATTACHMENTS_NOTIFY")
  200.                     net.WriteString(result)
  201.                 net.Send(self)
  202.             end
  203.         end
  204.     end
  205. end
  206.  
  207. function CustomizableWeaponry:giveAllAttachments(ply, att)
  208.     if not att or not ply or not IsValid(ply) then
  209.         return
  210.     end
  211.    
  212.     for k, v in ipairs(CustomizableWeaponry.registeredAttachments) do
  213.         ply.CWAttachments[v.name] = true
  214.     end
  215.    
  216.     umsg.Start("CW20_ALLATTACHMENTS", ply)
  217.         umsg.String(att)
  218.     umsg.End()
  219. end
  220.  
  221. function CustomizableWeaponry:removeAllAttachments(ply)
  222.     net.Start("CW20_OVERWRITEATTACHMENTS")
  223.         net.WriteString("")
  224.     net.Send(ply)
  225.    
  226.     table.Empty(ply.CWAttachments)
  227. end
  228.  
  229. function CustomizableWeaponry:removeAttachment(ply, att, noNetwork)
  230.     if not att or not ply or not IsValid(ply) then
  231.         return
  232.     end
  233.    
  234.     ply.CWAttachments[att] = nil
  235.  
  236.     if not noNetwork then
  237.         umsg.Start("CW20_REMOVEATTACHMENT", ply)
  238.             umsg.String(att)
  239.         umsg.End()
  240.     end
  241. end
  242.  
  243. -- builds attachment name strings for display on package entities
  244. function CustomizableWeaponry:ent_buildAttachmentNameStrings(tbl)
  245.     local result = {}
  246.    
  247.     for k, v in ipairs(tbl) do
  248.         local att = CustomizableWeaponry.registeredAttachmentsSKey[v]
  249.        
  250.         if att then
  251.             table.insert(result, {name = att.name, display = att.displayName})
  252.         end
  253.     end
  254.    
  255.     return result
  256. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement