Files
CommandHook/CommandHook/CommandData.cs
T

61 lines
2.1 KiB
C#

using Vintagestory.API.Server;
namespace CommandHook;
/// <summary>
/// The data passed to Before/After listeners for a single command invocation.
/// Passed by ref through the hot path, no allocation per command.
/// </summary>
public struct CommandData
{
/// <summary>
/// The player who ran the command, or null if it came from the server console.
/// </summary>
public readonly IServerPlayer? Sender;
/// <summary>
/// The full command text as typed, including the leading slash.
/// </summary>
public readonly string FullCommand;
/// <summary>
/// The command name only, without the leading slash (matches what listeners
/// register in <see cref="ICommandHookListener.Commands"/>).
/// </summary>
public readonly string CommandName;
private byte flags;
private const byte FlagIsPlayerCommand = 1 << 0;
private const byte FlagCancel = 1 << 1;
/// <summary>
/// True if a player ran this command, false if it came from the server console.
/// Equivalent to <c>Sender != null</c>.
/// </summary>
public bool IsPlayerCommand => (flags & FlagIsPlayerCommand) != 0;
/// <summary>
/// Set this to true in a Before listener to stop the command from executing.
/// Stops any remaining Before listeners from running too, and skips After entirely.
/// </summary>
public bool Cancel
{
get => (flags & FlagCancel) != 0;
set => flags = value ? (byte)(flags | FlagCancel) : (byte)(flags & ~FlagCancel);
}
/// <summary>
/// Creates the command data for a single invocation.
/// </summary>
/// <param name="sender">The player who ran the command, or null for console.</param>
/// <param name="commandName">The command name without the leading slash.</param>
/// <param name="fullCommand">The full command text as typed, including the leading slash.</param>
public CommandData(IServerPlayer? sender, string commandName, string fullCommand)
{
Sender = sender;
CommandName = commandName;
FullCommand = fullCommand;
flags = sender != null ? FlagIsPlayerCommand : (byte)0;
}
}