mirror of
https://github.com/smartcmd/MinecraftConsoles.git
synced 2026-04-24 16:08:43 +00:00
Added enablePlugin() and disablePlugin() (#13)
* Exposed loaded plugins in FourKitHost with public getLoadedPlugins() * Fixed bad reference to ServerPlugins * Forgot that the PluginLoader was nullable, handled it * Implemented getPlugin(name) and getPlugins() in FourKit.cs * Implemented enablePlugin(plugin) and disablePlugin(plugin) in FourKit.cs --------- Co-authored-by: UniPM <zoc6x8voc@mozmail.com>
This commit is contained in:
parent
18a673bd46
commit
94bed94d00
|
|
@ -365,7 +365,7 @@ public static class FourKit
|
|||
/// <param name="name">Name of the plugin to check.</param>
|
||||
/// <returns>Plugin if it exists, otherwise null</returns>
|
||||
public static ServerPlugin? getPlugin(string name) {
|
||||
var loadedPlugins = Minecraft.Server.FourKit.FourKitHost.getLoadedPlugins().Where(x => x.name == name);
|
||||
var loadedPlugins = FourKitHost.getLoadedPlugins().Where(x => x.name == name);
|
||||
|
||||
if (loadedPlugins.Count() > 1) ServerLog.Warn("fourkit", $"More than one instance of a(n) '{name}' plugin.");
|
||||
return loadedPlugins.FirstOrDefault();
|
||||
|
|
@ -375,5 +375,17 @@ public static class FourKit
|
|||
/// Gets a list of all currently loaded plugins.
|
||||
/// </summary>
|
||||
/// <returns>The array of plugins.</returns>
|
||||
public static ServerPlugin[] getPlugins() => Minecraft.Server.FourKit.FourKitHost.getLoadedPlugins().ToArray(); // returns an array for better compatibility for bukkit->fourkit
|
||||
public static ServerPlugin[] getPlugins() => FourKitHost.getLoadedPlugins().ToArray(); // returns an array for better compatibility for bukkit->fourkit
|
||||
|
||||
/// <summary>
|
||||
/// Enables the specified plugin.
|
||||
/// </summary>
|
||||
/// <param name="plugin">Plugin to enable.</param>
|
||||
public static void enablePlugin(ServerPlugin plugin) => FourKitHost.s_loader?.EnablePlugin(plugin);
|
||||
|
||||
/// <summary>
|
||||
/// Disables the specified plugin.
|
||||
/// </summary>
|
||||
/// <param name="plugin">Plugin to disable.</param>
|
||||
public static void disablePlugin(ServerPlugin plugin) => FourKitHost.s_loader?.DisablePlugin(plugin);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ namespace Minecraft.Server.FourKit;
|
|||
|
||||
public static partial class FourKitHost
|
||||
{
|
||||
private static PluginLoader? s_loader;
|
||||
internal static PluginLoader? s_loader;
|
||||
|
||||
public static IReadOnlyList<Minecraft.Server.FourKit.Plugin.ServerPlugin> getLoadedPlugins() => s_loader?.Plugins ?? [];
|
||||
public static IReadOnlyList<Plugin.ServerPlugin> getLoadedPlugins() => s_loader?.Plugins ?? [];
|
||||
|
||||
[UnmanagedCallersOnly]
|
||||
public static void Initialize()
|
||||
|
|
|
|||
|
|
@ -119,41 +119,52 @@ internal sealed class PluginLoader
|
|||
{
|
||||
foreach (var plugin in _plugins)
|
||||
{
|
||||
try
|
||||
{
|
||||
InvokePluginMethod(plugin, "onEnable", "OnEnable");
|
||||
string pName = GetPluginString(plugin, "name", "getName", "GetName", plugin.GetType().Name);
|
||||
ServerLog.Info("fourkit", $"Enabled: {pName}");
|
||||
|
||||
FourKit.FireEvent(new PluginEnableEvent(plugin));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string pName = GetPluginString(plugin, "name", "getName", "GetName", plugin.GetType().Name);
|
||||
ServerLog.Error("fourkit", $"Error enabling {pName}: {ex.Message}");
|
||||
}
|
||||
EnablePlugin(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
public void DisableAll()
|
||||
{
|
||||
for (int i = _plugins.Count - 1; i >= 0; i--)
|
||||
foreach (var plugin in _plugins)
|
||||
{
|
||||
try
|
||||
{
|
||||
InvokePluginMethod(_plugins[i], "onDisable", "OnDisable");
|
||||
string pName = GetPluginString(_plugins[i], "name", "getName", "GetName", _plugins[i].GetType().Name);
|
||||
ServerLog.Info("fourkit", $"Disabled: {pName}");
|
||||
|
||||
FourKit.FireEvent(new PluginDisableEvent(_plugins[i]));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string pName = GetPluginString(_plugins[i], "name", "getName", "GetName", _plugins[i].GetType().Name);
|
||||
ServerLog.Error("fourkit", $"Error disabling {pName}: {ex.Message}");
|
||||
}
|
||||
DisablePlugin(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
public void EnablePlugin(ServerPlugin plugin)
|
||||
{
|
||||
try
|
||||
{
|
||||
InvokePluginMethod(plugin, "onEnable", "OnEnable");
|
||||
string pName = GetPluginString(plugin, "name", "getName", "GetName", plugin.GetType().Name);
|
||||
ServerLog.Info("fourkit", $"Enabled: {pName}");
|
||||
|
||||
FourKit.FireEvent(new PluginEnableEvent(plugin));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string pName = GetPluginString(plugin, "name", "getName", "GetName", plugin.GetType().Name);
|
||||
ServerLog.Error("fourkit", $"Error enabling {pName}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public void DisablePlugin(ServerPlugin plugin)
|
||||
{
|
||||
try
|
||||
{
|
||||
InvokePluginMethod(plugin, "onDisable", "OnDisable");
|
||||
string pName = GetPluginString(plugin, "name", "getName", "GetName", plugin.GetType().Name);
|
||||
ServerLog.Info("fourkit", $"Disabled: {pName}");
|
||||
|
||||
FourKit.FireEvent(new PluginDisableEvent(plugin));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string pName = GetPluginString(plugin, "name", "getName", "GetName", plugin.GetType().Name);
|
||||
ServerLog.Error("fourkit", $"Error disabling {pName}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static void InvokePluginMethod(ServerPlugin plugin, string camelName, string pascalName)
|
||||
{
|
||||
Type type = plugin.GetType();
|
||||
|
|
|
|||
Loading…
Reference in a new issue