_config = Config.ReadObject<Configuration>();The line above, which seems to be a standard in everyone's plugin, is breaking my plugin and sanity.
_config is always null if I have a config file already in the oxide/config directory. If not, my plugin works perfectly using config defaults.
I have to assume that my config file isn't being read, likely due to formatting possibly, but I can't find any documentation on formatting standards. Compared to other languages, C# seems border-line impossible to trouble-shoot.. error messages are the furthest thing from clear or helpful (my error response is below).
15:59 [Info] GroupWatch was compiled successfully in 302ms
15:59 [Error] Could not initialize plugin 'GroupWatch v0.0.8' (NullReferenceException: Object reference not set to an instance of an object)
at Oxide.Plugins.GroupWatch.LoadConfig () [0x00029] in <a9d7b4c1f6a94bba801764eca8477443>:0
at Oxide.Core.Plugins.Plugin.HandleAddedToManager (Oxide.Core.Plugins.PluginManager manager) [0x0000f] in <99d545163fdd4d57a562df7989f2ca0a>:0
at Oxide.Core.Plugins.CSPlugin.HandleAddedToManager (Oxide.Core.Plugins.PluginManager manager) [0x00000] in <99d545163fdd4d57a562df7989f2ca0a>:0
at Oxide.Plugins.CSharpPlugin.HandleAddedToManager (Oxide.Core.Plugins.PluginManager manager) [0x00000] in <cc99cb05b42e4ea494cdf294badea406>:0
at Oxide.Plugins.CovalencePlugin.HandleAddedToManager (Oxide.Core.Plugins.PluginManager manager) [0x000b3] in <cc99cb05b42e4ea494cdf294badea406>:0
at Oxide.Core.Plugins.PluginManager.AddPlugin (Oxide.Core.Plugins.Plugin plugin) [0x00027] in <99d545163fdd4d57a562df7989f2ca0a>:0
at Oxide.Core.OxideMod.PluginLoaded (Oxide.Core.Plugins.Plugin plugin) [0x00035] in <99d545163fdd4d57a562df7989f2ca0a>:0
15:59 [Info] Rolling back plugin to last good version: GroupWatch
15:59 [Info] Loaded plugin Group Watch v0.0.8 by RustonautsMy config file looks ok (below).
{
"exists": true,
"WatchGroups": [
{
"groupName": "example-group",
"enabled": true,
"responseCmd": null,
"responseKit": "example-kit"
},
{
"groupName": "example-group-6",
"enabled": false,
"responseCmd": "Puts('hello')",
"responseKit": "example-kit2"
}
]
}My entire plugin is below,
using Newtonsoft.Json;
using Oxide.Core.Plugins;
using System;
using System.Collections.Generic;
namespace Oxide.Plugins
{
[Info("Group Watch", "Rustonauts", "0.0.8")]
[Description("Allows server owners to execute commands once a player is added to specific groups.")]
class GroupWatch : CovalencePlugin
{
private Configuration _config;
private void Init()
{
Puts("Initializing...");
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_config = Config.ReadObject<Configuration>();
if (_config == null) throw new Exception();
Puts("Config loaded");
}
catch
{
Puts("config error, create defaults");
LoadDefaultConfig();
}
}
protected override void LoadDefaultConfig()
{
_config = new Configuration();
}
protected override void SaveConfig()
{
Config.WriteObject(_config, true);
}
private class WatchGroupSettings
{
public string groupName;
public bool enabled;
public string responseCmd;
public string responseKit;
}
private class WatchGroup
{
public string groupName;
public bool enabled;
public string responseCmd;
public string responseKit;
public WatchGroup(WatchGroupSettings settings)
{
groupName = settings.groupName;
enabled = settings.enabled;
responseCmd = settings.responseCmd;
responseKit = settings.responseKit;
}
}
private class Configuration
{
public bool exists { get; set; } = true;
public IEnumerable<WatchGroup> WatchGroups { get; set; } = new[]
{
new WatchGroup(new WatchGroupSettings()
{
groupName = "example-group",
enabled = true,
responseCmd = null,
responseKit = "example-kit"
}),
new WatchGroup(new WatchGroupSettings()
{
groupName = "example-group-6",
enabled = false,
responseCmd = "Puts('hello')",
responseKit = "example-kit2"
})
};
}
void OnUserGroupAdded(string id, string groupName)
{
Puts($"Player added to group");
foreach (var group in _config.WatchGroups)
{
//Puts($"watched: \"{group.groupName}\".");
}
}
}
}
Any suggestions would be greatly appreciated. Thank you in advance!