How to unload a plugin when it loads?Solved

When loading a plugin in "OnServerInitialized()" I check some conditions and if they are not met, I do not want the plugin to work at all.
So I call "Interface.Oxide.UnloadPlugin()" to unload it, but in the console it writes that the plugin is unloaded and then writes that it is loaded.
The plugin doesn't seem to work, but if you make requests to it from another plugin, it accepts them and a bunch of errors occur because it is not fully loaded.

For example:

private void OnServerInitialized()
{
     if (!SomePlugin)
     {
          PrintError("Missing plugin 'SomePlugin'");
          Interface.Oxide.UnloadPlugin(Title);
          return;
     }
}

Sorry for my bad english.

you could use covalence.Server.Command($"o.unload {Title}" );

njDh1NzhmMfGBeG.png Razor

you could use covalence.Server.Command($"o.unload {Title}" );

What the OP is using would be the better way, the command just runs the above method.

Using UnloadPlugin would not cause the plugin to load again, so there'd be something else causing the issue.

Is there any //Required with in that plugin?

MSMrULtzyhU4pCk.png Wulf

What the OP is using would be the better way, the command just runs the above method.

Using UnloadPlugin would not cause the plugin to load again, so there'd be something else causing the issue.

I have created two new sample files "TestPlugin" and "TestPlugin2".
For the "TestPlugin" plugin to work, the "SomePlugin" plugin must be present, otherwise it is unloaded. It also has an api function "ApiGet", which outputs a list to the console.
It turns out that, in theory, the "TestPlugin" plugin should not load and the "ApiGet" function should not work.
But if we call it in the plugin "TestPlugin2", then it will work, although the plugin "TestPlugin" is unloaded.

TestPlugin:

using System.Collections.Generic;
using Oxide.Core;
using Oxide.Core.Plugins;

namespace Oxide.Plugins
{
    [Info("TestPlugin", "Timon1221", "1.0.0")]
    class TestPlugin : RustPlugin
    {
        [PluginReference]
        private Plugin SomePlugin;

        List<string> list = new List<string>
        {
            "test1",
            "test2",
            "test3"
        };
        
        private void OnServerInitialized()
        {
            if (!SomePlugin)
            {
                PrintError("Missing plugin 'SomePlugin'");
                Interface.Oxide.UnloadPlugin(Title);
                return;
            }
        }

        void ApiGet()
        {
            foreach (var item in list) Puts(item);
        }
    }
}

TestPlugin2:

using Oxide.Core.Plugins;

namespace Oxide.Plugins
{
    [Info("TestPlugin2", "Timon1221", "1.0.0")]
    class TestPlugin2 : RustPlugin
    {
        [PluginReference]
        private Plugin TestPlugin;

        [ConsoleCommand("tcommand")]
        void GetList() => TestPlugin.Call("ApiGet");
    }
}

Console (I just load the "TestPlugin" plugin, it wrote that it was unloaded and then that it was loaded):

I'd suggest using the // Requires: PluginName then, there's no reason to manually handle that.

Locked automatically