Skip to content

Commit

Permalink
Event stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides committed Dec 31, 2023
1 parent 05c4df7 commit 107c766
Show file tree
Hide file tree
Showing 39 changed files with 304 additions and 104 deletions.
6 changes: 5 additions & 1 deletion Obsidian.API/Events/BaseMinecraftEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
/// <summary>
/// Represents the base class for classes that contain minecraft event data.
/// </summary>
public class BaseMinecraftEventArgs : AsyncEventArgs
public abstract class BaseMinecraftEventArgs : AsyncEventArgs, INamedEvent
{
public static string Name => string.Empty;

/// <summary>
/// Server this event took place in.
/// </summary>
public IServer Server { get; }

public static EventType EventType { get; }

/// <summary>
/// Constructs a new instance of the <see cref="BaseMinecraftEventArgs"/> class.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/BlockBreakEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class BlockBreakEventArgs : BlockEventArgs, ICancellable
{
public static new string Name => "BlockBreak";

/// <summary>
/// Player that has broken the block.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/BlockEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public abstract class BlockEventArgs : BaseMinecraftEventArgs
{
public static new string Name => "BlockEvent";

/// <summary>
/// The impacted block.
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion Obsidian.API/Events/ContainerClickEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using Obsidian.API.BlockStates;
using System.Diagnostics.CodeAnalysis;

namespace Obsidian.API.Events;

public sealed class ContainerClickEventArgs : ContainerEventArgs, ICancellable
{
public static new string Name => "ContainerClick";

/// <summary>
/// Gets the current item that was clicked
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/ContainerClosedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Obsidian.API.Events;
public sealed class ContainerClosedEventArgs : ContainerEventArgs, ICancellable
{
public static new string Name => "ContainerClosed";

public bool IsCancelled { get; private set; }

internal ContainerClosedEventArgs(IPlayer player, IServer server) : base(player, server)
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/ContainerEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class ContainerEventArgs : PlayerEventArgs
{
public static new string Name => "ContainerEvent";

public required BaseContainer Container { get; init; }

protected ContainerEventArgs(IPlayer player, IServer server) : base(player, server)
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/EntityEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class EntityEventArgs : BaseMinecraftEventArgs, ICancellable
{
public static new string Name => "EntityEvent";

/// <summary>
/// The entity involved in this event.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/EntityInteractEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class EntityInteractEventArgs : EntityEventArgs
{
public static new string Name => "EntityInteract";

/// <summary>
/// The player who interacted with the entity.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/IncomingChatMessageEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class IncomingChatMessageEventArgs : PlayerEventArgs, ICancellable
{
public static new string Name => "IncomingChatMessage";

/// <summary>
/// The message that was sent.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/PacketReceivedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public sealed class PacketReceivedEventArgs : PlayerEventArgs, ICancellable
{
public static new string Name => "PacketReceived";

/// <summary>
/// Id of the received packet.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/PermissionGrantedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class PermissionGrantedEventArgs : PlayerEventArgs
{
public static new string Name => "PermissionGranted";

public string Permission { get; }

public PermissionGrantedEventArgs(IPlayer player, IServer server, string permission) : base(player, server)
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/PermissionRevokedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class PermissionRevokedEventArgs : PlayerEventArgs
{
public static new string Name => "PermissionRevoked";

public string Permission { get; }

public PermissionRevokedEventArgs(IPlayer player, IServer server, string permission) : base(player, server)
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/PlayerAttackEntityEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//TODO check if player crits and calculate damage
public class PlayerAttackEntityEventArgs : EntityEventArgs
{
public static new string Name => "PlayerAttackEntity";

/// <summary>
/// The player who interacted with the entity.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/PlayerEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class PlayerEventArgs : BaseMinecraftEventArgs
{
public static new string Name => "PlayerEvent";

/// <summary>
/// The player involved in this event.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/PlayerInteractEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
/// </summary>
public sealed class PlayerInteractEventArgs : PlayerEventArgs, ICancellable
{
public static new string Name => "PlayerInteract";

/// <summary>
/// The item that was being held when interacting.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/PlayerJoinEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class PlayerJoinEventArgs : PlayerEventArgs
{
public static new string Name => "PlayerJoin";

/// <summary>
/// The date the player joined.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/PlayerLeaveEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class PlayerLeaveEventArgs : PlayerEventArgs
{
public static new string Name => "PlayerLeave";

/// <summary>
/// The date the player left.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/PlayerTeleportEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class PlayerTeleportEventArgs : PlayerEventArgs
{
public static new string Name => "PlayerTeleport";

public VectorF OldPosition { get; }
public VectorF NewPosition { get; }

Expand Down
2 changes: 2 additions & 0 deletions Obsidian.API/Events/ServerStatusRequestEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class ServerStatusRequestEventArgs : BaseMinecraftEventArgs
{
public static new string Name => "ServerStatusRequest";

public IServerStatus Status { get; }

internal ServerStatusRequestEventArgs(IServer server, IServerStatus status) : base(server)
Expand Down
21 changes: 21 additions & 0 deletions Obsidian.API/_Enums/EventType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Obsidian.API;

public enum EventType
{
PacketReceived,
QueuePacket,
PlayerJoin,
PlayerLeave,
PlayerTeleported,
PermissionGranted,
PermissionRevoked,
ContainerClick,
BlockBreak,
IncomingChatMessage,
ServerStatusRequest,
EntityInteract,
PlayerAttackEntity,
PlayerInteract,
ContainerClosed,
Custom
}
5 changes: 5 additions & 0 deletions Obsidian.API/_Interfaces/INamedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Obsidian.API;
public interface INamedEvent
{
public static abstract string Name { get; }
}
14 changes: 7 additions & 7 deletions Obsidian/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public async Task StartConnectionAsync()

var configurationPacketReceived = new PacketReceivedEventArgs(Player, this.server, id, data);

await this.server.Events.PacketReceived.InvokeAsync(configurationPacketReceived);
await this.server.packetReceived.InvokeAsync(configurationPacketReceived);

if (!configurationPacketReceived.IsCancelled)
await this.handler.HandleConfigurationPackets(id, data, this);
Expand All @@ -328,7 +328,7 @@ public async Task StartConnectionAsync()

var playPacketReceived = new PacketReceivedEventArgs(Player, this.server, id, data);

await this.server.Events.PacketReceived.InvokeAsync(playPacketReceived);
await this.server.packetReceived.InvokeAsync(playPacketReceived);

if (!playPacketReceived.IsCancelled)
await handler.HandlePlayPackets(id, data, this);
Expand All @@ -345,7 +345,7 @@ public async Task StartConnectionAsync()
if (State == ClientState.Play)
{
Debug.Assert(Player is not null);
await this.server.Events.PlayerLeave.InvokeAsync(new PlayerLeaveEventArgs(Player, this.server, DateTimeOffset.Now));
await this.server.playerLeave.InvokeAsync(new PlayerLeaveEventArgs(Player, this.server, DateTimeOffset.Now));
}

Disconnected?.Invoke(this);
Expand All @@ -365,7 +365,7 @@ private async Task HandleServerStatusRequestAsync()
{
var status = new ServerStatus(this.server);

_ = await this.server.Events.ServerStatusRequest.InvokeAsync(new ServerStatusRequestEventArgs(this.server, status));
_ = await this.server.serverStatusRequest.InvokeAsync(new ServerStatusRequestEventArgs(this.server, status));

SendPacket(new RequestResponse(status));
}
Expand Down Expand Up @@ -569,7 +569,7 @@ await QueuePacketAsync(new SynchronizePlayerPositionPacket

await Player.UpdateChunksAsync(distance: 7);
await SendInfoAsync();
await this.server.Events.PlayerJoin.InvokeAsync(new PlayerJoinEventArgs(Player, this.server, DateTimeOffset.Now));
await this.server.playerJoin.InvokeAsync(new PlayerJoinEventArgs(Player, this.server, DateTimeOffset.Now));
}

#region Packet sending
Expand Down Expand Up @@ -741,10 +741,10 @@ internal void SendPacket(IClientboundPacket packet)

internal async Task QueuePacketAsync(IClientboundPacket packet)
{
var args = await this.server.Events.QueuePacket.InvokeAsync(new QueuePacketEventArgs(this, packet));
var args = await this.server.queuePacket.InvokeAsync(new QueuePacketEventArgs(this.server, this, packet));
if (args.IsCancelled)
{
Logger.LogDebug("Packet {PacketId} was sent to the queue, however an event handler registered in {Name} has cancelled it.", args.Packet.Id, nameof(Server.Events));
Logger.LogDebug("Packet {PacketId} was sent to the queue, however an event handler has cancelled it.", args.Packet.Id);
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions Obsidian/Entities/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public async override Task TeleportAsync(VectorF pos)

var tid = Globals.Random.Next(0, 999);

await client.server.Events.PlayerTeleported.InvokeAsync(
await client.server.playerTeleported.InvokeAsync(
new PlayerTeleportEventArgs
(
this,
Expand Down Expand Up @@ -786,7 +786,7 @@ public async Task<bool> GrantPermissionAsync(string permissionNode)
await SavePermsAsync();

if (result)
await this.client.server.Events.PermissionGranted.InvokeAsync(new PermissionGrantedEventArgs(this, this.client.server, permissionNode));
await this.client.server.permissionGranted.InvokeAsync(new PermissionGrantedEventArgs(this, this.client.server, permissionNode));

return result;
}
Expand All @@ -808,7 +808,7 @@ public async Task<bool> RevokePermissionAsync(string permissionNode)
parent.Children.Remove(childToRemove);

await this.SavePermsAsync();
await this.client.server.Events.PermissionRevoked.InvokeAsync(new PermissionRevokedEventArgs(this, this.client.server, permissionNode));
await this.client.server.permissionRevoked.InvokeAsync(new PermissionRevokedEventArgs(this, this.client.server, permissionNode));

return true;
}
Expand Down
16 changes: 12 additions & 4 deletions Obsidian/Events/AsyncEvent.T.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
using System.Reflection;
using Obsidian.API.Events;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;

namespace Obsidian.Events;

public sealed class AsyncEvent<T> : IEventRegistry
public sealed class AsyncEvent<T> where T : BaseMinecraftEventArgs, INamedEvent
{
public string? Name { get; } // Name must be set in order to be visible to plugins
public string Name { get; } // Name must be set in order to be visible to plugins

private readonly SemaphoreSlim semaphore = new(1);
private readonly List<Hook<T>> hooks = [];
private readonly Action<AsyncEvent<T>, Exception>? exceptionHandler;

public AsyncEvent()
{
this.Name = T.Name;
}

public AsyncEvent(string? name, Action<AsyncEvent<T>, Exception>? exceptionHandler)
public AsyncEvent(Action<AsyncEvent<T>, Exception>? exceptionHandler)
{
Name = T.Name;
this.exceptionHandler = exceptionHandler;
}

public AsyncEvent(string name, Action<AsyncEvent<T>, Exception>? exceptionHandler)
{
Name = name;
this.exceptionHandler = exceptionHandler;
Expand Down
4 changes: 2 additions & 2 deletions Obsidian/Events/EventArgs/BasePacketEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Obsidian.Events.EventArgs;

public class BasePacketEventArgs : AsyncEventArgs
public class BasePacketEventArgs : BaseMinecraftEventArgs
{
/// <summary>
/// The client that invoked the event.
Expand All @@ -15,7 +15,7 @@ public class BasePacketEventArgs : AsyncEventArgs
/// </summary>
public IPacket Packet { get; private set; }

internal BasePacketEventArgs(Client client, IPacket packet)
internal BasePacketEventArgs(Server server, Client client, IPacket packet) : base(server)
{
Client = client;
Packet = packet;
Expand Down
4 changes: 3 additions & 1 deletion Obsidian/Events/EventArgs/QueuePacketEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ namespace Obsidian.Events.EventArgs;

public class QueuePacketEventArgs : BasePacketEventArgs, ICancellable
{
public static new string Name => "QueuePacket";

/// <inheritdoc />
public bool IsCancelled { get; private set; }

internal QueuePacketEventArgs(Client client, IPacket packet) : base(client, packet)
internal QueuePacketEventArgs(Server server, Client client, IPacket packet) : base(server, client, packet)
{
}

Expand Down
2 changes: 1 addition & 1 deletion Obsidian/Events/IEventRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Obsidian.Events;

public interface IEventRegistry
{
public string? Name { get; }
public string Name { get; }

public bool TryRegisterEvent(MethodInfo method, object? instance, out Delegate? @delegate);
public bool UnregisterEvent(Delegate @delegate);
Expand Down
8 changes: 5 additions & 3 deletions Obsidian/Events/MinecraftEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ internal readonly struct MinecraftEvent
{
public required Type EventType { get; init; }

public required Type ModuleType { get; init; }
public Type? ModuleType { get; init; }

public required PluginContainer PluginContainer { get; init; }

public required Priority Priority { get; init; }

public required ObjectFactory ModuleFactory { get; init; }
public ObjectFactory? ModuleFactory { get; init; }

public required ObjectMethodExecutor MethodExecutor { get; init; }
public ObjectMethodExecutor? MethodExecutor { get; init; }

public Delegate? MethodDelegate { get; init; }
}
Loading

0 comments on commit 107c766

Please sign in to comment.