Rust Legacy Structure build lock

Discussion in 'Plugin Requests' started by kiko, Jan 12, 2016.

  1. hello
    it's possible to upgrade this plugin to oxide 2
    thank for your answerd
    Code:
    PLUGIN.Title = "Structure build lock"
    PLUGIN.Description = "Block anyone from building on your structure"
    PLUGIN.Author = "Reneb"
    PLUGIN.Version = "1.0"local AllStructures = util.GetStaticPropertyGetter( Rust.StructureMaster, "AllStructures")
    local getStructureMasterOwnerId = util.GetFieldGetter(Rust.StructureMaster, "ownerID", true)function PLUGIN:Init()
      
       local b, res = config.Read( "structures-lock" )
       self.Config = res or {}
       if (not b) then
         self:LoadDefaultConfig()
         if (res) then config.Save( "structures-lock" ) end
       end
       if(self.Config.Version ~= 1.0) then
         print("Structure Build Lock: Old Config version detected, loading default configs")
         self:LoadDefaultConfig()
         config.Save( "structures-lock" )
       end
       self.LockFile = util.GetDatafile( "structures-lock" )
      local txt = self.LockFile:GetText()
      if (txt ~= "") then
      self.Lock = json.decode( txt )
      else
      self.Lock = {}
      end
    end
    function PLUGIN:postInit()
       self:AddChatCommand("lock", self.cmdLock)
    end
    function PLUGIN:cmdLock(netuser,cmd,args)
       local userID = rust.GetUserID(netuser)
       if(not self.Lock[userID]) then
         self.Lock[userID] = true
         rust.SendChatToUser(netuser, self.Config.chatname, "Your buildings are now locked, no one will be able to build on them")
        
       else
         self.Lock[userID] = false
         rust.SendChatToUser(netuser, self.Config.chatname, "Your buildings are now unlocked, anyone can build on them")
       end
       self:Save()
    end
    function PLUGIN:Save()
       self.LockFile:SetText( json.encode( self.Lock ) )
       self.LockFile:Save()
    end
    function PLUGIN:GetStructureOwnerID(structure)
      return getStructureMasterOwnerId(structure)
    endfunction PLUGIN:LoadDefaultConfig()
       self.Config.chatname = "Lock"
       self.Config.Version = 1.0
       self.Config.NotifyUser = true
    endfunction PLUGIN:TwoDDistance(p1, p2)
      return math.sqrt(math.pow(p1.x - p2.x,2) + math.pow(p1.z - p2.z,2))
    endfunction PLUGIN:SendToUserByPos(pos,msg)
       for _, netuser in pairs( rust.GetAllNetUsers() ) do
         if(netuser.playerClient) then
           if(self:TwoDDistance(pos,netuser.playerClient.lastKnownPosition) < 10) then
             rust.SendChatToUser(netuser, self.Config.chatname, msg)
           end
         end
       end
    end
    function PLUGIN:FindStructure(pos)
       for i=0, AllStructures().Count-1 do
         local master = AllStructures()
         if(master) then
           local comp = master:GetComponentFromPositionWorld(pos)
           if(comp ~= nil) then
             return master, comp
           end
         end
       end  
       return false
    endlocal NetCullRemove = util.FindOverloadedMethod(Rust.NetCull._type, "Destroy", bf.public_static, {UnityEngine.GameObject})
    function RemoveObject(object)
       local objs = util.ArrayFromTable(cs.gettype("System.Object"), {object})
       cs.convertandsetonarray( objs, 0, object , UnityEngine.GameObject._type )
       NetCullRemove:Invoke(nil, objs)
    end
    function PLUGIN:CheckIfCanOrRemove(pos)
       local master, structure = self:FindStructure(pos)
       if(not master) then error("couldn't find the new structure") return end
       local masterid = self:GetStructureOwnerID(master)
       if(not masterid) then error("couldn't find the masterid of a structure") return end
       if(self.Lock[tostring(masterid)]) then
         RemoveObject(structure.GameObject)
         if(self.Config.NotifyUser) then
           self:SendToUserByPos(pos, "This building is locked, use /lock to activate/deactivate building on your structures only")
         end
       end
    end
    function PLUGIN:OnPlaceStructure(structure,pos)
       if(string.find(structure.name, "Stairs") or string.find(structure.name, "Pillar") or string.find(structure.name, "Foundation")) then
         timer.Once(0.2, function() self:CheckIfCanOrRemove(pos) end)
       end
    end
     
    Last edited by a moderator: Jan 12, 2016
  2. Wulf

    Wulf Community Admin

    Please keep plugin requests to one per thread, and also check the existing requests. If you want a plugin from another game ported, just link the plugin, no need to copy the entire code here.
     
  3. Put as resolved '-'
     
  4. Would it be possible to leave this plugin compatible with oxide 2.0?
    Code:
    LUGIN.Title = "Structure build lock"
    PLUGIN.Description = "Block anyone from building on your structure"
    PLUGIN.Author = "Reneb"
    PLUGIN.Version = "1.0"local AllStructures = util.GetStaticPropertyGetter( Rust.StructureMaster, "AllStructures")
    local getStructureMasterOwnerId = util.GetFieldGetter(Rust.StructureMaster, "ownerID", true)function PLUGIN:Init()
     
       local b, res = config.Read( "structures-lock" )
       self.Config = res or {}
       if (not b) then
         self:LoadDefaultConfig()
         if (res) then config.Save( "structures-lock" ) end
       end
       if(self.Config.Version ~= 1.0) then
         print("Structure Build Lock: Old Config version detected, loading default configs")
         self:LoadDefaultConfig()
         config.Save( "structures-lock" )
       end
       self.LockFile = util.GetDatafile( "structures-lock" )
      local txt = self.LockFile:GetText()
      if (txt ~= "") then
      self.Lock = json.decode( txt )
      else
      self.Lock = {}
      end
    end
    function PLUGIN:postInit()
       self:AddChatCommand("lock", self.cmdLock)
    end
    function PLUGIN:cmdLock(netuser,cmd,args)
       local userID = rust.GetUserID(netuser)
       if(not self.Lock[userID]) then
         self.Lock[userID] = true
         rust.SendChatToUser(netuser, self.Config.chatname, "Your buildings are now locked, no one will be able to build on them")
       
       else
         self.Lock[userID] = false
         rust.SendChatToUser(netuser, self.Config.chatname, "Your buildings are now unlocked, anyone can build on them")
       end
       self:Save()
    end
    function PLUGIN:Save()
       self.LockFile:SetText( json.encode( self.Lock ) )
       self.LockFile:Save()
    end
    function PLUGIN:GetStructureOwnerID(structure)
      return getStructureMasterOwnerId(structure)
    endfunction PLUGIN:LoadDefaultConfig()
       self.Config.chatname = "Lock"
       self.Config.Version = 1.0
       self.Config.NotifyUser = true
    endfunction PLUGIN:TwoDDistance(p1, p2)
      return math.sqrt(math.pow(p1.x - p2.x,2) + math.pow(p1.z - p2.z,2))
    endfunction PLUGIN:SendToUserByPos(pos,msg)
       for _, netuser in pairs( rust.GetAllNetUsers() ) do
         if(netuser.playerClient) then
           if(self:TwoDDistance(pos,netuser.playerClient.lastKnownPosition) < 10) then
             rust.SendChatToUser(netuser, self.Config.chatname, msg)
           end
         end
       end
    end
    function PLUGIN:FindStructure(pos)
       for i=0, AllStructures().Count-1 do
         local master = AllStructures()
         if(master) then
           local comp = master:GetComponentFromPositionWorld(pos)
           if(comp ~= nil) then
             return master, comp
           end
         end
       end 
       return false
    endlocal NetCullRemove = util.FindOverloadedMethod(Rust.NetCull._type, "Destroy", bf.public_static, {UnityEngine.GameObject})
    function RemoveObject(object)
       local objs = util.ArrayFromTable(cs.gettype("System.Object"), {object})
       cs.convertandsetonarray( objs, 0, object , UnityEngine.GameObject._type )
       NetCullRemove:Invoke(nil, objs)
    end
    function PLUGIN:CheckIfCanOrRemove(pos)
       local master, structure = self:FindStructure(pos)
       if(not master) then error("couldn't find the new structure") return end
       local masterid = self:GetStructureOwnerID(master)
       if(not masterid) then error("couldn't find the masterid of a structure") return end
       if(self.Lock[tostring(masterid)]) then
         RemoveObject(structure.GameObject)
         if(self.Config.NotifyUser) then
           self:SendToUserByPos(pos, "This building is locked, use /lock to activate/deactivate building on your structures only")
         end
       end
    end
    function PLUGIN:OnPlaceStructure(structure,pos)
       if(string.find(structure.name, "Stairs") or string.find(structure.name, "Pillar") or string.find(structure.name, "Foundation")) then
         timer.Once(0.2, function() self:CheckIfCanOrRemove(pos) end)
       end
    end