Solved A better C# example?

Discussion in 'Rust Development' started by UnrulyMyth, Feb 7, 2015.

  1. I was wondering if I could get a more detailed C# example. I find myself having trouble with a few of the hooks, namely OnPlayerChat. If I could just get a quick example with a few hooks and how to save and load to/from the config file that'd be fantastic.
     
  2. look into my plugins, Die wilde betty worked with me on making those config. and it works pretty well.

    i can give a fast exemple here, give me couple mins.
    [DOUBLEPOST=1423300819][/DOUBLEPOST]
    Code:
    // Reference: Oxide.Ext.Rustusing System.Collections.Generic;
    using System;
    using System.Reflection;
    using System.Data;
    using UnityEngine;
    using Oxide.Core;
    namespace Oxide.Plugins
    {
        [Info("RemoverTool", "Reneb", 2.0)]
        class RemoverTool : RustPlugin
        {
    // Here you need to initiate all your variables that you will use in your config, with the correct format that you want them in C#
            private bool Changed;        private int removeAuth;
            private int removeAdmin;
            private int removeAll;
            private int removeTarget;
            private double deactivateTimer;
            private double deactivateMaxTimer;
            private bool refundAllowed;
            private float refundRate;
            private bool useToolCupboard;
            private string noAccess;
            private string cantRemove;
            private string tooFar;//Loaded = Init, it's when the plugin is first launched
            void Loaded()
            {
                LoadVariables();
            }
    // Get Config here you can use it as it is
    // it will let you have 1 sub category for your config, in lua it would look like:
    // self.Config.Messages = {}
    // self.Config.Messages.Welcome = "Welcome message"
    // but you wont be able to do: self.Config.Messages.Test = {}
    // Only 1 sub category
            object GetConfig(string menu, string datavalue, object defaultValue)
            {
                var data = Config[menu] as Dictionary<string, object>;
                if (data == null)
                {
                    data = new Dictionary<string, object>();
                    Config[menu] = data;
                    Changed = true;
                }
                object value;
                if (!data.TryGetValue(datavalue, out value))
                {
                    value = defaultValue;
                    data[datavalue] = value;
                    Changed = true;
                }
                return value;
            }        void LoadVariables()
            {
    // So you need: GetConfig("SUBCATEGORY","VARIABLE","DEFAULT VALUE")
    // then you need to convert it in the type you went them in your plugin.
    // Here it would look like this in the config file:
    // {
    // "Remove":{
    // "target":1,
    // "basic":1,
    // "admin":2,
    // "all":2,
    // },
    // "RemoveTimer":{
    // "default":30,
    // "max":600
    // },
    // "Refund":{
    // "activated":true,
    // "rate":0.5
    // },
    // "ToolCupboard":{
    // "activated":true
    // }
    // Only problem with this method it that you cant make a config without sub categories, but it doesn't really matter.
                removeTarget = Convert.ToInt32(GetConfig("Remove", "target", 1));
                removeAuth = Convert.ToInt32(GetConfig("Remove", "basic", 0));
                removeAdmin = Convert.ToInt32(GetConfig("Remove", "admin", 2));
                removeAll = Convert.ToInt32(GetConfig("Remove", "all", 2));
                deactivateTimer = Convert.ToDouble(GetConfig("RemoveTimer", "default", 30));
                deactivateMaxTimer = Convert.ToDouble(GetConfig("RemoveTimer", "max", 600));
                refundAllowed = Convert.ToBoolean(GetConfig("Refund", "activated", true));
                refundRate = Convert.ToSingle(GetConfig("Refund", "rate", 0.5));
                useToolCupboard = Convert.ToBoolean(GetConfig("ToolCupboard", "activated", true));            noAccess = Convert.ToString(GetConfig("Messages", "NoAccess", "You don't have the permissions to use this command"));
                cantRemove = Convert.ToString(GetConfig("Messages", "cantRemove", "You are not allowed to remove this"));
                tooFar = Convert.ToString(GetConfig("Messages", "tooFar", "You must get closer to remove this"));            if (Changed)
                {
    // SaveConfig() is an Oxide function, so it's normal that it points at nothing in the plugin
                    SaveConfig();
                    Changed = false;
                }
            }
    // LoadDefaultConfig is only called on first execution of the plugin or if someone deletes the config file.
            void LoadDefaultConfig()
            {
                Puts("RemoverTool: Creating a new config file");
                Config.Clear(); // force clean new config
                LoadVariables();
            }
    }
    
    Awesome thing about this way is that it is dynamic, so you don't need to force players to change there config file every update, if something is missing it will just add it to the config file.
    [DOUBLEPOST=1423300946][/DOUBLEPOST]and welcome in the C# plugin world :)
     
    Last edited by a moderator: Feb 7, 2015
  3. Awesome, thanks a lot! So my other problem is I can't get OnPlayerChat to work correctly. It complains about the chat.say? Could I get an example with that. Being a bit needy but I tried everything I could think of to get it to work and could not.
     
  4. show us your code, i didn't code anything with this hook so i dont have any examples with me.
     
  5. Well I think I am basically asking what the parameters are for it. The hooks says that it's chat.say? But I get an just trying to call the hook with that parameter and nothing else. Maybe I'm not using the correct library or something...? I was also wondering how to set up a timer, if that's doable.
     
    Last edited by a moderator: Feb 7, 2015
  6. == OnPlayerChat(Assembly-CSharp/chat.say arg)
    - Called from Assembly-CSharp/chat.say
    - Returning a non-null value overrides default behaviour of chat, not commands

    Its pretty clear knowing that arg is actually ConsoleSystem.Arg so you just need a method that matches those parameters.

    Code:
    object OnPlayerChat(ConsoleSystem.Arg arg)
            {
                BasePlayer basePlayer = arg.Player();
                string str = arg.GetString(0, "text").Trim();
                if (!basePlayer || str.StartsWith("/") || str.StartsWith("\\") || str.Length <= 0)
                {
                    return "Handled";
                }
                if (str.Length > 128)
                {
                    str = str.Substring(0, 128);
                }
                string chatColor = "#fa5";
                string displayName = basePlayer.displayName;
                string message = string.Format("<color={2}>{0}</color>  {1}", displayName.Replace('<', ' ').Replace('>', ' '), str.Replace('<', ' ').Replace('>', ' '), chatColor);
                ConsoleSystem.Broadcast("chat.add", new object[] { basePlayer.userID, message, 1f });
                return "Handled";
            }
    
     
  7. Haha, clear to those that know what they are doing. I do not though. I did wind up figuring it out myself but I thought I was doing it wrong. Thanks for the clarification though!
     
  8. Wulf

    Wulf Community Admin

    You're still ahead of me with C#. ;)
     
  9. Lol, thanks for the positivity!