From fd69ae3a5755e6cf45bb1021d72626e4c4056084 Mon Sep 17 00:00:00 2001
From: Misfiy <85962933+Misfiy@users.noreply.github.com>
Date: Fri, 11 Jul 2025 16:50:36 +0200
Subject: [PATCH 01/24] meh
---
SecretAPI/Features/Commands/CommandResult.cs | 18 +++++++++++++
SecretAPI/Features/Commands/CustomCommand.cs | 26 +++++++++++++++++++
.../Features/Commands/CustomCommandHandler.cs | 21 +++++++++++++++
3 files changed, 65 insertions(+)
create mode 100644 SecretAPI/Features/Commands/CommandResult.cs
create mode 100644 SecretAPI/Features/Commands/CustomCommand.cs
create mode 100644 SecretAPI/Features/Commands/CustomCommandHandler.cs
diff --git a/SecretAPI/Features/Commands/CommandResult.cs b/SecretAPI/Features/Commands/CommandResult.cs
new file mode 100644
index 0000000..e010621
--- /dev/null
+++ b/SecretAPI/Features/Commands/CommandResult.cs
@@ -0,0 +1,18 @@
+namespace SecretAPI.Features.Commands
+{
+ ///
+ /// Gets the result of .
+ ///
+ public struct CommandResult
+ {
+ ///
+ /// Gets a value indicating whether parsing was successful.
+ ///
+ public bool CouldParse;
+
+ ///
+ /// If parsing failed, will provide the fail reason, otherwise null.
+ ///
+ public string? FailedResponse;
+ }
+}
\ No newline at end of file
diff --git a/SecretAPI/Features/Commands/CustomCommand.cs b/SecretAPI/Features/Commands/CustomCommand.cs
new file mode 100644
index 0000000..caf2f7c
--- /dev/null
+++ b/SecretAPI/Features/Commands/CustomCommand.cs
@@ -0,0 +1,26 @@
+namespace SecretAPI.Features.Commands
+{
+ using System;
+ using CommandSystem;
+
+ ///
+ /// Defines the base of a custom .
+ ///
+ public abstract class CustomCommand : ICommand
+ {
+ ///
+ public abstract string Command { get; }
+
+ ///
+ public abstract string[] Aliases { get; }
+
+ ///
+ public abstract string Description { get; }
+
+ ///
+ public bool Execute(ArraySegment arguments, ICommandSender sender, out string response)
+ {
+ CommandResult result = CustomCommandHandler.TryCall(sender, arguments);
+ }
+ }
+}
\ No newline at end of file
diff --git a/SecretAPI/Features/Commands/CustomCommandHandler.cs b/SecretAPI/Features/Commands/CustomCommandHandler.cs
new file mode 100644
index 0000000..b198f82
--- /dev/null
+++ b/SecretAPI/Features/Commands/CustomCommandHandler.cs
@@ -0,0 +1,21 @@
+namespace SecretAPI.Features.Commands
+{
+ using System;
+ using CommandSystem;
+
+ ///
+ /// Handles parsing .
+ ///
+ public static class CustomCommandHandler
+ {
+ ///
+ /// Attempts to pass a command message and gives a result.
+ ///
+ /// The sender of the command.
+ /// The arguments provided to the command.
+ /// The .
+ public static CommandResult TryCall(ICommandSender sender, ArraySegment arguments)
+ {
+ }
+ }
+}
\ No newline at end of file
From 3021a014cf4ff5dcb7297fa36eb0e057b7be8a7f Mon Sep 17 00:00:00 2001
From: Misfiy <85962933+Misfiy@users.noreply.github.com>
Date: Fri, 11 Jul 2025 17:04:24 +0200
Subject: [PATCH 02/24] idk!!!!
---
...CommandResult.cs => CommandParseResult.cs} | 6 ++---
SecretAPI/Features/Commands/CustomCommand.cs | 4 +--
.../Features/Commands/CustomCommandHandler.cs | 27 ++++++++++++++++---
3 files changed, 28 insertions(+), 9 deletions(-)
rename SecretAPI/Features/Commands/{CommandResult.cs => CommandParseResult.cs} (70%)
diff --git a/SecretAPI/Features/Commands/CommandResult.cs b/SecretAPI/Features/Commands/CommandParseResult.cs
similarity index 70%
rename from SecretAPI/Features/Commands/CommandResult.cs
rename to SecretAPI/Features/Commands/CommandParseResult.cs
index e010621..6e23b3c 100644
--- a/SecretAPI/Features/Commands/CommandResult.cs
+++ b/SecretAPI/Features/Commands/CommandParseResult.cs
@@ -1,9 +1,9 @@
namespace SecretAPI.Features.Commands
{
///
- /// Gets the result of .
+ /// Gets the result of a .
///
- public struct CommandResult
+ public struct CommandParseResult
{
///
/// Gets a value indicating whether parsing was successful.
@@ -13,6 +13,6 @@ public struct CommandResult
///
/// If parsing failed, will provide the fail reason, otherwise null.
///
- public string? FailedResponse;
+ public string FailedResponse;
}
}
\ No newline at end of file
diff --git a/SecretAPI/Features/Commands/CustomCommand.cs b/SecretAPI/Features/Commands/CustomCommand.cs
index caf2f7c..e3cdf61 100644
--- a/SecretAPI/Features/Commands/CustomCommand.cs
+++ b/SecretAPI/Features/Commands/CustomCommand.cs
@@ -19,8 +19,6 @@ public abstract class CustomCommand : ICommand
///
public bool Execute(ArraySegment arguments, ICommandSender sender, out string response)
- {
- CommandResult result = CustomCommandHandler.TryCall(sender, arguments);
- }
+ => CustomCommandHandler.TryCall(this, sender, arguments, out response);
}
}
\ No newline at end of file
diff --git a/SecretAPI/Features/Commands/CustomCommandHandler.cs b/SecretAPI/Features/Commands/CustomCommandHandler.cs
index b198f82..096f90e 100644
--- a/SecretAPI/Features/Commands/CustomCommandHandler.cs
+++ b/SecretAPI/Features/Commands/CustomCommandHandler.cs
@@ -9,13 +9,34 @@
public static class CustomCommandHandler
{
///
- /// Attempts to pass a command message and gives a result.
+ /// Attempts to call the correct command and gives a result.
///
+ /// The command currently being called from.
/// The sender of the command.
/// The arguments provided to the command.
- /// The .
- public static CommandResult TryCall(ICommandSender sender, ArraySegment arguments)
+ /// The response to give to the player.
+ /// Whether the command was a success.
+ public static bool TryCall(CustomCommand command, ICommandSender sender, ArraySegment arguments, out string response)
{
+ CommandParseResult parseResult = TryParse(command, arguments);
+ if (!parseResult.CouldParse)
+ {
+ response = parseResult.FailedResponse;
+ return false;
+ }
+ }
+
+ public static CommandParseResult TryParse(CustomCommand command, ArraySegment arguments)
+ {
+ // IDK!!!
+ if (arguments.Count < 1)
+ {
+ return new CommandParseResult()
+ {
+ CouldParse = false,
+ FailedResponse = "Could not parse.",
+ };
+ }
}
}
}
\ No newline at end of file
From aec1d0b36dbc14b9e3beedf793c512f127b86273 Mon Sep 17 00:00:00 2001
From: Misfiy <85962933+Misfiy@users.noreply.github.com>
Date: Sun, 13 Jul 2025 16:18:06 +0200
Subject: [PATCH 03/24] i hate this ???
---
SecretAPI/Features/Commands/CustomCommand.cs | 5 ++
.../Features/Commands/CustomCommandHandler.cs | 81 +++++++++++++++++++
2 files changed, 86 insertions(+)
diff --git a/SecretAPI/Features/Commands/CustomCommand.cs b/SecretAPI/Features/Commands/CustomCommand.cs
index e3cdf61..097d94d 100644
--- a/SecretAPI/Features/Commands/CustomCommand.cs
+++ b/SecretAPI/Features/Commands/CustomCommand.cs
@@ -17,6 +17,11 @@ public abstract class CustomCommand : ICommand
///
public abstract string Description { get; }
+ ///
+ /// Gets an array of the sub commands for this command.
+ ///
+ public CustomCommand[] SubCommands { get; } = [];
+
///
public bool Execute(ArraySegment arguments, ICommandSender sender, out string response)
=> CustomCommandHandler.TryCall(this, sender, arguments, out response);
diff --git a/SecretAPI/Features/Commands/CustomCommandHandler.cs b/SecretAPI/Features/Commands/CustomCommandHandler.cs
index 096f90e..a66c26f 100644
--- a/SecretAPI/Features/Commands/CustomCommandHandler.cs
+++ b/SecretAPI/Features/Commands/CustomCommandHandler.cs
@@ -1,6 +1,9 @@
namespace SecretAPI.Features.Commands
{
using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Reflection;
using CommandSystem;
///
@@ -28,6 +31,8 @@ public static bool TryCall(CustomCommand command, ICommandSender sender, ArraySe
public static CommandParseResult TryParse(CustomCommand command, ArraySegment arguments)
{
+ const BindingFlags methodFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
+
// IDK!!!
if (arguments.Count < 1)
{
@@ -37,6 +42,82 @@ public static CommandParseResult TryParse(CustomCommand command, ArraySegment methods = command.GetType().GetMethods(methodFlags).Where(IsValidExecuteMethod);
+ foreach (MethodInfo method in methods)
+ {
+ CommandParseResult result = ValidateAllMethodParameters(method, arguments);
+ if (result.CouldParse)
+ {
+ return new CommandParseResult()
+ {
+ CouldParse = true,
+ FailedResponse = string.Empty,
+ };
+ }
+ }
+ }
+
+ private static CommandParseResult ValidateAllMethodParameters(MethodInfo method, ArraySegment arguments)
+ {
+ for (int index = 0; index < method.GetParameters().Length; index++)
+ {
+ ParameterInfo parameter = method.GetParameters()[index];
+ CommandParseResult result = ValidateParameter(parameter, arguments.ElementAtOrDefault(index));
+ if (!result.CouldParse)
+ return result;
+ }
+ }
+
+ private static CommandParseResult ValidateParameter(ParameterInfo parameter, string? argument)
+ {
+ // if arg doesnt exist & param is optional, then its validated
+ if (argument == null && parameter.IsOptional)
+ {
+ return new CommandParseResult()
+ {
+ CouldParse = true,
+ };
+ }
+
+ try
+ {
+ Type type = parameter.ParameterType;
+
+ if (type.IsEnum)
+ {
+ if (Enum.TryParse(type, argument, true, out object? enumValue))
+ {
+ }
+
+ return false;
+ }
+
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+ private static bool IsValidExecuteMethod(MethodInfo method)
+ {
+ // isnt an Execute command
+ if (method.Name != "Execute")
+ return false;
+
+ ParameterInfo[] parameters = method.GetParameters();
+
+ // params isnt 3, so its not default
+ if (parameters.Length != 3)
+ return true;
+
+ // make sure params arent the types of the original default to prevent infinite loop
+ return !(parameters[0].ParameterType == typeof(ArraySegment)
+ && parameters[1].ParameterType == typeof(ICommandSender)
+ && parameters[2].IsOut
+ && parameters[2].ParameterType == typeof(string));
}
}
}
\ No newline at end of file
From e6b27fa970c07776cc4b2f1bb6f5b351a069a285 Mon Sep 17 00:00:00 2001
From: Misfiy <85962933+Misfiy@users.noreply.github.com>
Date: Sun, 13 Jul 2025 16:26:39 +0200
Subject: [PATCH 04/24] properenum validation
---
.../Features/Commands/CommandParseResult.cs | 5 +++
.../Features/Commands/CustomCommandHandler.cs | 45 +++++++++----------
2 files changed, 27 insertions(+), 23 deletions(-)
diff --git a/SecretAPI/Features/Commands/CommandParseResult.cs b/SecretAPI/Features/Commands/CommandParseResult.cs
index 6e23b3c..65fe7a0 100644
--- a/SecretAPI/Features/Commands/CommandParseResult.cs
+++ b/SecretAPI/Features/Commands/CommandParseResult.cs
@@ -14,5 +14,10 @@ public struct CommandParseResult
/// If parsing failed, will provide the fail reason, otherwise null.
///
public string FailedResponse;
+
+ ///
+ /// The argument for the argument.
+ ///
+ public object? ParamArgument;
}
}
\ No newline at end of file
diff --git a/SecretAPI/Features/Commands/CustomCommandHandler.cs b/SecretAPI/Features/Commands/CustomCommandHandler.cs
index a66c26f..85b3134 100644
--- a/SecretAPI/Features/Commands/CustomCommandHandler.cs
+++ b/SecretAPI/Features/Commands/CustomCommandHandler.cs
@@ -29,20 +29,16 @@ public static bool TryCall(CustomCommand command, ICommandSender sender, ArraySe
}
}
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
public static CommandParseResult TryParse(CustomCommand command, ArraySegment arguments)
{
const BindingFlags methodFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
- // IDK!!!
- if (arguments.Count < 1)
- {
- return new CommandParseResult()
- {
- CouldParse = false,
- FailedResponse = "Could not parse.",
- };
- }
-
IEnumerable methods = command.GetType().GetMethods(methodFlags).Where(IsValidExecuteMethod);
foreach (MethodInfo method in methods)
{
@@ -77,28 +73,31 @@ private static CommandParseResult ValidateParameter(ParameterInfo parameter, str
return new CommandParseResult()
{
CouldParse = true,
+ ParamArgument = parameter.DefaultValue,
};
}
- try
- {
- Type type = parameter.ParameterType;
+ Type type = parameter.ParameterType;
- if (type.IsEnum)
+ if (type.IsEnum)
+ {
+ if (Enum.TryParse(type, argument, true, out object? enumValue))
{
- if (Enum.TryParse(type, argument, true, out object? enumValue))
+ return new CommandParseResult()
{
- }
-
- return false;
+ CouldParse = true,
+ ParamArgument = enumValue,
+ };
}
- return true;
- }
- catch
- {
- return false;
+ return new CommandParseResult()
+ {
+ CouldParse = false,
+ FailedResponse = $"Could not pass into valid enum value. Enum required: {type.Name}.",
+ };
}
+
+ return true;
}
private static bool IsValidExecuteMethod(MethodInfo method)
From 9516c89845c16d69e0a700437884263fbffde00d Mon Sep 17 00:00:00 2001
From: Misfiy <85962933+Misfiy@users.noreply.github.com>
Date: Sun, 13 Jul 2025 16:54:47 +0200
Subject: [PATCH 05/24] stoopid
---
.../Features/Commands/CommandParseResult.cs | 2 +-
SecretAPI/Features/Commands/CommandResult.cs | 30 +++++++++++++++++++
.../Features/Commands/CustomCommandHandler.cs | 11 +++++--
3 files changed, 39 insertions(+), 4 deletions(-)
create mode 100644 SecretAPI/Features/Commands/CommandResult.cs
diff --git a/SecretAPI/Features/Commands/CommandParseResult.cs b/SecretAPI/Features/Commands/CommandParseResult.cs
index 65fe7a0..a243616 100644
--- a/SecretAPI/Features/Commands/CommandParseResult.cs
+++ b/SecretAPI/Features/Commands/CommandParseResult.cs
@@ -3,7 +3,7 @@
///
/// Gets the result of a .
///
- public struct CommandParseResult
+ internal struct CommandParseResult
{
///
/// Gets a value indicating whether parsing was successful.
diff --git a/SecretAPI/Features/Commands/CommandResult.cs b/SecretAPI/Features/Commands/CommandResult.cs
new file mode 100644
index 0000000..62ff5ce
--- /dev/null
+++ b/SecretAPI/Features/Commands/CommandResult.cs
@@ -0,0 +1,30 @@
+namespace SecretAPI.Features.Commands
+{
+ using System.Reflection;
+
+ ///
+ /// The result of to know what to do.
+ ///
+ internal struct CommandResult
+ {
+ ///
+ /// Gets a value indicating whether parsing was successful.
+ ///
+ public bool CouldParse;
+
+ ///
+ /// If parsing failed, will provide the fail reason, otherwise null.
+ ///
+ public string FailedResponse;
+
+ ///
+ /// If parsing succeded, the method to call with .
+ ///
+ public MethodInfo Method;
+
+ ///
+ /// If parsing succeeded, the arguments provided.
+ ///
+ public object[]? ProvidedArguments;
+ }
+}
\ No newline at end of file
diff --git a/SecretAPI/Features/Commands/CustomCommandHandler.cs b/SecretAPI/Features/Commands/CustomCommandHandler.cs
index 85b3134..f4abfa1 100644
--- a/SecretAPI/Features/Commands/CustomCommandHandler.cs
+++ b/SecretAPI/Features/Commands/CustomCommandHandler.cs
@@ -21,12 +21,17 @@ public static class CustomCommandHandler
/// Whether the command was a success.
public static bool TryCall(CustomCommand command, ICommandSender sender, ArraySegment arguments, out string response)
{
- CommandParseResult parseResult = TryParse(command, arguments);
+ CommandResult parseResult = TryParse(command, arguments);
if (!parseResult.CouldParse)
{
response = parseResult.FailedResponse;
return false;
}
+
+ parseResult.Method.Invoke(null, parseResult.ProvidedArguments);
+
+ // TODO: get result & put it into response
+ return true;
}
///
@@ -35,7 +40,7 @@ public static bool TryCall(CustomCommand command, ICommandSender sender, ArraySe
///
///
///
- public static CommandParseResult TryParse(CustomCommand command, ArraySegment arguments)
+ public static CommandResult TryParse(CustomCommand command, ArraySegment arguments)
{
const BindingFlags methodFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
@@ -45,7 +50,7 @@ public static CommandParseResult TryParse(CustomCommand command, ArraySegment
Date: Wed, 6 Aug 2025 18:30:52 +0200
Subject: [PATCH 06/24] ok slight more
---
.../Attribute/ExecuteCommandAttribute.cs | 12 +++++++
SecretAPI/Features/Commands/CustomCommand.cs | 2 +-
.../Features/Commands/CustomCommandHandler.cs | 32 ++++++++-----------
3 files changed, 26 insertions(+), 20 deletions(-)
create mode 100644 SecretAPI/Attribute/ExecuteCommandAttribute.cs
diff --git a/SecretAPI/Attribute/ExecuteCommandAttribute.cs b/SecretAPI/Attribute/ExecuteCommandAttribute.cs
new file mode 100644
index 0000000..213607f
--- /dev/null
+++ b/SecretAPI/Attribute/ExecuteCommandAttribute.cs
@@ -0,0 +1,12 @@
+namespace SecretAPI.Attribute
+{
+ using System;
+
+ ///
+ /// Attribute used to identify a method as a possible execution result.
+ ///
+ [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
+ public class ExecuteCommandAttribute : Attribute
+ {
+ }
+}
\ No newline at end of file
diff --git a/SecretAPI/Features/Commands/CustomCommand.cs b/SecretAPI/Features/Commands/CustomCommand.cs
index 097d94d..05d6485 100644
--- a/SecretAPI/Features/Commands/CustomCommand.cs
+++ b/SecretAPI/Features/Commands/CustomCommand.cs
@@ -20,7 +20,7 @@ public abstract class CustomCommand : ICommand
///
/// Gets an array of the sub commands for this command.
///
- public CustomCommand[] SubCommands { get; } = [];
+ public virtual CustomCommand[] SubCommands { get; } = [];
///
public bool Execute(ArraySegment arguments, ICommandSender sender, out string response)
diff --git a/SecretAPI/Features/Commands/CustomCommandHandler.cs b/SecretAPI/Features/Commands/CustomCommandHandler.cs
index f4abfa1..85a26e7 100644
--- a/SecretAPI/Features/Commands/CustomCommandHandler.cs
+++ b/SecretAPI/Features/Commands/CustomCommandHandler.cs
@@ -5,12 +5,15 @@
using System.Linq;
using System.Reflection;
using CommandSystem;
+ using SecretAPI.Attribute;
///
/// Handles parsing .
///
public static class CustomCommandHandler
{
+ private static Dictionary commandExecuteMethods = new();
+
///
/// Attempts to call the correct command and gives a result.
///
@@ -40,12 +43,9 @@ public static bool TryCall(CustomCommand command, ICommandSender sender, ArraySe
///
///
///
- public static CommandResult TryParse(CustomCommand command, ArraySegment arguments)
+ private static CommandResult TryParse(CustomCommand command, ArraySegment arguments)
{
- const BindingFlags methodFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
-
- IEnumerable methods = command.GetType().GetMethods(methodFlags).Where(IsValidExecuteMethod);
- foreach (MethodInfo method in methods)
+ foreach (MethodInfo method in GetMethods(command))
{
CommandParseResult result = ValidateAllMethodParameters(method, arguments);
if (result.CouldParse)
@@ -105,23 +105,17 @@ private static CommandParseResult ValidateParameter(ParameterInfo parameter, str
return true;
}
- private static bool IsValidExecuteMethod(MethodInfo method)
+ private static MethodInfo[] GetMethods(CustomCommand command)
{
- // isnt an Execute command
- if (method.Name != "Execute")
- return false;
-
- ParameterInfo[] parameters = method.GetParameters();
+ const BindingFlags methodFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
- // params isnt 3, so its not default
- if (parameters.Length != 3)
- return true;
+ if (!commandExecuteMethods.TryGetValue(command, out MethodInfo[] methods))
+ {
+ methods = command.GetType().GetMethods(methodFlags).Where(m => m.GetCustomAttribute() != null).ToArray();
+ commandExecuteMethods.Add(command, methods);
+ }
- // make sure params arent the types of the original default to prevent infinite loop
- return !(parameters[0].ParameterType == typeof(ArraySegment)
- && parameters[1].ParameterType == typeof(ICommandSender)
- && parameters[2].IsOut
- && parameters[2].ParameterType == typeof(string));
+ return methods;
}
}
}
\ No newline at end of file
From ecf4152a3bd4b4bd15cd2ac5bb532fcb68ca3e12 Mon Sep 17 00:00:00 2001
From: Misfiy <85962933+Misfiy@users.noreply.github.com>
Date: Wed, 6 Aug 2025 18:45:25 +0200
Subject: [PATCH 07/24] rahhh
---
.../Features/Commands/CustomCommandHandler.cs | 46 ++++++++++++++-----
1 file changed, 34 insertions(+), 12 deletions(-)
diff --git a/SecretAPI/Features/Commands/CustomCommandHandler.cs b/SecretAPI/Features/Commands/CustomCommandHandler.cs
index 85a26e7..341406d 100644
--- a/SecretAPI/Features/Commands/CustomCommandHandler.cs
+++ b/SecretAPI/Features/Commands/CustomCommandHandler.cs
@@ -5,6 +5,7 @@
using System.Linq;
using System.Reflection;
using CommandSystem;
+ using LabApi.Features.Wrappers;
using SecretAPI.Attribute;
///
@@ -12,6 +13,11 @@
///
public static class CustomCommandHandler
{
+ ///
+ ///
+ ///
+ public const string SelfPlayerName = "self";
+
private static Dictionary commandExecuteMethods = new();
///
@@ -24,7 +30,9 @@ public static class CustomCommandHandler
/// Whether the command was a success.
public static bool TryCall(CustomCommand command, ICommandSender sender, ArraySegment arguments, out string response)
{
- CommandResult parseResult = TryParse(command, arguments);
+ Player senderPlayer = Player.Get(sender) ?? Server.Host!;
+
+ CommandResult parseResult = TryParse(command, senderPlayer, arguments);
if (!parseResult.CouldParse)
{
response = parseResult.FailedResponse;
@@ -37,17 +45,11 @@ public static bool TryCall(CustomCommand command, ICommandSender sender, ArraySe
return true;
}
- ///
- ///
- ///
- ///
- ///
- ///
- private static CommandResult TryParse(CustomCommand command, ArraySegment arguments)
+ private static CommandResult TryParse(CustomCommand command, Player sender, ArraySegment arguments)
{
foreach (MethodInfo method in GetMethods(command))
{
- CommandParseResult result = ValidateAllMethodParameters(method, arguments);
+ CommandParseResult result = ValidateAllMethodParameters(method, sender, arguments);
if (result.CouldParse)
{
return new CommandResult()
@@ -59,18 +61,18 @@ private static CommandResult TryParse(CustomCommand command, ArraySegment arguments)
+ private static CommandParseResult ValidateAllMethodParameters(MethodInfo method, Player sender, ArraySegment arguments)
{
for (int index = 0; index < method.GetParameters().Length; index++)
{
ParameterInfo parameter = method.GetParameters()[index];
- CommandParseResult result = ValidateParameter(parameter, arguments.ElementAtOrDefault(index));
+ CommandParseResult result = ValidateParameter(parameter, sender, arguments.ElementAtOrDefault(index));
if (!result.CouldParse)
return result;
}
}
- private static CommandParseResult ValidateParameter(ParameterInfo parameter, string? argument)
+ private static CommandParseResult ValidateParameter(ParameterInfo parameter, Player sender, string? argument)
{
// if arg doesnt exist & param is optional, then its validated
if (argument == null && parameter.IsOptional)
@@ -102,6 +104,26 @@ private static CommandParseResult ValidateParameter(ParameterInfo parameter, str
};
}
+ if (parameter.Name == SelfPlayerName)
+ {
+ if (typeof(Player).IsAssignableFrom(parameter.ParameterType))
+ {
+ return new CommandParseResult()
+ {
+ CouldParse = true,
+ ParamArgument = sender,
+ };
+ }
+ else if (typeof(ReferenceHub).IsAssignableFrom(parameter.ParameterType))
+ {
+ return new CommandParseResult()
+ {
+ CouldParse = true,
+ ParamArgument = sender.ReferenceHub,
+ };
+ }
+ }
+
return true;
}
From ba7b32ca7c9e6ee7aba65a20f4cb2be022093561 Mon Sep 17 00:00:00 2001
From: Misfiy <85962933+Misfiy@users.noreply.github.com>
Date: Wed, 6 Aug 2025 19:06:50 +0200
Subject: [PATCH 08/24] command example
---
.../Commands/ExampleExplodeCommand.cs | 27 +++++++++++++++++++
1 file changed, 27 insertions(+)
create mode 100644 SecretAPI.Examples/Commands/ExampleExplodeCommand.cs
diff --git a/SecretAPI.Examples/Commands/ExampleExplodeCommand.cs b/SecretAPI.Examples/Commands/ExampleExplodeCommand.cs
new file mode 100644
index 0000000..7781dc8
--- /dev/null
+++ b/SecretAPI.Examples/Commands/ExampleExplodeCommand.cs
@@ -0,0 +1,27 @@
+namespace SecretAPI.Examples.Commands
+{
+ using LabApi.Features.Wrappers;
+ using SecretAPI.Attribute;
+ using SecretAPI.Features.Commands;
+
+ ///
+ /// An example of a that explodes a player.
+ ///
+ public class ExampleExplodeCommand : CustomCommand
+ {
+ ///
+ public override string Command { get; } = "explode";
+
+ ///
+ public override string[] Aliases { get; } = [];
+
+ ///
+ public override string Description { get; } = "Explodes a player";
+
+ [ExecuteCommand]
+ private void Run(Player self, Player target)
+ {
+ TimedGrenadeProjectile.SpawnActive(target.Position, ItemType.GrenadeHE, self);
+ }
+ }
+}
\ No newline at end of file
From 7775cd2ce970dab028e6cc3353a92705d7f7f521 Mon Sep 17 00:00:00 2001
From: Misfiy <85962933+Misfiy@users.noreply.github.com>
Date: Wed, 6 Aug 2025 19:56:37 +0200
Subject: [PATCH 09/24] idfk
---
.../Commands/ExampleExplodeCommand.cs | 4 +-
...mandResult.cs => CommandArgParseResult.cs} | 15 +---
.../Commands/CommandMethodParseResult.cs | 15 ++++
.../Features/Commands/CommandParseResult.cs | 13 ++-
.../Features/Commands/CustomCommandHandler.cs | 85 +++++++++++++------
5 files changed, 92 insertions(+), 40 deletions(-)
rename SecretAPI/Features/Commands/{CommandResult.cs => CommandArgParseResult.cs} (51%)
create mode 100644 SecretAPI/Features/Commands/CommandMethodParseResult.cs
diff --git a/SecretAPI.Examples/Commands/ExampleExplodeCommand.cs b/SecretAPI.Examples/Commands/ExampleExplodeCommand.cs
index 7781dc8..fdf2036 100644
--- a/SecretAPI.Examples/Commands/ExampleExplodeCommand.cs
+++ b/SecretAPI.Examples/Commands/ExampleExplodeCommand.cs
@@ -19,9 +19,9 @@ public class ExampleExplodeCommand : CustomCommand
public override string Description { get; } = "Explodes a player";
[ExecuteCommand]
- private void Run(Player self, Player target)
+ private void Run(Player sender, Player target)
{
- TimedGrenadeProjectile.SpawnActive(target.Position, ItemType.GrenadeHE, self);
+ TimedGrenadeProjectile.SpawnActive(target.Position, ItemType.GrenadeHE, sender);
}
}
}
\ No newline at end of file
diff --git a/SecretAPI/Features/Commands/CommandResult.cs b/SecretAPI/Features/Commands/CommandArgParseResult.cs
similarity index 51%
rename from SecretAPI/Features/Commands/CommandResult.cs
rename to SecretAPI/Features/Commands/CommandArgParseResult.cs
index 62ff5ce..a7b9963 100644
--- a/SecretAPI/Features/Commands/CommandResult.cs
+++ b/SecretAPI/Features/Commands/CommandArgParseResult.cs
@@ -1,11 +1,9 @@
namespace SecretAPI.Features.Commands
{
- using System.Reflection;
-
///
- /// The result of to know what to do.
+ /// Gets the result of a .
///
- internal struct CommandResult
+ internal struct CommandArgParseResult
{
///
/// Gets a value indicating whether parsing was successful.
@@ -18,13 +16,8 @@ internal struct CommandResult
public string FailedResponse;
///
- /// If parsing succeded, the method to call with .
- ///
- public MethodInfo Method;
-
- ///
- /// If parsing succeeded, the arguments provided.
+ /// The argument for the argument.
///
- public object[]? ProvidedArguments;
+ public object ParamArgument;
}
}
\ No newline at end of file
diff --git a/SecretAPI/Features/Commands/CommandMethodParseResult.cs b/SecretAPI/Features/Commands/CommandMethodParseResult.cs
new file mode 100644
index 0000000..f5999f5
--- /dev/null
+++ b/SecretAPI/Features/Commands/CommandMethodParseResult.cs
@@ -0,0 +1,15 @@
+namespace SecretAPI.Features.Commands
+{
+ ///
+ /// Defines the return type of .
+ ///
+ internal struct CommandMethodParseResult
+ {
+#pragma warning disable SA1600 // Elements should be documented
+ internal bool CouldParse;
+
+ internal string FailedResponse;
+
+ internal object[]? Arguments;
+ }
+}
\ No newline at end of file
diff --git a/SecretAPI/Features/Commands/CommandParseResult.cs b/SecretAPI/Features/Commands/CommandParseResult.cs
index a243616..4b56236 100644
--- a/SecretAPI/Features/Commands/CommandParseResult.cs
+++ b/SecretAPI/Features/Commands/CommandParseResult.cs
@@ -1,7 +1,9 @@
namespace SecretAPI.Features.Commands
{
+ using System.Reflection;
+
///
- /// Gets the result of a .
+ /// The result of to know what to do.
///
internal struct CommandParseResult
{
@@ -16,8 +18,13 @@ internal struct CommandParseResult
public string FailedResponse;
///
- /// The argument for the argument.
+ /// If parsing succeded, the method to call with .
+ ///
+ public MethodInfo Method;
+
+ ///
+ /// If parsing succeeded, the arguments provided.
///
- public object? ParamArgument;
+ public object[]? ProvidedArguments;
}
}
\ No newline at end of file
diff --git a/SecretAPI/Features/Commands/CustomCommandHandler.cs b/SecretAPI/Features/Commands/CustomCommandHandler.cs
index 341406d..524781d 100644
--- a/SecretAPI/Features/Commands/CustomCommandHandler.cs
+++ b/SecretAPI/Features/Commands/CustomCommandHandler.cs
@@ -6,6 +6,7 @@
using System.Reflection;
using CommandSystem;
using LabApi.Features.Wrappers;
+ using NorthwoodLib.Pools;
using SecretAPI.Attribute;
///
@@ -14,9 +15,9 @@
public static class CustomCommandHandler
{
///
- ///
+ /// The name of the or argument represensing the command sender.
///
- public const string SelfPlayerName = "self";
+ public const string SenderPlayerName = "sender";
private static Dictionary commandExecuteMethods = new();
@@ -32,55 +33,86 @@ public static bool TryCall(CustomCommand command, ICommandSender sender, ArraySe
{
Player senderPlayer = Player.Get(sender) ?? Server.Host!;
- CommandResult parseResult = TryParse(command, senderPlayer, arguments);
- if (!parseResult.CouldParse)
+ CommandParseResult parseParseResult = TryParse(command, senderPlayer, arguments);
+ if (!parseParseResult.CouldParse)
{
- response = parseResult.FailedResponse;
+ response = parseParseResult.FailedResponse;
return false;
}
- parseResult.Method.Invoke(null, parseResult.ProvidedArguments);
+ parseParseResult.Method.Invoke(null, parseParseResult.ProvidedArguments);
// TODO: get result & put it into response
return true;
}
- private static CommandResult TryParse(CustomCommand command, Player sender, ArraySegment arguments)
+ private static CommandParseResult TryParse(CustomCommand command, Player sender, ArraySegment arguments)
{
foreach (MethodInfo method in GetMethods(command))
{
- CommandParseResult result = ValidateAllMethodParameters(method, sender, arguments);
+ CommandMethodParseResult result = ValidateAllMethodParameters(method, sender, arguments);
+
+ // parsed correctly, return with correct arguments
if (result.CouldParse)
{
- return new CommandResult()
+ return new CommandParseResult()
{
CouldParse = true,
FailedResponse = string.Empty,
+ Method = method,
+ ProvidedArguments = result.Arguments,
};
}
+
+ // failed to parse, return and show failure
+ return new CommandParseResult()
+ {
+ CouldParse = false,
+ FailedResponse = result.FailedResponse,
+ };
}
}
- private static CommandParseResult ValidateAllMethodParameters(MethodInfo method, Player sender, ArraySegment arguments)
+ private static CommandMethodParseResult ValidateAllMethodParameters(MethodInfo method, Player sender, ArraySegment arguments)
{
- for (int index = 0; index < method.GetParameters().Length; index++)
+ ParameterInfo[] parameters = method.GetParameters();
+ List