From 39b476ccade92b7017cd6dd435651342629e59e9 Mon Sep 17 00:00:00 2001 From: B3none Date: Sun, 28 Jan 2024 04:24:36 +0000 Subject: [PATCH] Copied WithCommands as base example --- examples/WithMenus/README.md | 4 ++ examples/WithMenus/WithCommands.csproj | 12 +++++ examples/WithMenus/WithCommandsPlugin.cs | 63 ++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 examples/WithMenus/README.md create mode 100644 examples/WithMenus/WithCommands.csproj create mode 100644 examples/WithMenus/WithCommandsPlugin.cs diff --git a/examples/WithMenus/README.md b/examples/WithMenus/README.md new file mode 100644 index 00000000..159317d0 --- /dev/null +++ b/examples/WithMenus/README.md @@ -0,0 +1,4 @@ +# With Commands +This is an example that shows how to register chat & console commands. + +All commands that are prefixed with "css_" will automatically be registered as a chat command without the prefix. i.e. `css_ping` can be called with `!ping` or `/ping`. \ No newline at end of file diff --git a/examples/WithMenus/WithCommands.csproj b/examples/WithMenus/WithCommands.csproj new file mode 100644 index 00000000..21eb946f --- /dev/null +++ b/examples/WithMenus/WithCommands.csproj @@ -0,0 +1,12 @@ + + + net7.0 + enable + enable + false + false + + + + + diff --git a/examples/WithMenus/WithCommandsPlugin.cs b/examples/WithMenus/WithCommandsPlugin.cs new file mode 100644 index 00000000..b523b178 --- /dev/null +++ b/examples/WithMenus/WithCommandsPlugin.cs @@ -0,0 +1,63 @@ +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Core.Attributes; +using CounterStrikeSharp.API.Core.Attributes.Registration; +using CounterStrikeSharp.API.Modules.Admin; +using CounterStrikeSharp.API.Modules.Commands; + +namespace WithCommands; + +[MinimumApiVersion(80)] +public class WithCommandsPlugin : BasePlugin +{ + public override string ModuleName => "Example: With Commands"; + public override string ModuleVersion => "1.0.0"; + public override string ModuleAuthor => "CounterStrikeSharp & Contributors"; + public override string ModuleDescription => "A simple plugin that registers some commands"; + + public override void Load(bool hotReload) + { + // All commands that are prefixed with "css_" will automatically be registered as a chat command without the prefix. + // i.e. `css_ping` can be called with `!ping` or `/ping`. + // Commands can be registered using the instance `AddCommand` method. + AddCommand("css_ping", "Responds to the caller with \"pong\"", (player, commandInfo) => + { + // The player is null, then the command has been called by the server console. + if (player == null) + { + commandInfo.ReplyToCommand("pong server"); + return; + } + + commandInfo.ReplyToCommand("pong"); + }); + } + + // Commands can also be registered using the `Command` attribute. + [ConsoleCommand("css_hello", "Responds to the caller with \"pong\"")] + // The `CommandHelper` attribute can be used to provide additional information about the command. + [CommandHelper(minArgs: 1, usage: "[name]", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] + [RequiresPermissions("@css/cvar")] + public void OnHelloCommand(CCSPlayerController? player, CommandInfo commandInfo) + { + // The first argument is the command name, in this case "css_hello". + commandInfo.GetArg(0); // css_hello + + // The second argument is the first argument passed to the command, in this case "name". + // The `minArgs` helper parameter is used to ensure that the second argument is present. + var name = commandInfo.GetArg(1); + + commandInfo.ReplyToCommand($"Hello {name}"); + } + + // Permissions can be added to commands using the `RequiresPermissions` attribute. + // See the admin documentation for more information on permissions. + [RequiresPermissions("@css/kick")] + [CommandHelper(minArgs: 1, usage: "[id]", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] + public void OnSpecialCommand(CCSPlayerController? player, CommandInfo commandInfo) + { + var id = commandInfo.GetArg(1); + + Server.ExecuteCommand($"kick {id}"); + } +} \ No newline at end of file