Simple static variable API for plugin developers

Supported Games
GameServerKingsGameServerKings

This is a simple static (non-file saving) variable system for developers. Useful for plugins intended without file usage.

More to come, cross plugin temporary variable communication etc etc. This is the bones for something special I hope.

For Developers

Hooks

// Main Hooks
InitPlugin(Plugin plugin, bool debug = false)
PluginExists(Plugin plugin)
DestroyPlugin(Plugin plugin, bool debug = false)

// General Variable Hooks
SetStaticVariable(Plugin plugin, string variable, string value, bool debug = false)
GetStaticVariable(Plugin plugin, string variable, bool debug = false)
RemoveStaticVariable(Plugin plugin, string variable, bool debug = false)

// Player Variable Hooks
InitPlayer(Plugin plugin, BasePlayer player, bool debug = false)
RemovePlayer(Plugin plugin, BasePlayer player, bool debug = false)
PlayerExists(Plugin plugin, BasePlayer player)

SetStaticPlayerVariable(Plugin plugin, BasePlayer player, string variable, string value, bool debug = false)
GetStaticPlayerVariable(Plugin plugin, BasePlayer player, string variable, bool debug = false)
RemoveStaticPlayerVariable(Plugin plugin, BasePlayer player, string variable, bool debug = false)
PlayerHasVariables(Plugin plugin, BasePlayer player)

Example Plugin

using System;
using Oxide.Core.Plugins;
namespace Oxide.Plugins
{
    [Info("TestPlugin", "Norn", 0.1)]
    [Description("Demonstration of MagicVariables.")]
    public class TestPlugin : RustPlugin
    {
        [PluginReference]
        Plugin MagicVariables;
        Timer uptime_timer;
        void OnServerInitialized()
        {
            if (MagicVariables)
            {
                if (!Convert.ToBoolean(MagicVariables.Call("PluginExists", this)))
                {
                    MagicVariables.Call("InitPlugin", this, true);
                }
                uptime_timer = timer.Repeat(1, 0, () => UptimeTimer());
            }
        }
        void OnPlayerInit(BasePlayer player)
        {
            if (!Convert.ToBoolean(MagicVariables.Call("PlayerExists", this, player)))
            {
                MagicVariables.Call("InitPlayer", this, player, true);
                MagicVariables.Call("SetStaticPlayerVariable", this, player, "connectionTime", "0");
            }
        }
        void OnPlayerDisconnected(BasePlayer player, string reason)
        {
            if (Convert.ToBoolean(MagicVariables.Call("PlayerExists", this, player)))
            {
                MagicVariables.Call("RemovePlayer", this, player, true);
            }
        }
        private void Unload()
        {
            if (uptime_timer != null)
            {
                uptime_timer.Destroy();
            }
            MagicVariables.Call("DestroyPlugin", this, true);
        }
        [ChatCommand("uptime")]
        void cmdUptime(BasePlayer player, string cmd, string[] args)
        {
            if (!Convert.ToBoolean(MagicVariables.Call("PlayerExists", this, player)))
            {
                MagicVariables.Call("InitPlayer", this, player, true);
                MagicVariables.Call("SetStaticPlayerVariable", this, player, "connectionTime", "1");
            }
            int uptime = Convert.ToInt32(MagicVariables.Call("GetStaticVariable", this, "upTime"));
            TimeSpan t = TimeSpan.FromSeconds(uptime);
            string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
            PrintToChat(player, "<color=yellow>"   this.Title   "</color>: "   answer   " <color=green>plugin uptime</color>.");
            int player_uptime = Convert.ToInt32(MagicVariables.Call("GetStaticPlayerVariable", this, player, "connectionTime"));
            t = TimeSpan.FromSeconds(player_uptime);
            answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
            PrintToChat(player, "<color=yellow>"   this.Title   "</color>: "   answer   " <color=green>player uptime</color>.");
        }
        private void UptimeTimer()
        {
            int uptime = Convert.ToInt32(MagicVariables.Call("GetStaticVariable", this, "upTime"));
            int new_uptime = uptime   1;
            MagicVariables.Call("SetStaticVariable", this, "upTime", new_uptime.ToString());
            foreach (BasePlayer player in BasePlayer.activePlayerList)
            {
                if (player != null)
                {
                    int player_uptime = Convert.ToInt32(MagicVariables.Call("GetStaticPlayerVariable", this, player, "connectionTime"));
                    player_uptime = player_uptime   1;
                    MagicVariables.Call("SetStaticPlayerVariable", this, player, "connectionTime", player_uptime.ToString());
                }
            }
        }
    }
}
This plugin is unlicensed and the original author reserves all rights to it.

The original author may request that this plugin be removed and there is a risk that the plugin may be unavailable in the future.

Note that the current maintainer may not have permission to assign a license to the plugin if they are not the original author, so explicit permission would need to be obtained from the original author in order for the plugin to remain openly available and guarantee that it will be around for all to enjoy going forward.

9.6K downloads
0 watchers

importedapivariables