Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local PLUGIN = PLUGIN
- PLUGIN.name = "Doors Saver"
- PLUGIN.author = "Niko"
- PLUGIN.description = "Saves purchased by players doors."
- PLUGIN.DOORS_BUFFER = PLUGIN.DOORS_BUFFER or {}
- PLUGIN.DOORS_ACCESS_BUFFER = PLUGIN.DOORS_ACCESS_BUFFER or {}
- PLUGIN.DOORS_TITLES_BUFFER = PLUGIN.DOORS_TITLES_BUFFER or {}
- if SERVER then
- local DOORS_PL = nut.plugin.list.doors
- if not DOORS_PL then
- ErrorNoHalt( "Error: No Doors plugin found!\n" )
- return
- end
- function PLUGIN:LoadData()
- local data = self:getData() or {}
- if data then
- self.DOORS_BUFFER = data.doors_buff or {}
- self.DOORS_ACCESS_BUFFER = data.doors_acc_buff or {}
- self.DOORS_TITLES_BUFFER = data.titles or {}
- for k, v in next, self.DOORS_BUFFER do
- local door = ents.GetMapCreatedEntity( k )
- if door and door:IsValid() and not door:getNetVar( "disabled" ) then
- local name = self.DOORS_TITLES_BUFFER[ k ]
- if name then
- door:setNetVar( "title", name )
- end
- door:setNetVar( "ownable", nil )
- DOORS_PL:callOnDoorChildren( door, function( child )
- child:setNetVar( "ownable", nil )
- end )
- door:Fire( "Lock" )
- end
- end
- end
- end
- function PLUGIN:SaveDoors()
- local data = {
- doors_buff = self.DOORS_BUFFER,
- doors_acc_buff = self.DOORS_ACCESS_BUFFER,
- titles = self.DOORS_TITLES_BUFFER
- }
- self:setData( data )
- end
- function PLUGIN:OnPlayerPurchaseDoor( ply, ent, isBuy )
- local char = ply:getChar()
- if char then
- local door_id = ent:MapCreationID()
- if door_id then
- if isBuy then
- self.DOORS_BUFFER[ door_id ] = char:getID()
- else
- self.DOORS_BUFFER[ door_id ] = nil
- self.DOORS_ACCESS_BUFFER[ door_id ] = nil
- end
- end
- end
- self:SaveDoors()
- end
- function PLUGIN:PrePlayerLoadedCharacter( ply, curChar, prevChar )
- if prevChar then
- local prevID = prevChar:GetID()
- for k, v in next, self.DOORS_BUFFER do
- if v == prevID then
- local door = ents.GetMapCreatedEntity( k )
- if door and door:IsValid() and not door:getNetVar( "disabled" ) then
- self.DOORS_ACCESS_BUFFER[ k ] = door.nutAccess
- self.DOORS_TITLES_BUFFER[ k ] = door:setNetVar( "title", door:setNetVar( "name", "Purchased" ) )
- door:setNetVar( "ownable", nil )
- DOORS_PL:callOnDoorChildren( door, function( child )
- child:setNetVar( "ownable", nil )
- end )
- end
- end
- end
- end
- local HaveDoor = false
- local curID = curChar:GetID()
- for k, v in next, self.DOORS_BUFFER do
- if v == curID then
- local door = ents.GetMapCreatedEntity( k )
- if door and door:IsValid() and not door:getNetVar( "disabled" ) then
- door:setNetVar( "ownable", true )
- door:SetDTEntity( 0, ply )
- if prevChar then
- local access = self.DOORS_ACCESS_BUFFER[ k ]
- if access then
- for k, v in next, access do
- if k and k:IsValid() and k:getChar() then
- door.nutAccess[ k ] = v
- end
- end
- door.nutAccess[ ply ] = DOOR_OWNER
- end
- else
- door.nutAccess = {
- [ ply ] = DOOR_OWNER
- }
- end
- DOORS_PL:callOnDoorChildren(door, function(child)
- child:setNetVar( "ownable", true )
- child:SetDTEntity( 0, ply )
- end)
- local doors = curChar:GetVar( "doors" ) or {}
- doors[ #doors + 1 ] = door
- curChar:SetVar( "doors", doors, true )
- HaveDoor = true
- end
- end
- end
- if HaveDoor then
- self:SaveDoors()
- end
- end
- function PLUGIN:PlayerDisconnected( ply )
- local char = ply:getChar()
- if char then
- local HaveDoor = false
- local charID = char:getID()
- for k, v in next, self.DOORS_BUFFER do
- if v == charID then
- local door = ents.GetMapCreatedEntity( k )
- if door and door:IsValid() and not door:getNetVar( "disabled" ) then
- self.DOORS_ACCESS_BUFFER[ k ] = door.nutAccess
- self.DOORS_TITLES_BUFFER[ k ] = door:getNetVar( "title", door:getNetVar( "name", "Purchased" ) )
- door:setNetVar( "ownable", nil )
- DOORS_PL:callOnDoorChildren( door, function( child )
- child:setNetVar( "ownable", nil )
- end )
- HaveDoor = true
- end
- end
- end
- if HaveDoor then
- self:SaveDoors()
- end
- end
- end
- function PLUGIN:CharacterDeleted( ply, id )
- local HaveDoor = false
- for k, v in next, self.DOORS_BUFFER do
- if v == id then
- local door = ents.GetMapCreatedEntity( k )
- if door and door:IsValid() and not door:getNetVar( "disabled" ) then
- self.DOORS_BUFFER[ k ] = nil
- self.DOORS_ACCESS_BUFFER[ k ] = nil
- self.DOORS_TITLES_BUFFER[ k ] = nil
- door:removeDoorAccessData()
- HaveDoor = true
- end
- end
- end
- if HaveDoor then
- self:SaveDoors()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement