Configuration null for some reasonSolved

I am working on a fun plugin where i want to be able to add strings(permissions) to a list but the command doesnt seem to fire.

I kinda need this plugin finished asap so any help would be appreciated

 

using System.Collections.Generic;

namespace Oxide.Plugins
{
    [Info("FactionsPlus", "Xmaniaxz", "0.0.1")]
    internal class FactionsPlus : RustPlugin
    {
        public Configuration config;

        #region INIT

        private void Init()
        {
            Puts("Initializing Plugin");
            Puts("Loading configuration.");
            config = Config.ReadObject<Configuration>();
            Config.WriteObject(config, true);
            Permissions();
            Commands();
        }

        #endregion INIT

        #region Config

        public class Configuration
        {
            public List<string> PermissionsList;
        }

        protected override void LoadDefaultConfig()
        {
            config = new Configuration()
            {
                PermissionsList = new List<string>()
                {
                    "ugather.blessed"                    
                }
            };
        }

        private void SaveConfig(Configuration config)
        {
            Config.WriteObject(config, true);
            SaveConfig();
        }

        #endregion Config

        #region Permissions & Commands

        private void Commands()
        {
            cmd.AddChatCommand("Addblessed", this, "BlessedAdd");
            Puts("Loaded Command: Addblessed ");
            cmd.AddChatCommand("ShowBlessed", this, "DebugBlessed");
            Puts("Loaded Command: DebugBlessed ");
        }

        private void Permissions()
        {
            permission.RegisterPermission("FactionsPlus.Holder", this);
            permission.RegisterPermission("FactionsPlus.Admin", this);
        }

        private void GrantBlessedPermissions(string groupperms)
        {
            foreach (string perm in config.PermissionsList)
            {
                if (!permission.GroupHasPermission(groupperms, perm))
                    permission.GrantGroupPermission(groupperms, perm, null);
            }
        }

        private void RevokeBlessedPermissions(string groupperms)
        {
            foreach (string perm in config.PermissionsList) 
            {
                if (permission.GroupHasPermission(groupperms, perm))
                    permission.RevokeGroupPermission(groupperms, perm);
            }
        }

        #endregion Permissions & Commands

        #region Commands
        [ChatCommand("ShowBlessed")]
        private void DebugBlessed(BasePlayer player, Configuration config)
        {
            Puts("ShowBlessed Called!");
            if (permission.UserHasPermission(player.UserIDString, "FactionsPlus.Admin"))
            {
                if (config.PermissionsList.Count == 0)
                {
                    player.IPlayer.Reply("List is Empty");
                }
                foreach (var entry in config.PermissionsList)
                {
                    player.IPlayer.Reply(entry);
                }
            }
            else
                player.IPlayer.Reply("You don't have the required permission");
        }
        [ChatCommand("AddBlessed")]
        private void BlessedAdd(BasePlayer player,Configuration config,string[] arg)
        {
            Puts("BlessedAdd Called!");
            if (!permission.UserHasPermission(player.UserIDString, "FactionsPlus.Admin"))
            {
                player.IPlayer.Reply("You don't have the required permission to add permissions");
            }
            else
            {
                foreach (var entry in config.PermissionsList)
                {
                    bool IsAssigned = false;
                    if (entry == arg[0])
                    {
                        IsAssigned = true;
                    }
                    if (!IsAssigned)
                    {
                        config.PermissionsList.Add(arg[0]);
                        string[] groups = permission.GetPermissionGroups("FactionsPlus.Holder");
                        foreach (var data in groups)
                        {
                            GrantBlessedPermissions(data);
                            player.IPlayer.Reply($"You have added {arg} to the blessed team");
                        }
                        IsAssigned = false;
                        break;
                    }
                }
            }
        }

        #endregion Commands

        #region Hooks

        private void Unload()
        {
            SaveConfig(config);
        }

        private void OnEntityDeath(BuildingPrivlidge ToolCupboard, HitInfo info)
        {
            Puts("Checking for TC");
            BasePlayer owner = BasePlayer.FindByID(ToolCupboard.OwnerID);
            BasePlayer attacker = info?.InitiatorPlayer;
            if (owner != null && attacker != null)
            {
                if (owner.displayName != attacker.displayName)
                {
                    covalence.Server.Broadcast($"{attacker.displayName}'s team, destroyed the tool cupboard of {owner.displayName}'s team");
                    CheckIfPlayerGroupHasPermission(owner, attacker);
                }
            }
        }

        private void CheckIfPlayerGroupHasPermission(BasePlayer player, BasePlayer attacker)
        {
            string[] groups = permission.GetUserGroups(player.IPlayer.Id);
            string[] Attackergroups = permission.GetUserGroups(attacker.IPlayer.Id);
            foreach (var entry in groups)
            {
                if (permission.GroupHasPermission(entry, "FactionsPlus.Holder"))
                {
                    permission.RevokeGroupPermission(entry, "FactionPlus.Holder");
                    RevokeBlessedPermissions(entry);
                    covalence.Server.Broadcast($"{player.displayName}'s team has lost the Holy Grail!");
                    foreach (var attackentry in Attackergroups)
                    {
                        GrantBlessedPermissions(attackentry);
                        permission.GrantGroupPermission(attackentry, "FactionPlus.Holder", null);
                        covalence.Server.Broadcast($"{player.displayName}'s team stole the Holy Grail and have become blessed!");
                    }
                }
            }
        }

        #endregion Hooks
    }
}

BasePlayer player, Configuration config

Thet isn't a valid signature for a command method.

BasePlayer/IPlayer, string command, string[] args

Should I use that (I had this previously). it gives me the error that it cannot find the object reference.

I'd need to see what you are using, but I suspect that isn't the cause of your NRE.

Shall I show you the error?

Merged post

[FactionsPlus] ShowBlessed Called! Failed to call hook 'DebugBlessed' on plugin 'FactionsPlus v0.0.1' (NullReferenceException: Object reference not set to an instance of an object) at Oxide.Plugins.FactionsPlus.DebugBlessed (BasePlayer player, System.String command, System.String[] args) [0x0002c] in <461e00cabecd4e2e9f73d85d4424d96f>:0 at Oxide.Plugins.FactionsPlus.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x0016e] in <461e00cabecd4e2e9f73d85d4424d96f>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00079] in <80b90e8213db44b29ec2d4111764172c>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x000d8] in :0 at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00060] in :0

They error would mean something in that method is null, not an error with the signature. You'd need to add some debug output to see what may be null, but right now it is looking like something with your configuration.

My config does say null. I tried to add a string but it didnt allow it apperantly

Merged post

using System.Collections.Generic;
using Newtonsoft.Json;
namespace Oxide.Plugins
{
    [Info("FactionsPlus", "Xmaniaxz", "0.0.1")]
    internal class FactionsPlus : RustPlugin
    {
        public Configuration config;

        #region INIT

        private void Init()
        {
            Puts("Initializing Plugin");
            Puts("Loading configuration.");
            config = Config.ReadObject<Configuration>();
            Config.WriteObject(config, true);
            Permissions();
            Commands();
        }

        #endregion INIT

        #region Config

        public class Configuration
        {
            [JsonProperty(PropertyName = "Permissions : The list of permissions that will be granted at to the blessed team.")]
            public List<string> PermissionsList;
        }

        protected override void LoadDefaultConfig()
        {
            config = new Configuration()
            {
                PermissionsList = new List<string>()
                {
                    { "ugather.blessed" }                    
                }
            };
            SaveConfig(config);
        }

        private void SaveConfig(Configuration config)
        {
            Config.WriteObject(config, true);
            SaveConfig();
        }

        #endregion Config

        #region Permissions & Commands

        private void Commands()
        {
            cmd.AddChatCommand("Addblessed", this, "BlessedAdd");
            Puts("Loaded Command: Addblessed ");
            cmd.AddChatCommand("ShowBlessed", this, "DebugBlessed");
            Puts("Loaded Command: DebugBlessed ");
        }

        private void Permissions()
        {
            permission.RegisterPermission("FactionsPlus.Holder", this);
            permission.RegisterPermission("FactionsPlus.Admin", this);
        }

        private void GrantBlessedPermissions(string groupperms)
        {
            foreach (string perm in config.PermissionsList)
            {
                if (!permission.GroupHasPermission(groupperms, perm))
                    permission.GrantGroupPermission(groupperms, perm, null);
            }
        }

        private void RevokeBlessedPermissions(string groupperms)
        {
            foreach (string perm in config.PermissionsList) 
            {
                if (permission.GroupHasPermission(groupperms, perm))
                    permission.RevokeGroupPermission(groupperms, perm);
            }
        }

        #endregion Permissions & Commands

        #region Commands
        [ChatCommand("ShowBlessed")]
        private void DebugBlessed(BasePlayer player,string command,string[] args)
        {
            Puts("ShowBlessed Called!");
            if (permission.UserHasPermission(player.UserIDString, "FactionsPlus.Admin"))
            {
                if (config.PermissionsList.Count == 0)
                {
                    player.IPlayer.Reply("List is Empty");
                }
                foreach (var entry in config.PermissionsList)
                {
                    player.IPlayer.Reply(entry);
                }
            }
            else
                player.IPlayer.Reply("You don't have the required permission");
        }
        [ChatCommand("AddBlessed")]
        private void BlessedAdd(BasePlayer player,string command,string[] args)
        {
            Puts("BlessedAdd Called!");
            if (!permission.UserHasPermission(player.UserIDString, "FactionsPlus.Admin"))
            {
                player.IPlayer.Reply("You don't have the required permission to add permissions");
            }
            else
            {
                foreach (var entry in config.PermissionsList)
                {
                    bool IsAssigned = false;
                    if (entry == args[0])
                    {
                        IsAssigned = true;
                    }
                    if (!IsAssigned)
                    {
                        config.PermissionsList.Add(args[0]);
                        string[] groups = permission.GetPermissionGroups("FactionsPlus.Holder");
                        foreach (var data in groups)
                        {
                            GrantBlessedPermissions(data);
                            player.IPlayer.Reply($"You have added {args} to the blessed team");
                        }
                        IsAssigned = false;
                        break;
                    }
                }
            }
        }

        #endregion Commands

        #region Hooks

        private void Unload()
        {
            SaveConfig(config);
        }

        private void OnEntityDeath(BuildingPrivlidge ToolCupboard, HitInfo info)
        {
            Puts("Checking for TC");
            BasePlayer owner = BasePlayer.FindByID(ToolCupboard.OwnerID);
            BasePlayer attacker = info?.InitiatorPlayer;
            if (owner != null && attacker != null)
            {
                if (owner.displayName != attacker.displayName)
                {
                    covalence.Server.Broadcast($"{attacker.displayName}'s team, destroyed the tool cupboard of {owner.displayName}'s team");
                    CheckIfPlayerGroupHasPermission(owner, attacker);
                }
            }
        }

        private void CheckIfPlayerGroupHasPermission(BasePlayer player, BasePlayer attacker)
        {
            string[] groups = permission.GetUserGroups(player.IPlayer.Id);
            string[] Attackergroups = permission.GetUserGroups(attacker.IPlayer.Id);
            foreach (var entry in groups)
            {
                if (permission.GroupHasPermission(entry, "FactionsPlus.Holder"))
                {
                    permission.RevokeGroupPermission(entry, "FactionPlus.Holder");
                    RevokeBlessedPermissions(entry);
                    covalence.Server.Broadcast($"{player.displayName}'s team has lost the Holy Grail!");
                    foreach (var attackentry in Attackergroups)
                    {
                        GrantBlessedPermissions(attackentry);
                        permission.GrantGroupPermission(attackentry, "FactionPlus.Holder", null);
                        covalence.Server.Broadcast($"{player.displayName}'s team stole the Holy Grail and have become blessed!");
                    }
                }
            }
        }

        #endregion Hooks
    }
}


Merged post

The Config Output

This is the output of my config file

I can't see your image.

{
  "Permissions : The list of permissions that will be granted at to the blessed team.": null
}


Merged post

same text as my image

Have you tried deleting the file and allowing it to be re-created? LoadDefaultConfig is only ever called once, so if you didn't create default data originally, then it would be null and not updated automatically.

I have indeed tried to remove it multiple times giving the same output.

Save the config after it loads the default config in put somthing there to verify its called..

LoadDefaultConfig()​
Locked automatically