This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

nwvars.lua

By: KatNotDinner on Apr 29th, 2012  |  syntax: Lua  |  size: 14.08 KB  |  views: 98  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*PLUGIN.Name = "Server-side NWVars"
  2. PLUGIN.Description = "Server-side managment of nwvars"
  3. PLUGIN.Author = "KatNotDinner"*/
  4.  
  5. if CLIENT then
  6.         return
  7. end
  8.  
  9. /*function PLUGIN.Init()
  10.         print( "This is the server side NWVars plugin." )
  11. end*/
  12. --Major update tons of fixes and optimizations both on client and server
  13. local nwents = {}
  14. local meta1 = FindMetaTable( "Entity" )
  15. local meta2 = FindMetaTable( "Player" )
  16.  
  17. function meta1:SetNWString( str, val )
  18.     nwents[self] = nwents[self] or true
  19.         self.NWvars = self.NWvars or {}
  20.         self.NWvars.String = self.NWvars.String or {}
  21.         --self.NWvars.String[ str ] = self.NWvars.String[ str ] or {}
  22.         self.NWvars.String[ str ] = val
  23.         net.Start( "String" )
  24.             net.WriteEntity( self )
  25.             net.WriteString( str )
  26.             net.WriteString( val )
  27.         net.Broadcast()
  28. end
  29.  
  30. function meta1:SetNWEntity( str, ent )
  31.     nwents[self] = nwents[self] or true
  32.         self.NWvars = self.NWvars or {}
  33.         self.NWvars.Entity = self.NWvars.Entity or {}
  34.         --self.NWvars.Entity[ str ] = self.NWvars.Entity[ str ] or {}
  35.         self.NWvars.Entity[ str ] = ent
  36.         net.Start( "Entity" )
  37.             net.WriteEntity( self )
  38.             net.WriteString( str )
  39.             net.WriteEntity( ent )
  40.         net.Broadcast()
  41. end
  42.  
  43. function meta1:SetNWBool( str, bool )
  44.     nwents[self] = nwents[self] or true
  45.         self.NWvars = self.NWvars or {}
  46.         self.NWvars.Bool = self.NWvars.Bool or {}
  47.         --self.NWvars.Bool[ str ] = self.NWvars.Bool[ str ] or {}
  48.         self.NWvars.Bool[ str ] = bool
  49.         net.Start( "Bool" )
  50.             net.WriteEntity( self )
  51.             net.WriteString( str )
  52.                 net.WriteBit( bool )
  53.         net.Broadcast()
  54. end
  55.  
  56. function meta1:SetNWFloat( str, fl )
  57.     nwents[self] = nwents[self] or true
  58.         self.NWvars = self.NWvars or {}
  59.         self.NWvars.Float = self.NWvars.Float or {}
  60.         --self.NWvars.Float[ str ] = self.NWvars.Float[ str ] or {}
  61.         self.NWvars.Float[ str ] = fl
  62.         net.Start( "Float" )
  63.             net.WriteEntity( self )
  64.                 net.WriteString( str )
  65.                 net.WriteFloat( fl )
  66.         net.Broadcast()
  67. end
  68.  
  69. function meta1:SetNWInt( str, int )
  70.         print("\n"..int)
  71.     nwents[self] = nwents[self] or true
  72.         self.NWvars = self.NWvars or {}
  73.         self.NWvars.Int = self.NWvars.Int or {}
  74.         --self.NWvars.Int[ str ] = self.NWvars.Int[ str ] or {}
  75.         self.NWvars.Int[ str ] = int
  76.         net.Start( "Int" )
  77.             net.WriteEntity( self )
  78.             net.WriteString( str )
  79.             net.WriteInt( int, 32 )
  80.         net.Broadcast()
  81. end
  82.  
  83. function meta1:SetNWAngle( str, ang, b )
  84.     nwents[self] = nwents[self] or true
  85.         self.NWvars = self.NWvars or {}
  86.         self.NWvars.Angle = self.NWvars.Angle or {}
  87.         --self.NWvars.Angle[ str ] = self.NWvars.Angle[ str ] or {}
  88.         self.NWvars.Angle[ str ] = ang
  89.         net.Start( "Angle" )
  90.             net.WriteEntity( self )
  91.             net.WriteString( str )
  92.             net.WriteAngle( ang )
  93.         net.Broadcast()
  94. end
  95.  
  96. function meta1:SetNWVector( str, vec )
  97.     nwents[self] = nwents[self] or true
  98.         self.NWvars = self.NWvars or {}
  99.         self.NWvars.Vector = self.NWvars.Vector or {}
  100.         --self.NWvars.Vector[ str ] = self.NWvars.Vector[ str ] or {}
  101.         self.NWvars.Vector[ str ] = vec
  102.         net.Start( "Vector" )
  103.             net.WriteEntity( self )
  104.             net.WriteString( str )
  105.             net.WriteVector( vec )
  106.         net.Broadcast()
  107. end
  108.  
  109. function meta1:GetNWBool( str )
  110.         if !self.NWvars then Error( "No NWVars for Entity "..self:EntIndex().."!" ) debug.Trace() return end
  111.     if !self.NWvars.Bool[ str ] then return end
  112.         return self.NWvars.Bool[ str ]
  113. end
  114.  
  115. function meta1:GetNWString( str )
  116.         if !self.NWvars then Error( "No NWVars for Entity "..self:EntIndex().."!" ) debug.Trace() return end
  117.     if !self.NWvars.String[ str ] then return end
  118.         return self.NWvars.String[ str ]
  119. end
  120.  
  121. function meta1:GetNWAngle( str )
  122.         if !self.NWvars then Error( "No NWVars for Entity "..self:EntIndex().."!" ) debug.Trace() return end
  123.     if !self.NWvars.Angle[ str ] then return end
  124.         return self.NWvars.Angle[ str ]
  125. end
  126.  
  127. function meta1:GetNWVector( str )
  128.         if !self.NWvars then Error( "No NWVars for Entity "..self:EntIndex().."!" ) debug.Trace() return end
  129.     if !self.NWvars.Vector[ str ] then return end
  130.         return self.NWvars.Vector[ str ]
  131. end
  132.  
  133. function meta1:GetNWFloat( str )
  134.         if !self.NWvars then Error( "No NWVars for Entity "..self:EntIndex().."!" ) debug.Trace() return end
  135.        
  136.     if !self.NWvars.Float[ str ] then return end
  137.         return self.NWvars.Float[ str ]
  138. end
  139.  
  140. function meta1:GetNWInt( str )
  141.         if !self.NWvars then Error( "No NWVars for Entity "..self:EntIndex().."!" ) debug.Trace() return end
  142.     if !self.NWvars.Int[ str ] then return end
  143.         return self.NWvars.Int[ str ]
  144. end
  145.  
  146. function meta1:GetNWEntity( str )
  147.         if !self.NWvars then Error( "No NWVars for Entity "..self:EntIndex().."!" ) debug.Trace() return end
  148.     if !self.NWvars.Entity[ str ] then return end
  149.         return self.NWvars.Entity[ str ]
  150. end
  151.  
  152. function meta1:GetNetworkedVar( str )
  153.     if self.NWvars.Vector[ str ] then
  154.         return self.NWvars.Vector[ str ]
  155.         elseif self.NWvars.Entity[ str ] then
  156.             return self.NWvars.Entity[ str ]
  157.         elseif self.NWvars.String[ str ] then
  158.             return self.NWvars.String[ str ]
  159.         elseif self.NWvars.Float[ str ] then
  160.             return self.NWvars.Float
  161.         elseif self.NWvars.Angle[ str ] then
  162.             return self.NWvars.Angle[ str ]
  163.         elseif self.NWvars.Bool[ str ] then
  164.             return self.NWvars.Bool[ str ]
  165.         end
  166. end
  167.  
  168. function meta1:SetNetworkedVar( str, var )
  169.     if type(var) == "string" then
  170.             self:SetNWString( str, var )
  171.                 return
  172.         elseif type(var) == "number" then
  173.             if tostring(var):find( "." ) then
  174.                     self:SetNWFloat( str, var )
  175.                 else
  176.                     self:SetNWInt( str, var )
  177.                 end
  178.         elseif type(var) == "bool" then
  179.             self:SetNWBool( str, var )
  180.         elseif var.p and var.y and var.r then
  181.             self:SetNWAngle( str, var )
  182.         elseif var.x and var.y and var.z then
  183.             self:SetNWVector( str, var )
  184.         elseif IsValid(var) then
  185.             self:SetNWEntity( str, var )
  186.         end
  187. end
  188.  
  189. /*function meta2:SetNWString( str, val )
  190.     nwents[self] = nwents[self] or true
  191.         self.NWvars = self.NWvars or {}
  192.         self.NWvars.String[ str ] = self.NWvars.String[ str ] or {}
  193.         self.NWvars.String[ str ] = val
  194.         net.Start( "String" )
  195.             net.WriteEntity( self )
  196.             net.WriteString( str )
  197.             net.WriteString( val )
  198.         net.Broadcast()
  199. end
  200.  
  201. function meta2:SetNWEntity( str, ent )
  202.     nwents[self] = nwents[self] or true
  203.         self.NWvars = self.NWvars or {}
  204.         self.NWvars.Entity[ str ] = self.NWvars.Entity[ str ] or {}
  205.         self.NWvars.Entity[ str ] = ent
  206.         net.Start( "Entity" )
  207.             net.WriteEntity( self )
  208.             net.WriteString( str )
  209.             net.WriteEntity( ent )
  210.         net.Broadcast()
  211. end
  212.  
  213. function meta2:SetNWBool( str, bool )
  214.     nwents[self] = nwents[self] or true
  215.         self.NWvars = self.NWvars or {}
  216.         self.NWvars.Bool[ str ] = self.NWvars.Bool[ str ] or {}
  217.         self.NWvars.Bool[ str ] = bool
  218.         net.Start( "Bool" )
  219.             net.WriteEntity( self )
  220.             net.WriteString( str )
  221.                 if bool then   
  222.                         net.WriteInt( 1 )
  223.                 else
  224.                         net.WriteInt( 2 )
  225.                 end
  226.         net.Broadcast()
  227. end
  228.  
  229. function meta2:SetNWFloat( str, fl )
  230.     nwents[self] = nwents[self] or true
  231.         self.NWvars = self.NWvars or {}
  232.         self.NWvars.Float[ str ] = self.NWvars.Float[ str ] or {}
  233.         self.NWvars.Float[ str ] = fl
  234.         net.Start( "Float" )
  235.             net.WriteEntity( self )
  236.                 net.WriteString( str )
  237.                 net.WriteFloat( fl )
  238.         net.Broadcast()
  239. end
  240.  
  241. function meta2:SetNWInt( str, int )
  242.     nwents[self] = nwents[self] or true
  243.         self.NWvars = self.NWvars or {}
  244.         self.NWvars.Int[ str ] = self.NWvars.Int[ str ] or {}
  245.         self.NWvars.Int[ str ] = int
  246.         net.Start( "Int" )
  247.             net.WriteEntity( self )
  248.             net.WriteString( str )
  249.             net.WriteInt( int )
  250.         net.Broadcast()
  251. end
  252.  
  253. function meta2:SetNWAngle( str, ang, b )
  254.     nwents[self] = nwents[self] or true
  255.         self.NWvars = self.NWvars or {}
  256.         self.NWvars.Angle[ str ] = self.NWvars.Angle[ str ] or {}
  257.         self.NWvars.Angle[ str ] = ang
  258.         net.Start( "Angle" )
  259.             net.WriteEntity( self )
  260.             net.WriteString( str )
  261.             net.WriteAngle( ang )
  262.         net.Broadcast()
  263. end
  264.  
  265. function meta2:SetNWVector( str, vec )
  266.     nwents[self] = nwents[self] or true
  267.         self.NWvars = self.NWvars or {}
  268.         self.NWvars.Vector[ str ] = self.NWvars.Vector[ str ] or {}
  269.         self.NWvars.Vector[ str ] = vec
  270.         net.Start( "Vector" )
  271.             net.WriteEntity( self )
  272.             net.WriteString( str )
  273.             net.WriteVector( vec )
  274.         net.Broadcast()
  275. end
  276.  
  277. function meta2:SetNetworkedVar( str, var )
  278.     if type(var) == "string" then
  279.             self:SetNWString( str, var )
  280.                 return
  281.         elseif type(var) == "number" then
  282.             if tostring(var):find( "." ) then
  283.                     self:SetNWFloat( str, var )
  284.                 else
  285.                     self:SetNWInt( str, var )
  286.                 end
  287.         elseif type(var) == "bool" then
  288.             self:SetNWBool( str, var )
  289.         elseif var.p and var.y and var.r then
  290.             self:SetNWAngle( str, var )
  291.         elseif var.x and var.y and var.z then
  292.             self:SetNWVector( str, var )
  293.         elseif IsValid(var) then
  294.             self:SetNWEntity( str, var )
  295.         end
  296. end
  297.  
  298. function meta2:GetNWBool( str )
  299.     if !self.NWvars.Bool[ str ] then return end
  300.         return self.NWvars.Bool[ str ]
  301. end
  302.  
  303. function meta2:GetNWString( str )
  304.     if !self.NWvars.String[ str ] then return end
  305.         return self.NWvars.String[ str ]
  306. end
  307.  
  308. function meta2:GetNWAngle( str )
  309.     if !self.NWvars.Angle[ str ] then return end
  310.         return self.NWvars.Angle[ str ]
  311. end
  312.  
  313. function meta2:GetNWVector( str )
  314.     if !self.NWvars.Vector[ str ] then return end
  315.         return self.NWvars.Vector[ str ]
  316. end
  317.  
  318. function meta2:GetNWFloat( str )
  319.     if !self.NWvars.Float[ str ] then return end
  320.         return self.NWvars.Float[ str ]
  321. end
  322.  
  323. function meta2:GetNWInt( str )
  324.     if !self.NWvars.Int[ str ] then return end
  325.         return self.NWvars.Int[ str ]
  326. end
  327.  
  328. function meta2:GetNWEntity( str )
  329.     if !self.NWvars.Entity[ str ] then return end
  330.         return self.NWvars.Entity[ str ]
  331. end
  332.  
  333. function meta2:GetNetworkedVar( str )
  334.     if self.NWvars.Vector[ str ] then
  335.         return self.NWvars.Vector[ str ]
  336.         elseif self.NWvars.Entity[ str ] then
  337.             return self.NWvars.Entity[ str ]
  338.         elseif self.NWvars.String[ str ] then
  339.             return self.NWvars.String[ str ]
  340.         elseif self.NWvars.Float[ str ] then
  341.             return self.NWvars.Float
  342.         elseif self.NWvars.Angle[ str ] then
  343.             return self.NWvars.Angle[ str ]
  344.         elseif self.NWvars.Bool[ str ] then
  345.             return self.NWvars.Bool[ str ]
  346.         end
  347. end
  348. */
  349. hook.Add( "PlayerInitialSpawn", "NWVars", function( ply )
  350.         local time = 0 -- We should create some delays between the datastreams
  351.         if nwents then
  352.                 timer.Simple( 0, function()
  353.                         for k, v in pairs(nwents) do
  354.                                 time = time + .3
  355.                                 timer.Simple( time, function()
  356.                                         if k.NWvars then
  357.                                                 net.Start( "NWVars", ply )
  358.                                                         net.WriteEntity( k )
  359.                                                         net.WriteTable(k.NWvars)
  360.                                                 net.Send(ply)
  361.                                         end
  362.                                 end )
  363.                         end
  364.                 end )
  365.         end
  366. end )
  367.  
  368. hook.Add( "PlayerDisconnected", "lolwut", function( ply )
  369.     if nwents and nwents[ply] then
  370.             nwents[ply] = nil
  371.         end
  372. end )
  373.  
  374. net.Receive( "AngleCL", function(ply, len ) -- kinda messed up argument names
  375.     local ent = net.ReadEntity()
  376.         local str = net.ReadString()
  377.         local var = net.ReadAngle()
  378.         ent.NWvars = ent.NWvars or {}
  379.         ent.NWvars.Angle = ent.NWvars.Angle or {}
  380.         ent.NWvars.Angle[ str ] = ent.NWvars.Angle[ str ] or {}
  381.         ent.NWvars.Angle[ str ] = var
  382.         timer.Simple( .5, function()
  383.         ent:SetNWAngle( str, var )
  384.         end )
  385. end )
  386.  
  387. net.Receive( "VectorCL", function(ply, len )
  388.     local ent = net.ReadEntity()
  389.         local str = net.ReadString()
  390.         local var = net.ReadVector()
  391.         ent.NWvars = ent.NWvars or {}
  392.         ent.NWvars.Vector = ent.NWvars.Vector or {}
  393.         ent.NWvars.Vector[ str ] = ent.NWvars.Vector[ str ] or {}
  394.         ent.NWvars.Vector[ str ] = var
  395.         timer.Simple( .5, function()
  396.         ent:SetNWVector( str, var )
  397.         end )
  398. end )
  399.  
  400. net.Receive( "EntityCL", function(ply, len )
  401.     local ent = net.ReadEntity()
  402.         local str = net.ReadString()
  403.         local var = net.ReadEntity()
  404.         ent.NWvars = ent.NWvars or {}
  405.         ent.NWvars.Entity = ent.NWvars.Entity or {}
  406.         ent.NWvars.Entity[ str ] = ent.NWvars.String[ str ] or {}
  407.         ent.NWvars.Entity[ str ] = var
  408.         timer.Simple( .5, function()
  409.         ent:SetNWEntity( str, var )
  410.         end )
  411. end )
  412.  
  413. net.Receive( "StringCL", function(ply, len )
  414.     local ent = net.ReadEntity()
  415.         local str = net.ReadString()
  416.         local var = net.ReadString()
  417.         ent.NWvars = ent.NWvars or {}
  418.         ent.NWvars.String = ent.NWvars.String or {}
  419.         ent.NWvars.String[ str ] = ent.NWvars.String[ str ] or {}
  420.         ent.NWvars.String[ str ] = var
  421.         timer.Simple( .5, function()
  422.         ent:SetNWString( str, var )
  423.         end )
  424. end )
  425.  
  426. net.Receive( "FloatCL", function(ply, len )
  427.     local ent = net.ReadEntity()
  428.         local str = net.ReadString()
  429.         local var = net.ReadFloat()
  430.         ent.NWvars = ent.NWvars or {}
  431.         ent.NWvars.Float = ent.NWvars.Float or {}
  432.         ent.NWvars.Float[ str ] = ent.NWvars.Float[ str ] or {}
  433.         ent.NWvars.Float[ str ] = var
  434.         timer.Simple( .5, function()
  435.         ent:SetNWFloat( str, var )
  436.         end )
  437. end )
  438.  
  439. net.Receive( "IntCL", function( le, ply )
  440.     local ent = net.ReadEntity()
  441.         local str = net.ReadString()
  442.         local var = net.ReadInt( 32 )
  443.         ent.NWvars = ent.NWvars or {}
  444.         ent.NWvars.Int = ent.NWvars.Int or {}
  445.         ent.NWvars.Int[ str ] = ent.NWvars.Int[ str ] or {}
  446.         ent.NWvars.Int[ str ] = var
  447.         timer.Simple( .5, function()
  448.         ent:SetNWInt( str, var )
  449.         end )
  450. end  )
  451.  
  452. net.Receive( "BoolCL", function(ply, len )
  453.     local ent = net.ReadEntity()
  454.         local str = net.ReadString()
  455.         local var = net.ReadBit()
  456.         ent.NWvars = ent.NWvars or {}
  457.         ent.NWvars.Bool = ent.NWvars.Bool or {}
  458.         ent.NWvars.Bool[ str ] = ent.NWvars.Bool[ str ] or {}
  459.         ent.NWvars.Bool[ str ] = var
  460.         timer.Simple( .5, function()
  461.         ent:SetNWBool( str, var )
  462.         end )
  463. end )
  464.  
  465. meta1.SetNWVar = meta1.SetNetworkedVar -- making sure that I override ALL of the nwvar functions
  466. meta1.SetNetworkedInt = meta1.SetNWInt
  467. meta1.SetNetworkedFloat = meta1.SetNWFloat
  468. meta1.SetNetworkedEntity = meta1.SetNWEntity
  469. meta1.SetNetworkedString = meta1.SetNWString
  470. meta1.SetNetworkedBool = meta1.SetNWBool
  471. meta1.SetNetworkedAngle = meta1.SetNWAngle
  472. meta1.SetNetworkedVector = meta1.SetNWVector
  473. /*
  474. _R.Player.SetNWVar = meta2.SetNetworkedVar -- making sure that I override ALL of the nwvar functions
  475. _R.Player.SetNetworkedInt = meta2.SetNWInt
  476. _R.Player.SetNetworkedFloat = meta2.SetNWFloat
  477. _R.Player.SetNetworkedEntity = meta2.SetNWEntity
  478. _R.Player.SetNetworkedString = meta2.SetNWString
  479. _R.Player.SetNetworkedBool = meta2.SetNWBool
  480. _R.Player.SetNetworkedAngle = meta2.SetNWAngle
  481. _R.Player.SetNetworkedVector = meta2.SetNWVector*/
clone this paste RAW Paste Data