Added /giveme by pulling some logic into methods (in /give) that can be re-used by /giveme

This commit is contained in:
Robin Kanters 2015-05-06 21:30:38 +02:00
parent 475b57c69b
commit da748d84df
4 changed files with 97 additions and 34 deletions

View File

@ -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());

View File

@ -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)

View File

@ -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 + " <Item ID> [Amount]");
}
}
}

View File

@ -41,6 +41,7 @@
<Compile Include="Commands\Command.cs" />
<Compile Include="Commands\CommandManager.cs" />
<Compile Include="Commands\GiveCommand.cs" />
<Compile Include="Commands\GiveMeCommand.cs" />
<Compile Include="Commands\HelpCommand.cs" />
<Compile Include="Commands\PingCommand.cs" />
<Compile Include="Program.cs" />