From da748d84df798860e7c7c88e7367afb9182c33e9 Mon Sep 17 00:00:00 2001 From: Robin Kanters Date: Wed, 6 May 2015 21:30:38 +0200 Subject: [PATCH] Added /giveme by pulling some logic into methods (in /give) that can be re-used by /giveme --- TrueCraft/Commands/CommandManager.cs | 1 + TrueCraft/Commands/GiveCommand.cs | 80 ++++++++++++++++------------ TrueCraft/Commands/GiveMeCommand.cs | 49 +++++++++++++++++ TrueCraft/TrueCraft.csproj | 1 + 4 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 TrueCraft/Commands/GiveMeCommand.cs diff --git a/TrueCraft/Commands/CommandManager.cs b/TrueCraft/Commands/CommandManager.cs index 464a1b5..bfd4d01 100644 --- a/TrueCraft/Commands/CommandManager.cs +++ b/TrueCraft/Commands/CommandManager.cs @@ -21,6 +21,7 @@ namespace TrueCraft.Commands { Commands.Add(new PingCommand()); Commands.Add(new GiveCommand()); + Commands.Add(new GiveMeCommand()); Commands.Add(new HelpCommand()); Commands.Add(new ResendInvCommand()); Commands.Add(new PositionCommand()); diff --git a/TrueCraft/Commands/GiveCommand.cs b/TrueCraft/Commands/GiveCommand.cs index db5021e..9fe7a03 100644 --- a/TrueCraft/Commands/GiveCommand.cs +++ b/TrueCraft/Commands/GiveCommand.cs @@ -38,47 +38,59 @@ namespace TrueCraft.Commands if(arguments.Length >= 4) amount = arguments[3]; - var receivingPlayer = - client.Server.Clients.FirstOrDefault(c => String.Equals(c.Username, username, StringComparison.CurrentCultureIgnoreCase)); + 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)) + if (!short.TryParse(itemid, out id) || !Int32.TryParse(amount, out count)) return false; + + if (receivingPlayer == null) { - if (receivingPlayer == null) - { - client.SendMessage("No client with the username \"" + username + "\" was found."); - return; - } - - if (client.Server.ItemRepository.GetItemProvider(id) == null) - { - client.SendMessage("Invalid item id \"" + id + "\"."); - return; - } - - var inventory = receivingPlayer.Inventory as InventoryWindow; - if (inventory != null) - { - sbyte toAdd; - while (count > 0) - { - if (count >= 64) - toAdd = 64; - else - toAdd = (sbyte)count; - - count -= toAdd; - - inventory.PickUpStack(new ItemStack(id, toAdd)); - } - } - - return; + client.SendMessage("No client with the username \"" + username + "\" was found."); + return true; } - Help(client, alias, arguments); + 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; + + while (count > 0) + { + sbyte amountToGive; + if (count >= 64) + amountToGive = 64; + else + amountToGive = (sbyte) count; + + count -= amountToGive; + + inventory.PickUpStack(new ItemStack(id, amountToGive)); + } + + return true; } public override void Help(IRemoteClient client, string alias, string[] arguments) diff --git a/TrueCraft/Commands/GiveMeCommand.cs b/TrueCraft/Commands/GiveMeCommand.cs new file mode 100644 index 0000000..6bbdd77 --- /dev/null +++ b/TrueCraft/Commands/GiveMeCommand.cs @@ -0,0 +1,49 @@ +using TrueCraft.API.Networking; + +namespace TrueCraft.Commands +{ + public class GiveMeCommand : GiveCommand + { + public override string Name + { + get { return "giveme"; } + } + + public override string[] Aliases + { + get { return new string[0]; } + } + + public override string Description + { + get { return "Give yourself an amount of items."; } + } + + public override void Handle(IRemoteClient client, string alias, string[] arguments) + { + if (arguments.Length < 2) + { + Help(client, alias, arguments); + return; + } + + string itemid = arguments[1], + amount = "1"; + + if (arguments.Length >= 3) + amount = arguments[2]; + + var receivingPlayer = client; + + if (!GiveItem(receivingPlayer, itemid, amount, client)) + { + Help(client, alias, arguments); + } + } + + public override void Help(IRemoteClient client, string alias, string[] arguments) + { + client.SendMessage("Correct usage is /" + alias + " [Amount]"); + } + } +} \ No newline at end of file diff --git a/TrueCraft/TrueCraft.csproj b/TrueCraft/TrueCraft.csproj index ba91d96..c790758 100644 --- a/TrueCraft/TrueCraft.csproj +++ b/TrueCraft/TrueCraft.csproj @@ -41,6 +41,7 @@ +