Rust/Legacy/Snippets

From Oxide Wiki
Jump to: navigation, search

Skeleton Plugin with Single Command

PLUGIN.Title = "Example"
PLUGIN.Description = "Example Plugin"
PLUGIN.Author = "Your Name"
PLUGIN.Version = "1.0"
 
-- Called when oxide loads or user types oxide.reload example at F1 console
function PLUGIN:Init()
    self:AddChatCommand("example", self.cmdList)
end
 
-- Called when user types /example
function PLUGIN:cmdList(netuser, cmd, args)
    rust.SendChatToUser(netuser, "Your plugin works!")
end
 
-- Automated Oxide help function (added to /help list)
function PLUGIN:SendHelpText(netuser)
	rust.SendChatToUser(netuser, "Use /example to spawn in a pillar behind your head.")
end

Modular Design by using Plugin-Find

It appears dofile/loadfile/... doesn't work, or none of us have gotten them working yet. Main Code that will "import" the other module, and use it. The other "module" is just a plugin that doesn't register any commands or do anything.

Main Code of your plugin

-- Library loader
local alib = nil
function loadLibrary()
    if not alib then
        alib = cs.findplugin("ALibrary")
        if not alib then
            return false
        end
    end
    return true
end
 
function PLUGIN:Init()
    self:AddChatCommand("cmd", self.cmdExample)
end
 
function PLUGIN:cmdExample(netuser, cmd, args)
    -- Load the library
    loadLibrary()
 
    alib:ExampleLibFunc(netuser, "hello")
end

Your "module" (a second plugin) that you share to all your other plugins.

function PLUGIN:Init()
    -- Nothing required, just be a plugin
end
 
function PLUGIN:ExampleLibFunc(netuser, string)
    rust.SendChatToUser(netuser, string)
end

Get the compass direction that netuser is looking at

Returns the number of degrees from North that the player is looking at.

function LookingDirection()
    local controllable = netuser.playerClient.controllable
    local char = controllable:GetComponent( "Character" )
 
    local forward = char.forward -- This should be a forward vector
    local pitch = char.eyesPitch -- You can use angles if you'd prefer
    local yaw = char.eyesYaw -- You can use angles if you'd prefer
 
    -- Convert unit circle angle to compass angle.
    -- Known error: char.eyesYaw randomly returns a String value and breaks output
    local direction = (yaw + 90) % 360
 
    return direction
end

Find the closest object that the netuser is looking at

Returns a UnityEngine.RaycastHit type.

local Raycast = util.FindOverloadedMethod(UnityEngine.Physics, "RaycastAll", bf.public_static, { UnityEngine.Ray })
cs.registerstaticmethod("tmp", Raycast)
local RaycastAll = tmp
tmp = nil
local function TraceEyes(netuser)
    local controllable = netuser.playerClient.controllable
    local char = controllable:GetComponent("Character")
    local ray = char.eyesRay
    local hits = RaycastAll(ray)
    local tbl = cs.createtablefromarray(hits)
    if #tbl == 0 then return end
    local closest = tbl[1]
    local closestdist = closest.distance
    for i = 2, #tbl do
        if tbl[i].distance < closestdist then
            closest = tbl[i]
            closestdist = closest.distance
        end
    end
    return closest
end

Get data from the character component without going through LoadAvatar

local controllable = netuser.playerClient.controllable
local char = controllable:GetComponent("Character")
local forward = char.forward -- This should be a forward vector.
local pitch = char.eyesPitch -- You can use angles if you'd prefer
local yaw = char.eyesYaw -- You can use angles if you'd prefer

Restrict commands to admins

function PLUGIN:cmdAdminTest(netuser, cmd, args)
    if netuser:CanAdmin() then
        rust.Notice(netuser, "You are an admin!")
    else
        rust.Notice(netuser, "You are not an admin!")
    end
end

Get the current time of server or OS

The server uptime, server OS time, and in-game time are available.

local osTime = util.GetStaticPropertyGetter(System.DateTime, "Now")
 
function GetUptime()
    local serverUptime = UnityEngine.Time.realtimeSinceStartup
    return serverUptime
end
 
local currentDate = osTime()
local currentUptime = GetUptime()

Get a list of all structures / building owners in the game

This example script will get all the structures created by someone and show you who owns them. Thanks to Rexas in this forums post.

local GetStructureMasterOwnerIDField = field_get(Rust.StructureMaster, "ownerID", true)
local AllStructures = static_property_get(Rust.StructureMaster, "AllStructures")
 
function PLUGIN:Init()
    -- Bind a slash command
    self:AddChatCommand("test", self.test)
end
 
function PLUGIN:test(netuser)
    -- Get the array that holds all StructureMaster objects in the world.
 
    -- Loop all the strucutres
    for i = 0, AllStructures().Count - 1 do
        -- Print the ID attached to this structure
        print(self:GetStructureOwnerID(AllStructures()[i]))
    end
end
 
-- Get the ID for this structure
function PLUGIN:GetStructureOwnerID(structure)
    return GetStructureMasterOwnerIDField(structure)
end
 
function PLUGIN:OnStructureDecay(structure)
    print("------------------ structure ----------------")
    print(structure)
    print(self:GetStructureOwnerID(structure))
end