<commit_before>﻿using System;
using System.Linq;
using TrueCraft.Core.Windows;
using TrueCraft.API;
using TrueCraft.API.Networking;

namespace TrueCraft.Commands
{
    public class GiveCommand : Command
    {
        public override string Name
        {
            get { return "give"; }
        }

        public override string Description
        {
            get { return "Give the specified player an amount of items."; }
        }

        public override string[] Aliases
        {
            get { return new string[1]{ "i" }; }
        }

        public override void Handle(IRemoteClient client, string alias, string[] arguments)
        {
            if (arguments.Length < 3)
            {
                Help(client, alias, arguments);
                return;
            }

            string  username    = arguments[1],
                    itemid      = arguments[2],
                    amount      = "1";

            if(arguments.Length >= 4)
                    amount = arguments[3];
            
            var receivingPlayer = GetPlayerByName(client, username);

            if (!GiveItem(receivingPlayer, itemid, amount, client))
            {
                Help(client, alias, arguments);
            }
        }

        protected static IRemoteClient GetPlayerByName(IRemoteClient client, string username)
        {
            var receivingPlayer =
                client.Server.Clients.FirstOrDefault(
                    c => String.Equals(c.Username, username, StringComparison.CurrentCultureIgnoreCase));
            return receivingPlayer;
        }

        protected static bool GiveItem(IRemoteClient receivingPlayer, string itemid, string amount, IRemoteClient client)
        {
            short id;
            int count;
            string username = receivingPlayer.Username;

            if (!short.TryParse(itemid, out id) || !Int32.TryParse(amount, out count)) return false;
            
            if (receivingPlayer == null)
            {
                client.SendMessage("No client with the username \"" + username + "\" was found.");
                return true;
            }

            if (client.Server.ItemRepository.GetItemProvider(id) == null)
            {
                client.SendMessage("Invalid item id \"" + id + "\".");
                return true;
            }

            var inventory = receivingPlayer.Inventory as InventoryWindow;
            if (inventory == null) return false;
<commit_msg>Fixed exception when trying to give items to a player who couldn't be found<commit_after>﻿using System;
using System.Linq;
using TrueCraft.Core.Windows;
using TrueCraft.API;
using TrueCraft.API.Networking;

namespace TrueCraft.Commands
{
    public class GiveCommand : Command
    {
        public override string Name
        {
            get { return "give"; }
        }

        public override string Description
        {
            get { return "Give the specified player an amount of items."; }
        }

        public override string[] Aliases
        {
            get { return new string[1]{ "i" }; }
        }

        public override void Handle(IRemoteClient client, string alias, string[] arguments)
        {
            if (arguments.Length < 3)
            {
                Help(client, alias, arguments);
                return;
            }

            string  username    = arguments[1],
                    itemid      = arguments[2],
                    amount      = "1";

            if(arguments.Length >= 4)
                    amount = arguments[3];
            
            var receivingPlayer = GetPlayerByName(client, username);

            if (receivingPlayer == null)
            {
                client.SendMessage("No client with the username \"" + username + "\" was found.");
                return;
            }

            if (!GiveItem(receivingPlayer, itemid, amount, client))
            {
                Help(client, alias, arguments);
            }
        }

        protected static IRemoteClient GetPlayerByName(IRemoteClient client, string username)
        {
            var receivingPlayer =
                client.Server.Clients.FirstOrDefault(
                    c => String.Equals(c.Username, username, StringComparison.CurrentCultureIgnoreCase));
            return receivingPlayer;
        }

        protected static bool GiveItem(IRemoteClient receivingPlayer, string itemid, string amount, IRemoteClient client)
        {
            short id;
            int count;

            if (!short.TryParse(itemid, out id) || !Int32.TryParse(amount, out count)) return false;

            if (client.Server.ItemRepository.GetItemProvider(id) == null)
            {
                client.SendMessage("Invalid item id \"" + id + "\".");
                return true;
            }

            string username = receivingPlayer.Username;
            var inventory = receivingPlayer.Inventory as InventoryWindow;
            if (inventory == null) return false;

{'fraction_non_alphanumeric': 0.08541708341668333, 'fraction_numerical': 0.0036007201440288058, 'mean_word_length': 1.6055237102657633, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3846732', 'n_tokens_mistral': 1226, 'n_tokens_neox': 1167, 'n_words': 332}