Skip to content

Commit

Permalink
Prevent cancelled events from being un-cancelled (#343)
Browse files Browse the repository at this point in the history
* Modify IsCancellable and AsyncEventArgs so they cannot be undone. Update consumers of ICancellable.

* Missed a consumer

* Missed a few more
  • Loading branch information
Foxtrek64 committed May 9, 2023
1 parent 1e1ae0e commit 29ed323
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 19 deletions.
12 changes: 10 additions & 2 deletions Obsidian.API/Events/AsyncEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
public abstract class AsyncEventArgs : EventArgs
{
/// <summary>
/// Gets or sets whether the event was completely handled. Setting this to true will prevent remaining handlers from running.
/// Gets a value indicating whether the event was completely handled.
/// </summary>
public bool Handled { get; set; }
public bool Handled { get; private set; }

/// <summary>
/// Marks an event as handled. Setting this flag will prevent remaining handlers from running.
/// </summary>
public void SetHandled()
{
Handled = true;
}
}
8 changes: 7 additions & 1 deletion Obsidian.API/Events/BlockBreakEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ public class BlockBreakEventArgs : BlockEventArgs, ICancellable
public IPlayer Player { get; }

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

internal BlockBreakEventArgs(IServer server, IPlayer player, IBlock block, Vector location) : base(server, block, location)
{
Player = player;
}

/// <inheritdoc />
public void Cancel()
{
IsCancelled = true;
}
}
9 changes: 8 additions & 1 deletion Obsidian.API/Events/ContainerClickEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ public class ContainerClickEventArgs : PlayerEventArgs, ICancellable
/// </summary>
public int Slot { get; set; }

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

internal ContainerClickEventArgs(IPlayer player, BaseContainer container, ItemStack item) : base(player)
{
this.Container = container;
this.Item = item;
}

/// <inheritdoc />
public void Cancel()
{
IsCancelled = true;
}
}
9 changes: 8 additions & 1 deletion Obsidian.API/Events/EntityEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ public class EntityEventArgs : BaseMinecraftEventArgs, ICancellable
/// </summary>
public IEntity Entity { get; }

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

public EntityEventArgs(IEntity entity, IServer server) : base(server)
{
this.Entity = entity;
}

/// <inheritdoc />
public void Cancel()
{
IsCancelled = true;
}
}
10 changes: 8 additions & 2 deletions Obsidian.API/Events/ICancellable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
public interface ICancellable
{
/// <summary>
/// Gets or sets a value indicating whether the default behaviour should be cancelled.
/// Gets a value indicating whether the default behaviour should be cancelled.
/// </summary>
public bool Cancel { get; set; }
public bool IsCancelled { get; }

/// <summary>
/// Sets a flag which prevents default (vanilla) behavior from executing.
/// </summary>
/// <remarks>This flag only affects vanilla behavior. If you wish to prevent other handlers from running, see <see cref="AsyncEventArgs.IsHandled"/>.</remarks>
public void Cancel();
}
15 changes: 14 additions & 1 deletion Obsidian.API/Events/IncomingChatMessageEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@ public class IncomingChatMessageEventArgs : PlayerEventArgs, ICancellable
/// </summary>
public string Format { get; set; }

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

/// <summary>
/// Initializes a new instance of the <see cref="IncomingChatMessageEventArgs"/> class.
/// </summary>
/// <param name="player">The player which sent the message.</param>
/// <param name="message">The message which was sent.</param>
/// <param name="format">Any formatting appied to the message.</param>
public IncomingChatMessageEventArgs(IPlayer player, string message, string format) : base(player)
{
this.Message = message;
this.Format = format;
}

/// <inheritdoc />
public void Cancel()
{
IsCancelled = true;
}
}
13 changes: 10 additions & 3 deletions Obsidian.API/Events/PacketReceivedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ public sealed class PacketReceivedEventArgs : PlayerEventArgs, ICancellable
/// </summary>
public ReadOnlyMemory<byte> Data { get; }

/// <summary>
/// <inheritdoc />
/// <remarks>
/// If true, packet will be ignored by the server.
/// </summary>
public bool Cancel { get; set; }
/// </remarks>
public bool IsCancelled { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="PacketReceivedEventArgs"/> class
Expand All @@ -29,4 +30,10 @@ public PacketReceivedEventArgs(IPlayer player, int id, ReadOnlyMemory<byte> data
Id = id;
Data = data;
}

/// <inheritdoc />
public void Cancel()
{
IsCancelled = true;
}
}
9 changes: 8 additions & 1 deletion Obsidian.API/Events/PlayerInteractEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ public sealed class PlayerInteractEventArgs : PlayerEventArgs, ICancellable
/// </summary>
public Vector? BlockLocation { get; init; }

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

public PlayerInteractEventArgs(IPlayer player) : base(player) { }

/// <inheritdoc />
public void Cancel()
{
IsCancelled = true;
}
}
4 changes: 2 additions & 2 deletions Obsidian/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public async Task StartConnectionAsync()
var packetReceivedEventArgs = new PacketReceivedEventArgs(Player, id, data);
await Server.Events.InvokePacketReceivedAsync(packetReceivedEventArgs);

if (!packetReceivedEventArgs.Cancel)
if (!packetReceivedEventArgs.IsCancelled)
{
await handler.HandlePlayPackets(id, data, this);
}
Expand Down Expand Up @@ -727,7 +727,7 @@ internal void SendPacket(IClientboundPacket packet)
internal async Task QueuePacketAsync(IClientboundPacket packet)
{
var args = await Server.Events.InvokeQueuePacketAsync(new QueuePacketEventArgs(this, packet));
if (args.Cancel)
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));
}
Expand Down
9 changes: 8 additions & 1 deletion Obsidian/Events/EventArgs/QueuePacketEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ namespace Obsidian.Events.EventArgs;

public class QueuePacketEventArgs : BasePacketEventArgs, ICancellable
{
public bool Cancel { get; set; }
/// <inheritdoc />
public bool IsCancelled { get; private set; }

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

/// <inheritdoc />
public void Cancel()
{
IsCancelled = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private async Task HandleMouseClick(BaseContainer container, Server server, Play
Slot = slot
});

if (@event.Cancel)
if (@event.IsCancelled)
return;

player.LastClickedItem = CarriedItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async ValueTask HandleAsync(Server server, Player player)
await player.World.SetBlockUntrackedAsync(Position, BlocksRegistry.Air, true);

var blockBreakEvent = await server.Events.InvokeBlockBreakAsync(new BlockBreakEventArgs(server, player, block, Position));
if (blockBreakEvent.Cancel)
if (blockBreakEvent.IsCancelled)
return;
}

Expand Down
2 changes: 1 addition & 1 deletion Obsidian/Server.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private async Task OnPlayerInteract(PlayerInteractEventArgs e)
var server = e.Server as Server;
var player = e.Player as Player;

if (e.Cancel)
if (e.IsCancelled)
return;

if (block is not null)
Expand Down
2 changes: 1 addition & 1 deletion Obsidian/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ internal async Task HandleIncomingMessageAsync(ChatMessagePacket packet, Client
var message = packet.Message;

var chat = await Events.InvokeIncomingChatMessageAsync(new IncomingChatMessageEventArgs(source.Player, message, format));
if (chat.Cancel)
if (chat.IsCancelled)
return;

//TODO add bool for sending secure chat messages
Expand Down

0 comments on commit 29ed323

Please sign in to comment.