Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. local PLUGIN = PLUGIN
  2. PLUGIN.name = "Doors Saver"
  3. PLUGIN.author = "Niko"
  4. PLUGIN.description = "Saves purchased by players doors."
  5.  
  6. PLUGIN.DOORS_BUFFER = PLUGIN.DOORS_BUFFER or {}
  7. PLUGIN.DOORS_ACCESS_BUFFER = PLUGIN.DOORS_ACCESS_BUFFER or {}
  8. PLUGIN.DOORS_TITLES_BUFFER = PLUGIN.DOORS_TITLES_BUFFER or {}
  9.  
  10.  
  11. if SERVER then
  12. local DOORS_PL = nut.plugin.list.doors
  13.  
  14. if not DOORS_PL then
  15. ErrorNoHalt( "Error: No Doors plugin found!\n" )
  16. return
  17. end
  18.  
  19. function PLUGIN:LoadData()
  20. local data = self:getData() or {}
  21. if data then
  22. self.DOORS_BUFFER = data.doors_buff or {}
  23. self.DOORS_ACCESS_BUFFER = data.doors_acc_buff or {}
  24. self.DOORS_TITLES_BUFFER = data.titles or {}
  25.  
  26. for k, v in next, self.DOORS_BUFFER do
  27. local door = ents.GetMapCreatedEntity( k )
  28. if door and door:IsValid() and not door:getNetVar( "disabled" ) then
  29. local name = self.DOORS_TITLES_BUFFER[ k ]
  30. if name then
  31. door:setNetVar( "title", name )
  32. end
  33.  
  34. door:setNetVar( "ownable", nil )
  35.  
  36. DOORS_PL:callOnDoorChildren( door, function( child )
  37. child:setNetVar( "ownable", nil )
  38. end )
  39.  
  40. door:Fire( "Lock" )
  41. end
  42. end
  43. end
  44. end
  45.  
  46. function PLUGIN:SaveDoors()
  47. local data = {
  48. doors_buff = self.DOORS_BUFFER,
  49. doors_acc_buff = self.DOORS_ACCESS_BUFFER,
  50. titles = self.DOORS_TITLES_BUFFER
  51. }
  52. self:setData( data )
  53. end
  54.  
  55. function PLUGIN:OnPlayerPurchaseDoor( ply, ent, isBuy )
  56. local char = ply:getChar()
  57. if char then
  58. local door_id = ent:MapCreationID()
  59. if door_id then
  60. if isBuy then
  61. self.DOORS_BUFFER[ door_id ] = char:getID()
  62. else
  63. self.DOORS_BUFFER[ door_id ] = nil
  64. self.DOORS_ACCESS_BUFFER[ door_id ] = nil
  65. end
  66. end
  67. end
  68.  
  69. self:SaveDoors()
  70. end
  71.  
  72. function PLUGIN:PrePlayerLoadedCharacter( ply, curChar, prevChar )
  73. if prevChar then
  74. local prevID = prevChar:GetID()
  75. for k, v in next, self.DOORS_BUFFER do
  76. if v == prevID then
  77. local door = ents.GetMapCreatedEntity( k )
  78. if door and door:IsValid() and not door:getNetVar( "disabled" ) then
  79. self.DOORS_ACCESS_BUFFER[ k ] = door.nutAccess
  80. self.DOORS_TITLES_BUFFER[ k ] = door:setNetVar( "title", door:setNetVar( "name", "Purchased" ) )
  81.  
  82. door:setNetVar( "ownable", nil )
  83.  
  84. DOORS_PL:callOnDoorChildren( door, function( child )
  85. child:setNetVar( "ownable", nil )
  86. end )
  87. end
  88. end
  89. end
  90. end
  91.  
  92. local HaveDoor = false
  93.  
  94. local curID = curChar:GetID()
  95. for k, v in next, self.DOORS_BUFFER do
  96. if v == curID then
  97. local door = ents.GetMapCreatedEntity( k )
  98. if door and door:IsValid() and not door:getNetVar( "disabled" ) then
  99. door:setNetVar( "ownable", true )
  100.  
  101. door:SetDTEntity( 0, ply )
  102.  
  103. if prevChar then
  104. local access = self.DOORS_ACCESS_BUFFER[ k ]
  105.  
  106. if access then
  107. for k, v in next, access do
  108. if k and k:IsValid() and k:getChar() then
  109. door.nutAccess[ k ] = v
  110. end
  111. end
  112.  
  113. door.nutAccess[ ply ] = DOOR_OWNER
  114. end
  115. else
  116. door.nutAccess = {
  117. [ ply ] = DOOR_OWNER
  118. }
  119. end
  120.  
  121. DOORS_PL:callOnDoorChildren(door, function(child)
  122. child:setNetVar( "ownable", true )
  123.  
  124. child:SetDTEntity( 0, ply )
  125. end)
  126.  
  127. local doors = curChar:GetVar( "doors" ) or {}
  128. doors[ #doors + 1 ] = door
  129. curChar:SetVar( "doors", doors, true )
  130.  
  131. HaveDoor = true
  132. end
  133. end
  134. end
  135.  
  136. if HaveDoor then
  137. self:SaveDoors()
  138. end
  139. end
  140.  
  141. function PLUGIN:PlayerDisconnected( ply )
  142. local char = ply:getChar()
  143. if char then
  144. local HaveDoor = false
  145.  
  146. local charID = char:getID()
  147. for k, v in next, self.DOORS_BUFFER do
  148. if v == charID then
  149. local door = ents.GetMapCreatedEntity( k )
  150. if door and door:IsValid() and not door:getNetVar( "disabled" ) then
  151. self.DOORS_ACCESS_BUFFER[ k ] = door.nutAccess
  152. self.DOORS_TITLES_BUFFER[ k ] = door:getNetVar( "title", door:getNetVar( "name", "Purchased" ) )
  153.  
  154. door:setNetVar( "ownable", nil )
  155.  
  156. DOORS_PL:callOnDoorChildren( door, function( child )
  157. child:setNetVar( "ownable", nil )
  158. end )
  159.  
  160. HaveDoor = true
  161. end
  162. end
  163. end
  164.  
  165. if HaveDoor then
  166. self:SaveDoors()
  167. end
  168. end
  169. end
  170.  
  171. function PLUGIN:CharacterDeleted( ply, id )
  172. local HaveDoor = false
  173.  
  174. for k, v in next, self.DOORS_BUFFER do
  175. if v == id then
  176. local door = ents.GetMapCreatedEntity( k )
  177. if door and door:IsValid() and not door:getNetVar( "disabled" ) then
  178. self.DOORS_BUFFER[ k ] = nil
  179. self.DOORS_ACCESS_BUFFER[ k ] = nil
  180. self.DOORS_TITLES_BUFFER[ k ] = nil
  181. door:removeDoorAccessData()
  182.  
  183. HaveDoor = true
  184. end
  185. end
  186. end
  187.  
  188. if HaveDoor then
  189. self:SaveDoors()
  190. end
  191. end
  192. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement