Simple Rust Plugin Template

thethingtracks
3 min readDec 25, 2021

--

Prerequisites

The Code

The following code provides a simple foundation to start developing plugins:

using Newtonsoft.Json;
using UnityEngine;
using System;
using System.Collections.Generic;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using Oxide.Core.Libraries.Covalence;
namespace Oxide.Plugins
{
[Info("Plugin Name", "Author/s", "0.0.1")]
[Description("One sentence plugin description.")]
class PluginName : CovalencePlugin
{
private void OnServerInitialized()
{
AddCovalenceCommand("ping", "PingPong");
}
private void PingPong(IPlayer player, string command, string[] args)
{
player.Reply("Pong");
}
}
}

Code Breakdown

First, you need to access a few different namespaces so we have access to all of the methods/functions commonly used for Rust plugin development. You could use more or less namespaces. It’s recommended that when you finish a plugin, you trim the list to remove any unused namespaces to be a tidy developer.

For more information, reference the namespace documentation from Microsoft.

using Newtonsoft.Json;
using UnityEngine;
using System;
using System.Collections.Generic;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using Oxide.Core.Libraries.Covalence;

We then start writing our plugin inside the Oxide.Plugins namespace. Every plugin needs to exist within this namespace. All of the code you write (excluding the above using namespace code) needs to exist within the Oxide.Plugins namespace:

namespace Oxide.Plugins
{
// All plugin code goes here.
}

Every plugin needs to have an Oxide.Plugins.InfoAttribute and Oxide.Plugins.DescriptionAttribute. For more information on C# attributes, reference the attribute documentation from Microsoft.

The info attribute contains the plugin name, authors and a version number.
The description attribute contains a small description of your plugins.

[Info("Plugin Name", "Author/s", "0.0.1")]
[Description("One sentence plugin description.")]

Next is the main class for your plugin; the name of your .cs file needs to be identical to this class name. For example, if your plugin is called RandomTeleport, then the .cs file needs to be called RandomTeleport.cs and the following class needs to be called RandomTeleport.

The plugin class also needs to inherit the CovalencePlugin class. For more information on C# inheritance, reference this example from W3 Schools.

class PluginName : CovalencePlugin
{
// Main plugin code lives in here.
}

The OnServerInitialized() method will be called when your plugin is initialized. One way of adding usable commands is to register them using the AddCovalenceCommand(command, method name). In the following example, the command ping (/ping) will run a method called PingPong.

private void OnServerInitialized()
{
AddCovalenceCommand("ping", "PingPong");
}

Finally; a method called by a command will accept an IPlayer, string and string array types.

IPlayer references the player (an IPlayer object) who ran the command. The first string represents the command run. The string array holds an array of strings, with each array item split by the spaces in the command.

private void PingPong(IPlayer player, string command, string[] args)
{
player.Reply("Command: " + command);
player.Reply("Pong");
}

UMod Plugin Standards

There are a few plugin standards when it comes to UMod; these are generally accepted in the community across the board, so they’re good to follow.

The core one related to this guide is the style guide:

UMod Style Guide
UMod Approval Guide

--

--