Skip to content

Instantly share code, notes, and snippets.

@DustinAlandzes
Created June 3, 2018 02:47
Show Gist options
  • Save DustinAlandzes/3c18afa2c4faede72689f85aefb97224 to your computer and use it in GitHub Desktop.
Save DustinAlandzes/3c18afa2c4faede72689f85aefb97224 to your computer and use it in GitHub Desktop.
Some C# OxideMod examples from the documentation: http://docs.oxidemod.org/rust/#getting-started
using Oxide.Core.Libraries.Covalence;
namespace Oxide.Plugins
{
[Info("EpicStuff", "Unknown", 1.0)]
[Description("This example illustrates how to use a GET WebRequest")]
class EpicStuff : CovalencePlugin
{
[Command("get")]
void GetRequest(IPlayer player, string command, string[] args)
{
// Set a custom timeout (in milliseconds)
var timeout = 200f;
// Set some custom request headers (eg. for HTTP Basic Auth)
var headers = new Dictionary<string, string> { { "header", "value" } };
webrequest.EnqueueGet("http://www.google.com/search?q=oxide", (code, response) => GetCallback(code, response, player), this, headers, timeout);
}
void GetCallback(int code, string response, IPlayer player)
{
if (response == null || code != 200)
{
Puts($"Error: {code} - Couldn't get an answer from Google for {player.Name}");
return;
}
Puts($"Google answered for {player.Name}: {response}");
}
[Command("post")]
void PostRequest(IPlayer player, string command, string[] args)
{
// Set a timeout (in milliseconds)
var timeout = 200f;
// Set some custom request headers (eg. for HTTP Basic Auth)
var headers = new Dictionary<string, string> { { "header", "value" } };
webrequest.EnqueuePost("http://www.google.com/search?q=oxide", "param1=value1&param2=value2", (code, response) => PostCallback(code, response, player), this, headers, timeout);
}
void PostCallback(int code, string response, IPlayer player)
{
if (response == null || code != 200)
{
Puts($"Error: {code} - Couldn't get an answer from Google for {player.Name}");
return;
}
Puts("Google answered for " + player.Name + ": " + response);
}
}
}
namespace Oxide.Plugins
{
[Info("SecondEpicStuff", "Unknown", 0.1)]
[Description("Makes more epic stuff happen")]
class SecondEpicStuff : CovalencePlugin
{
// First, add a reference to the plugin you are trying to call
// The name of this field needs to be the exact name of the desired plugin
// eg. We are referencing the example plugin above which is called 'EpicStuff'
[PluginReference]
Plugin EpicStuff;
// It's a good idea to check if the plugin you're trying to call
// has been loaded by oxide (otherwise you can't call the method)
void OnServerInitialized()
{
// Note: Trying to do this check in the plugin Init() method may
// fail, as the plugin load order may be different each time
if (EpicStuff == null)
{
PrintWarning("Plugin 'EpicStuff' was not found!");
}
}
void CallPlugin()
{
// Plugin methods return objects, so cast the API call to the type
// you're expecting
var getTypedReturn = (bool)EpicStuff?.Call("GetReturn");
// Send parameters through as variables after the method name
var takeParam = (string)EpicStuff?.Call("TakeParam", "param1", 1024);
// Use JSON.net to process the returned object
var returnedObject = EpicStuff?.Call("ReturnObject");
// Call a plugin to do some work without returning anything
EpicStuff?.Call("SendMessage");
}
}
}
namespace Oxide.Plugins
{
[Info("Epic Stuff", "Unknown", 1.0)]
[Description("This example illustrates how to use a basic configuration file")]
class EpicStuff : CovalencePlugin
{
protected override void LoadDefaultConfig()
{
PrintWarning("Creating a new configuration file");
Config.Clear();
Config["ShowJoinMessage"] = true;
Config["ShowLeaveMessage"] = true;
Config["JoinMessage"] = "Welcome to this server";
Config["LeaveMessage"] = "Goodbye";
SaveConfig();
}
}
}
using System.Collections.Generic;
using Oxide.Core;
namespace Oxide.Plugins
{
[Info("Epic Stuff", "Unknown", 1.0)]
[Description("This example illustrates how to create a data file")]
class EpicStuff : CovalencePlugin
{
class StoredData
{
public HashSet<PlayerInfo> Players = new HashSet<PlayerInfo>();
public StoredData()
{
}
}
class PlayerInfo
{
public string UserId;
public string Name;
public PlayerInfo()
{
}
public PlayerInfo(BasePlayer player)
{
UserId = player.userID.ToString();
Name = player.displayName;
}
}
StoredData storedData;
void Loaded()
{
storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>("MyDataFile");
}
}
}
namespace Oxide.Plugins
{
[Info("EpicStuff", "Unknown", 0.1)]
[Description("Makes epic stuff happen")]
class EpicStuff : CovalencePlugin
{
// Plugin methods can be a simple bool that is returned
bool GetReturn()
{
return true;
}
// Plugin methods can take parameters and return simple types
string TakeParam(string param, int secondParam)
{
if (param == "first parameter") return param;
else return "First parameter didn't match!";
}
// To return complex types, they should first be converted
// into builtin types (e.g. JSON.net types like JObject, JArray, etc. or builtin
// collections like System.Collections.Generic.Dictionary)
JObject ReturnObject()
{
var myObject = new JObject();
myObject["key"] = "value";
myObject["array"] = new JArray();
return myObject;
}
// Plugin methods don't have to return something
void SendMessage()
{
Puts("You just called the 'SendMessage' method!");
}
}
}
using System.Collections.Generic;
using Oxide.Core;
namespace Oxide.Plugins
{
[Info("Epic Stuff", "Unknown", 1.0)]
[Description("This example illustrates how to save to a data file")]
class EpicStuff : CovalencePlugin
{
class StoredData
{
public HashSet<PlayerInfo> Players = new HashSet<PlayerInfo>();
public StoredData()
{
}
}
class PlayerInfo
{
public string UserId;
public string Name;
public PlayerInfo()
{
}
public PlayerInfo(IPlayer player)
{
UserId = player.Id;
Name = player.Name;
}
}
StoredData storedData;
void Loaded()
{
storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>("MyDataFile");
}
[Command("Test")]
void Test(IPlayer player, string command, string[] args)
{
var info = new PlayerInfo(player);
if (storedData.Players.Contains(info))
PrintToChat(player, "Your data has already been added to the file");
else
{
PrintToChat(player, "Saving your data to the file");
storedData.Players.Add(info);
Interface.Oxide.DataFileSystem.WriteObject("MyDataFile", storedData);
}
}
}
}
namespace Oxide.Plugins
{
class Main : CovalencePlugin
{
void singleTimer()
{
timer.Once(3f, () =>
{
Puts("Hello world!")
});
}
void repeatingTimer()
{
timer.Repeat(5f, 0, () =>
{
Puts("Hello world!")
});
}
void nextFrameTimer()
{
NextFrame(() =>
{
Puts("Hello world!");
});
}
}
}
using Oxide.Core.Libraries.Covalence;
namespace Oxide.Plugins
{
[Info("EpicStuff", "Unknown", 1.0)]
[Description("This example illustrates how to use a GET WebRequest")]
class EpicStuff : CovalencePlugin
{
[Command("get")]
void GetRequest(IPlayer player, string command, string[] args)
{
webrequest.EnqueueGet("http://www.google.com/search?q=oxide", (code, response) => GetCallback(code, response, player), this);
}
void GetCallback(int code, string response, IPlayer player)
{
if (response == null || code != 200)
{
Puts($"Error: {code} - Couldn't get an answer from Google for {player.Name}");
return;
}
Puts($"Google answered for {player.Name}: {response}");
}
[Command("post")]
void PostRequest(IPlayer player, string command, string[] args)
{
webrequest.EnqueuePost("http://www.google.com/search?q=oxide", "param1=value1&param2=value2", (code, response) => PostCallback(code, response, player), this);
}
void PostCallback(int code, string response, IPlayer player)
{
if (response == null || code != 200)
{
Puts($"Error: {code} - Couldn't get an answer from Google for {player.Name}");
return;
}
Puts("Google answered for " + player.Name + ": " + response);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment