Add PlayerPreLoginEvent (#8)

* PlayerPreLoginEvent, comments for more events

* basic plugin events

* plugin failed to load event

* add docs

---------

Co-authored-by: sylvessa <225480449+sylvessa@users.noreply.github.com>
This commit is contained in:
DrPerkyLegit 2026-03-29 13:56:24 -04:00 committed by GitHub
parent 33e0ecac56
commit da2aaf1247
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 241 additions and 4 deletions

View file

@ -0,0 +1,40 @@
namespace Minecraft.Server.FourKit.Event.Player;
using Minecraft.Server.FourKit.Net;
/// <summary>
/// Stores details for players attempting to log in.
/// </summary>
public class PlayerPreLoginEvent : Event, Cancellable
{
private string name;
private InetSocketAddress ipAddress; //bukkit uses InetAddress but we expose port also
private bool _cancelled;
internal PlayerPreLoginEvent(string name, InetSocketAddress ipAddress) : base()
{
this.name = name;
this.ipAddress = ipAddress;
}
/// <summary>
/// Gets the player's name.
/// </summary>
/// <returns>The player's name.</returns>
public string getName() => name;
/// <summary>
/// Gets the player IP address.
/// </summary>
/// <returns>The IP address.</returns>
public InetSocketAddress getAddress() => ipAddress;
/// <inheritdoc/>
public bool isCancelled() => _cancelled;
/// <inheritdoc/>
public void setCancelled(bool cancel) => _cancelled = cancel;
}

View file

@ -0,0 +1,11 @@
namespace Minecraft.Server.FourKit.Event.Server;
using Minecraft.Server.FourKit.Plugin;
public class PluginDisableEvent : PluginEvent
{
internal PluginDisableEvent(ServerPlugin plugin) : base(plugin)
{
}
}

View file

@ -0,0 +1,11 @@
namespace Minecraft.Server.FourKit.Event.Server;
using Minecraft.Server.FourKit.Plugin;
public class PluginEnableEvent : PluginEvent
{
internal PluginEnableEvent(ServerPlugin plugin) : base(plugin)
{
}
}

View file

@ -0,0 +1,16 @@
namespace Minecraft.Server.FourKit.Event.Server;
using Minecraft.Server.FourKit.Plugin;
public abstract class PluginEvent : ServerEvent
{
private readonly ServerPlugin _plugin;
internal protected PluginEvent(ServerPlugin plugin) : base()
{
_plugin = plugin;
}
/// <summary>Returns the plugin involved in this event.</summary>
public ServerPlugin getPlugin() => _plugin;
}

View file

@ -0,0 +1,18 @@
namespace Minecraft.Server.FourKit.Event.Server;
using Minecraft.Server.FourKit.Plugin;
public class PluginLoadFailedEvent : ServerEvent
{
private readonly string _fileName;
private readonly string _message;
internal PluginLoadFailedEvent(string fileName, string message) : base()
{
_fileName = fileName;
_message = message;
}
public string getFileName() => _fileName;
public string getMessage() => _message;
}

View file

@ -0,0 +1,11 @@
namespace Minecraft.Server.FourKit.Event.Server;
using Minecraft.Server.FourKit.Plugin;
public abstract class ServerEvent : Event
{
internal protected ServerEvent() : base()
{
}
}

View file

@ -7,11 +7,36 @@ using Minecraft.Server.FourKit.Event.Entity;
using Minecraft.Server.FourKit.Event.Player;
using Minecraft.Server.FourKit.Event.Inventory;
using Minecraft.Server.FourKit.Inventory;
using Minecraft.Server.FourKit.Net;
namespace Minecraft.Server.FourKit;
public static partial class FourKitHost
{
[UnmanagedCallersOnly]
public static int FirePlayerPreLogin(IntPtr namePtr, int nameByteLen, IntPtr ipPtr, int ipByteLen, int port)
{
try
{
string name = nameByteLen > 0
? Marshal.PtrToStringUTF8(namePtr, nameByteLen) ?? string.Empty
: string.Empty;
string ipStr = ipByteLen > 0
? Marshal.PtrToStringUTF8(ipPtr, ipByteLen) ?? string.Empty
: string.Empty;
var evt = new PlayerPreLoginEvent(name, new InetSocketAddress(new InetAddress(ipStr), port));
FourKit.FireEvent(evt);
return evt.isCancelled() ? 1 : 0;
}
catch (Exception ex)
{
ServerLog.Error("fourkit", $"FirePlayerJoin error: {ex}");
return 0;
}
}
[UnmanagedCallersOnly]
public static void FirePlayerJoin(int entityId, IntPtr namePtr, int nameByteLen, IntPtr uuidPtr, int uuidByteLen)
{

View file

@ -1,3 +1,4 @@
using Minecraft.Server.FourKit.Event.Server;
using Minecraft.Server.FourKit.Plugin;
using System.Reflection;
@ -44,15 +45,30 @@ internal sealed class PluginLoader
if (mainDll != null)
{
try { LoadPluginAssembly(mainDll); }
catch (Exception ex) { ServerLog.Error("fourkit", $"Failed to load {Path.GetFileName(mainDll)}: {ex.Message}"); }
try
{
LoadPluginAssembly(mainDll);
}
catch (Exception ex)
{
ServerLog.Error("fourkit", $"Failed to load {Path.GetFileName(mainDll)}: {ex.Message}");
FourKit.FireEvent(new PluginLoadFailedEvent(mainDll, ex.Message));
}
}
else
{
foreach (var dll in allDlls)
{
try { LoadPluginAssembly(dll); }
catch (Exception ex) { ServerLog.Error("fourkit", $"Failed to load {Path.GetFileName(dll)}: {ex.Message}"); }
try
{
LoadPluginAssembly(dll);
}
catch (Exception ex)
{
ServerLog.Error("fourkit", $"Failed to load {Path.GetFileName(dll)}: {ex.Message}");
FourKit.FireEvent(new PluginLoadFailedEvent(dll, ex.Message));
}
}
}
}
@ -108,6 +124,8 @@ internal sealed class PluginLoader
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)
{
@ -126,6 +144,8 @@ internal sealed class PluginLoader
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)
{