EasyCommandsTShock 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasyCommandsTShock --version 1.1.0                
NuGet\Install-Package EasyCommandsTShock -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EasyCommandsTShock" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasyCommandsTShock --version 1.1.0                
#r "nuget: EasyCommandsTShock, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EasyCommandsTShock as a Cake Addin
#addin nuget:?package=EasyCommandsTShock&version=1.1.0

// Install EasyCommandsTShock as a Cake Tool
#tool nuget:?package=EasyCommandsTShock&version=1.1.0                

Easy Commands for TShock

This library removes the boilerplate of writing code for TShock commands by handling input parsing and validation for you so that you can focus on what's important. Essentially, you make a method, and the arguments of the method generate the arguments of the command. You can make optional commands, subcommands, and commands with multi-word arguments that don't require quotes. It lets you go from this:

private static void Give(CommandArgs args)
{
  if (args.Parameters.Count < 2)
  {
    args.Player.SendErrorMessage(
      "Invalid syntax! Proper syntax: {0}give <item type/id> <player> [item amount] [prefix id/name]", Specifier);
    return;
  }
  if (args.Parameters[0].Length == 0)
  {
    args.Player.SendErrorMessage("Missing item name/id.");
    return;
  }
  if (args.Parameters[1].Length == 0)
  {
    args.Player.SendErrorMessage("Missing player name.");
    return;
  }
  int itemAmount = 0;
  int prefix = 0;
  var items = TShock.Utils.GetItemByIdOrName(args.Parameters[0]);
  args.Parameters.RemoveAt(0);
  string plStr = args.Parameters[0];
  args.Parameters.RemoveAt(0);
  if (args.Parameters.Count == 1)
    int.TryParse(args.Parameters[0], out itemAmount);
  if (items.Count == 0)
  {
    args.Player.SendErrorMessage("Invalid item type!");
  }
  else if (items.Count > 1)
  {
    TShock.Utils.SendMultipleMatchError(args.Player, items.Select(i => $"{i.Name}({i.netID})"));
  }
  else
  {
    var item = items[0];

    if (args.Parameters.Count == 2)
    {
      int.TryParse(args.Parameters[0], out itemAmount);
      var prefixIds = TShock.Utils.GetPrefixByIdOrName(args.Parameters[1]);
      if (item.accessory && prefixIds.Contains(PrefixID.Quick))
      {
        prefixIds.Remove(PrefixID.Quick);
        prefixIds.Remove(PrefixID.Quick2);
        prefixIds.Add(PrefixID.Quick2);
      }
      else if (!item.accessory && prefixIds.Contains(PrefixID.Quick))
        prefixIds.Remove(PrefixID.Quick2);
      if (prefixIds.Count == 1)
        prefix = prefixIds[0];
    }

    if (item.type >= 1 && item.type < Main.maxItemTypes)
    {
      var players = TShock.Utils.FindPlayer(plStr);
      if (players.Count == 0)
      {
        args.Player.SendErrorMessage("Invalid player!");
      }
      else if (players.Count > 1)
      {
        TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name));
      }
      else
      {
        var plr = players[0];
        if (plr.InventorySlotAvailable || (item.type > 70 && item.type < 75) || item.ammo > 0 || item.type == 58 || item.type == 184)
        {
          if (itemAmount == 0 || itemAmount > item.maxStack)
            itemAmount = item.maxStack;
          if (plr.GiveItemCheck(item.type, EnglishLanguage.GetItemNameById(item.type), itemAmount, prefix))
          {
            args.Player.SendSuccessMessage(string.Format("Gave {0} {1} {2}(s).", plr.Name, itemAmount, item.Name));
            plr.SendSuccessMessage(string.Format("{0} gave you {1} {2}(s).", args.Player.Name, itemAmount, item.Name));
          }
          else
          {
            args.Player.SendErrorMessage("You cannot spawn banned items.");
          }

        }
        else
        {
          args.Player.SendErrorMessage("Player does not have free slots!");
        }
      }
    }
    else
    {
      args.Player.SendErrorMessage("Invalid item type!");
    }
  }
}

to this:

[Command("easy-give")]
[CommandPermissions("tshock.item.give")]
[HelpText("Gives another player an item.")]
public void Give(Item item, TSPlayer player, int amount = 0, [ItemPrefix]int prefix = 0)
{
    if(amount == 0 || amount > item.maxStack)
    {
        amount = item.maxStack;
    }
    if(player.GiveItemCheck(item.type, EnglishLanguage.GetItemNameById(item.type), item.width, item.height, amount, prefix))
    {
        Sender.SendSuccessMessage(string.Format("Gave {0} {1} {2}(s).", player.Name, amount, item.Name));
        player.SendSuccessMessage(string.Format("{0} gave you {1} {2}(s).", Sender.Name, amount, item.Name));
    }
    else
    {
        Fail("You cannot spawn banned items.");
    }
}

Installation

To install, simply go to Visual Studio's Package Manager Console in your TShock plugin project and run these commands:

Install-Package EasyCommands
Install-Package EasyCommandsTShock

When you run your plugin, you will need to copy EasyCommands.dll and EasyCommandsTShock.dll into your ServerPlugins folder as well as your own plugin.

How to use

This library is an extension of my accompanying, more general-purpose library, Easy Commands. Read up on the documentation there to get a more in-depth view of how everything works. You can also view the Example project in this repository to see the code in action.

If you want to run the Example project, build it and copy EasyCommands.dll, EasyCommandsTShock.dll, and Example.dll to your ServerPlugins folder.

To register your commands, all you need to do is create a command handler and have it register the namespace where your commands are. See Plugin.cs:

CommandHandler = new TShockCommandHandler();
// You can also use a Type for this argument to register a single class
CommandHandler.RegisterCommands("Example.Commands");

In your commands namespace, you can create command callbacks by creating classes that inherit from EasyCommands.CommandCallbacks<TSPlayer>. To see the documentation for the syntax of these command callbacks, please see this documentation. You can use the [HelpText], [CommandPermissions], [AllowServer], and [DoLog] attributes with the command callbacks.

By default, this library supports using these types for method arguments: string, int, double, float, bool, TSPlayer, User, Item, Group, NPC, Region, Color, team color using [TeamColor], buff using [Buff], and item prefix using [ItemPrefix] (NOTE: item prefix may not currently work with the "quick" prefix; see here). If those arguments aren't enough, you can add your own parameter handlers. See this documentation.

Product Compatible and additional computed target framework versions.
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.5.4 445 7/22/2023
1.2.5.3 149 5/29/2023
1.2.5.2 144 5/29/2023
1.2.5.1 317 12/26/2022
1.2.5 305 12/26/2022
1.2.4 489 2/27/2022
1.2.3 472 5/15/2021
1.2.2 398 12/14/2020
1.2.1 390 12/13/2020
1.2.0 454 6/13/2020
1.1.1 627 5/22/2019
1.1.0 532 5/21/2019
1.0.0 557 5/16/2019

Update to EasyCommands version 1.1, which added flags and allowed the use of string[] for the first argument of parsing rules.